percpu_ida.h 2.3 KB

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