pt_iter.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES
  4. *
  5. * Iterators for Generic Page Table
  6. */
  7. #ifndef __GENERIC_PT_PT_ITER_H
  8. #define __GENERIC_PT_PT_ITER_H
  9. #include "pt_common.h"
  10. #include <linux/errno.h>
  11. /*
  12. * Use to mangle symbols so that backtraces and the symbol table are
  13. * understandable. Any non-inlined function should get mangled like this.
  14. */
  15. #define NS(fn) CONCATENATE(PTPFX, fn)
  16. /**
  17. * pt_check_range() - Validate the range can be iterated
  18. * @range: Range to validate
  19. *
  20. * Check that VA and last_va fall within the permitted range of VAs. If the
  21. * format is using PT_FEAT_SIGN_EXTEND then this also checks the sign extension
  22. * is correct.
  23. */
  24. static inline int pt_check_range(struct pt_range *range)
  25. {
  26. pt_vaddr_t prefix;
  27. PT_WARN_ON(!range->max_vasz_lg2);
  28. if (pt_feature(range->common, PT_FEAT_SIGN_EXTEND)) {
  29. PT_WARN_ON(range->common->max_vasz_lg2 != range->max_vasz_lg2);
  30. prefix = fvalog2_div(range->va, range->max_vasz_lg2 - 1) ?
  31. PT_VADDR_MAX :
  32. 0;
  33. } else {
  34. prefix = pt_full_va_prefix(range->common);
  35. }
  36. if (!fvalog2_div_eq(range->va, prefix, range->max_vasz_lg2) ||
  37. !fvalog2_div_eq(range->last_va, prefix, range->max_vasz_lg2))
  38. return -ERANGE;
  39. return 0;
  40. }
  41. /**
  42. * pt_index_to_va() - Update range->va to the current pts->index
  43. * @pts: Iteration State
  44. *
  45. * Adjust range->va to match the current index. This is done in a lazy manner
  46. * since computing the VA takes several instructions and is rarely required.
  47. */
  48. static inline void pt_index_to_va(struct pt_state *pts)
  49. {
  50. pt_vaddr_t lower_va;
  51. lower_va = log2_mul(pts->index, pt_table_item_lg2sz(pts));
  52. pts->range->va = fvalog2_set_mod(pts->range->va, lower_va,
  53. pt_table_oa_lg2sz(pts));
  54. }
  55. /*
  56. * Add index_count_lg2 number of entries to pts's VA and index. The VA will be
  57. * adjusted to the end of the contiguous block if it is currently in the middle.
  58. */
  59. static inline void _pt_advance(struct pt_state *pts,
  60. unsigned int index_count_lg2)
  61. {
  62. pts->index = log2_set_mod(pts->index + log2_to_int(index_count_lg2), 0,
  63. index_count_lg2);
  64. }
  65. /**
  66. * pt_entry_fully_covered() - Check if the item or entry is entirely contained
  67. * within pts->range
  68. * @pts: Iteration State
  69. * @oasz_lg2: The size of the item to check, pt_table_item_lg2sz() or
  70. * pt_entry_oa_lg2sz()
  71. *
  72. * Returns: true if the item is fully enclosed by the pts->range.
  73. */
  74. static inline bool pt_entry_fully_covered(const struct pt_state *pts,
  75. unsigned int oasz_lg2)
  76. {
  77. struct pt_range *range = pts->range;
  78. /* Range begins at the start of the entry */
  79. if (log2_mod(pts->range->va, oasz_lg2))
  80. return false;
  81. /* Range ends past the end of the entry */
  82. if (!log2_div_eq(range->va, range->last_va, oasz_lg2))
  83. return true;
  84. /* Range ends at the end of the entry */
  85. return log2_mod_eq_max(range->last_va, oasz_lg2);
  86. }
  87. /**
  88. * pt_range_to_index() - Starting index for an iteration
  89. * @pts: Iteration State
  90. *
  91. * Return: the starting index for the iteration in pts.
  92. */
  93. static inline unsigned int pt_range_to_index(const struct pt_state *pts)
  94. {
  95. unsigned int isz_lg2 = pt_table_item_lg2sz(pts);
  96. PT_WARN_ON(pts->level > pts->range->top_level);
  97. if (pts->range->top_level == pts->level)
  98. return log2_div(fvalog2_mod(pts->range->va,
  99. pts->range->max_vasz_lg2),
  100. isz_lg2);
  101. return log2_mod(log2_div(pts->range->va, isz_lg2),
  102. pt_num_items_lg2(pts));
  103. }
  104. /**
  105. * pt_range_to_end_index() - Ending index iteration
  106. * @pts: Iteration State
  107. *
  108. * Return: the last index for the iteration in pts.
  109. */
  110. static inline unsigned int pt_range_to_end_index(const struct pt_state *pts)
  111. {
  112. unsigned int isz_lg2 = pt_table_item_lg2sz(pts);
  113. struct pt_range *range = pts->range;
  114. unsigned int num_entries_lg2;
  115. if (range->va == range->last_va)
  116. return pts->index + 1;
  117. if (pts->range->top_level == pts->level)
  118. return log2_div(fvalog2_mod(pts->range->last_va,
  119. pts->range->max_vasz_lg2),
  120. isz_lg2) +
  121. 1;
  122. num_entries_lg2 = pt_num_items_lg2(pts);
  123. /* last_va falls within this table */
  124. if (log2_div_eq(range->va, range->last_va, num_entries_lg2 + isz_lg2))
  125. return log2_mod(log2_div(pts->range->last_va, isz_lg2),
  126. num_entries_lg2) +
  127. 1;
  128. return log2_to_int(num_entries_lg2);
  129. }
  130. static inline void _pt_iter_first(struct pt_state *pts)
  131. {
  132. pts->index = pt_range_to_index(pts);
  133. pts->end_index = pt_range_to_end_index(pts);
  134. PT_WARN_ON(pts->index > pts->end_index);
  135. }
  136. static inline bool _pt_iter_load(struct pt_state *pts)
  137. {
  138. if (pts->index >= pts->end_index)
  139. return false;
  140. pt_load_entry(pts);
  141. return true;
  142. }
  143. /**
  144. * pt_next_entry() - Advance pts to the next entry
  145. * @pts: Iteration State
  146. *
  147. * Update pts to go to the next index at this level. If pts is pointing at a
  148. * contiguous entry then the index may advance my more than one.
  149. */
  150. static inline void pt_next_entry(struct pt_state *pts)
  151. {
  152. if (pts->type == PT_ENTRY_OA &&
  153. !__builtin_constant_p(pt_entry_num_contig_lg2(pts) == 0))
  154. _pt_advance(pts, pt_entry_num_contig_lg2(pts));
  155. else
  156. pts->index++;
  157. pt_index_to_va(pts);
  158. }
  159. /**
  160. * for_each_pt_level_entry() - For loop wrapper over entries in the range
  161. * @pts: Iteration State
  162. *
  163. * This is the basic iteration primitive. It iterates over all the entries in
  164. * pts->range that fall within the pts's current table level. Each step does
  165. * pt_load_entry(pts).
  166. */
  167. #define for_each_pt_level_entry(pts) \
  168. for (_pt_iter_first(pts); _pt_iter_load(pts); pt_next_entry(pts))
  169. /**
  170. * pt_load_single_entry() - Version of pt_load_entry() usable within a walker
  171. * @pts: Iteration State
  172. *
  173. * Alternative to for_each_pt_level_entry() if the walker function uses only a
  174. * single entry.
  175. */
  176. static inline enum pt_entry_type pt_load_single_entry(struct pt_state *pts)
  177. {
  178. pts->index = pt_range_to_index(pts);
  179. pt_load_entry(pts);
  180. return pts->type;
  181. }
  182. static __always_inline struct pt_range _pt_top_range(struct pt_common *common,
  183. uintptr_t top_of_table)
  184. {
  185. struct pt_range range = {
  186. .common = common,
  187. .top_table =
  188. (struct pt_table_p *)(top_of_table &
  189. ~(uintptr_t)PT_TOP_LEVEL_MASK),
  190. .top_level = top_of_table % (1 << PT_TOP_LEVEL_BITS),
  191. };
  192. struct pt_state pts = { .range = &range, .level = range.top_level };
  193. unsigned int max_vasz_lg2;
  194. max_vasz_lg2 = common->max_vasz_lg2;
  195. if (pt_feature(common, PT_FEAT_DYNAMIC_TOP) &&
  196. pts.level != PT_MAX_TOP_LEVEL)
  197. max_vasz_lg2 = min_t(unsigned int, common->max_vasz_lg2,
  198. pt_num_items_lg2(&pts) +
  199. pt_table_item_lg2sz(&pts));
  200. /*
  201. * The top range will default to the lower region only with sign extend.
  202. */
  203. range.max_vasz_lg2 = max_vasz_lg2;
  204. if (pt_feature(common, PT_FEAT_SIGN_EXTEND))
  205. max_vasz_lg2--;
  206. range.va = fvalog2_set_mod(pt_full_va_prefix(common), 0, max_vasz_lg2);
  207. range.last_va =
  208. fvalog2_set_mod_max(pt_full_va_prefix(common), max_vasz_lg2);
  209. return range;
  210. }
  211. /**
  212. * pt_top_range() - Return a range that spans part of the top level
  213. * @common: Table
  214. *
  215. * For PT_FEAT_SIGN_EXTEND this will return the lower range, and cover half the
  216. * total page table. Otherwise it returns the entire page table.
  217. */
  218. static __always_inline struct pt_range pt_top_range(struct pt_common *common)
  219. {
  220. /*
  221. * The top pointer can change without locking. We capture the value and
  222. * it's level here and are safe to walk it so long as both values are
  223. * captured without tearing.
  224. */
  225. return _pt_top_range(common, READ_ONCE(common->top_of_table));
  226. }
  227. /**
  228. * pt_all_range() - Return a range that spans the entire page table
  229. * @common: Table
  230. *
  231. * The returned range spans the whole page table. Due to how PT_FEAT_SIGN_EXTEND
  232. * is supported range->va and range->last_va will be incorrect during the
  233. * iteration and must not be accessed.
  234. */
  235. static inline struct pt_range pt_all_range(struct pt_common *common)
  236. {
  237. struct pt_range range = pt_top_range(common);
  238. if (!pt_feature(common, PT_FEAT_SIGN_EXTEND))
  239. return range;
  240. /*
  241. * Pretend the table is linear from 0 without a sign extension. This
  242. * generates the correct indexes for iteration.
  243. */
  244. range.last_va = fvalog2_set_mod_max(0, range.max_vasz_lg2);
  245. return range;
  246. }
  247. /**
  248. * pt_upper_range() - Return a range that spans part of the top level
  249. * @common: Table
  250. *
  251. * For PT_FEAT_SIGN_EXTEND this will return the upper range, and cover half the
  252. * total page table. Otherwise it returns the entire page table.
  253. */
  254. static inline struct pt_range pt_upper_range(struct pt_common *common)
  255. {
  256. struct pt_range range = pt_top_range(common);
  257. if (!pt_feature(common, PT_FEAT_SIGN_EXTEND))
  258. return range;
  259. range.va = fvalog2_set_mod(PT_VADDR_MAX, 0, range.max_vasz_lg2 - 1);
  260. range.last_va = PT_VADDR_MAX;
  261. return range;
  262. }
  263. /**
  264. * pt_make_range() - Return a range that spans part of the table
  265. * @common: Table
  266. * @va: Start address
  267. * @last_va: Last address
  268. *
  269. * The caller must validate the range with pt_check_range() before using it.
  270. */
  271. static __always_inline struct pt_range
  272. pt_make_range(struct pt_common *common, pt_vaddr_t va, pt_vaddr_t last_va)
  273. {
  274. struct pt_range range =
  275. _pt_top_range(common, READ_ONCE(common->top_of_table));
  276. range.va = va;
  277. range.last_va = last_va;
  278. return range;
  279. }
  280. /*
  281. * Span a slice of the table starting at a lower table level from an active
  282. * walk.
  283. */
  284. static __always_inline struct pt_range
  285. pt_make_child_range(const struct pt_range *parent, pt_vaddr_t va,
  286. pt_vaddr_t last_va)
  287. {
  288. struct pt_range range = *parent;
  289. range.va = va;
  290. range.last_va = last_va;
  291. PT_WARN_ON(last_va < va);
  292. PT_WARN_ON(pt_check_range(&range));
  293. return range;
  294. }
  295. /**
  296. * pt_init() - Initialize a pt_state on the stack
  297. * @range: Range pointer to embed in the state
  298. * @level: Table level for the state
  299. * @table: Pointer to the table memory at level
  300. *
  301. * Helper to initialize the on-stack pt_state from walker arguments.
  302. */
  303. static __always_inline struct pt_state
  304. pt_init(struct pt_range *range, unsigned int level, struct pt_table_p *table)
  305. {
  306. struct pt_state pts = {
  307. .range = range,
  308. .table = table,
  309. .level = level,
  310. };
  311. return pts;
  312. }
  313. /**
  314. * pt_init_top() - Initialize a pt_state on the stack
  315. * @range: Range pointer to embed in the state
  316. *
  317. * The pt_state points to the top most level.
  318. */
  319. static __always_inline struct pt_state pt_init_top(struct pt_range *range)
  320. {
  321. return pt_init(range, range->top_level, range->top_table);
  322. }
  323. typedef int (*pt_level_fn_t)(struct pt_range *range, void *arg,
  324. unsigned int level, struct pt_table_p *table);
  325. /**
  326. * pt_descend() - Recursively invoke the walker for the lower level
  327. * @pts: Iteration State
  328. * @arg: Value to pass to the function
  329. * @fn: Walker function to call
  330. *
  331. * pts must point to a table item. Invoke fn as a walker on the table
  332. * pts points to.
  333. */
  334. static __always_inline int pt_descend(struct pt_state *pts, void *arg,
  335. pt_level_fn_t fn)
  336. {
  337. int ret;
  338. if (PT_WARN_ON(!pts->table_lower))
  339. return -EINVAL;
  340. ret = (*fn)(pts->range, arg, pts->level - 1, pts->table_lower);
  341. return ret;
  342. }
  343. /**
  344. * pt_walk_range() - Walk over a VA range
  345. * @range: Range pointer
  346. * @fn: Walker function to call
  347. * @arg: Value to pass to the function
  348. *
  349. * Walk over a VA range. The caller should have done a validity check, at
  350. * least calling pt_check_range(), when building range. The walk will
  351. * start at the top most table.
  352. */
  353. static __always_inline int pt_walk_range(struct pt_range *range,
  354. pt_level_fn_t fn, void *arg)
  355. {
  356. return fn(range, arg, range->top_level, range->top_table);
  357. }
  358. /*
  359. * pt_walk_descend() - Recursively invoke the walker for a slice of a lower
  360. * level
  361. * @pts: Iteration State
  362. * @va: Start address
  363. * @last_va: Last address
  364. * @fn: Walker function to call
  365. * @arg: Value to pass to the function
  366. *
  367. * With pts pointing at a table item this will descend and over a slice of the
  368. * lower table. The caller must ensure that va/last_va are within the table
  369. * item. This creates a new walk and does not alter pts or pts->range.
  370. */
  371. static __always_inline int pt_walk_descend(const struct pt_state *pts,
  372. pt_vaddr_t va, pt_vaddr_t last_va,
  373. pt_level_fn_t fn, void *arg)
  374. {
  375. struct pt_range range = pt_make_child_range(pts->range, va, last_va);
  376. if (PT_WARN_ON(!pt_can_have_table(pts)) ||
  377. PT_WARN_ON(!pts->table_lower))
  378. return -EINVAL;
  379. return fn(&range, arg, pts->level - 1, pts->table_lower);
  380. }
  381. /*
  382. * pt_walk_descend_all() - Recursively invoke the walker for a table item
  383. * @parent_pts: Iteration State
  384. * @fn: Walker function to call
  385. * @arg: Value to pass to the function
  386. *
  387. * With pts pointing at a table item this will descend and over the entire lower
  388. * table. This creates a new walk and does not alter pts or pts->range.
  389. */
  390. static __always_inline int
  391. pt_walk_descend_all(const struct pt_state *parent_pts, pt_level_fn_t fn,
  392. void *arg)
  393. {
  394. unsigned int isz_lg2 = pt_table_item_lg2sz(parent_pts);
  395. return pt_walk_descend(parent_pts,
  396. log2_set_mod(parent_pts->range->va, 0, isz_lg2),
  397. log2_set_mod_max(parent_pts->range->va, isz_lg2),
  398. fn, arg);
  399. }
  400. /**
  401. * pt_range_slice() - Return a range that spans indexes
  402. * @pts: Iteration State
  403. * @start_index: Starting index within pts
  404. * @end_index: Ending index within pts
  405. *
  406. * Create a range than spans an index range of the current table level
  407. * pt_state points at.
  408. */
  409. static inline struct pt_range pt_range_slice(const struct pt_state *pts,
  410. unsigned int start_index,
  411. unsigned int end_index)
  412. {
  413. unsigned int table_lg2sz = pt_table_oa_lg2sz(pts);
  414. pt_vaddr_t last_va;
  415. pt_vaddr_t va;
  416. va = fvalog2_set_mod(pts->range->va,
  417. log2_mul(start_index, pt_table_item_lg2sz(pts)),
  418. table_lg2sz);
  419. last_va = fvalog2_set_mod(
  420. pts->range->va,
  421. log2_mul(end_index, pt_table_item_lg2sz(pts)) - 1, table_lg2sz);
  422. return pt_make_child_range(pts->range, va, last_va);
  423. }
  424. /**
  425. * pt_top_memsize_lg2()
  426. * @common: Table
  427. * @top_of_table: Top of table value from _pt_top_set()
  428. *
  429. * Compute the allocation size of the top table. For PT_FEAT_DYNAMIC_TOP this
  430. * will compute the top size assuming the table will grow.
  431. */
  432. static inline unsigned int pt_top_memsize_lg2(struct pt_common *common,
  433. uintptr_t top_of_table)
  434. {
  435. struct pt_range range = _pt_top_range(common, top_of_table);
  436. struct pt_state pts = pt_init_top(&range);
  437. unsigned int num_items_lg2;
  438. num_items_lg2 = common->max_vasz_lg2 - pt_table_item_lg2sz(&pts);
  439. if (range.top_level != PT_MAX_TOP_LEVEL &&
  440. pt_feature(common, PT_FEAT_DYNAMIC_TOP))
  441. num_items_lg2 = min(num_items_lg2, pt_num_items_lg2(&pts));
  442. /* Round up the allocation size to the minimum alignment */
  443. return max(ffs_t(u64, PT_TOP_PHYS_MASK),
  444. num_items_lg2 + ilog2(PT_ITEM_WORD_SIZE));
  445. }
  446. /**
  447. * pt_compute_best_pgsize() - Determine the best page size for leaf entries
  448. * @pgsz_bitmap: Permitted page sizes
  449. * @va: Starting virtual address for the leaf entry
  450. * @last_va: Last virtual address for the leaf entry, sets the max page size
  451. * @oa: Starting output address for the leaf entry
  452. *
  453. * Compute the largest page size for va, last_va, and oa together and return it
  454. * in lg2. The largest page size depends on the format's supported page sizes at
  455. * this level, and the relative alignment of the VA and OA addresses. 0 means
  456. * the OA cannot be stored with the provided pgsz_bitmap.
  457. */
  458. static inline unsigned int pt_compute_best_pgsize(pt_vaddr_t pgsz_bitmap,
  459. pt_vaddr_t va,
  460. pt_vaddr_t last_va,
  461. pt_oaddr_t oa)
  462. {
  463. unsigned int best_pgsz_lg2;
  464. unsigned int pgsz_lg2;
  465. pt_vaddr_t len = last_va - va + 1;
  466. pt_vaddr_t mask;
  467. if (PT_WARN_ON(va >= last_va))
  468. return 0;
  469. /*
  470. * Given a VA/OA pair the best page size is the largest page size
  471. * where:
  472. *
  473. * 1) VA and OA start at the page. Bitwise this is the count of least
  474. * significant 0 bits.
  475. * This also implies that last_va/oa has the same prefix as va/oa.
  476. */
  477. mask = va | oa;
  478. /*
  479. * 2) The page size is not larger than the last_va (length). Since page
  480. * sizes are always power of two this can't be larger than the
  481. * largest power of two factor of the length.
  482. */
  483. mask |= log2_to_int(vafls(len) - 1);
  484. best_pgsz_lg2 = vaffs(mask);
  485. /* Choose the highest bit <= best_pgsz_lg2 */
  486. if (best_pgsz_lg2 < PT_VADDR_MAX_LG2 - 1)
  487. pgsz_bitmap = log2_mod(pgsz_bitmap, best_pgsz_lg2 + 1);
  488. pgsz_lg2 = vafls(pgsz_bitmap);
  489. if (!pgsz_lg2)
  490. return 0;
  491. pgsz_lg2--;
  492. PT_WARN_ON(log2_mod(va, pgsz_lg2) != 0);
  493. PT_WARN_ON(oalog2_mod(oa, pgsz_lg2) != 0);
  494. PT_WARN_ON(va + log2_to_int(pgsz_lg2) - 1 > last_va);
  495. PT_WARN_ON(!log2_div_eq(va, va + log2_to_int(pgsz_lg2) - 1, pgsz_lg2));
  496. PT_WARN_ON(
  497. !oalog2_div_eq(oa, oa + log2_to_int(pgsz_lg2) - 1, pgsz_lg2));
  498. return pgsz_lg2;
  499. }
  500. #define _PT_MAKE_CALL_LEVEL(fn) \
  501. static __always_inline int fn(struct pt_range *range, void *arg, \
  502. unsigned int level, \
  503. struct pt_table_p *table) \
  504. { \
  505. static_assert(PT_MAX_TOP_LEVEL <= 5); \
  506. if (level == 0) \
  507. return CONCATENATE(fn, 0)(range, arg, 0, table); \
  508. if (level == 1 || PT_MAX_TOP_LEVEL == 1) \
  509. return CONCATENATE(fn, 1)(range, arg, 1, table); \
  510. if (level == 2 || PT_MAX_TOP_LEVEL == 2) \
  511. return CONCATENATE(fn, 2)(range, arg, 2, table); \
  512. if (level == 3 || PT_MAX_TOP_LEVEL == 3) \
  513. return CONCATENATE(fn, 3)(range, arg, 3, table); \
  514. if (level == 4 || PT_MAX_TOP_LEVEL == 4) \
  515. return CONCATENATE(fn, 4)(range, arg, 4, table); \
  516. return CONCATENATE(fn, 5)(range, arg, 5, table); \
  517. }
  518. static inline int __pt_make_level_fn_err(struct pt_range *range, void *arg,
  519. unsigned int unused_level,
  520. struct pt_table_p *table)
  521. {
  522. static_assert(PT_MAX_TOP_LEVEL <= 5);
  523. return -EPROTOTYPE;
  524. }
  525. #define __PT_MAKE_LEVEL_FN(fn, level, descend_fn, do_fn) \
  526. static inline int fn(struct pt_range *range, void *arg, \
  527. unsigned int unused_level, \
  528. struct pt_table_p *table) \
  529. { \
  530. return do_fn(range, arg, level, table, descend_fn); \
  531. }
  532. /**
  533. * PT_MAKE_LEVELS() - Build an unwound walker
  534. * @fn: Name of the walker function
  535. * @do_fn: Function to call at each level
  536. *
  537. * This builds a function call tree that can be fully inlined.
  538. * The caller must provide a function body in an __always_inline function::
  539. *
  540. * static __always_inline int do_fn(struct pt_range *range, void *arg,
  541. * unsigned int level, struct pt_table_p *table,
  542. * pt_level_fn_t descend_fn)
  543. *
  544. * An inline function will be created for each table level that calls do_fn with
  545. * a compile time constant for level and a pointer to the next lower function.
  546. * This generates an optimally inlined walk where each of the functions sees a
  547. * constant level and can codegen the exact constants/etc for that level.
  548. *
  549. * Note this can produce a lot of code!
  550. */
  551. #define PT_MAKE_LEVELS(fn, do_fn) \
  552. __PT_MAKE_LEVEL_FN(CONCATENATE(fn, 0), 0, __pt_make_level_fn_err, \
  553. do_fn); \
  554. __PT_MAKE_LEVEL_FN(CONCATENATE(fn, 1), 1, CONCATENATE(fn, 0), do_fn); \
  555. __PT_MAKE_LEVEL_FN(CONCATENATE(fn, 2), 2, CONCATENATE(fn, 1), do_fn); \
  556. __PT_MAKE_LEVEL_FN(CONCATENATE(fn, 3), 3, CONCATENATE(fn, 2), do_fn); \
  557. __PT_MAKE_LEVEL_FN(CONCATENATE(fn, 4), 4, CONCATENATE(fn, 3), do_fn); \
  558. __PT_MAKE_LEVEL_FN(CONCATENATE(fn, 5), 5, CONCATENATE(fn, 4), do_fn); \
  559. _PT_MAKE_CALL_LEVEL(fn)
  560. #endif