pid_list.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Do not include this file directly. */
  3. #ifndef _TRACE_INTERNAL_PID_LIST_H
  4. #define _TRACE_INTERNAL_PID_LIST_H
  5. /*
  6. * In order to keep track of what pids to trace, a tree is created much
  7. * like page tables are used. This creates a sparse bit map, where
  8. * the tree is filled in when needed. A PID is at most 30 bits (see
  9. * linux/thread.h), and is broken up into 3 sections based on the bit map
  10. * of the bits. The 8 MSB is the "upper1" section. The next 8 MSB is the
  11. * "upper2" section and the 14 LSB is the "lower" section.
  12. *
  13. * A trace_pid_list structure holds the "upper1" section, in an
  14. * array of 256 pointers (1 or 2K in size) to "upper_chunk" unions, where
  15. * each has an array of 256 pointers (1 or 2K in size) to the "lower_chunk"
  16. * structures, where each has an array of size 2K bytes representing a bitmask
  17. * of the 14 LSB of the PID (256 * 8 = 2048)
  18. *
  19. * When a trace_pid_list is allocated, it includes the 256 pointer array
  20. * of the upper1 unions. Then a "cache" of upper and lower is allocated
  21. * where these will be assigned as needed.
  22. *
  23. * When a bit is set in the pid_list bitmask, the pid to use has
  24. * the 8 MSB masked, and this is used to index the array in the
  25. * pid_list to find the next upper union. If the element is NULL,
  26. * then one is retrieved from the upper_list cache. If none is
  27. * available, then -ENOMEM is returned.
  28. *
  29. * The next 8 MSB is used to index into the "upper2" section. If this
  30. * element is NULL, then it is retrieved from the lower_list cache.
  31. * Again, if one is not available -ENOMEM is returned.
  32. *
  33. * Finally the 14 LSB of the PID is used to set the bit in the 16384
  34. * bitmask (made up of 2K bytes).
  35. *
  36. * When the second upper section or the lower section has their last
  37. * bit cleared, they are added back to the free list to be reused
  38. * when needed.
  39. */
  40. #define UPPER_BITS 8
  41. #define UPPER_MAX (1 << UPPER_BITS)
  42. #define UPPER1_SIZE (1 << UPPER_BITS)
  43. #define UPPER2_SIZE (1 << UPPER_BITS)
  44. #define LOWER_BITS 14
  45. #define LOWER_MAX (1 << LOWER_BITS)
  46. #define LOWER_SIZE (LOWER_MAX / BITS_PER_LONG)
  47. #define UPPER1_SHIFT (LOWER_BITS + UPPER_BITS)
  48. #define UPPER2_SHIFT LOWER_BITS
  49. #define LOWER_MASK (LOWER_MAX - 1)
  50. #define UPPER_MASK (UPPER_MAX - 1)
  51. /* According to linux/thread.h pids can not be bigger than or equal to 1 << 30 */
  52. #define MAX_PID (1 << 30)
  53. /* Just keep 6 chunks of both upper and lower in the cache on alloc */
  54. #define CHUNK_ALLOC 6
  55. /* Have 2 chunks free, trigger a refill of the cache */
  56. #define CHUNK_REALLOC 2
  57. union lower_chunk {
  58. union lower_chunk *next;
  59. unsigned long data[LOWER_SIZE]; // 2K in size
  60. };
  61. union upper_chunk {
  62. union upper_chunk *next;
  63. union lower_chunk *data[UPPER2_SIZE]; // 1 or 2K in size
  64. };
  65. struct trace_pid_list {
  66. raw_spinlock_t lock;
  67. struct irq_work refill_irqwork;
  68. union upper_chunk *upper[UPPER1_SIZE]; // 1 or 2K in size
  69. union upper_chunk *upper_list;
  70. union lower_chunk *lower_list;
  71. int free_upper_chunks;
  72. int free_lower_chunks;
  73. };
  74. #endif /* _TRACE_INTERNAL_PID_LIST_H */