percpu_ida.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef __PERCPU_IDA_H__
  2. #define __PERCPU_IDA_H__
  3. #include <linux/types.h>
  4. #include <linux/bitops.h>
  5. #include <linux/init.h>
  6. #include <linux/spinlock_types.h>
  7. #include <linux/wait.h>
  8. #include <linux/cpumask.h>
  9. struct percpu_ida_cpu;
  10. struct percpu_ida {
  11. /*
  12. * number of tags available to be allocated, as passed to
  13. * percpu_ida_init()
  14. */
  15. unsigned nr_tags;
  16. unsigned percpu_max_size;
  17. unsigned percpu_batch_size;
  18. struct percpu_ida_cpu __percpu *tag_cpu;
  19. /*
  20. * Bitmap of cpus that (may) have tags on their percpu freelists:
  21. * steal_tags() uses this to decide when to steal tags, and which cpus
  22. * to try stealing from.
  23. *
  24. * It's ok for a freelist to be empty when its bit is set - steal_tags()
  25. * will just keep looking - but the bitmap _must_ be set whenever a
  26. * percpu freelist does have tags.
  27. */
  28. cpumask_t cpus_have_tags;
  29. struct {
  30. spinlock_t lock;
  31. /*
  32. * When we go to steal tags from another cpu (see steal_tags()),
  33. * we want to pick a cpu at random. Cycling through them every
  34. * time we steal is a bit easier and more or less equivalent:
  35. */
  36. unsigned cpu_last_stolen;
  37. /* For sleeping on allocation failure */
  38. wait_queue_head_t wait;
  39. /*
  40. * Global freelist - it's a stack where nr_free points to the
  41. * top
  42. */
  43. unsigned nr_free;
  44. unsigned *freelist;
  45. } ____cacheline_aligned_in_smp;
  46. };
  47. /*
  48. * Number of tags we move between the percpu freelist and the global freelist at
  49. * a time
  50. */
  51. #define IDA_DEFAULT_PCPU_BATCH_MOVE 32U
  52. /* Max size of percpu freelist, */
  53. #define IDA_DEFAULT_PCPU_SIZE ((IDA_DEFAULT_PCPU_BATCH_MOVE * 3) / 2)
  54. int percpu_ida_alloc(struct percpu_ida *pool, gfp_t gfp);
  55. void percpu_ida_free(struct percpu_ida *pool, unsigned tag);
  56. void percpu_ida_destroy(struct percpu_ida *pool);
  57. int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
  58. unsigned long max_size, unsigned long batch_size);
  59. static inline int percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags)
  60. {
  61. return __percpu_ida_init(pool, nr_tags, IDA_DEFAULT_PCPU_SIZE,
  62. IDA_DEFAULT_PCPU_BATCH_MOVE);
  63. }
  64. typedef int (*percpu_ida_cb)(unsigned, void *);
  65. int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  66. void *data);
  67. unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu);
  68. #endif /* __PERCPU_IDA_H__ */