Skip to contentSkip to conversation

mm/compaction: annotate data race for zone->free_area[order].nr_free

PATCH Superseded Hui Zhu 3 participants 7 messages Oct 28 – Oct 30

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH 1/1 mm/compaction: annotate data race for zone->free_area[order].nr_free

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

  BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589

race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:

   fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   isolate_freepages+0x52/0x650 mm/compaction.c:1722
   compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
   compaction_alloc+0x21/0x30 mm/compaction.c:1880
   migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
   migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
   migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
   migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
   compact_zone+0x539/0x10b0 mm/compaction.c:2660
   compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
   try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
   __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
   __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
   __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
   alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
   alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
   io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
   io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
   io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
   io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
   io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
   __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
   __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
   __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
   x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
   do_syscall_x64 arch/x86/entry/common.c:51 [inline]
   do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
   entry_SYSCALL_64_after_hwframe+0x76/0xe0

value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

No functional change intended.

---
 mm/compaction.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/compaction.c +10 -2
index f1b2060eb2016..499ec7ceaa3ed 100644
@@ -1601,7 +1601,11 @@ static void fast_isolate_freepages(struct compact_control *cc)
 		unsigned int order_scanned = 0;
 		unsigned long high_pfn = 0;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
@@ -2031,7 +2035,11 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc)
 		unsigned long flags;
 		struct page *freepage;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 43 lines · Hui Zhu

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

  BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589

race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:

   fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   isolate_freepages+0x52/0x650 mm/compaction.c:1722
   compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
   compaction_alloc+0x21/0x30 mm/compaction.c:1880
   migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
   migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
   migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
   migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
   compact_zone+0x539/0x10b0 mm/compaction.c:2660
   compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
   try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
   __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
   __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
   __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
   alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
   alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
   io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
   io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
   io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
   io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
   io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
   __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
   __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
   __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
   x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
   do_syscall_x64 arch/x86/entry/common.c:51 [inline]
   do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
   entry_SYSCALL_64_after_hwframe+0x76/0xe0

value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

Quoted 8 lines

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

No functional change intended.

teawater? What?

Quoted 9 lines
---
 mm/compaction.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/compaction.c +0 -0
index f1b2060eb2016..499ec7ceaa3ed 100644

mm/compaction.c static void fast_isolate_freepages(struct compact_control *cc) · line 1607

Quoted 8 lines
mm/compaction.c +4 -1
@@ -1601,7 +1601,11 @@ static void fast_isolate_freepages(struct compact_control *cc)
 		unsigned int order_scanned = 0;
 		unsigned long high_pfn = 0;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */

The comment is noise, the reason of using data_race() is a well-known fact.

Apart from that, do we have the same problem with zone_watermark_ok? It also uses area->nr_free without zone lock.

Quoted 17 lines
mm/compaction.c +6 -1
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
@@ -2031,7 +2035,11 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc)
 		unsigned long flags;
 		struct page *freepage;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
reviewer
#2 lore raw
Quoted 70 lines · Dev Jain
Quoted 43 lines · Hui Zhu

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

  BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589

race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:

   fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   isolate_freepages+0x52/0x650 mm/compaction.c:1722
   compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
   compaction_alloc+0x21/0x30 mm/compaction.c:1880
   migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
   migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
   migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
   migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
   compact_zone+0x539/0x10b0 mm/compaction.c:2660
   compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
   try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
   __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
   __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
   __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
   alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
   alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
   io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
   io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
   io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
   io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
   io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
   __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
   __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
   __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
   x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
   do_syscall_x64 arch/x86/entry/common.c:51 [inline]
   do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
   entry_SYSCALL_64_after_hwframe+0x76/0xe0

value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

Quoted 8 lines

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

No functional change intended.

teawater? What?

Quoted 12 lines
---
 mm/compaction.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/compaction.c +0 -0
index f1b2060eb2016..499ec7ceaa3ed 100644
@@ -1601,7 +1601,11 @@ static void fast_isolate_freepages(struct compact_control *cc)
 		unsigned int order_scanned = 0;
 		unsigned long high_pfn = 0;
 
Quoted 7 lines

mm/compaction.c static void fast_isolate_freepages(struct compact_control *cc) · line 1607

Quoted 5 lines
mm/compaction.c +4 -1
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */

The comment is noise, the reason of using data_race() is a well-known fact.

IIRC, checkpatch.pl requires a comment for each data_race(). The comments need to stay until you send a patch to change checkpatch.pl.

Quoted 21 lines

Apart from that, do we have the same problem with zone_watermark_ok? It also uses area->nr_free without zone lock.

Quoted 17 lines
mm/compaction.c +6 -1
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
@@ -2031,7 +2035,11 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc)
 		unsigned long flags;
 		struct page *freepage;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
Signature · 2 lines
Best Regards,
Yan, Zi
#3 lore raw
Quoted 79 lines · Zi Yan
Quoted 70 lines · Dev Jain
Quoted 43 lines · Hui Zhu

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

  BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589

race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:

   fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   isolate_freepages+0x52/0x650 mm/compaction.c:1722
   compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
   compaction_alloc+0x21/0x30 mm/compaction.c:1880
   migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
   migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
   migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
   migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
   compact_zone+0x539/0x10b0 mm/compaction.c:2660
   compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
   try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
   __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
   __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
   __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
   alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
   alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
   io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
   io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
   io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
   io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
   io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
   __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
   __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
   __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
   x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
   do_syscall_x64 arch/x86/entry/common.c:51 [inline]
   do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
   entry_SYSCALL_64_after_hwframe+0x76/0xe0

value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

Quoted 8 lines

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

No functional change intended.

teawater? What?

Quoted 12 lines
---
 mm/compaction.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/compaction.c +0 -0
index f1b2060eb2016..499ec7ceaa3ed 100644
@@ -1601,7 +1601,11 @@ static void fast_isolate_freepages(struct compact_control *cc)
 		unsigned int order_scanned = 0;
 		unsigned long high_pfn = 0;
 
Quoted 7 lines

mm/compaction.c static void fast_isolate_freepages(struct compact_control *cc) · line 1607

Quoted 5 lines
mm/compaction.c +4 -1
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */

The comment is noise, the reason of using data_race() is a well-known fact.

Quoted 2 lines

IIRC, checkpatch.pl requires a comment for each data_race(). The comments need to stay until you send a patch to change checkpatch.pl.

Okay makes sense.

Quoted 26 lines
Quoted 22 lines

Apart from that, do we have the same problem with zone_watermark_ok? It also uses area->nr_free without zone lock.

Quoted 17 lines
mm/compaction.c +6 -1
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
@@ -2031,7 +2035,11 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc)
 		unsigned long flags;
 		struct page *freepage;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
author
#4 lore raw
In reply to Dev Jain
Quoted 50 lines · Dev Jain
Quoted 48 lines · Hui Zhu

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

   BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:
    fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
    isolate_freepages+0x52/0x650 mm/compaction.c:1722
    compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
    compaction_alloc+0x21/0x30 mm/compaction.c:1880
    migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
    migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
    migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
    migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
    compact_zone+0x539/0x10b0 mm/compaction.c:2660
    compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
    try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
    __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
    __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
    __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
    alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
    alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
    io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
    io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
    io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
    io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
    io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
    __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
    __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
    __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
    x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
    do_syscall_x64 arch/x86/entry/common.c:51 [inline]
    do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
    entry_SYSCALL_64_after_hwframe+0x76/0xe0

   value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

Quoted 4 lines
Quoted 3 lines

No functional change intended.

teawater? What?

Oops! Should be:

Quoted 21 lines
Quoted 11 lines

---
  mm/compaction.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
mm/compaction.c +0 -0
index f1b2060eb2016..499ec7ceaa3ed 100644

mm/compaction.c static void fast_isolate_freepages(struct compact_control *cc) · line 1607

Quoted 8 lines
mm/compaction.c +4 -1
@@ -1601,7 +1601,11 @@ static void fast_isolate_freepages(struct compact_control *cc)
  		unsigned int order_scanned = 0;
  		unsigned long high_pfn = 0;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */

The comment is noise, the reason of using data_race() is a well-known fact.

Quoted 2 lines

Apart from that, do we have the same problem with zone_watermark_ok? It also uses area->nr_free without zone lock.

Yes, __zone_watermark_ok() has the same lockless read of area->nr_free (mm/page_alloc.c:3646), but it does not need annotating.

mm/Makefile sets KCSAN_SANITIZE_page_alloc.o := n, and all writers of nr_free live in that same translation unit (__add_to_free_list() at page_alloc.c:792 and __del_page_from_free_list() at page_alloc.c:843, both static inline). With both sides uninstrumented, no watchpoint is ever installed and KCSAN cannot report it.

That is also why the compaction report says "race at unknown origin": the read side in compaction.c is instrumented, the writer in page_alloc.c is not, so KCSAN infers the race without being able to attribute it (kernel/kcsan/core.c:687). af1c31acc853 hit the same thing in vmstat.c.

The race is equally benign: a stale zero fails the watermark check and the allocator retries; a stale non-zero passes it, and __rmqueue() re-checks under zone->lock via find_suitable_fallback() (page_alloc.c:2266).

So annotating page_alloc.c:3646 would be a documentation-only change to another subsystem, silencing a report that cannot occur. I'd keep this patch scoped to mm/compaction. If the KCSAN exclusion for page_alloc.o is ever lifted, it will need the annotation then -- but that means sweeping the whole file.

I also checked the remaining readers of free_area[].nr_free; all are under zone->lock: show_mem.c:378, page_reporting.c:188, page_alloc.c:2266.

Best,
Hui

Quoted 17 lines
Quoted 17 lines
mm/compaction.c +6 -1
+		if (!data_race(area->nr_free))
  			continue;
 
  		spin_lock_irqsave(&cc->zone->lock, flags);
@@ -2031,7 +2035,11 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc)
  		unsigned long flags;
  		struct page *freepage;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
  			continue;
 
  		spin_lock_irqsave(&cc->zone->lock, flags);
Reviewed-by on v1 1/1
#5 lore raw
In reply to Hui Zhu
Quoted 48 lines · Hui Zhu

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

  BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589

race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:

   fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   isolate_freepages+0x52/0x650 mm/compaction.c:1722
   compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
   compaction_alloc+0x21/0x30 mm/compaction.c:1880
   migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
   migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
   migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
   migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
   compact_zone+0x539/0x10b0 mm/compaction.c:2660
   compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
   try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
   __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
   __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
   __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
   alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
   alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
   io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
   io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
   io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
   io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
   io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
   __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
   __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
   __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
   x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
   do_syscall_x64 arch/x86/entry/common.c:51 [inline]
   do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
   entry_SYSCALL_64_after_hwframe+0x76/0xe0

value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

Quoted 4 lines

No functional change intended.

---

Thanks. With the SOB fixed:

Is a Fixes: tag required? I think stable backport would be overkill but Fixes makes sense as this leads to a reproducible stack trace?

Quoted 33 lines
 mm/compaction.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
mm/compaction.c +10 -2
index f1b2060eb2016..499ec7ceaa3ed 100644
@@ -1601,7 +1601,11 @@ static void fast_isolate_freepages(struct compact_control *cc)
 		unsigned int order_scanned = 0;
 		unsigned long high_pfn = 0;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
@@ -2031,7 +2035,11 @@ static unsigned long fast_find_migrateblock(struct compact_control *cc)
 		unsigned long flags;
 		struct page *freepage;
 
