x86/mm/pat: take cpa_lock around large-page collapse
PATCH 1/1 x86/mm/pat: take cpa_lock around large-page collapse
Loading and unloading modules concurrently on several CPUs on a KASAN build, with a short delay injected at the CPA page-table lookup to widen the window, faults within minutes:
BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table)
cpa_collapse_large_pages() rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while __change_page_attr() fetches a PTE pointer from a lockless lookup_address_in_pgd_attr() and writes it with set_pte_atomic() only later. When module text is served from a shared large ROX mapping the two run on the same PMD:
CPU A (module load) CPU B (module finalize)
------------------- -----------------------
execmem_make_temp_rw
set_memory_nx
__change_page_attr
split 2M -> 4K table P
kpte = &P[i] (lockless)
execmem_restore_rox
set_memory_rox (CPA_COLLAPSE)
cpa_collapse_large_pages
rebuild leaf PMD
flush_tlb_all
pagetable_free(P)
set_pte_atomic(kpte, ...)
-> writes into freed PP is a page-table page (page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text.
The flush_tlb_all() before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in __change_page_attr() runs with interrupts on, so nothing stops it from holding a stale pointer into P.
Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under cpa_lock, the lock __change_page_attr() takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free.
debug_pagealloc bypasses cpa_lock in __change_page_attr() (the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race.
--- arch/x86/mm/pat/set_memory.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +15 -1
@@ -418,6 +418,16 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)int collapsed =0 ;int i; +/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ; + +spin_lock (&cpa_lock); +if (cpa->flags & (CPA_PAGES_ARRAY |CPA_ARRAY )) {for (i =0 ; i < cpa->numpages; i++) collapsed += collapse_large_pages(__cpa_addr(cpa, i),
@@ -431,8 +441,10 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) collapsed +=collapse_large_pages (addr, &pgtables); } -if (!collapsed) +if (!collapsed) { +spin_unlock (&cpa_lock);return ; + }flush_tlb_all ();
@@ -440,6 +452,8 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)list_del (&ptdesc->pt_list);pagetable_free (ptdesc); } + +spin_unlock (&cpa_lock); }static void cpa_flush (struct cpa_data *cpa,int cache)
base-commit: b81d185839fade27f7c4e885856696cf497d53c1
Signature · 1 line
2.53.0
Quoted 103 lines · Denis V. Lunev
This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive. Loading and unloading modules concurrently on several CPUs on a KASAN
build, with a short delay injected at the CPA page-table lookup to
widen the window, faults within minutes:BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table)
cpa_collapse_large_pages()rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while__change_page_attr()fetches a PTE pointer from a locklesslookup_address_in_pgd_attr()and writes it withset_pte_atomic()only later. When module text is served from a shared large ROX mapping the two run on the same PMD:CPU A (module load) CPU B (module finalize)
------------------- -----------------------
execmem_make_temp_rw
set_memory_nx__change_page_attr split 2M -> 4K table P kpte = &P[i] (lockless) execmem_restore_rox set_memory_rox (CPA_COLLAPSE) cpa_collapse_large_pages rebuild leaf PMD flush_tlb_all pagetable_free(P) set_pte_atomic(kpte, ...) -> writes into freed PP is a page-table page (
page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text.The
flush_tlb_all()before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in__change_page_attr()runs with interrupts on, so nothing stops it from holding a stale pointer into P.Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under
cpa_lock, the lock__change_page_attr()takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free.
debug_pageallocbypassescpa_lockin__change_page_attr()(the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race.Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Denis V. Lunev <[email protected]>--- arch/x86/mm/pat/set_memory.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +15
-1@@ -418,6 +418,16 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)int collapsed =0 ;int i; +/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ; + +spin_lock (&cpa_lock); +if (cpa->flags & (CPA_PAGES_ARRAY |CPA_ARRAY )) {for (i =0 ; i < cpa->numpages; i++) collapsed += collapse_large_pages(__cpa_addr(cpa, i),@@ -431,8 +441,10 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) collapsed +=collapse_large_pages (addr, &pgtables); } -if (!collapsed) +if (!collapsed) { +spin_unlock (&cpa_lock);return ; + }flush_tlb_all ();@@ -440,6 +452,8 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)list_del (&ptdesc->pt_list);pagetable_free (ptdesc); } + +spin_unlock (&cpa_lock); }static void cpa_flush (struct cpa_data *cpa,int cache)
Quoted 1 line
base-commit:
b81d185839fade27f7c4e885856696cf497d53c1
ping
Quoted 46 lines · Denis V. Lunev
Loading and unloading modules concurrently on several CPUs on a KASAN build, with a short delay injected at the CPA page-table lookup to widen the window, faults within minutes:
BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table)
cpa_collapse_large_pages()rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while__change_page_attr()fetches a PTE pointer from a locklesslookup_address_in_pgd_attr()and writes it withset_pte_atomic()only later. When module text is served from a shared large ROX mapping the two run on the same PMD:CPU A (module load) CPU B (module finalize)
------------------- -----------------------
execmem_make_temp_rw
set_memory_nx__change_page_attr split 2M -> 4K table P kpte = &P[i] (lockless) execmem_restore_rox set_memory_rox (CPA_COLLAPSE) cpa_collapse_large_pages rebuild leaf PMD flush_tlb_all pagetable_free(P) set_pte_atomic(kpte, ...) -> writes into freed PP is a page-table page (
page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text.The
flush_tlb_all()before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in__change_page_attr()runs with interrupts on, so nothing stops it from holding a stale pointer into P.Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under
cpa_lock, the lock__change_page_attr()takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free.
Quoted 8 lines
debug_pageallocbypassescpa_lockin__change_page_attr()(the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race.Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Denis V. Lunev <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 103 lines · Denis V. Lunev
This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive. Loading and unloading modules concurrently on several CPUs on a KASAN
build, with a short delay injected at the CPA page-table lookup to
widen the window, faults within minutes:BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table)
cpa_collapse_large_pages()rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while__change_page_attr()fetches a PTE pointer from a locklesslookup_address_in_pgd_attr()and writes it withset_pte_atomic()only later. When module text is served from a shared large ROX mapping the two run on the same PMD:CPU A (module load) CPU B (module finalize)
------------------- -----------------------
execmem_make_temp_rw
set_memory_nx__change_page_attr split 2M -> 4K table P kpte = &P[i] (lockless) execmem_restore_rox set_memory_rox (CPA_COLLAPSE) cpa_collapse_large_pages rebuild leaf PMD flush_tlb_all pagetable_free(P) set_pte_atomic(kpte, ...) -> writes into freed PP is a page-table page (
page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text.The
flush_tlb_all()before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in__change_page_attr()runs with interrupts on, so nothing stops it from holding a stale pointer into P.Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under
cpa_lock, the lock__change_page_attr()takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free.
debug_pageallocbypassescpa_lockin__change_page_attr()(the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race.Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Denis V. Lunev <[email protected]>--- arch/x86/mm/pat/set_memory.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +15
-1@@ -418,6 +418,16 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)int collapsed =0 ;int i; +/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ; + +spin_lock (&cpa_lock); +if (cpa->flags & (CPA_PAGES_ARRAY |CPA_ARRAY )) {for (i =0 ; i < cpa->numpages; i++) collapsed += collapse_large_pages(__cpa_addr(cpa, i),@@ -431,8 +441,10 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) collapsed +=collapse_large_pages (addr, &pgtables); } -if (!collapsed) +if (!collapsed) { +spin_unlock (&cpa_lock);return ; + }flush_tlb_all ();@@ -440,6 +452,8 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)list_del (&ptdesc->pt_list);pagetable_free (ptdesc); } + +spin_unlock (&cpa_lock); }static void cpa_flush (struct cpa_data *cpa,int cache)
Quoted 1 line
base-commit:
b81d185839fade27f7c4e885856696cf497d53c1
ping v2
cpa_collapse_large_pages() seems to be a never ending source of bugs. I'm tempted to say we should just axe it, call it a failed experiment, and move on with life.
Is it worth keeping?
Quoted 1 line · Dave Hansen
cpa_collapse_large_pages()seems to be a never ending source of bugs.
Yeah, this one, the race with ptdump and a year old issue on Xen PV. There might be more of course but I wouldn't call it endless.
Quoted 4 lines
I'm tempted to say we should just axe it, call it a failed experiment, and move on with life.
Is it worth keeping?
This what lets keeping executable mappings in 2M granularity. Modules, ftrace and kprobes are already using it and BPF is on the way:
https://lore.kernel.org/bpf/[email protected]/
Signature · 2 lines
Sincerely yours, Mike.
arch/x86/mm/pat/set_memory.c static void cpa_collapse_large_pages(struct cpa_data *cpa) · line 427
Quoted 7 lines · Denis V. Lunev
arch/x86/mm/pat/set_memory.c +7
-0+/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ;
Wow, that debug_pagealloc hack is ancient and came with the original introduction of cpa_lock:
Quoted 5 lines
commit
ad5ca55f6bdb47c957b681c7358bb3719ba4ee82Author: Suresh Siddha <[email protected]> Date: Tue Sep 23 14:00:42 2008 -0700x86, cpa: srlz
cpa(), global flush tlb after splitting big page and before doing cpa
My only question is *why*!?!? Why add extra locking complexity and rules to optimize debug_pagealloc, which is already horrendously slow.
I kinda think we should just _remove_ the locking which is conditional on debug_pagealloc_enabled().
Quoted 12 lines · Dave Hansen
arch/x86/mm/pat/set_memory.c static void cpa_collapse_large_pages(struct cpa_data *cpa) · line 427
Quoted 7 lines · Denis V. Lunev
arch/x86/mm/pat/set_memory.c +7
-0+/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ;Wow, that
debug_pageallochack is ancient and came with the original introduction ofcpa_lock:
Quoted 8 lines
Quoted 5 lines
commit
ad5ca55f6bdb47c957b681c7358bb3719ba4ee82Author: Suresh Siddha <[email protected]> Date: Tue Sep 23 14:00:42 2008 -0700x86, cpa: srlz
cpa(), global flush tlb after splitting big page and before doing cpaMy only question is *why*!?!? Why add extra locking complexity and rules to optimize
debug_pagealloc, which is already horrendously slow.
My guess would be that the logic was to lock only when a split is possible and since debug_pagealloc does not split anything, skip the lock.
Quoted 2 lines
I kinda think we should just _remove_ the locking which is conditional on
debug_pagealloc_enabled().
I agree.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 103 lines · Denis V. Lunev
This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive. Loading and unloading modules concurrently on several CPUs on a KASAN
build, with a short delay injected at the CPA page-table lookup to
widen the window, faults within minutes:BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table)
cpa_collapse_large_pages()rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while__change_page_attr()fetches a PTE pointer from a locklesslookup_address_in_pgd_attr()and writes it withset_pte_atomic()only later. When module text is served from a shared large ROX mapping the two run on the same PMD:CPU A (module load) CPU B (module finalize)
------------------- -----------------------
execmem_make_temp_rw
set_memory_nx__change_page_attr split 2M -> 4K table P kpte = &P[i] (lockless) execmem_restore_rox set_memory_rox (CPA_COLLAPSE) cpa_collapse_large_pages rebuild leaf PMD flush_tlb_all pagetable_free(P) set_pte_atomic(kpte, ...) -> writes into freed PP is a page-table page (
page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text.The
flush_tlb_all()before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in__change_page_attr()runs with interrupts on, so nothing stops it from holding a stale pointer into P.Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under
cpa_lock, the lock__change_page_attr()takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free.
debug_pageallocbypassescpa_lockin__change_page_attr()(the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race.Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Denis V. Lunev <[email protected]>--- arch/x86/mm/pat/set_memory.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +15
-1@@ -418,6 +418,16 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)int collapsed =0 ;int i; +/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ; + +spin_lock (&cpa_lock); +if (cpa->flags & (CPA_PAGES_ARRAY |CPA_ARRAY )) {for (i =0 ; i < cpa->numpages; i++) collapsed += collapse_large_pages(__cpa_addr(cpa, i),@@ -431,8 +441,10 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) collapsed +=collapse_large_pages (addr, &pgtables); } -if (!collapsed) +if (!collapsed) { +spin_unlock (&cpa_lock);return ; + }flush_tlb_all ();@@ -440,6 +452,8 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)list_del (&ptdesc->pt_list);pagetable_free (ptdesc); } + +spin_unlock (&cpa_lock); }static void cpa_flush (struct cpa_data *cpa,int cache)
Quoted 1 line
base-commit:
b81d185839fade27f7c4e885856696cf497d53c1
ok, guys :-)
What is the direction to go with this change?
(the patch is working for me well in downstream).
Thank you in advance,
Den
Quoted 102 lines · Denis V. Lunev
Quoted 101 lines · Denis V. Lunev
This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive. Loading and unloading modules concurrently on several CPUs on a KASAN
build, with a short delay injected at the CPA page-table lookup to
widen the window, faults within minutes:BUG: KASAN: use-after-free in __change_page_attr+0x7cc/0x7e0 Write of size 8 at addr ffff888181139718 by task modprobe ... The buggy address belongs to the physical page: pfn:0x181139 ... page_type: f2(table)
cpa_collapse_large_pages()rebuilds a leaf PMD from its 4K PTEs and frees the old PTE-table pages, while__change_page_attr()fetches a PTE pointer from a locklesslookup_address_in_pgd_attr()and writes it withset_pte_atomic()only later. When module text is served from a shared large ROX mapping the two run on the same PMD:CPU A (module load) CPU B (module finalize)
------------------- -----------------------
execmem_make_temp_rw
set_memory_nx__change_page_attr split 2M -> 4K table P kpte = &P[i] (lockless) execmem_restore_rox set_memory_rox (CPA_COLLAPSE) cpa_collapse_large_pages rebuild leaf PMD flush_tlb_all pagetable_free(P) set_pte_atomic(kpte, ...) -> writes into freed PP is a page-table page (
page_type: table), reused at once, so the write corrupts whatever got the page next: a bad-pte or bad-page splat, or a fatal fault once P has been turned into read-only text.The
flush_tlb_all()before the free does not close this: its IPI only serializes against page-table walkers that run with interrupts off (e.g. GUP-fast); the walk in__change_page_attr()runs with interrupts on, so nothing stops it from holding a stale pointer into P.Serialize the collapse - the PMD rebuild, TLB flush and PTE-table free - under
cpa_lock, the lock__change_page_attr()takes for the split path, so a concurrent walker can no longer hold a pointer into a table the collapse is about to free.
debug_pageallocbypassescpa_lockin__change_page_attr()(the direct map is 4K then, with no large pages to serialize), so the lock cannot order the two there. Skip the collapse in that config: it is only an optimization, and not freeing the tables leaves the unserialized walk nothing to race.Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Denis V. Lunev <[email protected]>--- arch/x86/mm/pat/set_memory.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +15
-1@@ -418,6 +418,16 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)int collapsed =0 ;int i; +/* +* debug_pagealloc bypasses cpa_lock, so __change_page_attr() walks +* unserialized and freeing collapsed PTE-tables could race it; skip +* the optional merge there. +*/ +if (debug_pagealloc_enabled ()) +return ; + +spin_lock (&cpa_lock); +if (cpa->flags & (CPA_PAGES_ARRAY |CPA_ARRAY )) {for (i =0 ; i < cpa->numpages; i++) collapsed += collapse_large_pages(__cpa_addr(cpa, i),@@ -431,8 +441,10 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) collapsed +=collapse_large_pages (addr, &pgtables); } -if (!collapsed) +if (!collapsed) { +spin_unlock (&cpa_lock);return ; + }flush_tlb_all ();@@ -440,6 +452,8 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)list_del (&ptdesc->pt_list);pagetable_free (ptdesc); } + +spin_unlock (&cpa_lock); }
Quoted 7 lines
Quoted 3 lines
arch/x86/mm/pat/set_memory.c +0
-0static void cpa_flush (struct cpa_data *cpa,int cache)base-commit:
b81d185839fade27f7c4e885856696cf497d53c1ok, guys :-)
What is the direction to go with this change?
(the patch is working for me well in downstream).
I am solving another issue in CPA, which I think can be solved by taking a different lock here which equally serialises CPA collapse walkers.
I will be doing a respin on that series relatively soon for it assuming the approach is workable.
See https://lore.kernel.org/linux-mm/aljb7hoFxL7aUZvG@lucifer/
Quoted 4 lines
Thank you in advance,
Den
Thanks, Lorenzo