Home | History | Annotate | Line # | Download | only in linux
linux_rcu.c revision 1.6
      1  1.6  riastrad /*	$NetBSD: linux_rcu.c,v 1.6 2021/12/19 12:40:03 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_rcu.c,v 1.6 2021/12/19 12:40:03 riastradh Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/param.h>
     36  1.1  riastrad #include <sys/types.h>
     37  1.5  riastrad 
     38  1.1  riastrad #include <sys/condvar.h>
     39  1.1  riastrad #include <sys/cpu.h>
     40  1.1  riastrad #include <sys/kthread.h>
     41  1.5  riastrad #include <sys/lockdebug.h>
     42  1.1  riastrad #include <sys/mutex.h>
     43  1.1  riastrad #include <sys/sdt.h>
     44  1.1  riastrad #include <sys/xcall.h>
     45  1.1  riastrad 
     46  1.1  riastrad #include <linux/rcupdate.h>
     47  1.1  riastrad #include <linux/slab.h>
     48  1.1  riastrad 
     49  1.1  riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, synchronize__start);
     50  1.1  riastrad SDT_PROBE_DEFINE1(sdt, linux, rcu, synchronize__cpu, "unsigned"/*cpu*/);
     51  1.1  riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, synchronize__done);
     52  1.1  riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, barrier__start);
     53  1.1  riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, barrier__done);
     54  1.1  riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__queue,
     55  1.1  riastrad     "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
     56  1.1  riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__run,
     57  1.1  riastrad     "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
     58  1.1  riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__done,
     59  1.1  riastrad     "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
     60  1.1  riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__queue,
     61  1.1  riastrad     "struct rcu_head *"/*head*/, "void *"/*obj*/);
     62  1.1  riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__free,
     63  1.1  riastrad     "struct rcu_head *"/*head*/, "void *"/*obj*/);
     64  1.1  riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__done,
     65  1.1  riastrad     "struct rcu_head *"/*head*/, "void *"/*obj*/);
     66  1.1  riastrad 
     67  1.1  riastrad static struct {
     68  1.1  riastrad 	kmutex_t	lock;
     69  1.1  riastrad 	kcondvar_t	cv;
     70  1.1  riastrad 	struct rcu_head	*first_callback;
     71  1.1  riastrad 	struct rcu_head	*first_kfree;
     72  1.1  riastrad 	struct lwp	*lwp;
     73  1.1  riastrad 	uint64_t	gen;
     74  1.1  riastrad 	bool		dying;
     75  1.1  riastrad } gc __cacheline_aligned;
     76  1.1  riastrad 
     77  1.1  riastrad static void
     78  1.1  riastrad synchronize_rcu_xc(void *a, void *b)
     79  1.1  riastrad {
     80  1.1  riastrad 
     81  1.1  riastrad 	SDT_PROBE1(sdt, linux, rcu, synchronize__cpu,  cpu_index(curcpu()));
     82  1.1  riastrad }
     83  1.1  riastrad 
     84  1.1  riastrad /*
     85  1.1  riastrad  * synchronize_rcu()
     86  1.1  riastrad  *
     87  1.1  riastrad  *	Wait for any pending RCU read section on every CPU to complete
     88  1.1  riastrad  *	by triggering on every CPU activity that is blocked by an RCU
     89  1.1  riastrad  *	read section.
     90  1.4  riastrad  *
     91  1.4  riastrad  *	May sleep.  (Practically guaranteed to sleep!)
     92  1.1  riastrad  */
     93  1.1  riastrad void
     94  1.1  riastrad synchronize_rcu(void)
     95  1.1  riastrad {
     96  1.1  riastrad 
     97  1.1  riastrad 	SDT_PROBE0(sdt, linux, rcu, synchronize__start);
     98  1.1  riastrad 	xc_wait(xc_broadcast(0, &synchronize_rcu_xc, NULL, NULL));
     99  1.1  riastrad 	SDT_PROBE0(sdt, linux, rcu, synchronize__done);
    100  1.1  riastrad }
    101  1.1  riastrad 
    102  1.1  riastrad /*
    103  1.4  riastrad  * synchronize_rcu_expedited()
    104  1.4  riastrad  *
    105  1.4  riastrad  *	Wait for any pending RCU read section on every CPU to complete
    106  1.4  riastrad  *	by triggering on every CPU activity that is blocked by an RCU
    107  1.4  riastrad  *	read section.  Try to get an answer faster than
    108  1.4  riastrad  *	synchronize_rcu, at the cost of more activity triggered on
    109  1.4  riastrad  *	other CPUs.
    110  1.4  riastrad  *
    111  1.4  riastrad  *	May sleep.  (Practically guaranteed to sleep!)
    112  1.4  riastrad  */
    113  1.4  riastrad void
    114  1.4  riastrad synchronize_rcu_expedited(void)
    115  1.4  riastrad {
    116  1.4  riastrad 
    117  1.4  riastrad 	synchronize_rcu();
    118  1.4  riastrad }
    119  1.4  riastrad 
    120  1.4  riastrad /*
    121  1.3  riastrad  * cookie = get_state_synchronize_rcu(), cond_synchronize_rcu(cookie)
    122  1.3  riastrad  *
    123  1.3  riastrad  *	Optimization for synchronize_rcu -- skip if it has already
    124  1.3  riastrad  *	happened between get_state_synchronize_rcu and
    125  1.3  riastrad  *	cond_synchronize_rcu.  get_state_synchronize_rcu implies a full
    126  1.3  riastrad  *	SMP memory barrier (membar_sync).
    127  1.3  riastrad  */
    128  1.3  riastrad unsigned long
    129  1.3  riastrad get_state_synchronize_rcu(void)
    130  1.3  riastrad {
    131  1.3  riastrad 
    132  1.3  riastrad 	membar_sync();
    133  1.3  riastrad 	return 0;
    134  1.3  riastrad }
    135  1.3  riastrad 
    136  1.3  riastrad void
    137  1.3  riastrad cond_synchronize_rcu(unsigned long cookie)
    138  1.3  riastrad {
    139  1.3  riastrad 
    140  1.3  riastrad 	synchronize_rcu();
    141  1.3  riastrad }
    142  1.3  riastrad 
    143  1.3  riastrad /*
    144  1.1  riastrad  * rcu_barrier()
    145  1.1  riastrad  *
    146  1.1  riastrad  *	Wait for all pending RCU callbacks to complete.
    147  1.1  riastrad  *
    148  1.1  riastrad  *	Does not imply, and is not implied by, synchronize_rcu.
    149  1.1  riastrad  */
    150  1.1  riastrad void
    151  1.1  riastrad rcu_barrier(void)
    152  1.1  riastrad {
    153  1.1  riastrad 	uint64_t gen;
    154  1.1  riastrad 
    155  1.1  riastrad 	SDT_PROBE0(sdt, linux, rcu, barrier__start);
    156  1.1  riastrad 	mutex_enter(&gc.lock);
    157  1.1  riastrad 	if (gc.first_callback != NULL || gc.first_kfree != NULL) {
    158  1.1  riastrad 		gen = gc.gen;
    159  1.1  riastrad 		do {
    160  1.1  riastrad 			cv_wait(&gc.cv, &gc.lock);
    161  1.1  riastrad 		} while (gc.gen == gen);
    162  1.1  riastrad 	}
    163  1.1  riastrad 	mutex_exit(&gc.lock);
    164  1.1  riastrad 	SDT_PROBE0(sdt, linux, rcu, barrier__done);
    165  1.1  riastrad }
    166  1.1  riastrad 
    167  1.1  riastrad /*
    168  1.1  riastrad  * call_rcu(head, callback)
    169  1.1  riastrad  *
    170  1.1  riastrad  *	Arrange to call callback(head) after any pending RCU read
    171  1.1  riastrad  *	sections on every CPU is complete.  Return immediately.
    172  1.1  riastrad  */
    173  1.1  riastrad void
    174  1.1  riastrad call_rcu(struct rcu_head *head, void (*callback)(struct rcu_head *))
    175  1.1  riastrad {
    176  1.1  riastrad 
    177  1.1  riastrad 	head->rcuh_u.callback = callback;
    178  1.1  riastrad 
    179  1.1  riastrad 	mutex_enter(&gc.lock);
    180  1.1  riastrad 	head->rcuh_next = gc.first_callback;
    181  1.1  riastrad 	gc.first_callback = head;
    182  1.1  riastrad 	cv_broadcast(&gc.cv);
    183  1.1  riastrad 	SDT_PROBE2(sdt, linux, rcu, call__queue,  head, callback);
    184  1.1  riastrad 	mutex_exit(&gc.lock);
    185  1.1  riastrad }
    186  1.1  riastrad 
    187  1.1  riastrad /*
    188  1.1  riastrad  * _kfree_rcu(head, obj)
    189  1.1  riastrad  *
    190  1.1  riastrad  *	kfree_rcu helper: schedule kfree(obj) using head for storage.
    191  1.1  riastrad  */
    192  1.1  riastrad void
    193  1.1  riastrad _kfree_rcu(struct rcu_head *head, void *obj)
    194  1.1  riastrad {
    195  1.1  riastrad 
    196  1.5  riastrad 	LOCKDEBUG_MEM_CHECK(obj, ((struct linux_malloc *)obj - 1)->lm_size);
    197  1.5  riastrad 
    198  1.1  riastrad 	head->rcuh_u.obj = obj;
    199  1.1  riastrad 
    200  1.1  riastrad 	mutex_enter(&gc.lock);
    201  1.1  riastrad 	head->rcuh_next = gc.first_kfree;
    202  1.1  riastrad 	gc.first_kfree = head;
    203  1.1  riastrad 	cv_broadcast(&gc.cv);
    204  1.1  riastrad 	SDT_PROBE2(sdt, linux, rcu, kfree__queue,  head, obj);
    205  1.1  riastrad 	mutex_exit(&gc.lock);
    206  1.1  riastrad }
    207  1.1  riastrad 
    208  1.1  riastrad static void
    209  1.1  riastrad gc_thread(void *cookie)
    210  1.1  riastrad {
    211  1.1  riastrad 	struct rcu_head *head_callback, *head_kfree, *head, *next;
    212  1.1  riastrad 
    213  1.1  riastrad 	mutex_enter(&gc.lock);
    214  1.1  riastrad 	for (;;) {
    215  1.1  riastrad 		/* Start with no work.  */
    216  1.1  riastrad 		bool work = false;
    217  1.1  riastrad 
    218  1.1  riastrad 		/* Grab the list of callbacks.  */
    219  1.1  riastrad 		if ((head_callback = gc.first_callback) != NULL) {
    220  1.1  riastrad 			gc.first_callback = NULL;
    221  1.1  riastrad 			work = true;
    222  1.1  riastrad 		}
    223  1.1  riastrad 
    224  1.1  riastrad 		/* Grab the list of objects to kfree.  */
    225  1.1  riastrad 		if ((head_kfree = gc.first_kfree) != NULL) {
    226  1.1  riastrad 			gc.first_kfree = NULL;
    227  1.1  riastrad 			work = true;
    228  1.1  riastrad 		}
    229  1.1  riastrad 
    230  1.1  riastrad 		/*
    231  1.1  riastrad 		 * If no work, then either stop, if we're dying, or
    232  1.1  riastrad 		 * wait for work, if not.
    233  1.1  riastrad 		 */
    234  1.1  riastrad 		if (!work) {
    235  1.1  riastrad 			if (gc.dying)
    236  1.1  riastrad 				break;
    237  1.1  riastrad 			cv_wait(&gc.cv, &gc.lock);
    238  1.1  riastrad 			continue;
    239  1.1  riastrad 		}
    240  1.1  riastrad 
    241  1.1  riastrad 		/* We have work to do.  Drop the lock to do it.  */
    242  1.1  riastrad 		mutex_exit(&gc.lock);
    243  1.1  riastrad 
    244  1.1  riastrad 		/* Wait for activity on all CPUs.  */
    245  1.1  riastrad 		synchronize_rcu();
    246  1.1  riastrad 
    247  1.1  riastrad 		/* Call the callbacks.  */
    248  1.1  riastrad 		for (head = head_callback; head != NULL; head = next) {
    249  1.1  riastrad 			void (*callback)(struct rcu_head *) =
    250  1.1  riastrad 			    head->rcuh_u.callback;
    251  1.1  riastrad 			next = head->rcuh_next;
    252  1.1  riastrad 			SDT_PROBE2(sdt, linux, rcu, call__run,
    253  1.1  riastrad 			    head, callback);
    254  1.1  riastrad 			(*callback)(head);
    255  1.1  riastrad 			/*
    256  1.1  riastrad 			 * Can't dereference head or invoke
    257  1.1  riastrad 			 * callback after this point.
    258  1.1  riastrad 			 */
    259  1.1  riastrad 			SDT_PROBE2(sdt, linux, rcu, call__done,
    260  1.1  riastrad 			    head, callback);
    261  1.1  riastrad 		}
    262  1.1  riastrad 
    263  1.1  riastrad 		/* Free the objects to kfree.  */
    264  1.1  riastrad 		for (head = head_kfree; head != NULL; head = next) {
    265  1.1  riastrad 			void *obj = head->rcuh_u.obj;
    266  1.1  riastrad 			next = head->rcuh_next;
    267  1.1  riastrad 			SDT_PROBE2(sdt, linux, rcu, kfree__free,  head, obj);
    268  1.1  riastrad 			kfree(obj);
    269  1.1  riastrad 			/* Can't dereference head or obj after this point.  */
    270  1.1  riastrad 			SDT_PROBE2(sdt, linux, rcu, kfree__done,  head, obj);
    271  1.1  riastrad 		}
    272  1.1  riastrad 
    273  1.1  riastrad 		/* Return to the lock.  */
    274  1.1  riastrad 		mutex_enter(&gc.lock);
    275  1.1  riastrad 
    276  1.1  riastrad 		/* Finished a batch of work.  Notify rcu_barrier.  */
    277  1.1  riastrad 		gc.gen++;
    278  1.1  riastrad 		cv_broadcast(&gc.cv);
    279  1.6  riastrad 
    280  1.6  riastrad 		/*
    281  1.6  riastrad 		 * Limit ourselves to one batch per tick, in an attempt
    282  1.6  riastrad 		 * to make the batches larger.
    283  1.6  riastrad 		 *
    284  1.6  riastrad 		 * XXX We should maybe also limit the size of each
    285  1.6  riastrad 		 * batch.
    286  1.6  riastrad 		 */
    287  1.6  riastrad 		(void)kpause("lxrcubat", /*intr*/false, /*timo*/1, &gc.lock);
    288  1.1  riastrad 	}
    289  1.1  riastrad 	KASSERT(gc.first_callback == NULL);
    290  1.1  riastrad 	KASSERT(gc.first_kfree == NULL);
    291  1.1  riastrad 	mutex_exit(&gc.lock);
    292  1.1  riastrad 
    293  1.1  riastrad 	kthread_exit(0);
    294  1.1  riastrad }
    295  1.1  riastrad 
    296  1.2  riastrad void
    297  1.2  riastrad init_rcu_head(struct rcu_head *head)
    298  1.2  riastrad {
    299  1.2  riastrad }
    300  1.2  riastrad 
    301  1.2  riastrad void
    302  1.2  riastrad destroy_rcu_head(struct rcu_head *head)
    303  1.2  riastrad {
    304  1.2  riastrad }
    305  1.2  riastrad 
    306  1.1  riastrad int
    307  1.1  riastrad linux_rcu_gc_init(void)
    308  1.1  riastrad {
    309  1.1  riastrad 	int error;
    310  1.1  riastrad 
    311  1.1  riastrad 	mutex_init(&gc.lock, MUTEX_DEFAULT, IPL_VM);
    312  1.1  riastrad 	cv_init(&gc.cv, "lnxrcugc");
    313  1.1  riastrad 	gc.first_callback = NULL;
    314  1.1  riastrad 	gc.first_kfree = NULL;
    315  1.1  riastrad 	gc.gen = 0;
    316  1.1  riastrad 	gc.dying = false;
    317  1.1  riastrad 
    318  1.1  riastrad 	error = kthread_create(PRI_NONE,
    319  1.1  riastrad 	    KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL, &gc_thread, NULL,
    320  1.1  riastrad 	    &gc.lwp, "lnxrcugc");
    321  1.1  riastrad 	if (error)
    322  1.1  riastrad 		goto fail;
    323  1.1  riastrad 
    324  1.1  riastrad 	/* Success!  */
    325  1.1  riastrad 	return 0;
    326  1.1  riastrad 
    327  1.1  riastrad fail:	cv_destroy(&gc.cv);
    328  1.1  riastrad 	mutex_destroy(&gc.lock);
    329  1.1  riastrad 	return error;
    330  1.1  riastrad }
    331  1.1  riastrad 
    332  1.1  riastrad void
    333  1.1  riastrad linux_rcu_gc_fini(void)
    334  1.1  riastrad {
    335  1.1  riastrad 
    336  1.1  riastrad 	mutex_enter(&gc.lock);
    337  1.1  riastrad 	gc.dying = true;
    338  1.1  riastrad 	cv_broadcast(&gc.cv);
    339  1.1  riastrad 	mutex_exit(&gc.lock);
    340  1.1  riastrad 
    341  1.1  riastrad 	kthread_join(gc.lwp);
    342  1.1  riastrad 	gc.lwp = NULL;
    343  1.1  riastrad 	KASSERT(gc.first_callback == NULL);
    344  1.1  riastrad 	KASSERT(gc.first_kfree == NULL);
    345  1.1  riastrad 	cv_destroy(&gc.cv);
    346  1.1  riastrad 	mutex_destroy(&gc.lock);
    347  1.1  riastrad }
    348