The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present()
@ 2026-11-17 14:00 Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 1/2] riscv: mm: do not make the linear map alias executable in set_memory_*() Mike Rapoport (Microsoft)
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 14:00 UTC (permalink / raw)
  To: Albert Ou, Palmer Dabbelt, Paul Walmsley
  Cc: Alexandre Ghiti, Jisheng Zhang, Mike Rapoport, linux-kernel,
	linux-riscv

These are two riscv fixes I found while working on a generic
implementation of the set_memory APIs.

The first one stops set_memory_x() from making the linear map alias of
module and BPF text executable.

The second one makes kernel_page_present() report the pages removed from
the linear map as not present. Today it says they are mapped, and
hibernation could copy them without mapping them first.

---
Changes in v2:
- read the PMD entry once in kernel_page_present() (Jisheng)
- Link to v1: https://patch.msgid.link/20261112-fixes-riscv-v1-0-1caac19897b5@kernel.org

---
Mike Rapoport (Microsoft) (2):
      riscv: mm: do not make the linear map alias executable in set_memory_*()
      riscv: mm: check the valid bit in kernel_page_present()

 arch/riscv/mm/pageattr.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261112-fixes-riscv-3e48788f58fe

--
Sincerely yours,
Mike.


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

* [PATCH v2 1/2] riscv: mm: do not make the linear map alias executable in set_memory_*()
  2026-11-17 14:00 [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Mike Rapoport (Microsoft)
@ 2026-11-17 14:00 ` Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 2/2] riscv: mm: check the valid bit in kernel_page_present() Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Jisheng Zhang
  2 siblings, 0 replies; 4+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 14:00 UTC (permalink / raw)
  To: Albert Ou, Palmer Dabbelt, Paul Walmsley
  Cc: Alexandre Ghiti, Jisheng Zhang, Mike Rapoport, linux-kernel,
	linux-riscv

set_memory_*() applies the requested change to the linear map alias of the
range. For set_memory_x() that makes the linear map alias of module text
and of BPF programs executable, although nothing should ever execute that
code through the linear map.

Keep _PAGE_EXEC out of the bits set_memory_*() sets on the linear map
alias.

Fixes: 311cd2f6e253 ("riscv: Fix set_memory_XX() and set_direct_map_XX() by splitting huge linear mappings")
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 arch/riscv/mm/pageattr.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c
index 5b3cf326455d..b13811375535 100644
--- a/arch/riscv/mm/pageattr.c
+++ b/arch/riscv/mm/pageattr.c
@@ -271,6 +271,10 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,
 		.set_mask = set_mask,
 		.clear_mask = clear_mask
 	};
+	struct pageattr_masks __maybe_unused lm_masks = {
+		.set_mask = __pgprot(pgprot_val(set_mask) & ~_PAGE_EXEC),
+		.clear_mask = clear_mask
+	};
 
 	if (!numpages)
 		return 0;
@@ -300,7 +304,8 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,
 				goto unlock;
 
 			ret = walk_kernel_page_table_range(lm_start, lm_end,
-						    &pageattr_ops, NULL, &masks);
+						    &pageattr_ops, NULL,
+						    &lm_masks);
 			if (ret)
 				goto unlock;
 		}
@@ -318,7 +323,7 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,
 			goto unlock;
 
 		ret = walk_kernel_page_table_range(lm_start, lm_end,
-					    &pageattr_ops, NULL, &masks);
+					    &pageattr_ops, NULL, &lm_masks);
 		if (ret)
 			goto unlock;
 	}

-- 
2.53.0


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

* [PATCH v2 2/2] riscv: mm: check the valid bit in kernel_page_present()
  2026-11-17 14:00 [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 1/2] riscv: mm: do not make the linear map alias executable in set_memory_*() Mike Rapoport (Microsoft)
@ 2026-11-17 14:00 ` Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Jisheng Zhang
  2 siblings, 0 replies; 4+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 14:00 UTC (permalink / raw)
  To: Albert Ou, Palmer Dabbelt, Paul Walmsley
  Cc: Alexandre Ghiti, Jisheng Zhang, Mike Rapoport, linux-kernel,
	linux-riscv

set_direct_map_invalid_noflush() clears _PAGE_PRESENT, but the kernel
mappings keep _PAGE_PROT_NONE set: it used to be _PAGE_READ and now it is
the same bit as _PAGE_GLOBAL. pte_present() and pmd_present() still
return true for the invalidated entry. kernel_page_present() then reports
that an unmapped page is mapped, and hibernation copies it without
remapping it first.

Test _PAGE_PRESENT directly for PMD and PTE leaves.

Fixes: 32a0de886eb3 ("arch, mm: make kernel_page_present() always available")
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 arch/riscv/mm/pageattr.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c
index b13811375535..5a1a3b4473c9 100644
--- a/arch/riscv/mm/pageattr.c
+++ b/arch/riscv/mm/pageattr.c
@@ -428,7 +428,7 @@ bool kernel_page_present(struct page *page)
 	pgd_t *pgd;
 	pud_t *pud;
 	p4d_t *p4d;
-	pmd_t *pmd;
+	pmd_t *pmd, pmdval;
 	pte_t *pte;
 
 	pgd = pgd_offset_k(addr);
@@ -450,11 +450,13 @@ bool kernel_page_present(struct page *page)
 		return true;
 
 	pmd = pmd_offset(pud, addr);
-	if (!pmd_present(pmdp_get(pmd)))
+	pmdval = pmdp_get(pmd);
+	if (!pmd_present(pmdval))
 		return false;
-	if (pmd_leaf(pmdp_get(pmd)))
-		return true;
+	if (pmd_leaf(pmdval))
+		return pmd_val(pmdval) & _PAGE_PRESENT;
 
+	/* pte_present() is true for PAGE_KERNEL with _PAGE_PRESENT cleared */
 	pte = pte_offset_kernel(pmd, addr);
-	return pte_present(ptep_get(pte));
+	return pte_val(ptep_get(pte)) & _PAGE_PRESENT;
 }

