Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit 1ac49c8

Browse files
Matthew Wilcox (Oracle)gregkh
authored andcommitted
XArray: Fix xas_create_range() when multi-order entry present
commit 3e3c658 upstream. If there is already an entry present that is of order >= XA_CHUNK_SHIFT when we call xas_create_range(), xas_create_range() will misinterpret that entry as a node and dereference xa_node->parent, generally leading to a crash that looks something like this: general protection fault, probably for non-canonical address 0xdffffc0000000001: 0000 [#1] PREEMPT SMP KASAN KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f] CPU: 0 PID: 32 Comm: khugepaged Not tainted 5.17.0-rc8-syzkaller-00003-g56e337f2cf13 #0 RIP: 0010:xa_parent_locked include/linux/xarray.h:1207 [inline] RIP: 0010:xas_create_range+0x2d9/0x6e0 lib/xarray.c:725 It's deterministically reproducable once you know what the problem is, but producing it in a live kernel requires khugepaged to hit a race. While the problem has been present since xas_create_range() was introduced, I'm not aware of a way to hit it before the page cache was converted to use multi-index entries. Fixes: 6b24ca4 ("mm: Use multi-index entries in the page cache") Reported-by: syzbot+0d2b0bf32ca5cfd09f2e@syzkaller.appspotmail.com Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 49f77ab commit 1ac49c8

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/test_xarray.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,25 @@ static noinline void check_create_range_4(struct xarray *xa,
14631463
XA_BUG_ON(xa, !xa_empty(xa));
14641464
}
14651465

1466+
static noinline void check_create_range_5(struct xarray *xa,
1467+
unsigned long index, unsigned int order)
1468+
{
1469+
XA_STATE_ORDER(xas, xa, index, order);
1470+
unsigned int i;
1471+
1472+
xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
1473+
1474+
for (i = 0; i < order + 10; i++) {
1475+
do {
1476+
xas_lock(&xas);
1477+
xas_create_range(&xas);
1478+
xas_unlock(&xas);
1479+
} while (xas_nomem(&xas, GFP_KERNEL));
1480+
}
1481+
1482+
xa_destroy(xa);
1483+
}
1484+
14661485
static noinline void check_create_range(struct xarray *xa)
14671486
{
14681487
unsigned int order;
@@ -1490,6 +1509,9 @@ static noinline void check_create_range(struct xarray *xa)
14901509
check_create_range_4(xa, (3U << order) + 1, order);
14911510
check_create_range_4(xa, (3U << order) - 1, order);
14921511
check_create_range_4(xa, (1U << 24) + 1, order);
1512+
1513+
check_create_range_5(xa, 0, order);
1514+
check_create_range_5(xa, (1U << order), order);
14931515
}
14941516

14951517
check_create_range_3();

lib/xarray.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ void xas_create_range(struct xa_state *xas)
722722

723723
for (;;) {
724724
struct xa_node *node = xas->xa_node;
725+
if (node->shift >= shift)
726+
break;
725727
xas->xa_node = xa_parent_locked(xas->xa, node);
726728
xas->xa_offset = node->offset - 1;
727729
if (node->offset != 0)

0 commit comments

Comments
 (0)