Skip to content

Commit 57417ce

Browse files
Matthew Wilcox (Oracle)torvalds
authored andcommitted
XArray: add xa_get_order
Patch series "Fix read-only THP for non-tmpfs filesystems". As described more verbosely in the [3/3] changelog, we can inadvertently put an order-0 page in the page cache which occupies 512 consecutive entries. Users are running into this if they enable the READ_ONLY_THP_FOR_FS config option; see https://bugzilla.kernel.org/show_bug.cgi?id=206569 and Qian Cai has also reported it here: https://lore.kernel.org/lkml/20200616013309.GB815@lca.pw/ This is a rather intrusive way of fixing the problem, but has the advantage that I've actually been testing it with the THP patches, which means that it sees far more use than it does upstream -- indeed, Song has been entirely unable to reproduce it. It also has the advantage that it removes a few patches from my gargantuan backlog of THP patches. This patch (of 3): This function returns the order of the entry at the index. We need this because there isn't space in the shadow entry to encode its order. [akpm@linux-foundation.org: export xa_get_order to modules] Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: "Kirill A . Shutemov" <kirill@shutemov.name> Cc: Qian Cai <cai@lca.pw> Cc: Song Liu <songliubraving@fb.com> Link: https://lkml.kernel.org/r/20200903183029.14930-1-willy@infradead.org Link: https://lkml.kernel.org/r/20200903183029.14930-2-willy@infradead.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f14312e commit 57417ce

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

include/linux/xarray.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,15 @@ void xas_pause(struct xa_state *);
15051505

15061506
void xas_create_range(struct xa_state *);
15071507

1508+
#ifdef CONFIG_XARRAY_MULTI
1509+
int xa_get_order(struct xarray *, unsigned long index);
1510+
#else
1511+
static inline int xa_get_order(struct xarray *xa, unsigned long index)
1512+
{
1513+
return 0;
1514+
}
1515+
#endif
1516+
15081517
/**
15091518
* xas_reload() - Refetch an entry from the xarray.
15101519
* @xas: XArray operation state.

lib/test_xarray.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,26 @@ static noinline void check_account(struct xarray *xa)
16491649
#endif
16501650
}
16511651

1652+
static noinline void check_get_order(struct xarray *xa)
1653+
{
1654+
unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
1655+
unsigned int order;
1656+
unsigned long i, j;
1657+
1658+
for (i = 0; i < 3; i++)
1659+
XA_BUG_ON(xa, xa_get_order(xa, i) != 0);
1660+
1661+
for (order = 0; order < max_order; order++) {
1662+
for (i = 0; i < 10; i++) {
1663+
xa_store_order(xa, i << order, order,
1664+
xa_mk_index(i << order), GFP_KERNEL);
1665+
for (j = i << order; j < (i + 1) << order; j++)
1666+
XA_BUG_ON(xa, xa_get_order(xa, j) != order);
1667+
xa_erase(xa, i << order);
1668+
}
1669+
}
1670+
}
1671+
16521672
static noinline void check_destroy(struct xarray *xa)
16531673
{
16541674
unsigned long index;
@@ -1697,6 +1717,7 @@ static int xarray_checks(void)
16971717
check_reserve(&array);
16981718
check_reserve(&xa0);
16991719
check_multi_store(&array);
1720+
check_get_order(&array);
17001721
check_xa_alloc();
17011722
check_find(&array);
17021723
check_find_entry(&array);

lib/xarray.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,46 @@ void *xa_store_range(struct xarray *xa, unsigned long first,
15921592
return xas_result(&xas, NULL);
15931593
}
15941594
EXPORT_SYMBOL(xa_store_range);
1595+
1596+
/**
1597+
* xa_get_order() - Get the order of an entry.
1598+
* @xa: XArray.
1599+
* @index: Index of the entry.
1600+
*
1601+
* Return: A number between 0 and 63 indicating the order of the entry.
1602+
*/
1603+
int xa_get_order(struct xarray *xa, unsigned long index)
1604+
{
1605+
XA_STATE(xas, xa, index);
1606+
void *entry;
1607+
int order = 0;
1608+
1609+
rcu_read_lock();
1610+
entry = xas_load(&xas);
1611+
1612+
if (!entry)
1613+
goto unlock;
1614+
1615+
if (!xas.xa_node)
1616+
goto unlock;
1617+
1618+
for (;;) {
1619+
unsigned int slot = xas.xa_offset + (1 << order);
1620+
1621+
if (slot >= XA_CHUNK_SIZE)
1622+
break;
1623+
if (!xa_is_sibling(xas.xa_node->slots[slot]))
1624+
break;
1625+
order++;
1626+
}
1627+
1628+
order += xas.xa_node->shift;
1629+
unlock:
1630+
rcu_read_unlock();
1631+
1632+
return order;
1633+
}
1634+
EXPORT_SYMBOL(xa_get_order);
15951635
#endif /* CONFIG_XARRAY_MULTI */
15961636

15971637
/**

0 commit comments

Comments
 (0)