Home | History | Annotate | Line # | Download | only in linux
linux_wait_bit.c revision 1.4
      1  1.4  riastrad /*	$NetBSD: linux_wait_bit.c,v 1.4 2021/12/19 11:26:50 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.4  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_wait_bit.c,v 1.4 2021/12/19 11:26:50 riastradh Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/param.h>
     36  1.1  riastrad #include <sys/types.h>
     37  1.1  riastrad #include <sys/bitops.h>
     38  1.1  riastrad #include <sys/condvar.h>
     39  1.1  riastrad #include <sys/mutex.h>
     40  1.1  riastrad #include <sys/systm.h>
     41  1.1  riastrad 
     42  1.1  riastrad #include <linux/bitops.h>
     43  1.1  riastrad #include <linux/sched.h>
     44  1.1  riastrad #include <linux/wait_bit.h>
     45  1.1  riastrad 
     46  1.1  riastrad static struct {
     47  1.1  riastrad 	struct waitbitentry {
     48  1.1  riastrad 		kmutex_t	lock;
     49  1.1  riastrad 		kcondvar_t	cv;
     50  1.1  riastrad 	}		ent;
     51  1.1  riastrad 	char		pad[CACHE_LINE_SIZE - sizeof(struct waitbitentry)];
     52  1.1  riastrad } waitbittab[PAGE_SIZE/CACHE_LINE_SIZE] __cacheline_aligned;
     53  1.1  riastrad CTASSERT(sizeof(waitbittab) == PAGE_SIZE);
     54  1.1  riastrad CTASSERT(sizeof(waitbittab[0]) == CACHE_LINE_SIZE);
     55  1.1  riastrad 
     56  1.1  riastrad int
     57  1.1  riastrad linux_wait_bit_init(void)
     58  1.1  riastrad {
     59  1.1  riastrad 	size_t i;
     60  1.1  riastrad 
     61  1.1  riastrad 	for (i = 0; i < __arraycount(waitbittab); i++) {
     62  1.1  riastrad 		mutex_init(&waitbittab[i].ent.lock, MUTEX_DEFAULT, IPL_VM);
     63  1.1  riastrad 		cv_init(&waitbittab[i].ent.cv, "waitbit");
     64  1.1  riastrad 	}
     65  1.1  riastrad 
     66  1.1  riastrad 	return 0;
     67  1.1  riastrad }
     68  1.1  riastrad 
     69  1.1  riastrad void
     70  1.1  riastrad linux_wait_bit_fini(void)
     71  1.1  riastrad {
     72  1.1  riastrad 	size_t i;
     73  1.1  riastrad 
     74  1.1  riastrad 	for (i = 0; i < __arraycount(waitbittab); i++) {
     75  1.1  riastrad 		cv_destroy(&waitbittab[i].ent.cv);
     76  1.1  riastrad 		mutex_destroy(&waitbittab[i].ent.lock);
     77  1.1  riastrad 	}
     78  1.1  riastrad }
     79  1.1  riastrad 
     80  1.1  riastrad static inline size_t
     81  1.1  riastrad wait_bit_hash(const volatile unsigned long *bitmap, unsigned bit)
     82  1.1  riastrad {
     83  1.1  riastrad 	/* Try to avoid cache line collisions.  */
     84  1.1  riastrad 	const volatile unsigned long *word = bitmap + bit/(NBBY*sizeof(*word));
     85  1.1  riastrad 
     86  1.1  riastrad 	return ((uintptr_t)word >> ilog2(CACHE_LINE_SIZE)) %
     87  1.1  riastrad 	    __arraycount(waitbittab);
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad static struct waitbitentry *
     91  1.1  riastrad wait_bit_enter(const volatile unsigned long *bitmap, unsigned bit)
     92  1.1  riastrad {
     93  1.1  riastrad 	struct waitbitentry *wbe = &waitbittab[wait_bit_hash(bitmap, bit)].ent;
     94  1.1  riastrad 
     95  1.1  riastrad 	mutex_enter(&wbe->lock);
     96  1.1  riastrad 
     97  1.1  riastrad 	return wbe;
     98  1.1  riastrad }
     99  1.1  riastrad 
    100  1.1  riastrad static void
    101  1.1  riastrad wait_bit_exit(struct waitbitentry *wbe)
    102  1.1  riastrad {
    103  1.1  riastrad 
    104  1.1  riastrad 	mutex_exit(&wbe->lock);
    105  1.1  riastrad }
    106  1.1  riastrad 
    107  1.1  riastrad void
    108  1.1  riastrad wake_up_bit(const volatile unsigned long *bitmap, unsigned bit)
    109  1.1  riastrad {
    110  1.1  riastrad 	struct waitbitentry *wbe;
    111  1.1  riastrad 
    112  1.1  riastrad 	wbe = wait_bit_enter(bitmap, bit);
    113  1.1  riastrad 	cv_broadcast(&wbe->cv);
    114  1.1  riastrad 	wait_bit_exit(wbe);
    115  1.1  riastrad }
    116  1.1  riastrad 
    117  1.4  riastrad void
    118  1.4  riastrad clear_and_wake_up_bit(int bit, volatile unsigned long *bitmap)
    119  1.4  riastrad {
    120  1.4  riastrad 	struct waitbitentry *wbe;
    121  1.4  riastrad 
    122  1.4  riastrad 	wbe = wait_bit_enter(bitmap, bit);
    123  1.4  riastrad 	clear_bit(bit, bitmap);
    124  1.4  riastrad 	cv_broadcast(&wbe->cv);
    125  1.4  riastrad 	wait_bit_exit(wbe);
    126  1.4  riastrad }
    127  1.4  riastrad 
    128  1.1  riastrad int
    129  1.3  riastrad wait_on_bit(const volatile unsigned long *bitmap, unsigned bit, int flags)
    130  1.3  riastrad {
    131  1.3  riastrad 	struct waitbitentry *wbe;
    132  1.3  riastrad 	int error, ret;
    133  1.3  riastrad 
    134  1.3  riastrad 	if (test_bit(bit, bitmap))
    135  1.3  riastrad 		return 0;
    136  1.3  riastrad 
    137  1.3  riastrad 	wbe = wait_bit_enter(bitmap, bit);
    138  1.3  riastrad 
    139  1.3  riastrad 	while (!test_bit(bit, bitmap)) {
    140  1.3  riastrad 		if (flags & TASK_UNINTERRUPTIBLE) {
    141  1.3  riastrad 			cv_wait(&wbe->cv, &wbe->lock);
    142  1.3  riastrad 		} else {
    143  1.3  riastrad 			error = cv_wait_sig(&wbe->cv, &wbe->lock);
    144  1.3  riastrad 			if (error == EINTR || error == ERESTART)
    145  1.3  riastrad 				ret = -ERESTARTSYS;
    146  1.3  riastrad 			else if (error != 0)
    147  1.3  riastrad 				ret = -error;
    148  1.3  riastrad 			if (ret)
    149  1.3  riastrad 				goto out;
    150  1.3  riastrad 		}
    151  1.3  riastrad 	}
    152  1.3  riastrad 
    153  1.3  riastrad 	/* Bit is set.  Return zero on success.   */
    154  1.3  riastrad 	ret = 0;
    155  1.3  riastrad 
    156  1.3  riastrad out:	wait_bit_exit(wbe);
    157  1.3  riastrad 	return ret;
    158  1.3  riastrad }
    159  1.3  riastrad 
    160  1.3  riastrad int
    161  1.1  riastrad wait_on_bit_timeout(const volatile unsigned long *bitmap, unsigned bit,
    162  1.1  riastrad     int flags, unsigned long timeout)
    163  1.1  riastrad {
    164  1.1  riastrad 	struct waitbitentry *wbe;
    165  1.1  riastrad 	int error, ret;
    166  1.1  riastrad 
    167  1.1  riastrad 	if (test_bit(bit, bitmap))
    168  1.1  riastrad 		return timeout;
    169  1.1  riastrad 
    170  1.1  riastrad 	wbe = wait_bit_enter(bitmap, bit);
    171  1.1  riastrad 
    172  1.1  riastrad 	while (!test_bit(bit, bitmap)) {
    173  1.1  riastrad 		unsigned starttime, endtime;
    174  1.1  riastrad 
    175  1.1  riastrad 		starttime = hardclock_ticks;
    176  1.1  riastrad 		if (flags & TASK_UNINTERRUPTIBLE) {
    177  1.1  riastrad 			error = cv_timedwait(&wbe->cv, &wbe->lock,
    178  1.1  riastrad 			    MIN(INT_MAX, timeout));
    179  1.1  riastrad 		} else {
    180  1.1  riastrad 			error = cv_timedwait_sig(&wbe->cv, &wbe->lock,
    181  1.1  riastrad 			    MIN(INT_MAX, timeout));
    182  1.1  riastrad 		}
    183  1.1  riastrad 		endtime = hardclock_ticks;
    184  1.1  riastrad 
    185  1.1  riastrad 		/* If we timed out, return zero time left.  */
    186  1.1  riastrad 		if (error == EWOULDBLOCK || endtime - starttime < timeout) {
    187  1.1  riastrad 			ret = 0;
    188  1.1  riastrad 			goto out;
    189  1.1  riastrad 		}
    190  1.1  riastrad 
    191  1.1  riastrad 		/* If we were interrupted, return -ERESTARTSYS.  */
    192  1.2  riastrad 		if (error == EINTR || error == ERESTART) {
    193  1.1  riastrad 			ret = -ERESTARTSYS;
    194  1.1  riastrad 			goto out;
    195  1.1  riastrad 		}
    196  1.1  riastrad 
    197  1.1  riastrad 		/* Otherwise, debit the time spent.  */
    198  1.1  riastrad 		timeout -= (endtime - starttime);
    199  1.1  riastrad 	}
    200  1.1  riastrad 	/* Bit is set.  Return the time left.  */
    201  1.1  riastrad 	ret = timeout;
    202  1.1  riastrad 
    203  1.1  riastrad out:	wait_bit_exit(wbe);
    204  1.1  riastrad 	return ret;
    205  1.1  riastrad }
    206