-		if (!area->nr_free)
+		/*
+		 * Access to nr_free is lockless as it is only a hint; a stale
+		 * value is harmless. Use data_race to avoid KCSAN warning.
+		 */
+		if (!data_race(area->nr_free))
 			continue;
 
 		spin_lock_irqsave(&cc->zone->lock, flags);
reviewer Acked-by on v1 1/1
#6 lore raw
In reply to Hui Zhu
Quoted 48 lines · Hui Zhu

From: teawater <[email protected]>

KCSAN reports a data-race on nr_free which also exists on mainline:

  BUG: KCSAN: data-race in fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589

race at unknown origin, with read to 0xffff9894ff3dcb60 of 8 bytes by task 179078 on cpu 12:

   fast_isolate_freepages+0x284/0x1550 mm/compaction.c:1589
   isolate_freepages+0x52/0x650 mm/compaction.c:1722
   compaction_alloc_noprof+0x36e/0x3e0 mm/compaction.c:1850
   compaction_alloc+0x21/0x30 mm/compaction.c:1880
   migrate_folio_unmap+0x7f/0x820 mm/migrate.c:1177
   migrate_pages_batch+0x37e/0x1200 mm/migrate.c:1762
   migrate_pages_sync+0xce/0x560 mm/migrate.c:1928
   migrate_pages+0x8c6/0xc20 mm/migrate.c:2037
   compact_zone+0x539/0x10b0 mm/compaction.c:2660
   compact_zone_order+0x1b8/0x230 mm/compaction.c:2795
   try_to_compact_pages+0x20b/0x630 mm/compaction.c:2851
   __alloc_pages_direct_compact+0x98/0x2b0 mm/page_alloc.c:3722
   __alloc_pages_slowpath.constprop.0+0x452/0xdf0 mm/page_alloc.c:4401
   __alloc_pages_noprof+0x28a/0x2d0 mm/page_alloc.c:4794
   alloc_pages_mpol_noprof+0x1b1/0x470 mm/mempolicy.c:2270
   alloc_pages_noprof+0x7a/0xe0 mm/mempolicy.c:2347
   io_mem_alloc_compound io_uring/io_uring.c:2848 [inline]
   io_pages_map+0xb9/0x330 io_uring/io_uring.c:2892
   io_allocate_scq_urings+0x231/0x300 io_uring/io_uring.c:3933
   io_uring_create+0x29c/0x9c0 io_uring/io_uring.c:4091
   io_uring_setup+0xb6/0x100 io_uring/io_uring.c:4203
   __do_sys_io_uring_setup io_uring/io_uring.c:4230 [inline]
   __se_sys_io_uring_setup io_uring/io_uring.c:4224 [inline]
   __x64_sys_io_uring_setup+0x66/0xe0 io_uring/io_uring.c:4224
   x64_sys_call+0x1816/0x1d90 arch/x86/include/generated/asm/syscalls_64.h:426
   do_syscall_x64 arch/x86/entry/common.c:51 [inline]
   do_syscall_64+0x70/0x2a0 arch/x86/entry/common.c:81
   entry_SYSCALL_64_after_hwframe+0x76/0xe0

value changed: 0x00000000000002f4 -> 0x00000000000002f5

nr_free is only updated with zone->lock held, but fast_isolate_freepages() and fast_find_migrateblock() read it locklessly to skip an order that has no free pages. page_alloc.o is excluded from KCSAN, so the writer is not instrumented and the report is of unknown origin.

The race is benign: a stale zero only skips the order for this round, and a stale non-zero just finds an empty MIGRATE_MOVABLE freelist under the lock. Annotate both reads with data_race(), following commit af1c31acc853 ("mm/vmstat: annotate data race for zone->free_area[order].nr_free").

Quoted 7 lines

No functional change intended.

---
 mm/compaction.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

With your sob fixed, feel free to add

Best Regards,
Yan, Zi