Skip to contentSkip to conversation

mm/page_alloc: skip debug_check_no_{obj,locks}_freed with FPI_TRYLOCK

PATCH Superseded Harry Yoo 3 participants 5 messages Feb 6 – Feb 7

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Harry Yoo

author
#0 lore raw

PATCH 1/1 mm/page_alloc: skip debug_check_no_{obj,locks}_freed with FPI_TRYLOCK

When CONFIG_DEBUG_OBJECTS_FREE is enabled, debug_check_no_{obj,locks}_freed() functions are called.

Since both of them spin on a lock, they are not safe to be called if the FPI_TRYLOCK flag is specified. This leads to a lockdep splat:

================================
WARNING: inconsistent lock state
6.19.0-rc5-slab-for-next+ #326 Tainted: G N
--------------------------------
inconsistent {INITIAL USE} -> {IN-NMI} usage.
kunit_try_catch/9046 [HC2[2]:SC0[0]:HE0:SE1] takes:
ffffffff84ed6bf8 (&obj_hash[i].lock){-.-.}-{2:2}, at: __debug_check_no_obj_freed+0xe0/0x300 {INITIAL USE} state was registered at:

    lock_acquire+0xd9/0x2f0
    _raw_spin_lock_irqsave+0x4c/0x80
    __debug_object_init+0x9d/0x1f0
    debug_object_init+0x34/0x50
    __init_work+0x28/0x40
    init_cgroup_housekeeping+0x151/0x210
    init_cgroup_root+0x3d/0x140
    cgroup_init_early+0x30/0x240
    start_kernel+0x3e/0xcd0
    x86_64_start_reservations+0x18/0x30
    x86_64_start_kernel+0xf3/0x140
    common_startup_64+0x13e/0x148
  irq event stamp: 2998
  hardirqs last  enabled at (2997): [<ffffffff8298b77a>] exc_nmi+0x11a/0x240
  hardirqs last disabled at (2998): [<ffffffff8298b991>] sysvec_irq_work+0x11/0x110
  softirqs last  enabled at (1416): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0
  softirqs last disabled at (1303): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0

other info that might help us debug this:

   Possible unsafe locking scenario:
         CPU0
         ----
    lock(&obj_hash[i].lock);
    <Interrupt>
      lock(&obj_hash[i].lock);

*** DEADLOCK ***

Fix this by adding an fpi_t parameter to free_pages_prepare() and skipping those checks if FPI_TRYLOCK is set. Since mm/compaction.c calls free_pages_prepare(), move the fpi_t definition to mm/internal.h.

Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
---
 mm/compaction.c |  2 +-
 mm/internal.h   | 35 ++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c | 42 ++++++------------------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)
mm/compaction.c +1 -1
index 1e8f8eca318c..9ffeb7c6d2b0 100644
@@ -1859,7 +1859,7 @@ static void compaction_free(struct folio *dst, unsigned long data)
 	struct page *page = &dst->page;
 
 	if (folio_put_testzero(dst)) {
-		free_pages_prepare(page, order);
+		free_pages_prepare(page, order, FPI_NONE);
 		list_add(&dst->lru, &cc->freepages[order]);
 		cc->nr_freepages += 1 << order;
 	}
mm/internal.h +34 -1
index 1f44ccb4badf..8fdffbe5cf1f 100644
@@ -820,7 +820,40 @@ static inline void prep_compound_tail(struct page *head, int tail_idx)
 }
 
 void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags);
-extern bool free_pages_prepare(struct page *page, unsigned int order);
+
+/* Free Page Internal flags: for internal, non-pcp variants of free_pages(). */
+typedef int __bitwise fpi_t;
+
+/* No special request */
+#define FPI_NONE		((__force fpi_t)0)
+
+/*
+ * Skip free page reporting notification for the (possibly merged) page.
+ * This does not hinder free page reporting from grabbing the page,
+ * reporting it and marking it "reported" -  it only skips notifying
+ * the free page reporting infrastructure about a newly freed page. For
+ * example, used when temporarily pulling a page from a freelist and
+ * putting it back unmodified.
+ */
+#define FPI_SKIP_REPORT_NOTIFY	((__force fpi_t)BIT(0))
+
+/*
+ * Place the (possibly merged) page to the tail of the freelist. Will ignore
+ * page shuffling (relevant code - e.g., memory onlining - is expected to
+ * shuffle the whole zone).
+ *
+ * Note: No code should rely on this flag for correctness - it's purely
+ *       to allow for optimizations when handing back either fresh pages
+ *       (memory onlining) or untouched pages (page isolation, free page
+ *       reporting).
+ */
+#define FPI_TO_TAIL		((__force fpi_t)BIT(1))
+
+/* Free the page without taking locks. Rely on trylock only. */
+#define FPI_TRYLOCK		((__force fpi_t)BIT(2))
+
+extern bool free_pages_prepare(struct page *page, unsigned int order,
+			       fpi_t fpi_flags);
 
 extern int user_min_free_kbytes;
 
