mm/powerpc: Fix _PAGE_PTE breaking swapoff
Subsystems (2)
PATCH v2 1/1 mm/powerpc: Fix _PAGE_PTE breaking swapoff
Core kernel expect swp_entry_t to be consisting of
only swap type and swap offset. We should not leak pte bits to swp_entry_t. This breaks swapoff which use the swap type and offset to build a swp_entry_t and later compare that to the swp_entry_t obtained from linux page table pte. Leaking pte bits to swp_entry_t breaks that comparison and results in us looping in try_to_unuse.
The stack trace can be anywhere below try_to_unuse() in mm/swapfile.c, since swapoff is circling around and around that function, reading from each used swap block into a page, then trying to find where that page belongs, looking at every non-file pte of every mm that ever swapped.
---
Changes from V1:
* improve change log and code comment
arch/powerpc/include/asm/book3s/64/pgtable.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +8 -3
@@ -158,9 +158,14 @@ static inline void pgd_set(pgd_t *pgdp, unsigned long val)#define __swp_entry (type, offset) ((swp_entry_t) { \ ((type) << _PAGE_BIT_SWAP_TYPE) \ | ((offset) << PTE_RPN_SHIFT) }) - -#define __pte_to_swp_entry (pte) ((swp_entry_t) { pte_val((pte)) }) -#define __swp_entry_to_pte (x) __pte((x).val) +/* +* swp_entry_t should be independent of pte bits. We build a swp_entry_t from +* swap type and offset we get from swap and convert that to pte to +* find a matching pte in linux page table. +* Clear bits not found in swap entries here +*/ +#define __pte_to_swp_entry (pte) ((swp_entry_t) { pte_val((pte)) & ~_PAGE_PTE }) +#define __swp_entry_to_pte (x) __pte((x).val | _PAGE_PTE)#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY #define _PAGE_SWP_SOFT_DIRTY (1UL << (SWP_TYPE_BITS + _PAGE_BIT_SWAP_TYPE))
Signature · 1 line
2.5.0
Quoted 7 lines · Aneesh Kumar K.V
Core kernel expect
swp_entry_tto be consisting of
only swap type and swap offset. We should not leak pte bits toswp_entry_t. This breaks swapoff which use the swap type and offset to build aswp_entry_tand later compare that to theswp_entry_tobtained from linux page table pte. Leaking pte bits toswp_entry_tbreaks that comparison and results in us looping intry_to_unuse.
Quoted 8 lines
The stack trace can be anywhere below
try_to_unuse()inmm/swapfile.c, since swapoff is circling around and around that function, reading from each used swap block into a page, then trying to find where that page belongs, looking at every non-file pte of every mm that ever swapped.Reported-by Hugh Dickins <[email protected]>Suggested-by Hugh Dickins <[email protected]>Signed-off-by Aneesh Kumar K.V <[email protected]>
I think we've seen enough of my name above, but if it helps further
Though I don't find the code comment below on swp_entry_t enlightening - your commit description above is much more helpful. If I were writing it, I might say... hmm, it's too hard: given all the convolutions, I gave up.
Quoted 31 lines
---
Changes from V1:
* improve change log and code commentarch/powerpc/include/asm/book3s/64/pgtable.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)arch/powerpc/include/asm/book3s/64/pgtable.h +8
-3@@ -158,9 +158,14 @@ static inline void pgd_set(pgd_t *pgdp, unsigned long val)#define __swp_entry (type, offset) ((swp_entry_t) { \ ((type) << _PAGE_BIT_SWAP_TYPE) \ | ((offset) << PTE_RPN_SHIFT) }) - -#define __pte_to_swp_entry (pte) ((swp_entry_t) { pte_val((pte)) }) -#define __swp_entry_to_pte (x) __pte((x).val) +/* +* swp_entry_t should be independent of pte bits. We build a swp_entry_t from +* swap type and offset we get from swap and convert that to pte to +* find a matching pte in linux page table. +* Clear bits not found in swap entries here +*/ +#define __pte_to_swp_entry (pte) ((swp_entry_t) { pte_val((pte)) & ~_PAGE_PTE }) +#define __swp_entry_to_pte (x) __pte((x).val | _PAGE_PTE)#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY #define _PAGE_SWP_SOFT_DIRTY (1UL << (SWP_TYPE_BITS + _PAGE_BIT_SWAP_TYPE))Signature · 1 line
2.5.0
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Michael Ellerman
reviewerQuoted 7 lines · Aneesh Kumar K.V
Core kernel expect
swp_entry_tto be consisting of
only swap type and swap offset. We should not leak pte bits toswp_entry_t. This breaks swapoff which use the swap type and offset to build aswp_entry_tand later compare that to theswp_entry_tobtained from linux page table pte. Leaking pte bits toswp_entry_tbreaks that comparison and results in us looping intry_to_unuse.
Quoted 8 lines
The stack trace can be anywhere below
try_to_unuse()inmm/swapfile.c, since swapoff is circling around and around that function, reading from each used swap block into a page, then trying to find where that page belongs, looking at every non-file pte of every mm that ever swapped.Reported-by Hugh Dickins <[email protected]>Suggested-by Hugh Dickins <[email protected]>Signed-off-by Aneesh Kumar K.V <[email protected]>
Thanks. I slightly edited the wording in the change log and added:
cheers
Quoted 12 lines · Aneesh Kumar K.V
Core kernel expect
swp_entry_tto be consisting of
only swap type and swap offset. We should not leak pte bits toswp_entry_t. This breaks swapoff which use the swap type and offset to build aswp_entry_tand later compare that to theswp_entry_tobtained from linux page table pte. Leaking pte bits toswp_entry_tbreaks that comparison and results in us looping intry_to_unuse.The stack trace can be anywhere below
try_to_unuse()inmm/swapfile.c, since swapoff is circling around and around that function, reading from each used swap block into a page, then trying to find where that page belongs, looking at every non-file pte of every mm that ever swapped.
Quoted 4 lines
Reported-by Hugh Dickins <[email protected]>Suggested-by Hugh Dickins <[email protected]>Signed-off-by Aneesh Kumar K.V <[email protected]>Acked-by Hugh Dickins <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/44734f23de2465c3c0d39e4a16
cheers