Issue #3487 has been reported by tuxillo.

----------------------------------------
Bug #3487: pmap: pmap_page_set_memattr() on one page changes the memory type of 
its whole 2MB direct-map page
http://bugs.dragonflybsd.org/issues/3487

* Author: tuxillo
* Status: New
* Priority: Normal
* Target version: 6.6
* Start date: 2026-11-29
----------------------------------------
As of master 98c93bc104, pmap_change_attr() on a direct-map address 
(sys/platform/pc64/x86_64/pmap.c) rewrites the cache bits of the 2MB page that 
maps it, because the direct map is built from 2MB pages. 
pmap_page_set_memattr(), which TTM calls on each page it makes write-combining 
(ttm_tt.c, ttm_bo_vm.c), as does i915 through set_pages_array_wc() for its 
page-table pages (i915_gem_gtt.c), so changes the memory type of the 511 pages 
around it as well: the kernel's direct map of them becomes WC, and setting the 
page back to WB makes all 512 WB again, including pages there that are still WC 
through their own mappings. x86 leaves such mismatched memory types undefined. 
98c93bc104 removed one caller, i915's WC mmap, and its message describes the 
same direct-map effect; the callers above remain.

I ran into it on a desktop with a local TTM change that takes WC pages from 
ordinary RAM; master takes them from the 128 MB reserve, but reaches the same 
code whenever TTM changes the caching of pages it already has. After a GL 
session, fork/exec-heavy work ran 7 to 10 times slower (a kernel build took 35 
minutes instead of 5). I haven't retested that with the patch.

The attached patch splits a 2MB direct-map page into 512 4KB pages, with the 
same memory and attributes, when a change covers only part of it, then changes 
only the pages asked for; the split stays. A change of a whole, aligned 2MB 
page still rewrites the 2MB entry. A range can only partly cover its first and 
last 2MB page, so at most two page table pages are needed; they are allocated 
before the new spinlock is taken, and if none can be had the whole 2MB page is 
changed as before, with a console message. FreeBSD's pmap_change_attr() also 
demotes the 2MB page (pmap_demote_pde()).

TESTING: on master efcec8993c (pmap.c has changed since only in pmap_enter(), 
by 98c93bc104; the patch applies on 98c93bc104 with a 6-line offset), in an 
8-vCPU QEMU guest under nvmm, with the attached bfutest.c module, which reads 
from the page tables (walked from %cr3, decoded with the PAT MSR) the memory 
type the direct map gives a page. It takes two pages A and B in one 2MB page 
and a third page N there, sets A WC, then B WC and A back to WB, then B back to 
WB; then sets a 2MB-aligned contigmalloc WC as a whole, one page of it back to 
WB, and all of it back to WB. Without the patch, setting A WC made A, B and N 
WC at 2MB, setting A back to WB took B's WC with it, and setting one page of 
the 2MB block back to WB made the whole block WB: every run failed. With the 
patch, only the pages asked for change, as 4KB pages, and the whole-block 
change keeps the 2MB page; three runs and 30 more, each with hold/release 
cycles of 2000 scattered WC pages, passed while tlbchurn kept the cpus busy 
with TLB shootdowns. The guest can't show speed differences, because nvmm maps 
guest memory with EPT_PG_IGNORE_PAT.

To run it: make SYSDIR=/usr/src/sys with the attached Makefile, kldload 
./bfutest.ko, sysctl debug.bfutest_run=1, dmesg | grep bfutest.

Separately, from reading the code: a change of a whole 2MB page invalidates 512 
pages, which pmap_inval_smp() turns into a %cr3 reload when there are more than 
128, and a %cr3 reload leaves the direct map's global (PG_G) TLB entries in 
place. That isn't part of this patch.

---Files--------------------------------
bfutest.c (8.83 KB)
Makefile (54 Bytes)
0001-pmap-change-the-memory-type-of-DMAP-pages-one-4KB-pa.patch (7.13 KB)


-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://bugs.dragonflybsd.org/my/account

Reply via email to