This is an early RFC. While all reviews are welcome, reviewing this code now will be a waste of time for the x86 subsystem maintainers. I would, however, appreciate a preliminary review from the folks on the to and cc list. I'm posting it to the list in case anyone else is interested in seeing this early version. Dave Hansen: I need your ack before this goes to the maintainers. Here it goes: On x86_64, Linux has direct mapping of almost all physical memory. For performance reasons, this mapping is usually set as large page like 2M or 1G per hardware's capability with read, write and non-execute protection. There are cases where some pages have to change their protection to RO and eXecutable, like pages that host module code or bpf prog. When these pages' protection are changed, the corresponding large mapping that cover these pages will have to be splitted into 4K first and then individual 4k page's protection changed accordingly, i.e. unaffected pages keep their original protection as RW and NX while affected pages' protection changed to RO and X. There is a problem due to this split: the large mapping will remain splitted even after the affected pages' protection are changed back to RW and NX, like when the module is unloaded or bpf progs are freed. After system runs a long time, there can be more and more large mapping being splitted, causing more and more dTLB misses and overall system performance getting hurt[1]. For this reason, people tried some techniques to reduce the harm of large mapping beling splitted, like bpf_prog_pack[2] which packs multiple bpf progs into a single page instead of allocating and changing one page's protection for each bpf prog. This approach made large mapping split happen much fewer. This patchset addresses this problem in another way: it merges splitted mappings back to a large mapping when protections of all entries of the splitted small mapping page table become same again, e.g. when the page whose protection was changed to RO+X now has its protection changed back to RW+NX due to reasons like module unload, bpf prog free, etc. and all other entries' protection are also RW+NX. One final note is, with features like bpf_prog_pack etc., there can be much fewer large mapping split IIUC; also, this patchset can not help when the page which has its protection changed keeps in use. So my take on this large mapping split problem is: to get the most value of keeping large mapping intact, features like bpf_prog_pack is important. This patchset can help to further reduce large mapping split when in use page that has special protection set finally gets released. [1]: http://lkml.kernel.org/r/CAPhsuW4eAm9QrAxhZMJu-bmvHnjWjuw86gFZzTHRaMEaeFhAxw@xxxxxxxxxxxxxx [2]: https://lore.kernel.org/lkml/20220204185742.271030-1-song@xxxxxxxxxx/ Aaron Lu (4): x86/mm/cpa: restore global bit when page is present x86/mm/cpa: merge splitted direct mapping when possible x86/mm/cpa: add merge event counter x86/mm/cpa: add a test interface to split direct map arch/x86/mm/pat/set_memory.c | 411 +++++++++++++++++++++++++++++++++- include/linux/mm_types.h | 6 + include/linux/page-flags.h | 6 + include/linux/vm_event_item.h | 2 + mm/vmstat.c | 2 + 5 files changed, 420 insertions(+), 7 deletions(-) -- 2.37.1