-- 
2.53.0


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

* Re: [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present()
  2026-11-17 14:00 [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 1/2] riscv: mm: do not make the linear map alias executable in set_memory_*() Mike Rapoport (Microsoft)
  2026-11-17 14:00 ` [PATCH v2 2/2] riscv: mm: check the valid bit in kernel_page_present() Mike Rapoport (Microsoft)
@ 2026-11-17 14:00 ` Jisheng Zhang
  2 siblings, 0 replies; 4+ messages in thread
From: Jisheng Zhang @ 2026-11-17 14:00 UTC (permalink / raw)
  To: Mike Rapoport (Microsoft)
  Cc: Albert Ou, Palmer Dabbelt, Paul Walmsley, Alexandre Ghiti,
	linux-kernel, linux-riscv

On Tue, Nov 17, 2026 at 04:00:19PM +0200, Mike Rapoport (Microsoft) wrote:
> These are two riscv fixes I found while working on a generic
> implementation of the set_memory APIs.
> 
> The first one stops set_memory_x() from making the linear map alias of
> module and BPF text executable.
> 
> The second one makes kernel_page_present() report the pages removed from
> the linear map as not present. Today it says they are mapped, and
> hibernation could copy them without mapping them first.
> 
> ---
> Changes in v2:
> - read the PMD entry once in kernel_page_present() (Jisheng)
> - Link to v1: https://patch.msgid.link/20261112-fixes-riscv-v1-0-1caac19897b5@kernel.org

for the series:

Reviewed-by: Jisheng Zhang <jszhang@kernel.org>


> 
> ---
> Mike Rapoport (Microsoft) (2):
>       riscv: mm: do not make the linear map alias executable in set_memory_*()
>       riscv: mm: check the valid bit in kernel_page_present()
> 
>  arch/riscv/mm/pageattr.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> change-id: 20261112-fixes-riscv-3e48788f58fe
> 
> --
> Sincerely yours,
> Mike.
> 

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 14:00 [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Mike Rapoport (Microsoft)
2026-11-17 14:00 ` [PATCH v2 1/2] riscv: mm: do not make the linear map alias executable in set_memory_*() Mike Rapoport (Microsoft)
2026-11-17 14:00 ` [PATCH v2 2/2] riscv: mm: check the valid bit in kernel_page_present() Mike Rapoport (Microsoft)
2026-11-17 14:00 ` [PATCH v2 0/2] riscv: mm: fix set_memory_x() and kernel_page_present() Jisheng Zhang

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