mm/page_alloc.c +6 -36

78 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 61 lines · Harry Yoo

When CONFIG_DEBUG_OBJECTS_FREE is enabled, debug_check_no_{obj,locks}_freed() functions are called.

Since both of them spin on a lock, they are not safe to be called if the FPI_TRYLOCK flag is specified. This leads to a lockdep splat:

================================
WARNING: inconsistent lock state
6.19.0-rc5-slab-for-next+ #326 Tainted: G N
--------------------------------
inconsistent {INITIAL USE} -> {IN-NMI} usage.
kunit_try_catch/9046 [HC2[2]:SC0[0]:HE0:SE1] takes:
ffffffff84ed6bf8 (&obj_hash[i].lock){-.-.}-{2:2}, at: __debug_check_no_obj_freed+0xe0/0x300 {INITIAL USE} state was registered at:

    lock_acquire+0xd9/0x2f0
    _raw_spin_lock_irqsave+0x4c/0x80
    __debug_object_init+0x9d/0x1f0
    debug_object_init+0x34/0x50
    __init_work+0x28/0x40
    init_cgroup_housekeeping+0x151/0x210
    init_cgroup_root+0x3d/0x140
    cgroup_init_early+0x30/0x240
    start_kernel+0x3e/0xcd0
    x86_64_start_reservations+0x18/0x30
    x86_64_start_kernel+0xf3/0x140
    common_startup_64+0x13e/0x148
  irq event stamp: 2998
  hardirqs last  enabled at (2997): [<ffffffff8298b77a>] exc_nmi+0x11a/0x240
  hardirqs last disabled at (2998): [<ffffffff8298b991>] sysvec_irq_work+0x11/0x110
  softirqs last  enabled at (1416): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0
  softirqs last disabled at (1303): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0

other info that might help us debug this:

   Possible unsafe locking scenario:
         CPU0
         ----
    lock(&obj_hash[i].lock);
    <Interrupt>
      lock(&obj_hash[i].lock);
   *** DEADLOCK ***

Fix this by adding an fpi_t parameter to free_pages_prepare() and skipping those checks if FPI_TRYLOCK is set. Since mm/compaction.c calls free_pages_prepare(), move the fpi_t definition to mm/internal.h.

Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
---
 mm/compaction.c |  2 +-
 mm/internal.h   | 35 ++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c | 42 ++++++------------------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)
mm/compaction.c +0 -0
index 1e8f8eca318c..9ffeb7c6d2b0 100644

mm/compaction.c static void compaction_free(struct folio *dst, unsigned long data) · line 1862

Quoted 5 lines
mm/compaction.c +1 -1
@@ -1859,7 +1859,7 @@ static void compaction_free(struct folio *dst, unsigned long data)
 	struct page *page = &dst->page;
 
 	if (folio_put_testzero(dst)) {
-		free_pages_prepare(page, order);
+		free_pages_prepare(page, order, FPI_NONE);

Is it OK to add something like free_pages_prepare_fpi_none() for this one to avoid the FPI flag move?

Best Regards,
Yan, Zi

author
#2 lore raw
Quoted 65 lines · Zi Yan
Quoted 63 lines · Harry Yoo

When CONFIG_DEBUG_OBJECTS_FREE is enabled, debug_check_no_{obj,locks}_freed() functions are called.

Since both of them spin on a lock, they are not safe to be called if the FPI_TRYLOCK flag is specified. This leads to a lockdep splat:

================================
WARNING: inconsistent lock state
6.19.0-rc5-slab-for-next+ #326 Tainted: G N
--------------------------------
inconsistent {INITIAL USE} -> {IN-NMI} usage.
kunit_try_catch/9046 [HC2[2]:SC0[0]:HE0:SE1] takes:
ffffffff84ed6bf8 (&obj_hash[i].lock){-.-.}-{2:2}, at: __debug_check_no_obj_freed+0xe0/0x300 {INITIAL USE} state was registered at:

    lock_acquire+0xd9/0x2f0
    _raw_spin_lock_irqsave+0x4c/0x80
    __debug_object_init+0x9d/0x1f0
    debug_object_init+0x34/0x50
    __init_work+0x28/0x40
    init_cgroup_housekeeping+0x151/0x210
    init_cgroup_root+0x3d/0x140
    cgroup_init_early+0x30/0x240
    start_kernel+0x3e/0xcd0
    x86_64_start_reservations+0x18/0x30
    x86_64_start_kernel+0xf3/0x140
    common_startup_64+0x13e/0x148
  irq event stamp: 2998
  hardirqs last  enabled at (2997): [<ffffffff8298b77a>] exc_nmi+0x11a/0x240
  hardirqs last disabled at (2998): [<ffffffff8298b991>] sysvec_irq_work+0x11/0x110
  softirqs last  enabled at (1416): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0
  softirqs last disabled at (1303): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0

other info that might help us debug this:

   Possible unsafe locking scenario:
         CPU0
         ----
    lock(&obj_hash[i].lock);
    <Interrupt>
      lock(&obj_hash[i].lock);
   *** DEADLOCK ***

Fix this by adding an fpi_t parameter to free_pages_prepare() and skipping those checks if FPI_TRYLOCK is set. Since mm/compaction.c calls free_pages_prepare(), move the fpi_t definition to mm/internal.h.

Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
---
 mm/compaction.c |  2 +-
 mm/internal.h   | 35 ++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c | 42 ++++++------------------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)
