]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
XArray: add xa_get_order
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 7 Jun 2021 20:08:43 +0000 (21:08 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 10 Jun 2021 11:37:14 +0000 (13:37 +0200)
commit 57417cebc96b57122a2207fc84a6077d20c84b4b upstream

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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/xarray.h
lib/test_xarray.c
lib/xarray.c

index 3b257c97837db3196c17c93c257d35c18bc2ae5c..63185fe7cd6a236857708b9625a26fb87a98191f 100644 (file)
@@ -1470,6 +1470,15 @@ void xas_pause(struct xa_state *);
 
 void xas_create_range(struct xa_state *);
 
+#ifdef CONFIG_XARRAY_MULTI
+int xa_get_order(struct xarray *, unsigned long index);
+#else
+static inline int xa_get_order(struct xarray *xa, unsigned long index)
+{
+       return 0;
+}
+#endif
+
 /**
  * xas_reload() - Refetch an entry from the xarray.
  * @xas: XArray operation state.
index d4f97925dbd8d6cc5a3b56db7b38da9fd05d2313..bdd4d7995f799d68e10198bcc15bdcf7f26b23d0 100644 (file)
@@ -1649,6 +1649,26 @@ static noinline void check_account(struct xarray *xa)
 #endif
 }
 
+static noinline void check_get_order(struct xarray *xa)
+{
+       unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
+       unsigned int order;
+       unsigned long i, j;
+
+       for (i = 0; i < 3; i++)
+               XA_BUG_ON(xa, xa_get_order(xa, i) != 0);
+
+       for (order = 0; order < max_order; order++) {
+               for (i = 0; i < 10; i++) {
+                       xa_store_order(xa, i << order, order,
+                                       xa_mk_index(i << order), GFP_KERNEL);
+                       for (j = i << order; j < (i + 1) << order; j++)
+                               XA_BUG_ON(xa, xa_get_order(xa, j) != order);
+                       xa_erase(xa, i << order);
+               }
+       }
+}
+
 static noinline void check_destroy(struct xarray *xa)
 {
        unsigned long index;
@@ -1697,6 +1717,7 @@ static int xarray_checks(void)
        check_reserve(&array);
        check_reserve(&xa0);
        check_multi_store(&array);
+       check_get_order(&array);
        check_xa_alloc();
        check_find(&array);
        check_find_entry(&array);
index 08d71c7b75990160bcc8dde17cd4012dcfe41229..57b9c144f7934bf28cc7561000ae71ef5ee65af8 100644 (file)
@@ -1592,6 +1592,46 @@ unlock:
        return xas_result(&xas, NULL);
 }
 EXPORT_SYMBOL(xa_store_range);
+
+/**
+ * xa_get_order() - Get the order of an entry.
+ * @xa: XArray.
+ * @index: Index of the entry.
+ *
+ * Return: A number between 0 and 63 indicating the order of the entry.
+ */
+int xa_get_order(struct xarray *xa, unsigned long index)
+{
+       XA_STATE(xas, xa, index);
+       void *entry;
+       int order = 0;
+
+       rcu_read_lock();
+       entry = xas_load(&xas);
+
+       if (!entry)
+               goto unlock;
+
+       if (!xas.xa_node)
+               goto unlock;
+
+       for (;;) {
+               unsigned int slot = xas.xa_offset + (1 << order);
+
+               if (slot >= XA_CHUNK_SIZE)
+                       break;
+               if (!xa_is_sibling(xas.xa_node->slots[slot]))
+                       break;
+               order++;
+       }
+
+       order += xas.xa_node->shift;
+unlock:
+       rcu_read_unlock();
+
+       return order;
+}
+EXPORT_SYMBOL(xa_get_order);
 #endif /* CONFIG_XARRAY_MULTI */
 
 /**