Path: blob/master/tools/testing/radix-tree/regression1.c
26285 views
// SPDX-License-Identifier: GPL-2.01/*2* Regression13* Description:4* Salman Qazi describes the following radix-tree bug:5*6* In the following case, we get can get a deadlock:7*8* 0. The radix tree contains two items, one has the index 0.9* 1. The reader (in this case find_get_pages) takes the rcu_read_lock.10* 2. The reader acquires slot(s) for item(s) including the index 0 item.11* 3. The non-zero index item is deleted, and as a consequence the other item12* is moved to the root of the tree. The place where it used to be is queued13* for deletion after the readers finish.14* 3b. The zero item is deleted, removing it from the direct slot, it remains in15* the rcu-delayed indirect node.16* 4. The reader looks at the index 0 slot, and finds that the page has 0 ref17* count18* 5. The reader looks at it again, hoping that the item will either be freed19* or the ref count will increase. This never happens, as the slot it is20* looking at will never be updated. Also, this slot can never be reclaimed21* because the reader is holding rcu_read_lock and is in an infinite loop.22*23* The fix is to re-use the same "indirect" pointer case that requires a slot24* lookup retry into a general "retry the lookup" bit.25*26* Running:27* This test should run to completion in a few seconds. The above bug would28* cause it to hang indefinitely.29*30* Upstream commit:31* Not yet32*/33#include <linux/kernel.h>34#include <linux/gfp.h>35#include <linux/slab.h>36#include <linux/radix-tree.h>37#include <linux/rcupdate.h>38#include <stdlib.h>39#include <pthread.h>40#include <stdio.h>41#include <assert.h>4243#include "regression.h"4445static RADIX_TREE(mt_tree, GFP_KERNEL);4647struct page {48pthread_mutex_t lock;49struct rcu_head rcu;50int count;51unsigned long index;52};5354static struct page *page_alloc(int index)55{56struct page *p;57p = malloc(sizeof(struct page));58p->count = 1;59p->index = index;60pthread_mutex_init(&p->lock, NULL);6162return p;63}6465static void page_rcu_free(struct rcu_head *rcu)66{67struct page *p = container_of(rcu, struct page, rcu);68assert(!p->count);69pthread_mutex_destroy(&p->lock);70free(p);71}7273static void page_free(struct page *p)74{75call_rcu(&p->rcu, page_rcu_free);76}7778static unsigned find_get_pages(unsigned long start,79unsigned int nr_pages, struct page **pages)80{81XA_STATE(xas, &mt_tree, start);82struct page *page;83unsigned int ret = 0;8485rcu_read_lock();86xas_for_each(&xas, page, ULONG_MAX) {87if (xas_retry(&xas, page))88continue;8990pthread_mutex_lock(&page->lock);91if (!page->count)92goto unlock;9394/* don't actually update page refcount */95pthread_mutex_unlock(&page->lock);9697/* Has the page moved? */98if (unlikely(page != xas_reload(&xas)))99goto put_page;100101pages[ret] = page;102ret++;103continue;104unlock:105pthread_mutex_unlock(&page->lock);106put_page:107xas_reset(&xas);108}109rcu_read_unlock();110return ret;111}112113static pthread_barrier_t worker_barrier;114115static void *regression1_fn(void *arg)116{117rcu_register_thread();118119if (pthread_barrier_wait(&worker_barrier) ==120PTHREAD_BARRIER_SERIAL_THREAD) {121int j;122123for (j = 0; j < 1000000; j++) {124struct page *p;125126p = page_alloc(0);127xa_lock(&mt_tree);128radix_tree_insert(&mt_tree, 0, p);129xa_unlock(&mt_tree);130131p = page_alloc(1);132xa_lock(&mt_tree);133radix_tree_insert(&mt_tree, 1, p);134xa_unlock(&mt_tree);135136xa_lock(&mt_tree);137p = radix_tree_delete(&mt_tree, 1);138pthread_mutex_lock(&p->lock);139p->count--;140pthread_mutex_unlock(&p->lock);141xa_unlock(&mt_tree);142page_free(p);143144xa_lock(&mt_tree);145p = radix_tree_delete(&mt_tree, 0);146pthread_mutex_lock(&p->lock);147p->count--;148pthread_mutex_unlock(&p->lock);149xa_unlock(&mt_tree);150page_free(p);151}152} else {153int j;154155for (j = 0; j < 100000000; j++) {156struct page *pages[10];157158find_get_pages(0, 10, pages);159}160}161162rcu_unregister_thread();163164return NULL;165}166167static pthread_t *threads;168void regression1_test(void)169{170int nr_threads;171int i;172long arg;173174/* Regression #1 */175printv(1, "running regression test 1, should finish in under a minute\n");176nr_threads = 2;177pthread_barrier_init(&worker_barrier, NULL, nr_threads);178179threads = malloc(nr_threads * sizeof(*threads));180181for (i = 0; i < nr_threads; i++) {182arg = i;183if (pthread_create(&threads[i], NULL, regression1_fn, (void *)arg)) {184perror("pthread_create");185exit(1);186}187}188189for (i = 0; i < nr_threads; i++) {190if (pthread_join(threads[i], NULL)) {191perror("pthread_join");192exit(1);193}194}195196free(threads);197198printv(1, "regression test 1, done\n");199}200201202