diff --git a/sys/compat/linuxkpi/common/include/linux/cleanup.h b/sys/compat/linuxkpi/common/include/linux/cleanup.h index 45c2a2359ddf..5bb146f082ed 100644 --- a/sys/compat/linuxkpi/common/include/linux/cleanup.h +++ b/sys/compat/linuxkpi/common/include/linux/cleanup.h @@ -1,58 +1,93 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2024-2025 The FreeBSD Foundation * * This software was developed by Björn Zeeb under sponsorship from * the FreeBSD Foundation. */ #ifndef _LINUXKPI_LINUX_CLEANUP_H #define _LINUXKPI_LINUX_CLEANUP_H #define __cleanup(_f) __attribute__((__cleanup__(_f))) /* * Note: "_T" are special as they are exposed into common code for * statements. Extra care should be taken when changing the code. */ #define DEFINE_GUARD(_n, _dt, _lock, _unlock) \ \ typedef _dt guard_ ## _n ## _t; \ \ static inline _dt \ guard_ ## _n ## _create( _dt _T) \ { \ _dt c; \ \ c = ({ _lock; _T; }); \ return (c); \ } \ \ static inline void \ guard_ ## _n ## _destroy(_dt *t) \ { \ _dt _T; \ \ _T = *t; \ if (_T) { _unlock; }; \ } /* We need to keep these calls unique. */ #define guard(_n) \ guard_ ## _n ## _t guard_ ## _n ## _ ## __COUNTER__ \ __cleanup(guard_ ## _n ## _destroy) = guard_ ## _n ## _create #define DEFINE_FREE(_n, _t, _f) \ static inline void \ __free_ ## _n(void *p) \ { \ _t _T; \ \ _T = *(_t *)p; \ _f; \ } #define __free(_n) __cleanup(__free_##_n) +/* + * Given this is a _0 version it should likely be broken up into parts. + * But we have no idead what a _1, _2, ... version would do different + * until we see a call. + * This is used for a not-real-type (rcu). We use a bool to "simulate" + * the lock held. Also _T still special, may not always be used, so tag + * with __unused (or better the LinuxKPI __maybe_unused). + */ +#define DEFINE_LOCK_GUARD_0(_n, _lock, _unlock, ...) \ + \ + typedef struct { \ + bool lock; \ + __VA_ARGS__; \ + } guard_ ## _n ## _t; \ + \ + static inline void \ + guard_ ## _n ## _destroy(guard_ ## _n ## _t *_T) \ + { \ + if (_T->lock) { \ + _unlock; \ + } \ + } \ + \ + static inline guard_ ## _n ## _t \ + guard_ ## _n ## _create(void) \ + { \ + guard_ ## _n ## _t _tmp; \ + guard_ ## _n ## _t *_T __maybe_unused; \ + \ + _tmp.lock = true; \ + _T = &_tmp; \ + _lock; \ + return (_tmp); \ + } + #endif /* _LINUXKPI_LINUX_CLEANUP_H */ diff --git a/sys/compat/linuxkpi/common/include/linux/rcupdate.h b/sys/compat/linuxkpi/common/include/linux/rcupdate.h index 85d766c8dbc9..4aceb7296cd6 100644 --- a/sys/compat/linuxkpi/common/include/linux/rcupdate.h +++ b/sys/compat/linuxkpi/common/include/linux/rcupdate.h @@ -1,165 +1,168 @@ /*- * Copyright (c) 2016-2017 Mellanox Technologies, Ltd. * All rights reserved. - * Copyright (c) 2024 The FreeBSD Foundation + * Copyright (c) 2024-2025 The FreeBSD Foundation * * Portions of this software were developed by Björn Zeeb * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_RCUPDATE_H_ #define _LINUXKPI_LINUX_RCUPDATE_H_ #include #include #include #include +#include #include extern int linuxkpi_rcu_debug; #define RCU_WARN_ONCE(c, ...) do { \ if (unlikely(linuxkpi_rcu_debug > 0)) \ WARN_ONCE((c), ##__VA_ARGS__); \ } while(0) #define LINUX_KFREE_RCU_OFFSET_MAX 4096 /* exclusive */ /* BSD specific defines */ #define RCU_TYPE_REGULAR 0 #define RCU_TYPE_SLEEPABLE 1 #define RCU_TYPE_MAX 2 #define RCU_INITIALIZER(v) \ ((__typeof(*(v)) *)(v)) #define RCU_INIT_POINTER(p, v) do { \ (p) = (v); \ } while (0) #define call_rcu(ptr, func) do { \ linux_call_rcu(RCU_TYPE_REGULAR, ptr, func); \ } while (0) #define rcu_barrier(void) do { \ linux_rcu_barrier(RCU_TYPE_REGULAR); \ } while (0) #define rcu_read_lock(void) do { \ linux_rcu_read_lock(RCU_TYPE_REGULAR); \ } while (0) #define rcu_read_unlock(void) do { \ linux_rcu_read_unlock(RCU_TYPE_REGULAR);\ } while (0) #define rcu_read_lock_held(void) \ linux_rcu_read_lock_held(RCU_TYPE_REGULAR) #define synchronize_rcu(void) do { \ linux_synchronize_rcu(RCU_TYPE_REGULAR); \ } while (0) #define synchronize_rcu_expedited(void) do { \ linux_synchronize_rcu(RCU_TYPE_REGULAR); \ } while (0) #define kfree_rcu(ptr, rcu_head) do { \ CTASSERT(offsetof(__typeof(*(ptr)), rcu_head) < \ LINUX_KFREE_RCU_OFFSET_MAX); \ call_rcu(&(ptr)->rcu_head, (rcu_callback_t)(uintptr_t) \ offsetof(__typeof(*(ptr)), rcu_head)); \ } while (0) #define rcu_access_pointer(p) \ ((__typeof(*p) *)READ_ONCE(p)) #define rcu_dereference(p) \ ((__typeof(*p) *)READ_ONCE(p)) #define __rcu_var_name(n, f, l) \ __CONCAT(__CONCAT(__CONCAT(rcu_, n), _), __COUNTER__) #define __rcu_dereference_protected(p, c, n) \ ({ \ RCU_WARN_ONCE(!(c), "%s:%d: condition for %s failed\n", \ __func__, __LINE__, __XSTRING(n)); \ rcu_dereference(p); \ }) #define rcu_dereference_protected(p, c) \ __rcu_dereference_protected((p), (c), \ __rcu_var_name(protected, __func__, __LINE__)) #define __rcu_dereference_check(p, c, n) \ ({ \ __typeof(*p) *n = rcu_dereference(p); \ RCU_WARN_ONCE(!(c), "%s:%d: condition for %s failed\n", \ __func__, __LINE__, __XSTRING(n)); \ n; \ }) #define rcu_dereference_check(p, c) \ __rcu_dereference_check((p), (c) || rcu_read_lock_held(), \ __rcu_var_name(check, __func__, __LINE__)) #define rcu_dereference_raw(p) \ ((__typeof(*p) *)READ_ONCE(p)) #define rcu_pointer_handoff(p) (p) #define rcu_assign_pointer(p, v) do { \ atomic_store_rel_ptr((volatile uintptr_t *)&(p), \ (uintptr_t)(v)); \ } while (0) #define rcu_replace_pointer(rcu, ptr, c) \ ({ \ typeof(ptr) __tmp = rcu_dereference_protected(rcu, c); \ rcu_assign_pointer(rcu, ptr); \ __tmp; \ }) #define rcu_swap_protected(rcu, ptr, c) do { \ typeof(ptr) p = rcu_dereference_protected(rcu, c); \ rcu_assign_pointer(rcu, ptr); \ (ptr) = p; \ } while (0) /* prototypes */ void linux_call_rcu(unsigned type, struct rcu_head *ptr, rcu_callback_t func); void linux_rcu_barrier(unsigned type); void linux_rcu_read_lock(unsigned type); void linux_rcu_read_unlock(unsigned type); bool linux_rcu_read_lock_held(unsigned); void linux_synchronize_rcu(unsigned type); /* Empty implementation for !DEBUG */ #define init_rcu_head(...) #define destroy_rcu_head(...) #define init_rcu_head_on_stack(...) #define destroy_rcu_head_on_stack(...) +DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock()) + #endif /* _LINUXKPI_LINUX_RCUPDATE_H_ */