Re: [PATCH 1/1] page cache: fix page_cache_next/prev_miss off by one

From: Mike Kravetz
Date: Tue Jun 06 2023 - 18:41:43 EST


On 06/05/23 17:26, Ackerley Tng wrote:
> Mike Kravetz <mike.kravetz@xxxxxxxxxx> writes:
>
> This doesn't seem to work as expected:
>
> Here's a test I did
>
> /* Modified so I can pass in an xarray for this test */
> static unsigned long page_cache_next_miss(struct xarray *xa, unsigned long
> index,
> unsigned long max_scan)
> {
> XA_STATE(xas, xa, index);
>
> while (max_scan--) {
> void *entry = xas_next(&xas);
> if (!entry || xa_is_value(entry))
> return xas.xa_index;
> if (xas.xa_index == 0 && index != 0)
> return xas.xa_index;
> }
>
> return xas.xa_index + 1;
> }
>
> static noinline void check_find_5(void)
> {
> struct xarray xa;
> unsigned long max_scan;
> void *ptr = malloc(10);
>
> xa_init(&xa);
> xa_store_range(&xa, 3, 5, ptr, GFP_KERNEL);
>
> max_scan = 3;
> printk("page_cache_next_miss(xa, %d, %ld): %ld\n", 4, max_scan,
> page_cache_next_miss(&xa, 4, max_scan));
>
> }
>
> The above gave me: page_cache_next_miss(xa, 4, 3): 7
>
> But I was expecting a return value of 6.
>
> I investigated a little, and it seems like entry at index 6 if we start
> iterating before 6 is 0xe, and xa_is_internal(entry) returns true.
>
> Not yet familiar with the internals of xarrays, not sure what the fix
> should be.

I am NOT an expert with xarray. However, the documentation says:

"Calling xa_store_range() stores the same entry in a range
of indices. If you do this, some of the other operations will behave
in a slightly odd way. For example, marking the entry at one index
may result in the entry being marked at some, but not all of the other
indices. Storing into one index may result in the entry retrieved by
some, but not all of the other indices changing."

This may be why your test is not functioning as expected? I modified
your check_find_5() routine as follows (within lib/test_xarray.c):

static noinline void check_find_5(struct xarray *xa, bool mult)
{
unsigned long max_scan;
void *p = &max_scan;

XA_BUG_ON(xa, !xa_empty(xa));

if (mult) {
xa_store(xa, 3, p, GFP_KERNEL);
xa_store(xa, 4, p, GFP_KERNEL);
xa_store(xa, 5, p, GFP_KERNEL);
} else {
xa_store_range(xa, 3, 5, p, GFP_KERNEL);
}

max_scan = 3;
if (mult)
printk("---> multiple stores\n");
else
printk("---> range store\n");

printk("page_cache_next_miss(xa, %d, %ld): %ld\n", 4, max_scan,
__page_cache_next_miss(xa, 4, max_scan));

if (mult) {
xa_store(xa, 3, NULL, GFP_KERNEL);
xa_store(xa, 4, NULL, GFP_KERNEL);
xa_store(xa, 5, NULL, GFP_KERNEL);
} else {
xa_store_range(xa, 3, 5, NULL, GFP_KERNEL);
}

xa_destroy(xa);
}

This results in:
[ 149.998676] ---> multiple stores
[ 149.999391] page_cache_next_miss(xa, 4, 3): 6
[ 150.003342] ---> range store
[ 150.007002] page_cache_next_miss(xa, 4, 3): 7

I am fairly confident the page cache code will make individual xa_store
calls as opposed to xa_store_range.
--
Mike Kravetz