mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*()
@ 2026-11-17  9:49 Christian
  2026-11-17  9:49 ` [PATCH 1/2] x86/mm/pat: skip HIGHMEM pages in a page array Christian
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Christian @ 2026-11-17  9:49 UTC (permalink / raw)
  To: Dave Hansen, Andy Lutomirski, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86
  Cc: H. Peter Anvin, linux-kernel

On i386 without PAE nearly all memory is HIGHMEM, and a DRM driver that
keeps its shmem buffers there (drm_gem_shmem_get_pages_locked() ->
set_pages_array_wc()) hits two problems in the page-array CPA paths:

1. The CPA walk takes __cpa_addr()'s 0 for a HIGHMEM page as a virtual
   address and oopses in __cpa_process_fault()'s warning path, with the
   CPA lock held, so every later CPA user hangs.

2. _set_pages_array() and set_pages_array_wb() skip HIGHMEM pages when
   they reserve and free the memory type, so a write-combined user mapping
   of such a page is left write-back by PAT. A device that does not snoop
   the CPU's caches then reads stale data.

Patch 1 skips HIGHMEM pages in the CPA walk, as there is nothing there for
it to change. Patch 2 reserves and frees their memory type like any other
page's.

Found and tested on an HP ElitePad 900 G1 (Atom Z2760, Clover Trail, i386
without PAE, 2 GB) running an out-of-tree PowerVR SGX545 DRM driver with
its buffers in HIGHMEM. Before: an oops in set_pages_array_wc() on the
first buffer and an unreachable machine (patch 1), then continuous GPU page
faults and firmware resets until the GPU wedged (patch 2). With both: no
GPU faults or kernel warnings through the driver's uAPI tests (50/50), 100
rendered frames checked against a reference, glmark2 and video playback.
Written and tested on 7.3.4; this series is rebased on v7.4-rc3, where it
builds for i386.

Christian (2):
  x86/mm/pat: skip HIGHMEM pages in a page array
  x86/mm/pat: track the memory type of HIGHMEM pages in a page array

 arch/x86/mm/pat/set_memory.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

-- 
2.47.3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] x86/mm/pat: skip HIGHMEM pages in a page array
  2026-11-17  9:49 [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Christian
@ 2026-11-17  9:49 ` Christian
  2026-11-17  9:49 ` [PATCH 2/2] x86/mm/pat: track the memory type of " Christian
  2026-11-19 19:45 ` [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Dave Hansen
  2 siblings, 0 replies; 7+ messages in thread
From: Christian @ 2026-11-17  9:49 UTC (permalink / raw)
  To: Dave Hansen, Andy Lutomirski, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86
  Cc: H. Peter Anvin, linux-kernel

set_pages_array_wc() walks a page array through __cpa_addr(), which
returns 0 for a HIGHMEM page because it has no linear mapping.
__cpa_process_fault() then takes that 0 for a virtual address and
dereferences it in its warning path, so changing a HIGHMEM page's
caching oopses with a NULL pointer dereference, with the CPA lock held:
every later CPA user (installing a seccomp filter, for one) then spins
forever.

A page without a linear mapping has nothing for CPA to change, so skip
it, and take the pfn from the array: that is where the primary path
reads its page from, and cpa_process_alias() must not derive an address
for a page that has none.

Measured on an HP ElitePad 900 G1 (Atom Z2760, i386 without PAE, kernel
7.3.4) with a graphics driver that keeps its buffers in HIGHMEM: every
buffer oopsed in set_pages_array_wc() -> drm_gem_shmem_get_pages_locked(),
and the machine stayed up but became unreachable.

Signed-off-by: Christian <servidorjarvis1@gmail.com>
---
 arch/x86/mm/pat/set_memory.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index b1d5c5a0f..4565438b6 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1847,6 +1847,17 @@ static int __cpa_process_fault(struct cpa_data
*cpa, unsigned long vaddr,
 		return 0;
 	}

+	/*
+	 * A HIGHMEM page in a page array has no linear mapping, so there is
+	 * nothing to change: __cpa_addr() gives 0 for it. Skip it; its pfn
+	 * keeps cpa_process_alias() off the direct map, where it isn't.
+	 */
+	if ((cpa->flags & CPA_PAGES_ARRAY) && !vaddr) {
+		cpa->numpages = 1;
+		cpa->pfn = page_to_pfn(cpa->pages[cpa->curpage]);
+		return 0;
+	}
+
 	/*
 	 * Ignore the NULL PTE for kernel identity mapping, as it is expected
 	 * to have holes.
-- 
2.47.3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] x86/mm/pat: track the memory type of HIGHMEM pages in a page array
  2026-11-17  9:49 [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Christian
  2026-11-17  9:49 ` [PATCH 1/2] x86/mm/pat: skip HIGHMEM pages in a page array Christian
@ 2026-11-17  9:49 ` Christian
  2026-11-19 19:45 ` [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Dave Hansen
  2 siblings, 0 replies; 7+ messages in thread
From: Christian @ 2026-11-17  9:49 UTC (permalink / raw)
  To: Dave Hansen, Andy Lutomirski, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86
  Cc: H. Peter Anvin, linux-kernel

_set_pages_array() reserves a memory type only for pages that are not
HIGHMEM, and set_pages_array_wb() frees it only for those. The reservation
is what makes PAT apply the type to user mappings of the page; it is not
the CPA walk, which has nothing to change on a page without a linear
mapping.

Without it a driver that keeps its buffers in HIGHMEM gets a
write-combined user mapping that PAT leaves as write-back, so the CPU
keeps its writes in cache and a device that does not snoop them reads
stale memory. With the graphics driver on the ElitePad that showed up as
continuous GPU page faults and microkernel resets until the device wedged;
the same driver with its buffers in lowmem had none.

Signed-off-by: Christian <servidorjarvis1@gmail.com>
---
 arch/x86/mm/pat/set_memory.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 4565438b6..77da61396 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -2578,9 +2578,11 @@ static int _set_pages_array(struct page
**pages, int numpages,
 	int free_idx;
 	int ret;

+	/*
+	 * HIGHMEM pages get their memory type too: CPA has no linear mapping of
+	 * theirs to change, but PAT applies the type to user mappings of them.
+	 */
 	for (i = 0; i < numpages; i++) {
-		if (PageHighMem(pages[i]))
-			continue;
 		start = page_to_pfn(pages[i]) << PAGE_SHIFT;
 		end = start + PAGE_SIZE;
 		if (memtype_reserve(start, end, new_type, NULL))
@@ -2605,8 +2607,6 @@ static int _set_pages_array(struct page **pages,
int numpages,
 err_out:
 	free_idx = i;
 	for (i = 0; i < free_idx; i++) {
-		if (PageHighMem(pages[i]))
-			continue;
 		start = page_to_pfn(pages[i]) << PAGE_SHIFT;
 		end = start + PAGE_SIZE;
 		memtype_free(start, end);
@@ -2648,8 +2648,6 @@ int set_pages_array_wb(struct page **pages, int numpages)
 		return retval;

 	for (i = 0; i < numpages; i++) {
-		if (PageHighMem(pages[i]))
-			continue;
 		start = page_to_pfn(pages[i]) << PAGE_SHIFT;
 		end = start + PAGE_SIZE;
 		memtype_free(start, end);
-- 
2.47.3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*()
  2026-11-17  9:49 [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Christian
  2026-11-17  9:49 ` [PATCH 1/2] x86/mm/pat: skip HIGHMEM pages in a page array Christian
  2026-11-17  9:49 ` [PATCH 2/2] x86/mm/pat: track the memory type of " Christian
@ 2026-11-19 19:45 ` Dave Hansen
  2026-11-20  7:50   ` Christian
  2 siblings, 1 reply; 7+ messages in thread
From: Dave Hansen @ 2026-11-19 19:45 UTC (permalink / raw)
  To: Christian, Dave Hansen, Andy Lutomirski, Peter Zijlstra,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86
  Cc: H. Peter Anvin, linux-kernel

On 11/17/26 01:49, Christian wrote:
> Found and tested on an HP ElitePad 900 G1 (Atom Z2760, Clover Trail, i386
> without PAE, 2 GB) running an out-of-tree PowerVR SGX545 DRM driver with
> its buffers in HIGHMEM.

Reproduce upstream and we'll talk.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*()
  2026-11-19 19:45 ` [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Dave Hansen
@ 2026-11-20  7:50   ` Christian
  2026-11-20 15:13     ` Dave Hansen
  0 siblings, 1 reply; 7+ messages in thread
From: Christian @ 2026-11-20  7:50 UTC (permalink / raw)
  To: dave.hansen
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, linux-kernel

On 11/19/26 11:45, Dave Hansen wrote:
> Reproduce upstream and we'll talk.

vgem does it. It keeps its buffers in shmem pages (GFP_HIGHUSER) and sets
map_wc, so on x86 mmap() reaches set_pages_array_wc() through
drm_gem_shmem_get_pages_locked(). On i386 without PAE, once RAM is above
lowmem, those pages are HIGHMEM.

Reproduce with v7.4-rc3:

  make i386_defconfig
  scripts/config -e HIGHMEM4G -e DRM_VGEM -e DEBUG_FS -e PTDUMP_DEBUGFS
  make -j$(nproc)   # no PAE: i386_defconfig is 32-bit only
  qemu-system-i386 -cpu n270 -m 2G -smp 2 -kernel arch/x86/boot/bzImage \
      -initrd <initramfs with the program below as /init> \
      -append console=ttyS0

(-m 512M stays below lowmem and is the control run; -cpu n270 has PAT,
but -cpu qemu32 oopses the same way without it.)

  BUG: kernel NULL pointer dereference, address: 00000000
  EIP: __cpa_process_fault+0x336/0x360
  Call Trace:
   __change_page_attr_set_clr+0x782/0xa10
   change_page_attr_set_clr+0xdc/0x540
   _set_pages_array+0xb2/0x160
   set_pages_array_wc+0xd/0x10
   drm_gem_shmem_get_pages_locked+0x86/0xe0
   drm_gem_shmem_mmap+0x97/0x130
   drm_gem_shmem_object_mmap+0x8/0x10
   drm_gem_mmap_obj+0x5a/0x180
   drm_gem_mmap+0x32/0xa0
   __mmap_region+0x930/0x1120
   ...

A second buffer's mmap() then never returns: the CPA lock is still held.

With patch 1 alone both buffers map, but the user mapping is write-back
(page_tables/current_kernel, CONFIG_PTDUMP_DEBUGFS):

  0xb6fdc000-0xb7fdc000   16M USR RW            x  pte

With -m 512M (no HIGHMEM) and with both patches it is write-combined:

  0xb6f76000-0xb7f76000   16M USR RW PWT        x  pte

The program:

// gcc -m32 -static -I/usr/include/libdrm vgem_highmem.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <unistd.h>
#include <drm/drm.h>
#include <drm/drm_mode.h>

int main(void)
{
	struct drm_mode_create_dumb c = { .width = 1024, .height = 4096, .bpp = 32 };
	struct drm_mode_map_dumb m = { 0 };
	char path[32], name[16];
	unsigned int *p;
	int fd = -1;

	mount("devtmpfs", "/dev", "devtmpfs", 0, NULL);	/* when run as /init */
	for (int i = 0; i < 8 && fd < 0; i++) {
		struct drm_version v = { .name = name, .name_len = sizeof(name) - 1 };

		snprintf(path, sizeof(path), "/dev/dri/card%d", i);
		fd = open(path, O_RDWR);
		memset(name, 0, sizeof(name));
		if (fd >= 0 && (ioctl(fd, DRM_IOCTL_VERSION, &v) || strcmp(name, "vgem"))) {
			close(fd);
			fd = -1;
		}
	}
	if (fd < 0 || ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &c))
		return perror("vgem"), 1;
	m.handle = c.handle;
	if (ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &m))
		return perror("MAP_DUMB"), 1;

	p = mmap(NULL, c.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, m.offset);
	if (p == MAP_FAILED)
		return perror("mmap"), 1;
	p[0] = 1;
	printf("mapped %llu bytes at %p\n", (unsigned long long)c.size, (void *)p);
	pause();
	return 0;
}

I can send a v2 with this in the changelogs, and with hpa and
linux-kernel on Cc, which I left out the first time.

Christian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*()
  2026-11-20  7:50   ` Christian
@ 2026-11-20 15:13     ` Dave Hansen
  2026-11-20 16:45       ` Christian
  0 siblings, 1 reply; 7+ messages in thread
From: Dave Hansen @ 2026-11-20 15:13 UTC (permalink / raw)
  To: Christian
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, linux-kernel

On 11/19/26 23:50, Christian wrote:
> On 11/19/26 11:45, Dave Hansen wrote:
>> Reproduce upstream and we'll talk.
> vgem does it. It keeps its buffers in shmem pages (GFP_HIGHUSER) and sets
> map_wc, so on x86 mmap() reaches set_pages_array_wc() through
> drm_gem_shmem_get_pages_locked(). On i386 without PAE, once RAM is above
> lowmem, those pages are HIGHMEM.

I congratulate your LLM and mourn the tokens that were sacrificed for this.

I'll say, though, for something this obscure, I really need to be
convinced that it's not going to regress anything. I also want to know
that you'll be around to help us fix this if you break something.

Please spend some time on the changelogs and please do double-check that
there wasn't a better way to have done this. Human, please. No LLMs.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*()
  2026-11-20 15:13     ` Dave Hansen
@ 2026-11-20 16:45       ` Christian
  0 siblings, 0 replies; 7+ messages in thread
From: Christian @ 2026-11-20 16:45 UTC (permalink / raw)
  To: dave.hansen
  Cc: dave.hansen, luto, peterz, tglx, mingo, bp, x86, hpa, linux-kernel

Hi Dave,

On 11/20/26 07:13, Dave Hansen wrote:
> I'll say, though, for something this obscure, I really need to be
> convinced that it's not going to regress anything. I also want to know
> that you'll be around to help us fix this if you break something.
>
> Please spend some time on the changelogs and please do double-check that
> there wasn't a better way to have done this. Human, please. No LLMs.

Thanks for the honest answer, that's fair.

About the AI part: English is not my first language (I'm from Spain),
and I mostly use AI tools to translate between Spanish and English. The
work itself is mine. I found this bug on my own tablet, I traced it
myself, and I'm going through it one step at a time. And yes, I'll be
around. If this breaks something, I'll help fix it.

I understand your concern about AI, and I won't send you unchecked junk.
I'm sorry if my first reply came across that way. From now on I'll work
in the way that suits you best.

You asked me to check if there was a better way, and for patch 1 there
is one. Up to v4.20, __change_page_attr() and cpa_process_alias()
returned early for a HIGHMEM page in a page array. Commit 16ebf031e8ab
("x86/mm/cpa: Add __cpa_addr() helper") moved that check into
__cpa_addr(), which now returns 0 for those pages, so the 0 goes on
into the page table lookup and ends up in __cpa_process_fault(). So v2
will put the early return back where it was, with a Fixes: tag, instead
of adding a special case to __cpa_process_fault(). I have tried it with
the vgem reproducer, with and without HIGHMEM, and both buffers map
fine.

For patch 2 I still want to look at the other option: TTM never passes
HIGHMEM pages to set_pages_array_*() at all, so maybe the callers should
keep WC buffers out of HIGHMEM instead of PAT tracking them. I'll
explain whichever way I pick in the changelog.

I'll take my time with v2 and rewrite the changelogs properly. Before
sending it I will check every claim three times and run the reproducer
again on each version.

Best regards,
Christian

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-20 16:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  9:49 [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Christian
2026-11-17  9:49 ` [PATCH 1/2] x86/mm/pat: skip HIGHMEM pages in a page array Christian
2026-11-17  9:49 ` [PATCH 2/2] x86/mm/pat: track the memory type of " Christian
2026-11-19 19:45 ` [PATCH 0/2] x86/mm/pat: HIGHMEM pages in set_pages_array_*() Dave Hansen
2026-11-20  7:50   ` Christian
2026-11-20 15:13     ` Dave Hansen
2026-11-20 16:45       ` Christian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®