xref: /linux/lib/bucket_locks.c (revision ff93bca7)
192f36ccaSTom Herbert #include <linux/export.h>
292f36ccaSTom Herbert #include <linux/kernel.h>
392f36ccaSTom Herbert #include <linux/mm.h>
492f36ccaSTom Herbert #include <linux/slab.h>
592f36ccaSTom Herbert #include <linux/vmalloc.h>
692f36ccaSTom Herbert 
792f36ccaSTom Herbert /* Allocate an array of spinlocks to be accessed by a hash. Two arguments
892f36ccaSTom Herbert  * indicate the number of elements to allocate in the array. max_size
992f36ccaSTom Herbert  * gives the maximum number of elements to allocate. cpu_mult gives
1092f36ccaSTom Herbert  * the number of locks per CPU to allocate. The size is rounded up
1192f36ccaSTom Herbert  * to a power of 2 to be suitable as a hash table.
1292f36ccaSTom Herbert  */
1392f36ccaSTom Herbert 
__alloc_bucket_spinlocks(spinlock_t ** locks,unsigned int * locks_mask,size_t max_size,unsigned int cpu_mult,gfp_t gfp,const char * name,struct lock_class_key * key)14*ff93bca7SCong Wang int __alloc_bucket_spinlocks(spinlock_t **locks, unsigned int *locks_mask,
15*ff93bca7SCong Wang 			     size_t max_size, unsigned int cpu_mult, gfp_t gfp,
16*ff93bca7SCong Wang 			     const char *name, struct lock_class_key *key)
1792f36ccaSTom Herbert {
1892f36ccaSTom Herbert 	spinlock_t *tlocks = NULL;
1992f36ccaSTom Herbert 	unsigned int i, size;
2092f36ccaSTom Herbert #if defined(CONFIG_PROVE_LOCKING)
2192f36ccaSTom Herbert 	unsigned int nr_pcpus = 2;
2292f36ccaSTom Herbert #else
2392f36ccaSTom Herbert 	unsigned int nr_pcpus = num_possible_cpus();
2492f36ccaSTom Herbert #endif
2592f36ccaSTom Herbert 
2692f36ccaSTom Herbert 	if (cpu_mult) {
2792f36ccaSTom Herbert 		nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
2892f36ccaSTom Herbert 		size = min_t(unsigned int, nr_pcpus * cpu_mult, max_size);
2992f36ccaSTom Herbert 	} else {
3092f36ccaSTom Herbert 		size = max_size;
3192f36ccaSTom Herbert 	}
3292f36ccaSTom Herbert 
3392f36ccaSTom Herbert 	if (sizeof(spinlock_t) != 0) {
34ce91f6eeSMichal Hocko 		tlocks = kvmalloc_array(size, sizeof(spinlock_t), gfp);
3592f36ccaSTom Herbert 		if (!tlocks)
3692f36ccaSTom Herbert 			return -ENOMEM;
37*ff93bca7SCong Wang 		for (i = 0; i < size; i++) {
3892f36ccaSTom Herbert 			spin_lock_init(&tlocks[i]);
39*ff93bca7SCong Wang 			lockdep_init_map(&tlocks[i].dep_map, name, key, 0);
40*ff93bca7SCong Wang 		}
4192f36ccaSTom Herbert 	}
4292f36ccaSTom Herbert 
4392f36ccaSTom Herbert 	*locks = tlocks;
4492f36ccaSTom Herbert 	*locks_mask = size - 1;
4592f36ccaSTom Herbert 
4692f36ccaSTom Herbert 	return 0;
4792f36ccaSTom Herbert }
48*ff93bca7SCong Wang EXPORT_SYMBOL(__alloc_bucket_spinlocks);
4992f36ccaSTom Herbert 
free_bucket_spinlocks(spinlock_t * locks)5092f36ccaSTom Herbert void free_bucket_spinlocks(spinlock_t *locks)
5192f36ccaSTom Herbert {
5292f36ccaSTom Herbert 	kvfree(locks);
5392f36ccaSTom Herbert }
5492f36ccaSTom Herbert EXPORT_SYMBOL(free_bucket_spinlocks);
55