Skip to contentSkip to conversation

mm: don't schedule deferred kernel page table freeing while booting

PATCH In review Mikhail Gavrilov 4 participants 4 messages Sep 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mm: don't schedule deferred kernel page table freeing while booting

Booting with a boot-time function tracer and a filter, for example

ftrace=function ftrace_filter=pud_free_pmd_page

panics on 7.3-rc4 as soon as the tracer starts:

  [   23.531178] Starting tracer 'function'
  [   23.675800] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000038: 0000 [#1] SMP KASAN NOPTI
  [   23.819917] KASAN: null-ptr-deref in range [0x00000000000001c0-0x00000000000001c7]
  [   23.964025] CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 7.3.0-rc4-fe2ec83746e5-with-fixes-v2+ #195 PREEMPT(undef)
  [   24.252248] RIP: 0010:__queue_work+0xab/0xf00
  [   25.981629] Call Trace:
  [   26.125727]  <TASK>
  [   26.413912]  ? pagetable_free_kernel+0x20/0x120
  [   26.990283]  queue_work_on+0x97/0xf0
  [   27.134382]  __cpa_collapse_large_pages+0x501/0x6f0
  [   27.566662]  cpa_flush+0x394/0x620
  [   27.998953]  change_page_attr_set_clr+0x321/0x4a0
  [   29.151729]  set_memory_rox+0xa2/0xf0
  [   29.584018]  create_trampoline+0x431/0x6f0
  ...
  [   44.343347] Kernel panic - not syncing: Attempted to kill the idle task!

The boot-time tracer is started from early_trace_init(), which runs before workqueue_init_early(). Making its trampoline read-only splits a large page, and CPA collapses it again right away. The split table has been a kernel page table since commit 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables"), so the collapse frees it through pagetable_free_kernel(), which queues work on system_percpu_wq - still NULL at that point. That commit is correct in itself; it only lets CPA reach pagetable_free_kernel() before the workqueue that function relies on exists.

Keep putting the table on the list, but don't schedule the work while the system is still booting. A core_initcall schedules it once to free whatever was queued by then.

Fixes 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables")

---
v3:
- Schedule the work once from a core_initcall to free whatever was queued during boot (Lorenzo Stoakes, Dave Hansen, David Hildenbrand). late_initcall would work just as well; workqueues exist from workqueue_init() on.
- Keep the fix in pagetable_free_kernel() rather than skipping the collapse during boot (Mike Rapoport): that would only avoid this caller, and any other early free would still need a workqueue.
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]

Tested on a Ryzen 9 7950X with a Radeon RX 7900 XTX (lockdep, KASAN), 7.3-rc4 plus unrelated local changes, by booting with

ftrace=function ftrace_filter=pud_free_pmd_page,pagetable_free_kernel,kernel_pgtable_work_func,kernel_pgtable_drain_early

The boot that panicked without the fix completes, and the trace shows kernel_pgtable_drain_early() and then kernel_pgtable_work_func() before any other kernel page table is freed.

 mm/pgtable-generic.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
mm/pgtable-generic.c +19 -1
index b91b1a98029c..cd227fc05d2d 100644
@@ -438,12 +438,30 @@ static void kernel_pgtable_work_func(struct work_struct *work)
 		__pagetable_free(pt);
 }
 
+static void schedule_kernel_pgtable_free(void)
+{
+	schedule_work(&kernel_pgtable_work.work);
+}
+
 void pagetable_free_kernel(struct ptdesc *pt)
 {
 	spin_lock(&kernel_pgtable_work.lock);
 	list_add(&pt->pt_list, &kernel_pgtable_work.list);
 	spin_unlock(&kernel_pgtable_work.lock);
 
-	schedule_work(&kernel_pgtable_work.work);
+	/*
+	 * The workqueue may not exist yet while the system is booting.
+	 * kernel_pgtable_drain_early() schedules the work once it does.
+	 */
+	if (system_state != SYSTEM_BOOTING)
+		schedule_kernel_pgtable_free();
+}
+
+static int __init kernel_pgtable_drain_early(void)
+{
+	/* Free the kernel page tables queued while booting. */
+	schedule_kernel_pgtable_free();
+	return 0;
 }
+core_initcall(kernel_pgtable_drain_early);
 #endif
Signature · 1 line
2.55.0
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 93 lines · Mikhail Gavrilov

Booting with a boot-time function tracer and a filter, for example

ftrace=function ftrace_filter=pud_free_pmd_page

panics on 7.3-rc4 as soon as the tracer starts:

  [   23.531178] Starting tracer 'function'
  [   23.675800] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000038: 0000 [#1] SMP KASAN NOPTI
  [   23.819917] KASAN: null-ptr-deref in range [0x00000000000001c0-0x00000000000001c7]
  [   23.964025] CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 7.3.0-rc4-fe2ec83746e5-with-fixes-v2+ #195 PREEMPT(undef)
  [   24.252248] RIP: 0010:__queue_work+0xab/0xf00
  [   25.981629] Call Trace:
  [   26.125727]  <TASK>
  [   26.413912]  ? pagetable_free_kernel+0x20/0x120
  [   26.990283]  queue_work_on+0x97/0xf0
  [   27.134382]  __cpa_collapse_large_pages+0x501/0x6f0
  [   27.566662]  cpa_flush+0x394/0x620
  [   27.998953]  change_page_attr_set_clr+0x321/0x4a0
  [   29.151729]  set_memory_rox+0xa2/0xf0
  [   29.584018]  create_trampoline+0x431/0x6f0
  ...
  [   44.343347] Kernel panic - not syncing: Attempted to kill the idle task!

The boot-time tracer is started from early_trace_init(), which runs before workqueue_init_early(). Making its trampoline read-only splits a large page, and CPA collapses it again right away. The split table has been a kernel page table since commit 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables"), so the collapse frees it through pagetable_free_kernel(), which queues work on system_percpu_wq - still NULL at that point. That commit is correct in itself; it only lets CPA reach pagetable_free_kernel() before the workqueue that function relies on exists.

Keep putting the table on the list, but don't schedule the work while the system is still booting. A core_initcall schedules it once to free whatever was queued by then.

Fixes 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables")

---
v3:
- Schedule the work once from a core_initcall to free whatever was queued during boot (Lorenzo Stoakes, Dave Hansen, David Hildenbrand). late_initcall would work just as well; workqueues exist from workqueue_init() on.
- Keep the fix in pagetable_free_kernel() rather than skipping the collapse during boot (Mike Rapoport): that would only avoid this caller, and any other early free would still need a workqueue.
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]

Tested on a Ryzen 9 7950X with a Radeon RX 7900 XTX (lockdep, KASAN), 7.3-rc4 plus unrelated local changes, by booting with

ftrace=function ftrace_filter=pud_free_pmd_page,pagetable_free_kernel,kernel_pgtable_work_func,kernel_pgtable_drain_early

The boot that panicked without the fix completes, and the trace shows kernel_pgtable_drain_early() and then kernel_pgtable_work_func() before any other kernel page table is freed.

 mm/pgtable-generic.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
mm/pgtable-generic.c +11 -1
index b91b1a98029c..cd227fc05d2d 100644
@@ -438,12 +438,30 @@ static void kernel_pgtable_work_func(struct work_struct *work)
 		__pagetable_free(pt);
 }
 
+static void schedule_kernel_pgtable_free(void)
+{
+	schedule_work(&kernel_pgtable_work.work);
+}
+
 void pagetable_free_kernel(struct ptdesc *pt)
 {
 	spin_lock(&kernel_pgtable_work.lock);
 	list_add(&pt->pt_list, &kernel_pgtable_work.list);
 	spin_unlock(&kernel_pgtable_work.lock);
 
-	schedule_work(&kernel_pgtable_work.work);
+	/*
+	 * The workqueue may not exist yet while the system is booting.
+	 * kernel_pgtable_drain_early() schedules the work once it does.
+	 */
+	if (system_state != SYSTEM_BOOTING)
+		schedule_kernel_pgtable_free();

mm/pgtable-generic.c static void kernel_pgtable_work_func(struct work_struct *work) · line 467

Quoted 10 lines
mm/pgtable-generic.c +8 -0
+}
+
+static int __init kernel_pgtable_drain_early(void)
+{
+	/* Free the kernel page tables queued while booting. */
+	schedule_kernel_pgtable_free();
+	return 0;
 }
+core_initcall(kernel_pgtable_drain_early);
 #endif
Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v3 1/1
#2 lore raw
In reply to Mikhail Gavrilov
Quoted 37 lines · Mikhail Gavrilov

Booting with a boot-time function tracer and a filter, for example

ftrace=function ftrace_filter=pud_free_pmd_page

panics on 7.3-rc4 as soon as the tracer starts:

  [   23.531178] Starting tracer 'function'
  [   23.675800] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000038: 0000 [#1] SMP KASAN NOPTI
  [   23.819917] KASAN: null-ptr-deref in range [0x00000000000001c0-0x00000000000001c7]
  [   23.964025] CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 7.3.0-rc4-fe2ec83746e5-with-fixes-v2+ #195 PREEMPT(undef)
  [   24.252248] RIP: 0010:__queue_work+0xab/0xf00
  [   25.981629] Call Trace:
  [   26.125727]  <TASK>
  [   26.413912]  ? pagetable_free_kernel+0x20/0x120
  [   26.990283]  queue_work_on+0x97/0xf0
  [   27.134382]  __cpa_collapse_large_pages+0x501/0x6f0
  [   27.566662]  cpa_flush+0x394/0x620
  [   27.998953]  change_page_attr_set_clr+0x321/0x4a0
  [   29.151729]  set_memory_rox+0xa2/0xf0
  [   29.584018]  create_trampoline+0x431/0x6f0
  ...
  [   44.343347] Kernel panic - not syncing: Attempted to kill the idle task!

The boot-time tracer is started from early_trace_init(), which runs before workqueue_init_early(). Making its trampoline read-only splits a large page, and CPA collapses it again right away. The split table has been a kernel page table since commit 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables"), so the collapse frees it through pagetable_free_kernel(), which queues work on system_percpu_wq - still NULL at that point. That commit is correct in itself; it only lets CPA reach pagetable_free_kernel() before the workqueue that function relies on exists.

Keep putting the table on the list, but don't schedule the work while the system is still booting. A core_initcall schedules it once to free whatever was queued by then.

Quoted 5 lines
Fixes 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables")

LGTM so:

Quoted 62 lines

---
v3:
- Schedule the work once from a core_initcall to free whatever was queued during boot (Lorenzo Stoakes, Dave Hansen, David Hildenbrand). late_initcall would work just as well; workqueues exist from workqueue_init() on.
- Keep the fix in pagetable_free_kernel() rather than skipping the collapse during boot (Mike Rapoport): that would only avoid this caller, and any other early free would still need a workqueue.
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]

Tested on a Ryzen 9 7950X with a Radeon RX 7900 XTX (lockdep, KASAN), 7.3-rc4 plus unrelated local changes, by booting with

ftrace=function ftrace_filter=pud_free_pmd_page,pagetable_free_kernel,kernel_pgtable_work_func,kernel_pgtable_drain_early

The boot that panicked without the fix completes, and the trace shows kernel_pgtable_drain_early() and then kernel_pgtable_work_func() before any other kernel page table is freed.

 mm/pgtable-generic.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
mm/pgtable-generic.c +19 -2
index b91b1a98029c..cd227fc05d2d 100644
@@ -438,12 +438,30 @@ static void kernel_pgtable_work_func(struct work_struct *work)
 		__pagetable_free(pt);
 }
 
+static void schedule_kernel_pgtable_free(void)
+{
+	schedule_work(&kernel_pgtable_work.work);
+}
+
 void pagetable_free_kernel(struct ptdesc *pt)
 {
 	spin_lock(&kernel_pgtable_work.lock);
 	list_add(&pt->pt_list, &kernel_pgtable_work.list);
 	spin_unlock(&kernel_pgtable_work.lock);
 
-	schedule_work(&kernel_pgtable_work.work);
+	/*
+	 * The workqueue may not exist yet while the system is booting.
+	 * kernel_pgtable_drain_early() schedules the work once it does.
+	 */
+	if (system_state != SYSTEM_BOOTING)
+		schedule_kernel_pgtable_free();
+}
+
+static int __init kernel_pgtable_drain_early(void)
+{
+	/* Free the kernel page tables queued while booting. */
+	schedule_kernel_pgtable_free();
+	return 0;
 }
+core_initcall(kernel_pgtable_drain_early);
 #endif
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

Reviewed-by Tested-by on v3 1/1
#3 lore raw
In reply to Mikhail Gavrilov
Quoted 93 lines · Mikhail Gavrilov