mm/compaction.c +0 -0
index 1e8f8eca318c..9ffeb7c6d2b0 100644
@@ -1859,7 +1859,7 @@ static void compaction_free(struct folio *dst, unsigned long data)
 	struct page *page = &dst->page;
 
Quoted 6 lines

mm/compaction.c static void compaction_free(struct folio *dst, unsigned long data) · line 1862

Quoted 3 lines
mm/compaction.c +1 -1
 	if (folio_put_testzero(dst)) {
-		free_pages_prepare(page, order);
+		free_pages_prepare(page, order, FPI_NONE);

Is it OK to add something like free_pages_prepare_fpi_none() for this one to avoid the FPI flag move?

Yeah, moving FPI flag definition isn't great :)

I'm totally fine with your suggestion,
as long as page allocator/compaction folks are fine with it!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#3 lore raw
Quoted 73 lines · Harry Yoo
Quoted 65 lines · Zi Yan
Quoted 63 lines · Harry Yoo

When CONFIG_DEBUG_OBJECTS_FREE is enabled, debug_check_no_{obj,locks}_freed() functions are called.

Since both of them spin on a lock, they are not safe to be called if the FPI_TRYLOCK flag is specified. This leads to a lockdep splat:

================================
WARNING: inconsistent lock state
6.19.0-rc5-slab-for-next+ #326 Tainted: G N
--------------------------------
inconsistent {INITIAL USE} -> {IN-NMI} usage.
kunit_try_catch/9046 [HC2[2]:SC0[0]:HE0:SE1] takes:
ffffffff84ed6bf8 (&obj_hash[i].lock){-.-.}-{2:2}, at: __debug_check_no_obj_freed+0xe0/0x300 {INITIAL USE} state was registered at:

    lock_acquire+0xd9/0x2f0
    _raw_spin_lock_irqsave+0x4c/0x80
    __debug_object_init+0x9d/0x1f0
    debug_object_init+0x34/0x50
    __init_work+0x28/0x40
    init_cgroup_housekeeping+0x151/0x210
    init_cgroup_root+0x3d/0x140
    cgroup_init_early+0x30/0x240
    start_kernel+0x3e/0xcd0
    x86_64_start_reservations+0x18/0x30
    x86_64_start_kernel+0xf3/0x140
    common_startup_64+0x13e/0x148
  irq event stamp: 2998
  hardirqs last  enabled at (2997): [<ffffffff8298b77a>] exc_nmi+0x11a/0x240
  hardirqs last disabled at (2998): [<ffffffff8298b991>] sysvec_irq_work+0x11/0x110
  softirqs last  enabled at (1416): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0
  softirqs last disabled at (1303): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0

other info that might help us debug this:

   Possible unsafe locking scenario:
         CPU0
         ----
    lock(&obj_hash[i].lock);
    <Interrupt>
      lock(&obj_hash[i].lock);
   *** DEADLOCK ***

Fix this by adding an fpi_t parameter to free_pages_prepare() and skipping those checks if FPI_TRYLOCK is set. Since mm/compaction.c calls free_pages_prepare(), move the fpi_t definition to mm/internal.h.

Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
---
 mm/compaction.c |  2 +-
 mm/internal.h   | 35 ++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c | 42 ++++++------------------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)
mm/compaction.c +0 -0
index 1e8f8eca318c..9ffeb7c6d2b0 100644
@@ -1859,7 +1859,7 @@ static void compaction_free(struct folio *dst, unsigned long data)
 	struct page *page = &dst->page;
 
Quoted 6 lines

mm/compaction.c static void compaction_free(struct folio *dst, unsigned long data) · line 1862

Quoted 3 lines
mm/compaction.c +1 -1
 	if (folio_put_testzero(dst)) {
-		free_pages_prepare(page, order);
+		free_pages_prepare(page, order, FPI_NONE);

Is it OK to add something like free_pages_prepare_fpi_none() for this one to avoid the FPI flag move?

Quoted 4 lines

Yeah, moving FPI flag definition isn't great :)

I'm totally fine with your suggestion,
as long as page allocator/compaction folks are fine with it!

Maybe even like this?
free_pages_prepare() which calls __free_pages_prepare(FPI_NONE)

author
#4 lore raw
Quoted 74 lines · Vlastimil Babka
Quoted 73 lines · Harry Yoo
Quoted 71 lines · Zi Yan
Quoted 61 lines · Harry Yoo

When CONFIG_DEBUG_OBJECTS_FREE is enabled, debug_check_no_{obj,locks}_freed() functions are called.

Since both of them spin on a lock, they are not safe to be called if the FPI_TRYLOCK flag is specified. This leads to a lockdep splat:

================================
WARNING: inconsistent lock state
6.19.0-rc5-slab-for-next+ #326 Tainted: G N
--------------------------------
inconsistent {INITIAL USE} -> {IN-NMI} usage.
kunit_try_catch/9046 [HC2[2]:SC0[0]:HE0:SE1] takes:
ffffffff84ed6bf8 (&obj_hash[i].lock){-.-.}-{2:2}, at: __debug_check_no_obj_freed+0xe0/0x300 {INITIAL USE} state was registered at:

    lock_acquire+0xd9/0x2f0
    _raw_spin_lock_irqsave+0x4c/0x80
    __debug_object_init+0x9d/0x1f0
    debug_object_init+0x34/0x50
    __init_work+0x28/0x40
    init_cgroup_housekeeping+0x151/0x210
    init_cgroup_root+0x3d/0x140
    cgroup_init_early+0x30/0x240
    start_kernel+0x3e/0xcd0
    x86_64_start_reservations+0x18/0x30
    x86_64_start_kernel+0xf3/0x140
    common_startup_64+0x13e/0x148
  irq event stamp: 2998
  hardirqs last  enabled at (2997): [<ffffffff8298b77a>] exc_nmi+0x11a/0x240
  hardirqs last disabled at (2998): [<ffffffff8298b991>] sysvec_irq_work+0x11/0x110
  softirqs last  enabled at (1416): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0
  softirqs last disabled at (1303): [<ffffffff813c1f72>] __irq_exit_rcu+0x132/0x1c0

other info that might help us debug this:

   Possible unsafe locking scenario:
         CPU0
         ----
    lock(&obj_hash[i].lock);
    <Interrupt>
      lock(&obj_hash[i].lock);
   *** DEADLOCK ***

Fix this by adding an fpi_t parameter to free_pages_prepare() and skipping those checks if FPI_TRYLOCK is set. Since mm/compaction.c calls free_pages_prepare(), move the fpi_t definition to mm/internal.h.

Fixes 8c57b687e833 ("mm, bpf: Introduce free_pages_nolock()")
---
 mm/compaction.c |  2 +-
 mm/internal.h   | 35 ++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c | 42 ++++++------------------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)
mm/compaction.c +0 -0
index 1e8f8eca318c..9ffeb7c6d2b0 100644

mm/compaction.c static void compaction_free(struct folio *dst, unsigned long data) · line 1862

Quoted 5 lines
mm/compaction.c +1 -1
@@ -1859,7 +1859,7 @@ static void compaction_free(struct folio *dst, unsigned long data)
 	struct page *page = &dst->page;
 
 	if (folio_put_testzero(dst)) {
-		free_pages_prepare(page, order);
+		free_pages_prepare(page, order, FPI_NONE);

Is it OK to add something like free_pages_prepare_fpi_none() for this one to avoid the FPI flag move?

Quoted 7 lines
Quoted 4 lines

Yeah, moving FPI flag definition isn't great :)

I'm totally fine with your suggestion,
as long as page allocator/compaction folks are fine with it!

Maybe even like this?
free_pages_prepare() which calls __free_pages_prepare(FPI_NONE)

Sounds good and will respin v2 soon, thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon