| 1 | /*	$OpenBSD: srp.h,v 1.16 2025/06/25 20:26:32 miod Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2014 Jonathan Matthew <jmatthew@openbsd.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef _SYS_SRP_H_ |
| 20 | #define _SYS_SRP_H_ |
| 21 | |
| 22 | #include <sys/refcnt.h> |
| 23 | |
| 24 | #ifndef __upunused |
| 25 | #ifdef MULTIPROCESSOR |
| 26 | #define __upunused |
| 27 | #else |
| 28 | #define __upunused __attribute__((__unused__)) |
| 29 | #endif |
| 30 | #endif /* __upunused */ |
| 31 | |
| 32 | struct srp { |
| 33 | 	void			*ref; |
| 34 | }; |
| 35 | |
| 36 | #define SRP_INITIALIZER() { NULL } |
| 37 | |
| 38 | struct srp_hazard { |
| 39 | 	struct srp		*sh_p; |
| 40 | 	void			*sh_v; |
| 41 | }; |
| 42 | |
| 43 | struct srp_ref { |
| 44 | 	struct srp_hazard	*hz; |
| 45 | } __upunused; |
| 46 | |
| 47 | #define SRP_HAZARD_NUM 16 |
| 48 | |
| 49 | struct srp_gc { |
| 50 | 	void			(*srp_gc_dtor)(void *, void *); |
| 51 | 	void			*srp_gc_cookie; |
| 52 | 	struct refcnt		srp_gc_refcnt; |
| 53 | }; |
| 54 | |
| 55 | #define SRP_GC_INITIALIZER(_d, _c) { (_d), (_c), REFCNT_INITIALIZER() } |
| 56 | |
| 57 | /* |
| 58 | * singly linked list built by following srps |
| 59 | */ |
| 60 | |
| 61 | struct srpl_rc { |
| 62 | 	void			(*srpl_ref)(void *, void *); |
| 63 | 	struct srp_gc		srpl_gc; |
| 64 | }; |
| 65 | #define srpl_cookie		srpl_gc.srp_gc_cookie |
| 66 | |
| 67 | #define SRPL_RC_INITIALIZER(_r, _u, _c) { _r, SRP_GC_INITIALIZER(_u, _c) } |
| 68 | |
| 69 | struct srpl { |
| 70 | 	struct srp		sl_head; |
| 71 | }; |
| 72 | |
| 73 | #define SRPL_HEAD(name, type)		struct srpl |
| 74 | |
| 75 | #define SRPL_ENTRY(type)						\ |
| 76 | struct {								\ |
| 77 | 	struct srp		se_next;				\ |
| 78 | } |
| 79 | |
| 80 | #ifdef _KERNEL |
| 81 | |
| 82 | void		 srp_startup(void); |
| 83 | void		 srp_gc_init(struct srp_gc *, void (*)(void *, void *), void *); |
| 84 | void		*srp_swap_locked(struct srp *, void *); |
| 85 | void		 srp_update_locked(struct srp_gc *, struct srp *, void *); |
| 86 | void		*srp_get_locked(struct srp *); |
| 87 | |
| 88 | void		 srp_init(struct srp *); |
| 89 | |
| 90 | #ifdef MULTIPROCESSOR |
| 91 | void		*srp_swap(struct srp *, void *); |
| 92 | void		 srp_update(struct srp_gc *, struct srp *, void *); |
| 93 | void		 srp_finalize(void *, const char *); |
| 94 | void		*srp_enter(struct srp_ref *, struct srp *); |
| 95 | void		*srp_follow(struct srp_ref *, struct srp *); |
| 96 | void		 srp_leave(struct srp_ref *); |
| 97 | #else /* MULTIPROCESSOR */ |
| 98 | |
| 99 | static inline void * |
| 100 | srp_enter(struct srp_ref *sr, struct srp *srp) |
| 101 | { |
| 102 | 	sr->hz = NULL; |
| 103 | 	return srp->ref; |
| 104 | } |
| 105 | |
| 106 | #define srp_swap(_srp, _v)		srp_swap_locked((_srp), (_v)) |
| 107 | #define srp_update(_gc, _srp, _v)	srp_update_locked((_gc), (_srp), (_v)) |
| 108 | #define srp_finalize(_v, _wchan)	((void)0) |
| 109 | #define srp_follow(_sr, _srp)		srp_enter(_sr, _srp) |
| 110 | #define srp_leave(_sr)			do { } while (0) |
| 111 | #endif /* MULTIPROCESSOR */ |
| 112 | |
| 113 | |
| 114 | void		srpl_rc_init(struct srpl_rc *, void (*)(void *, void *), |
| 115 | 		 void (*)(void *, void *), void *); |
| 116 | |
| 117 | #define SRPL_INIT(_sl)			srp_init(&(_sl)->sl_head) |
| 118 | |
| 119 | #define SRPL_FIRST(_sr, _sl)		srp_enter((_sr), &(_sl)->sl_head) |
| 120 | #define SRPL_NEXT(_sr, _e, _ENTRY)	srp_enter((_sr), &(_e)->_ENTRY.se_next) |
| 121 | #define SRPL_FOLLOW(_sr, _e, _ENTRY)	srp_follow((_sr), &(_e)->_ENTRY.se_next) |
| 122 | |
| 123 | #define SRPL_FOREACH(_c, _sr, _sl, _ENTRY)				\ |
| 124 | 	for ((_c) = SRPL_FIRST(_sr, _sl);				\ |
| 125 | 	 (_c) != NULL; 						\ |
| 126 | 	 (_c) = SRPL_FOLLOW(_sr, _c, _ENTRY)) |
| 127 | |
| 128 | #define SRPL_LEAVE(_sr)			srp_leave((_sr)) |
| 129 | |
| 130 | #define SRPL_FIRST_LOCKED(_sl)		srp_get_locked(&(_sl)->sl_head) |
| 131 | #define SRPL_EMPTY_LOCKED(_sl)		(SRPL_FIRST_LOCKED(_sl) == NULL) |
| 132 | |
| 133 | #define SRPL_NEXT_LOCKED(_e, _ENTRY)					\ |
| 134 | 	srp_get_locked(&(_e)->_ENTRY.se_next) |
| 135 | |
| 136 | #define SRPL_FOREACH_LOCKED(_c, _sl, _ENTRY)				\ |
| 137 | 	for ((_c) = SRPL_FIRST_LOCKED(_sl);				\ |
| 138 | 	 (_c) != NULL;						\ |
| 139 | 	 (_c) = SRPL_NEXT_LOCKED((_c), _ENTRY)) |
| 140 | |
| 141 | #define SRPL_FOREACH_SAFE_LOCKED(_c, _sl, _ENTRY, _tc)			\ |
| 142 | 	for ((_c) = SRPL_FIRST_LOCKED(_sl);				\ |
| 143 | 	 (_c) && ((_tc) = SRPL_NEXT_LOCKED(_c, _ENTRY), 1);		\ |
| 144 | 	 (_c) = (_tc)) |
| 145 | |
| 146 | #define SRPL_INSERT_HEAD_LOCKED(_rc, _sl, _e, _ENTRY) do {		\ |
| 147 | 	void *head;							\ |
| 148 | 									\ |
| 149 | 	srp_init(&(_e)->_ENTRY.se_next);				\ |
| 150 | 									\ |
| 151 | 	head = SRPL_FIRST_LOCKED(_sl);					\ |
| 152 | 	if (head != NULL) {						\ |
| 153 | 		(_rc)->srpl_ref(&(_rc)->srpl_cookie, head);		\ |
| 154 | 		srp_update_locked(&(_rc)->srpl_gc,			\ |
| 155 | 		 &(_e)->_ENTRY.se_next, head);	 		\ |
| 156 | 	}								\ |
| 157 | 									\ |
| 158 | 	(_rc)->srpl_ref(&(_rc)->srpl_cookie, _e);			\ |
| 159 | 	srp_update_locked(&(_rc)->srpl_gc, &(_sl)->sl_head, (_e));	\ |
| 160 | } while (0) |
| 161 | |
| 162 | #define SRPL_INSERT_AFTER_LOCKED(_rc, _se, _e, _ENTRY) do {		\ |
| 163 | 	void *next;							\ |
| 164 | 									\ |
| 165 | 	srp_init(&(_e)->_ENTRY.se_next);				\ |
| 166 | 									\ |
| 167 | 	next = SRPL_NEXT_LOCKED(_se, _ENTRY);				\ |
| 168 | 	if (next != NULL) {						\ |
| 169 | 		(_rc)->srpl_ref(&(_rc)->srpl_cookie, next);		\ |
| 170 | 		srp_update_locked(&(_rc)->srpl_gc,			\ |
| 171 | 		 &(_e)->_ENTRY.se_next, next);	 		\ |
| 172 | 	}								\ |
| 173 | 									\ |
| 174 | 	(_rc)->srpl_ref(&(_rc)->srpl_cookie, _e);			\ |
| 175 | 	srp_update_locked(&(_rc)->srpl_gc,				\ |
| 176 | 	 &(_se)->_ENTRY.se_next, (_e));				\ |
| 177 | } while (0) |
| 178 | |
| 179 | #define SRPL_REMOVE_LOCKED(_rc, _sl, _e, _type, _ENTRY) do {		\ |
| 180 | 	struct srp *ref;						\ |
| 181 | 	struct _type *c, *n;						\ |
| 182 | 									\ |
| 183 | 	ref = &(_sl)->sl_head;						\ |
| 184 | 	while ((c = srp_get_locked(ref)) != (_e))			\ |
| 185 | 		ref = &c->_ENTRY.se_next;				\ |
| 186 | 									\ |
| 187 | 	n = SRPL_NEXT_LOCKED(c, _ENTRY);				\ |
| 188 | 	if (n != NULL)							\ |
| 189 | 		(_rc)->srpl_ref(&(_rc)->srpl_cookie, n);		\ |
| 190 | 	srp_update_locked(&(_rc)->srpl_gc, ref, n);			\ |
| 191 | 	srp_update_locked(&(_rc)->srpl_gc, &c->_ENTRY.se_next, NULL);	\ |
| 192 | } while (0) |
| 193 | |
| 194 | #endif /* _KERNEL */ |
| 195 | |
| 196 | #endif /* _SYS_SRP_H_ */ |