* [PATCH] mm/ksm: fix pte_unmap_unlock of wrong address in break_ksm_pmd_entry
@ 2025-12-20 20:29 Sasha Levin
2025-12-21 8:46 ` David Hildenbrand (Red Hat)
2025-12-22 8:23 ` Chengming Zhou
0 siblings, 2 replies; 3+ messages in thread
From: Sasha Levin @ 2025-12-20 20:29 UTC (permalink / raw)
To: akpm, david
Cc: xu.xin16, chengming.zhou, pedrodemargomes, linux-mm,
linux-kernel, Sasha Levin
On ARM32 with HIGHMEM/HIGHPTE, break_ksm_pmd_entry() triggers a BUG
during KSM unmerging because pte_unmap_unlock() is passed a pointer
that may be beyond the mapped PTE page.
The issue occurs when the PTE iteration loop completes without finding
a KSM page. After the loop, 'ptep' has been incremented past the last
PTE entry. On ARM32 LPAE with 512 PTEs per page (512 * 8 = 4096 bytes),
this means ptep points to the next page, outside the kmap'd region.
When pte_unmap_unlock(ptep, ptl) calls kunmap_local(ptep), it unmaps
the wrong page address, leaving the original kmap slot still mapped.
The next kmap_local then finds this slot unexpectedly occupied:
WARNING: mm/highmem.c:622 kunmap_local_indexed (address mismatch)
kernel BUG at mm/highmem.c:564 __kmap_local_pfn_prot (slot not empty)
Fix this by passing start_ptep to pte_unmap_unlock(), which always
points within the originally mapped PTE page.
Reproducer: Run LTP ksm03 test on ARM32 with HIGHMEM enabled. The test
triggers KSM merging followed by unmerging (writing 0 then 2 to
/sys/kernel/mm/ksm/run), which exercises break_ksm_pmd_entry().
Fixes: 5d4939fc2258 ("ksm: perform a range-walk in break_ksm")
Assisted-by: claude-opus-4-5-20251101
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
mm/ksm.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/ksm.c b/mm/ksm.c
index cfc182255c7b..2d89a7c8b4eb 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -650,7 +650,7 @@ static int break_ksm_pmd_entry(pmd_t *pmdp, unsigned long addr, unsigned long en
}
}
out_unlock:
- pte_unmap_unlock(ptep, ptl);
+ pte_unmap_unlock(start_ptep, ptl);
return found;
}
--
2.51.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/ksm: fix pte_unmap_unlock of wrong address in break_ksm_pmd_entry
2025-12-20 20:29 [PATCH] mm/ksm: fix pte_unmap_unlock of wrong address in break_ksm_pmd_entry Sasha Levin
@ 2025-12-21 8:46 ` David Hildenbrand (Red Hat)
2025-12-22 8:23 ` Chengming Zhou
1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Red Hat) @ 2025-12-21 8:46 UTC (permalink / raw)
To: Sasha Levin, akpm
Cc: xu.xin16, chengming.zhou, pedrodemargomes, linux-mm, linux-kernel
On 12/20/25 21:29, Sasha Levin wrote:
> On ARM32 with HIGHMEM/HIGHPTE, break_ksm_pmd_entry() triggers a BUG
> during KSM unmerging because pte_unmap_unlock() is passed a pointer
> that may be beyond the mapped PTE page.
>
> The issue occurs when the PTE iteration loop completes without finding
> a KSM page. After the loop, 'ptep' has been incremented past the last
> PTE entry. On ARM32 LPAE with 512 PTEs per page (512 * 8 = 4096 bytes),
> this means ptep points to the next page, outside the kmap'd region.
>
> When pte_unmap_unlock(ptep, ptl) calls kunmap_local(ptep), it unmaps
> the wrong page address, leaving the original kmap slot still mapped.
> The next kmap_local then finds this slot unexpectedly occupied:
>
> WARNING: mm/highmem.c:622 kunmap_local_indexed (address mismatch)
> kernel BUG at mm/highmem.c:564 __kmap_local_pfn_prot (slot not empty)
>
> Fix this by passing start_ptep to pte_unmap_unlock(), which always
> points within the originally mapped PTE page.
>
> Reproducer: Run LTP ksm03 test on ARM32 with HIGHMEM enabled. The test
> triggers KSM merging followed by unmerging (writing 0 then 2 to
> /sys/kernel/mm/ksm/run), which exercises break_ksm_pmd_entry().
>
> Fixes: 5d4939fc2258 ("ksm: perform a range-walk in break_ksm")
> Assisted-by: claude-opus-4-5-20251101
> Signed-off-by: Sasha Levin <sashal@kernel.org>
> ---
> mm/ksm.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index cfc182255c7b..2d89a7c8b4eb 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -650,7 +650,7 @@ static int break_ksm_pmd_entry(pmd_t *pmdp, unsigned long addr, unsigned long en
> }
> }
> out_unlock:
> - pte_unmap_unlock(ptep, ptl);
> + pte_unmap_unlock(start_ptep, ptl);
> return found;
> }
>
Acked-by: David Hildenbrand (Red Hat) <david@kernel.org>
--
Cheers
David
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/ksm: fix pte_unmap_unlock of wrong address in break_ksm_pmd_entry
2025-12-20 20:29 [PATCH] mm/ksm: fix pte_unmap_unlock of wrong address in break_ksm_pmd_entry Sasha Levin
2025-12-21 8:46 ` David Hildenbrand (Red Hat)
@ 2025-12-22 8:23 ` Chengming Zhou
1 sibling, 0 replies; 3+ messages in thread
From: Chengming Zhou @ 2025-12-22 8:23 UTC (permalink / raw)
To: Sasha Levin, akpm, david
Cc: xu.xin16, pedrodemargomes, linux-mm, linux-kernel
On 2025/12/21 04:29, Sasha Levin wrote:
> On ARM32 with HIGHMEM/HIGHPTE, break_ksm_pmd_entry() triggers a BUG
> during KSM unmerging because pte_unmap_unlock() is passed a pointer
> that may be beyond the mapped PTE page.
>
> The issue occurs when the PTE iteration loop completes without finding
> a KSM page. After the loop, 'ptep' has been incremented past the last
> PTE entry. On ARM32 LPAE with 512 PTEs per page (512 * 8 = 4096 bytes),
> this means ptep points to the next page, outside the kmap'd region.
>
> When pte_unmap_unlock(ptep, ptl) calls kunmap_local(ptep), it unmaps
> the wrong page address, leaving the original kmap slot still mapped.
> The next kmap_local then finds this slot unexpectedly occupied:
>
> WARNING: mm/highmem.c:622 kunmap_local_indexed (address mismatch)
> kernel BUG at mm/highmem.c:564 __kmap_local_pfn_prot (slot not empty)
>
> Fix this by passing start_ptep to pte_unmap_unlock(), which always
> points within the originally mapped PTE page.
>
> Reproducer: Run LTP ksm03 test on ARM32 with HIGHMEM enabled. The test
> triggers KSM merging followed by unmerging (writing 0 then 2 to
> /sys/kernel/mm/ksm/run), which exercises break_ksm_pmd_entry().
>
> Fixes: 5d4939fc2258 ("ksm: perform a range-walk in break_ksm")
> Assisted-by: claude-opus-4-5-20251101
> Signed-off-by: Sasha Levin <sashal@kernel.org>
Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev>
Thanks.
> ---
> mm/ksm.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index cfc182255c7b..2d89a7c8b4eb 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -650,7 +650,7 @@ static int break_ksm_pmd_entry(pmd_t *pmdp, unsigned long addr, unsigned long en
> }
> }
> out_unlock:
> - pte_unmap_unlock(ptep, ptl);
> + pte_unmap_unlock(start_ptep, ptl);
> return found;
> }
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-12-22 8:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-20 20:29 [PATCH] mm/ksm: fix pte_unmap_unlock of wrong address in break_ksm_pmd_entry Sasha Levin
2025-12-21 8:46 ` David Hildenbrand (Red Hat)
2025-12-22 8:23 ` Chengming Zhou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox