generic_pt.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ========================
  3. Generic Radix Page Table
  4. ========================
  5. .. kernel-doc:: include/linux/generic_pt/common.h
  6. :doc: Generic Radix Page Table
  7. .. kernel-doc:: drivers/iommu/generic_pt/pt_defs.h
  8. :doc: Generic Page Table Language
  9. Usage
  10. =====
  11. Generic PT is structured as a multi-compilation system. Since each format
  12. provides an API using a common set of names there can be only one format active
  13. within a compilation unit. This design avoids function pointers around the low
  14. level API.
  15. Instead the function pointers can end up at the higher level API (i.e.
  16. map/unmap, etc.) and the per-format code can be directly inlined into the
  17. per-format compilation unit. For something like IOMMU each format will be
  18. compiled into a per-format IOMMU operations kernel module.
  19. For this to work the .c file for each compilation unit will include both the
  20. format headers and the generic code for the implementation. For instance in an
  21. implementation compilation unit the headers would normally be included as
  22. follows:
  23. generic_pt/fmt/iommu_amdv1.c::
  24. #include <linux/generic_pt/common.h>
  25. #include "defs_amdv1.h"
  26. #include "../pt_defs.h"
  27. #include "amdv1.h"
  28. #include "../pt_common.h"
  29. #include "../pt_iter.h"
  30. #include "../iommu_pt.h" /* The IOMMU implementation */
  31. iommu_pt.h includes definitions that will generate the operations functions for
  32. map/unmap/etc. using the definitions provided by AMDv1. The resulting module
  33. will have exported symbols named like pt_iommu_amdv1_init().
  34. Refer to drivers/iommu/generic_pt/fmt/iommu_template.h for an example of how the
  35. IOMMU implementation uses multi-compilation to generate per-format ops structs
  36. pointers.
  37. The format code is written so that the common names arise from #defines to
  38. distinct format specific names. This is intended to aid debuggability by
  39. avoiding symbol clashes across all the different formats.
  40. Exported symbols and other global names are mangled using a per-format string
  41. via the NS() helper macro.
  42. The format uses struct pt_common as the top-level struct for the table,
  43. and each format will have its own struct pt_xxx which embeds it to store
  44. format-specific information.
  45. The implementation will further wrap struct pt_common in its own top-level
  46. struct, such as struct pt_iommu_amdv1.
  47. Format functions at the struct pt_common level
  48. ----------------------------------------------
  49. .. kernel-doc:: include/linux/generic_pt/common.h
  50. :identifiers:
  51. .. kernel-doc:: drivers/iommu/generic_pt/pt_common.h
  52. Iteration Helpers
  53. -----------------
  54. .. kernel-doc:: drivers/iommu/generic_pt/pt_iter.h
  55. Writing a Format
  56. ----------------
  57. It is best to start from a simple format that is similar to the target. x86_64
  58. is usually a good reference for something simple, and AMDv1 is something fairly
  59. complete.
  60. The required inline functions need to be implemented in the format header.
  61. These should all follow the standard pattern of::
  62. static inline pt_oaddr_t amdv1pt_entry_oa(const struct pt_state *pts)
  63. {
  64. [..]
  65. }
  66. #define pt_entry_oa amdv1pt_entry_oa
  67. where a uniquely named per-format inline function provides the implementation
  68. and a define maps it to the generic name. This is intended to make debug symbols
  69. work better. inline functions should always be used as the prototypes in
  70. pt_common.h will cause the compiler to validate the function signature to
  71. prevent errors.
  72. Review pt_fmt_defaults.h to understand some of the optional inlines.
  73. Once the format compiles then it should be run through the generic page table
  74. kunit test in kunit_generic_pt.h using kunit. For example::
  75. $ tools/testing/kunit/kunit.py run --build_dir build_kunit_x86_64 --arch x86_64 --kunitconfig ./drivers/iommu/generic_pt/.kunitconfig amdv1_fmt_test.*
  76. [...]
  77. [11:15:08] Testing complete. Ran 9 tests: passed: 9
  78. [11:15:09] Elapsed time: 3.137s total, 0.001s configuring, 2.368s building, 0.311s running
  79. The generic tests are intended to prove out the format functions and give
  80. clearer failures to speed up finding the problems. Once those pass then the
  81. entire kunit suite should be run.
  82. IOMMU Invalidation Features
  83. ---------------------------
  84. Invalidation is how the page table algorithms synchronize with a HW cache of the
  85. page table memory, typically called the TLB (or IOTLB for IOMMU cases).
  86. The TLB can store present PTEs, non-present PTEs and table pointers, depending
  87. on its design. Every HW has its own approach on how to describe what has changed
  88. to have changed items removed from the TLB.
  89. PT_FEAT_FLUSH_RANGE
  90. ~~~~~~~~~~~~~~~~~~~
  91. PT_FEAT_FLUSH_RANGE is the easiest scheme to understand. It tries to generate a
  92. single range invalidation for each operation, over-invalidating if there are
  93. gaps of VA that don't need invalidation. This trades off impacted VA for number
  94. of invalidation operations. It does not keep track of what is being invalidated;
  95. however, if pages have to be freed then page table pointers have to be cleaned
  96. from the walk cache. The range can start/end at any page boundary.
  97. PT_FEAT_FLUSH_RANGE_NO_GAPS
  98. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  99. PT_FEAT_FLUSH_RANGE_NO_GAPS is similar to PT_FEAT_FLUSH_RANGE; however, it tries
  100. to minimize the amount of impacted VA by issuing extra flush operations. This is
  101. useful if the cost of processing VA is very high, for instance because a
  102. hypervisor is processing the page table with a shadowing algorithm.