Home | History | Annotate | Line # | Download | only in readhappy_mpsafe
      1  1.3  andvar /*	$NetBSD: readhappy_mpsafe.c,v 1.3 2025/06/27 21:36:22 andvar Exp $    */
      2  1.1   kamil 
      3  1.1   kamil /*-
      4  1.1   kamil  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1   kamil  * All rights reserved.
      6  1.1   kamil  *
      7  1.1   kamil  * Redistribution and use in source and binary forms, with or without
      8  1.1   kamil  * modification, are permitted provided that the following conditions
      9  1.1   kamil  * are met:
     10  1.1   kamil  * 1. Redistributions of source code must retain the above copyright
     11  1.1   kamil  *    notice, this list of conditions and the following disclaimer.
     12  1.1   kamil  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   kamil  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   kamil  *    documentation and/or other materials provided with the distribution.
     15  1.1   kamil  *
     16  1.1   kamil  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1   kamil  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1   kamil  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1   kamil  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1   kamil  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1   kamil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1   kamil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1   kamil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1   kamil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1   kamil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1   kamil  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1   kamil  */
     28  1.1   kamil 
     29  1.1   kamil #include <sys/cdefs.h>
     30  1.3  andvar __KERNEL_RCSID(0, "$NetBSD: readhappy_mpsafe.c,v 1.3 2025/06/27 21:36:22 andvar Exp $");
     31  1.1   kamil 
     32  1.1   kamil #include <sys/param.h>
     33  1.1   kamil #include <sys/module.h>
     34  1.1   kamil #include <sys/condvar.h>
     35  1.1   kamil #include <sys/conf.h>
     36  1.1   kamil #include <sys/device.h>
     37  1.1   kamil #include <sys/kernel.h>
     38  1.1   kamil #include <sys/mutex.h>
     39  1.1   kamil 
     40  1.1   kamil /*
     41  1.1   kamil  * This module is a modification of the readhappy module to illustrate
     42  1.1   kamil  * how to make a device MPSAFE (Multiprocessor Safe).
     43  1.1   kamil  *
     44  1.1   kamil  *  1. Supports opening device by multiple processes but allows only one at a time.
     45  1.1   kamil  *  2. Supports multiple read() functions but allows only one at a time.
     46  1.3  andvar  *  3. Uses mutex for ensuring synchronization.
     47  1.1   kamil  *
     48  1.1   kamil  * Create a device /dev/happy_mpsafe from which you can read sequential happy numbers.
     49  1.1   kamil  *
     50  1.1   kamil  * To use this device you need to do:
     51  1.2   kamil  *     mknod /dev/happy_mpsafe c 351 0
     52  1.1   kamil  *
     53  1.1   kamil  * To test whether the device is MPSAFE. Compile and run the test_readhappy file
     54  1.1   kamil  * provided.
     55  1.1   kamil  */
     56  1.1   kamil 
     57  1.1   kamil 
     58  1.1   kamil #define	HAPPY_NUMBER	1
     59  1.1   kamil 
     60  1.1   kamil #define	SAD_NUMBER	4
     61  1.1   kamil 
     62  1.1   kamil /*
     63  1.1   kamil  * kmutex_t variables have been added to the structure to
     64  1.1   kamil  * ensure proper synchronization while opened by multiple devices.
     65  1.1   kamil  *
     66  1.1   kamil  * kcondvar_t conditional variable added. Boolean part added to
     67  1.1   kamil  * check whether the device is in use.
     68  1.1   kamil  */
     69  1.1   kamil 
     70  1.1   kamil struct happy_softc {
     71  1.1   kamil 	kcondvar_t	cv;
     72  1.1   kamil 	bool	    inuse_cv;
     73  1.1   kamil 	unsigned	last;
     74  1.1   kamil 	kmutex_t	lock;
     75  1.1   kamil 	kmutex_t	read_lock;
     76  1.1   kamil };
     77  1.1   kamil 
     78  1.1   kamil 
     79  1.1   kamil static struct happy_softc sc;
     80  1.1   kamil 
     81  1.1   kamil static unsigned
     82  1.1   kamil dsum(unsigned n)
     83  1.1   kamil {
     84  1.1   kamil 	unsigned sum, x;
     85  1.1   kamil 
     86  1.1   kamil 	for (sum = 0; n; n /= 10) {
     87  1.1   kamil 		x = n % 10;
     88  1.1   kamil 		sum += x * x;
     89  1.1   kamil 	}
     90  1.1   kamil 	return sum;
     91  1.1   kamil }
     92  1.1   kamil 
     93  1.1   kamil static int
     94  1.1   kamil check_happy(unsigned n)
     95  1.1   kamil {
     96  1.1   kamil 	unsigned total;
     97  1.1   kamil 
     98  1.1   kamil 	KASSERT(mutex_owned(&sc.read_lock));
     99  1.1   kamil 
    100  1.1   kamil 	for (;;) {
    101  1.1   kamil 		total = dsum(n);
    102  1.1   kamil 
    103  1.1   kamil 		if (total == HAPPY_NUMBER)
    104  1.1   kamil 			return 1;
    105  1.1   kamil 		if (total == SAD_NUMBER)
    106  1.1   kamil 			return 0;
    107  1.1   kamil 
    108  1.1   kamil 		n = total;
    109  1.1   kamil 	}
    110  1.1   kamil }
    111  1.1   kamil 
    112  1.1   kamil dev_type_open(happy_open);
    113  1.1   kamil dev_type_close(happy_close);
    114  1.1   kamil dev_type_read(happy_read);
    115  1.1   kamil 
    116  1.1   kamil /*
    117  1.1   kamil  *  Notice that the .d_flag has a additional D_MPSAFE flag to
    118  1.1   kamil  *  tag is as a multiprocessor safe device.
    119  1.1   kamil  */
    120  1.1   kamil 
    121  1.1   kamil static struct cdevsw happy_cdevsw = {
    122  1.1   kamil 	.d_open = happy_open,
    123  1.1   kamil 	.d_close = happy_close,
    124  1.1   kamil 	.d_read = happy_read,
    125  1.1   kamil 	.d_write = nowrite,
    126  1.1   kamil 	.d_ioctl = noioctl,
    127  1.1   kamil 	.d_stop = nostop,
    128  1.1   kamil 	.d_tty = notty,
    129  1.1   kamil 	.d_poll = nopoll,
    130  1.1   kamil 	.d_mmap = nommap,
    131  1.1   kamil 	.d_kqfilter = nokqfilter,
    132  1.1   kamil 	.d_discard = nodiscard,
    133  1.1   kamil 	.d_flag = D_OTHER | D_MPSAFE,
    134  1.1   kamil };
    135  1.1   kamil 
    136  1.1   kamil /*
    137  1.1   kamil  * happy_open : used to open the device for read. mutex_enter and mutex_exit:
    138  1.1   kamil  * to lock the critical section and allow only a single process to open the
    139  1.1   kamil  * device at a time.
    140  1.1   kamil  */
    141  1.1   kamil int
    142  1.1   kamil happy_open(dev_t self __unused, int flag __unused, int mode __unused,
    143  1.1   kamil 	   struct lwp *l __unused)
    144  1.1   kamil {
    145  1.1   kamil 	int error;
    146  1.1   kamil 
    147  1.1   kamil 	error = 0;
    148  1.1   kamil 
    149  1.1   kamil 	mutex_enter(&sc.lock);
    150  1.1   kamil 	while (sc.inuse_cv == true) {
    151  1.1   kamil 		error = cv_wait_sig(&sc.cv, &sc.lock);
    152  1.1   kamil 		if (error)
    153  1.1   kamil 			break;
    154  1.1   kamil 	}
    155  1.1   kamil 	if (!error) {
    156  1.1   kamil 		sc.inuse_cv = true;
    157  1.1   kamil 		sc.last = 0;
    158  1.1   kamil 	}
    159  1.1   kamil 	mutex_exit(&sc.lock);
    160  1.1   kamil 
    161  1.1   kamil 	return 0;
    162  1.1   kamil }
    163  1.1   kamil 
    164  1.1   kamil /*
    165  1.1   kamil  * happy_close allows only a single process to close the device at a time.
    166  1.1   kamil  * It uses mutex_enter and mutex_exit for the same.
    167  1.1   kamil  */
    168  1.1   kamil int
    169  1.1   kamil happy_close(dev_t self __unused, int flag __unused, int mode __unused,
    170  1.1   kamil 	    struct lwp *l __unused)
    171  1.1   kamil {
    172  1.1   kamil 
    173  1.1   kamil 	mutex_enter(&sc.lock);
    174  1.1   kamil 	sc.inuse_cv = false;
    175  1.1   kamil 	cv_signal(&sc.cv);
    176  1.1   kamil 	mutex_exit(&sc.lock);
    177  1.1   kamil 
    178  1.1   kamil 	return 0;
    179  1.1   kamil }
    180  1.1   kamil 
    181  1.1   kamil /*
    182  1.1   kamil  * happy_read allows only a single file descriptor to read at a point of time
    183  1.1   kamil  * it uses mutex_enter and mutex_exit: to lock the critical section and allow
    184  1.1   kamil  * only a single process to open the device at a time.
    185  1.1   kamil  */
    186  1.1   kamil int
    187  1.1   kamil happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
    188  1.1   kamil {
    189  1.1   kamil 	char line[80];
    190  1.1   kamil 	int error, len;
    191  1.1   kamil 
    192  1.1   kamil 	mutex_enter(&sc.read_lock);
    193  1.1   kamil 
    194  1.1   kamil 	while (check_happy(++sc.last) == 0)
    195  1.1   kamil 		continue;
    196  1.1   kamil 
    197  1.1   kamil 	len = snprintf(line, sizeof(line), "%u\n", sc.last);
    198  1.1   kamil 
    199  1.1   kamil 	if (uio->uio_resid < len) {
    200  1.1   kamil 		--sc.last;
    201  1.1   kamil 		error = EINVAL;
    202  1.1   kamil 		goto fin;
    203  1.1   kamil 	}
    204  1.1   kamil 
    205  1.1   kamil 	error = uiomove(line, len, uio);
    206  1.1   kamil 
    207  1.1   kamil fin:
    208  1.1   kamil 	mutex_exit(&sc.read_lock);
    209  1.1   kamil 
    210  1.1   kamil 	return error;
    211  1.1   kamil }
    212  1.1   kamil 
    213  1.1   kamil MODULE(MODULE_CLASS_MISC, happy_mpsafe, NULL);
    214  1.1   kamil 
    215  1.1   kamil /*
    216  1.1   kamil  * Initializing mutex and conditional variables for read() and open().
    217  1.1   kamil  * when the module is being loaded.
    218  1.1   kamil  */
    219  1.1   kamil static int
    220  1.1   kamil happy_mpsafe_modcmd(modcmd_t cmd, void *arg __unused)
    221  1.1   kamil {
    222  1.2   kamil 	int cmajor = 351, bmajor = -1;
    223  1.1   kamil 
    224  1.1   kamil 	switch (cmd) {
    225  1.1   kamil 	case MODULE_CMD_INIT:
    226  1.1   kamil 		if (devsw_attach("happy_mpsafe", NULL, &bmajor, &happy_cdevsw,
    227  1.1   kamil 				 &cmajor))
    228  1.1   kamil 			return ENXIO;
    229  1.1   kamil 
    230  1.1   kamil 		mutex_init(&sc.lock, MUTEX_DEFAULT, IPL_NONE);
    231  1.1   kamil 		mutex_init(&sc.read_lock, MUTEX_DEFAULT, IPL_NONE);
    232  1.1   kamil 		cv_init(&sc.cv, "example conditional variable");
    233  1.1   kamil 		return 0;
    234  1.1   kamil 	case MODULE_CMD_FINI:
    235  1.1   kamil 		mutex_destroy(&sc.lock);
    236  1.1   kamil 		mutex_destroy(&sc.read_lock);
    237  1.1   kamil 		cv_destroy(&sc.cv);
    238  1.1   kamil 		devsw_detach(NULL, &happy_cdevsw);
    239  1.1   kamil 		return 0;
    240  1.1   kamil 	default:
    241  1.1   kamil 		return ENOTTY;
    242  1.1   kamil 	}
    243  1.1   kamil }
    244