]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
idr: Fix idr_get_next race with idr_remove
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Tue, 14 May 2019 20:05:45 +0000 (16:05 -0400)
committerKhalid Elmously <khalid.elmously@canonical.com>
Thu, 28 Nov 2019 04:59:27 +0000 (23:59 -0500)
BugLink: https://bugs.launchpad.net/bugs/1854216
commit 5c089fd0c73411f2170ab795c9ffc16718c7d007 upstream.

If the entry is deleted from the IDR between the call to
radix_tree_iter_find() and rcu_dereference_raw(), idr_get_next()
will return NULL, which will end the iteration prematurely.  We should
instead continue to the next entry in the IDR.  This only happens if the
iteration is protected by the RCU lock.  Most IDR users use a spinlock
or semaphore to exclude simultaneous modifications.  It was noticed once
the PID allocator was converted to use the IDR, as it uses the RCU lock,
but there may be other users elsewhere in the kernel.

We can't use the normal pattern of calling radix_tree_deref_retry()
(which catches both a retry entry in a leaf node and a node entry in
the root) as the IDR supports storing entries which are unaligned,
which will trigger an infinite loop if they are encountered.  Instead,
we have to explicitly check whether the entry is a retry entry.

Fixes: 0a835c4f090a ("Reimplement IDR and IDA using the radix tree")
Reported-by: Brendan Gregg <bgregg@netflix.com>
Tested-by: Brendan Gregg <bgregg@netflix.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
lib/idr.c
tools/testing/radix-tree/idr-test.c

index 2593ce513a180a4f6de3515350592c9d4e4981d1..8dda36edf3149604b96ad7afa0540b331542d0ad 100644 (file)
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -111,13 +111,27 @@ void *idr_get_next(struct idr *idr, int *nextid)
 {
        struct radix_tree_iter iter;
        void __rcu **slot;
-
-       slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid);
+       void *entry = NULL;
+
+       radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, *nextid) {
+               entry = rcu_dereference_raw(*slot);
+               if (!entry)
+                       continue;
+               if (!radix_tree_deref_retry(entry))
+                       break;
+               if (slot != (void *)&idr->idr_rt.rnode &&
+                               entry != (void *)RADIX_TREE_INTERNAL_NODE)
+                       break;
+               slot = radix_tree_iter_retry(&iter);
+       }
        if (!slot)
                return NULL;
 
+       if (WARN_ON_ONCE(iter.index > INT_MAX))
+               return NULL;
+
        *nextid = iter.index;
-       return rcu_dereference_raw(*slot);
+       return entry;
 }
 EXPORT_SYMBOL(idr_get_next);
 
index 8e61aad0ca3f7ff65ca7ef640d6f9c9c0e7b75a9..07cec1b5a0d86a0219252f07fa123530364bea38 100644 (file)
@@ -177,6 +177,57 @@ void idr_get_next_test(void)
        idr_destroy(&idr);
 }
 
+static inline void *idr_mk_value(unsigned long v)
+{
+       BUG_ON((long)v < 0);
+       return (void *)((v & 1) | 2 | (v << 1));
+}
+
+DEFINE_IDR(find_idr);
+
+static void *idr_throbber(void *arg)
+{
+       time_t start = time(NULL);
+       int id = *(int *)arg;
+
+       rcu_register_thread();
+       do {
+               idr_alloc(&find_idr, idr_mk_value(id), id, id + 1, GFP_KERNEL);
+               idr_remove(&find_idr, id);
+       } while (time(NULL) < start + 10);
+       rcu_unregister_thread();
+
+       return NULL;
+}
+
+void idr_find_test_1(int anchor_id, int throbber_id)
+{
+       pthread_t throbber;
+       time_t start = time(NULL);
+
+       pthread_create(&throbber, NULL, idr_throbber, &throbber_id);
+
+       BUG_ON(idr_alloc(&find_idr, idr_mk_value(anchor_id), anchor_id,
+                               anchor_id + 1, GFP_KERNEL) != anchor_id);
+
+       do {
+               int id = 0;
+               void *entry = idr_get_next(&find_idr, &id);
+               BUG_ON(entry != idr_mk_value(id));
+       } while (time(NULL) < start + 11);
+
+       pthread_join(throbber, NULL);
+
+       idr_remove(&find_idr, anchor_id);
+       BUG_ON(!idr_is_empty(&find_idr));
+}
+
+void idr_find_test(void)
+{
+       idr_find_test_1(100000, 0);
+       idr_find_test_1(0, 100000);
+}
+
 void idr_checks(void)
 {
        unsigned long i;
@@ -234,6 +285,7 @@ void idr_checks(void)
        idr_null_test();
        idr_nowait_test();
        idr_get_next_test();
+       idr_find_test();
 }
 
 /*