Booting with a boot-time function tracer and a filter, for example

ftrace=function ftrace_filter=pud_free_pmd_page

panics on 7.3-rc4 as soon as the tracer starts:

  [   23.531178] Starting tracer 'function'
  [   23.675800] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000038: 0000 [#1] SMP KASAN NOPTI
  [   23.819917] KASAN: null-ptr-deref in range [0x00000000000001c0-0x00000000000001c7]
  [   23.964025] CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 7.3.0-rc4-fe2ec83746e5-with-fixes-v2+ #195 PREEMPT(undef)
  [   24.252248] RIP: 0010:__queue_work+0xab/0xf00
  [   25.981629] Call Trace:
  [   26.125727]  <TASK>
  [   26.413912]  ? pagetable_free_kernel+0x20/0x120
  [   26.990283]  queue_work_on+0x97/0xf0
  [   27.134382]  __cpa_collapse_large_pages+0x501/0x6f0
  [   27.566662]  cpa_flush+0x394/0x620
  [   27.998953]  change_page_attr_set_clr+0x321/0x4a0
  [   29.151729]  set_memory_rox+0xa2/0xf0
  [   29.584018]  create_trampoline+0x431/0x6f0
  ...
  [   44.343347] Kernel panic - not syncing: Attempted to kill the idle task!

The boot-time tracer is started from early_trace_init(), which runs before workqueue_init_early(). Making its trampoline read-only splits a large page, and CPA collapses it again right away. The split table has been a kernel page table since commit 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables"), so the collapse frees it through pagetable_free_kernel(), which queues work on system_percpu_wq - still NULL at that point. That commit is correct in itself; it only lets CPA reach pagetable_free_kernel() before the workqueue that function relies on exists.

Keep putting the table on the list, but don't schedule the work while the system is still booting. A core_initcall schedules it once to free whatever was queued by then.

Fixes 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables")

---
v3:
- Schedule the work once from a core_initcall to free whatever was queued during boot (Lorenzo Stoakes, Dave Hansen, David Hildenbrand). late_initcall would work just as well; workqueues exist from workqueue_init() on.
- Keep the fix in pagetable_free_kernel() rather than skipping the collapse during boot (Mike Rapoport): that would only avoid this caller, and any other early free would still need a workqueue.
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]

Tested on a Ryzen 9 7950X with a Radeon RX 7900 XTX (lockdep, KASAN), 7.3-rc4 plus unrelated local changes, by booting with

ftrace=function ftrace_filter=pud_free_pmd_page,pagetable_free_kernel,kernel_pgtable_work_func,kernel_pgtable_drain_early

The boot that panicked without the fix completes, and the trace shows kernel_pgtable_drain_early() and then kernel_pgtable_work_func() before any other kernel page table is freed.

 mm/pgtable-generic.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
mm/pgtable-generic.c +11 -1
index b91b1a98029c..cd227fc05d2d 100644
@@ -438,12 +438,30 @@ static void kernel_pgtable_work_func(struct work_struct *work)
 		__pagetable_free(pt);
 }
 
+static void schedule_kernel_pgtable_free(void)
+{
+	schedule_work(&kernel_pgtable_work.work);
+}
+
 void pagetable_free_kernel(struct ptdesc *pt)
 {
 	spin_lock(&kernel_pgtable_work.lock);
 	list_add(&pt->pt_list, &kernel_pgtable_work.list);
 	spin_unlock(&kernel_pgtable_work.lock);
 
-	schedule_work(&kernel_pgtable_work.work);
+	/*
+	 * The workqueue may not exist yet while the system is booting.
+	 * kernel_pgtable_drain_early() schedules the work once it does.
+	 */
+	if (system_state != SYSTEM_BOOTING)
+		schedule_kernel_pgtable_free();

mm/pgtable-generic.c static void kernel_pgtable_work_func(struct work_struct *work) · line 467

Quoted 10 lines
mm/pgtable-generic.c +8 -0
+}
+
+static int __init kernel_pgtable_drain_early(void)
+{
+	/* Free the kernel page tables queued while booting. */
+	schedule_kernel_pgtable_free();
+	return 0;
 }
+core_initcall(kernel_pgtable_drain_early);
 #endif

tested it, boots cleanly and the panic is gone. LGTM.

--
cheers, jose a. p-a