^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /* Allocate an array of spinlocks to be accessed by a hash. Two arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * indicate the number of elements to allocate in the array. max_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * gives the maximum number of elements to allocate. cpu_mult gives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * the number of locks per CPU to allocate. The size is rounded up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * to a power of 2 to be suitable as a hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int __alloc_bucket_spinlocks(spinlock_t **locks, unsigned int *locks_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) size_t max_size, unsigned int cpu_mult, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) const char *name, struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) spinlock_t *tlocks = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned int i, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #if defined(CONFIG_PROVE_LOCKING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int nr_pcpus = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int nr_pcpus = num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (cpu_mult) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) nr_pcpus = min_t(unsigned int, nr_pcpus, 64UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) size = min_t(unsigned int, nr_pcpus * cpu_mult, max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) size = max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (sizeof(spinlock_t) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) tlocks = kvmalloc_array(size, sizeof(spinlock_t), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!tlocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) spin_lock_init(&tlocks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) lockdep_init_map(&tlocks[i].dep_map, name, key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *locks = tlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *locks_mask = size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) EXPORT_SYMBOL(__alloc_bucket_spinlocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void free_bucket_spinlocks(spinlock_t *locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) kvfree(locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) EXPORT_SYMBOL(free_bucket_spinlocks);