riscv: mm: fix set_memory_x() and kernel_page_present()
v2 cover riscv: mm: fix set_memory_x() and kernel_page_present()
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/[email protected]
---
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.
PATCH v2 1/2 riscv: mm: do not make the linear map alias executable in set_memory_*()
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.
--- arch/riscv/mm/pageattr.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/riscv/mm/pageattr.c +7 -2
@@ -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; }
Signature · 1 line
2.53.0
PATCH v2 2/2 riscv: mm: check the valid bit in kernel_page_present()
In reply to Mike Rapoport (Microsoft)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.
--- arch/riscv/mm/pageattr.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
arch/riscv/mm/pageattr.c +7 -5
@@ -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; }
Signature · 1 line
2.53.0
Quoted 6 lines · Mike Rapoport (Microsoft)
These are two riscv fixes I found while working on a generic implementation of the
set_memoryAPIs.The first one stops
set_memory_x()from making the linear map alias of module and BPF text executable.
Quoted 8 lines
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 inkernel_page_present()(Jisheng)
- Link to v1: https://patch.msgid.link/[email protected]
for the series:
Quoted 16 lines
---
Mike Rapoport (Microsoft) (2):
riscv: mm: do not make the linear map alias executable in set_memory_*()
riscv: mm: check the valid bit inkernel_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.