bucket_locks.c 1.4 KB

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