From nobody Wed Oct 23 04:16:26 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XYG2M0VH5z5Zn11; Wed, 23 Oct 2024 04:16:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XYG2L75g1z4nvB; Wed, 23 Oct 2024 04:16:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729656987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ySVaSB+i9vFoa2yWqGAoEu6qGlhYGIMfx/c7qx4MZ+g=; b=CDviB4WAbxw1plaYg+69xT2xYsNOyIhpeLSoCkYI1AWEWYVU2QhMnRnEUP+Kj3KCd7vT1t WYMoSJhYV6c+KOrre/+4xW3sEcC1BFR7EI8rBClXH0v352DergEn0Db9y80JkrrcAv/Iy0 Bwe+3bhvPscdd/FUyhXvfr6QqNQi783e9EK5GiJJbU1o+yTdFhPliU0Mg4H1r+JpIR1arR +aB+ffZplzKAeHm7CGNgRRS6Ax+vKcf6XSPPTfKKckF9g7t5WiYnh63SU2HvvLHG+x79Zb IFYL5aIiZBJjT5exol2tkOtabmTtDMmGuJCVUPflVzgtPHXHDPeGAYWo35FNWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729656987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ySVaSB+i9vFoa2yWqGAoEu6qGlhYGIMfx/c7qx4MZ+g=; b=EKfQGKHyvbah1ws9guaJyIECwRz2Ju5o4IMUiXEh5PH98jpIlpe7HTC+v+aXBgP21GfKnx EuK5mggZtS9OddCOCHB05yd2q7aRO8eF9Zh00ei//dGHpe4cu66OE48jFR5bW3HCTJOnja 9qj+QPNS5yUpIXAB9MWAmTJq+fNKLyeellByVOUsTZgKUjE2fBjw3pTzJAeTDmB6LKIJx4 DjAvJ3BPvb7QNC15NB5a708Tqx0/zobwyBiljGP0qEwTekrS3C3v89F2UZ0bh1tfLsW0YM mtPFIOUu0i3du1xtY259a1WYaWZsAnN0wC+n/ugvpiQ9A0zyD1e8iBCEvEGAhw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729656987; a=rsa-sha256; cv=none; b=mnH1MZwsCzzmAXN7m1Bx0SsUfBtPk3czboHRnPNd/Vvc/PBCLs5xmqYzwS839yI+0t8yrj Y8Ie8iIA75OSNYb8d7riJ0jdGGlLYddgA9S2A6h6S+MkMVwtZs8gGL7NNZJGF/o8EZIz79 ohvrwe8rjBGyc/N64N57+MIuyPaSZUYTj7bcjvmVLb15M9p47qmXcddLAshlSFz3MeWIFt LnR1AtiPcnrrugPKuOWFzpfOGT8wQTttVNlWMcS8LX2QSbGnHXX+IqHwgj+SKTvujJ4CaF mJu22aQ7udP1O3x+RfFuvqcXErU7vNyaaXQZjDVYZblEcYB+Wps9Lk5d9zxM5g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XYG2L6hnDzjrt; Wed, 23 Oct 2024 04:16:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49N4GQCp084943; Wed, 23 Oct 2024 04:16:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49N4GQWZ084940; Wed, 23 Oct 2024 04:16:26 GMT (envelope-from git) Date: Wed, 23 Oct 2024 04:16:26 GMT Message-Id: <202410230416.49N4GQWZ084940@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: 5c92f84bb607 - main - LinuxKPI: update rcu_dereference_*() and lockdep_is_held() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5c92f84bb607c692ce4fa762a7a3c4b86a2fa281 Auto-Submitted: auto-generated The branch main has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=5c92f84bb607c692ce4fa762a7a3c4b86a2fa281 commit 5c92f84bb607c692ce4fa762a7a3c4b86a2fa281 Author: Bjoern A. Zeeb AuthorDate: 2024-09-29 00:35:04 +0000 Commit: Bjoern A. Zeeb CommitDate: 2024-10-23 01:43:09 +0000 LinuxKPI: update rcu_dereference_*() and lockdep_is_held() Update rcu_dereference_{check,protected}() to call the check and log once if it fails and if the RCU debug sysctl is turned on. Also add proper checks for conditions passed in to these functions. For that implement linux_rcu_read_lock_held() (lots of help from wulf). (While here also remove extraneous extern for function prototypes). Update lockdep_is_held() to always be an inline function with argument annotation so that we do no longer have unused variables in callers which only call lockdep_is_held(). Sponsored by: The FreeBSD Foundation MFC after: 3 days Reviewed by: wulf Differential Revision: https://reviews.freebsd.org/D46842 --- sys/compat/linuxkpi/common/include/linux/lockdep.h | 28 ++++++----- .../linuxkpi/common/include/linux/rcupdate.h | 57 ++++++++++++++++++---- sys/compat/linuxkpi/common/src/linux_compat.c | 4 ++ sys/compat/linuxkpi/common/src/linux_rcu.c | 31 ++++++++++++ 4 files changed, 97 insertions(+), 23 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/lockdep.h b/sys/compat/linuxkpi/common/include/linux/lockdep.h index 5291be79218e..70a4e45867fe 100644 --- a/sys/compat/linuxkpi/common/include/linux/lockdep.h +++ b/sys/compat/linuxkpi/common/include/linux/lockdep.h @@ -70,17 +70,6 @@ struct pin_cookie { #define lockdep_assert_none_held_once() do { } while (0) -static __inline bool -lockdep_is_held(void *__m) -{ - struct lock_object *__lock; - struct thread *__td; - - __lock = __m; - return (LOCK_CLASS(__lock)->lc_owner(__lock, &__td) != 0); -} -#define lockdep_is_held_type(_m, _t) lockdep_is_held(_m) - #else #define lockdep_assert(cond) do { } while (0) #define lockdep_assert_once(cond) do { } while (0) @@ -91,10 +80,23 @@ lockdep_is_held(void *__m) #define lockdep_assert_held_once(m) do { (void)(m); } while (0) -#define lockdep_is_held(m) 1 -#define lockdep_is_held_type(_m, _t) 1 #endif +static __inline bool +lockdep_is_held(void *__m __diagused) +{ +#ifdef INVARIANTS + struct lock_object *__lock; + struct thread *__td; + + __lock = __m; + return (LOCK_CLASS(__lock)->lc_owner(__lock, &__td) != 0); +#else + return (true); +#endif +} +#define lockdep_is_held_type(_m, _t) lockdep_is_held(_m) + #define might_lock(m) do { } while (0) #define might_lock_read(m) do { } while (0) #define might_lock_nested(m, n) do { } while (0) diff --git a/sys/compat/linuxkpi/common/include/linux/rcupdate.h b/sys/compat/linuxkpi/common/include/linux/rcupdate.h index 95332217f8f5..85d766c8dbc9 100644 --- a/sys/compat/linuxkpi/common/include/linux/rcupdate.h +++ b/sys/compat/linuxkpi/common/include/linux/rcupdate.h @@ -1,6 +1,10 @@ /*- * Copyright (c) 2016-2017 Mellanox Technologies, Ltd. * All rights reserved. + * Copyright (c) 2024 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 @@ -26,11 +30,20 @@ #ifndef _LINUXKPI_LINUX_RCUPDATE_H_ #define _LINUXKPI_LINUX_RCUPDATE_H_ +#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 */ @@ -61,6 +74,9 @@ 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) @@ -79,14 +95,34 @@ #define rcu_access_pointer(p) \ ((__typeof(*p) *)READ_ONCE(p)) -#define rcu_dereference_protected(p, c) \ +#define rcu_dereference(p) \ ((__typeof(*p) *)READ_ONCE(p)) -#define rcu_dereference(p) \ - rcu_dereference_protected(p, 0) +#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_protected(p, c) +#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)) @@ -113,11 +149,12 @@ /* prototypes */ -extern void linux_call_rcu(unsigned type, struct rcu_head *ptr, rcu_callback_t func); -extern void linux_rcu_barrier(unsigned type); -extern void linux_rcu_read_lock(unsigned type); -extern void linux_rcu_read_unlock(unsigned type); -extern void linux_synchronize_rcu(unsigned type); +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(...) diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c index ebc1dbabd567..81d24603d1dd 100644 --- a/sys/compat/linuxkpi/common/src/linux_compat.c +++ b/sys/compat/linuxkpi/common/src/linux_compat.c @@ -117,6 +117,10 @@ int linuxkpi_debug; SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug, CTLFLAG_RWTUN, &linuxkpi_debug, 0, "Set to enable pr_debug() prints. Clear to disable."); +int linuxkpi_rcu_debug; +SYSCTL_INT(_compat_linuxkpi, OID_AUTO, rcu_debug, CTLFLAG_RWTUN, + &linuxkpi_rcu_debug, 0, "Set to enable RCU warning. Clear to disable."); + int linuxkpi_warn_dump_stack = 0; SYSCTL_INT(_compat_linuxkpi, OID_AUTO, warn_dump_stack, CTLFLAG_RWTUN, &linuxkpi_warn_dump_stack, 0, diff --git a/sys/compat/linuxkpi/common/src/linux_rcu.c b/sys/compat/linuxkpi/common/src/linux_rcu.c index 4879c30164e3..c0b864d269b3 100644 --- a/sys/compat/linuxkpi/common/src/linux_rcu.c +++ b/sys/compat/linuxkpi/common/src/linux_rcu.c @@ -2,6 +2,10 @@ * Copyright (c) 2016 Matthew Macy (mmacy@mattmacy.io) * Copyright (c) 2017-2021 Hans Petter Selasky (hselasky@freebsd.org) * All rights reserved. + * Copyright (c) 2024 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 @@ -245,6 +249,33 @@ linux_rcu_read_unlock(unsigned type) sched_unpin(); } +bool +linux_rcu_read_lock_held(unsigned type) +{ +#ifdef INVARINATS + struct linux_epoch_record *record __diagused; + struct task_struct *ts; + + MPASS(type < RCU_TYPE_MAX); + + if (RCU_SKIP()) + return (false); + + if (__current_unallocated(curthread)) + return (false); + + ts = current; + if (ts->rcu_recurse[type] == 0) + return (false); + + MPASS(curthread->td_pinned != 0); + MPASS((record = &DPCPU_GET(linux_epoch_record[type])) && + record->epoch_record.active != 0); +#endif + + return (true); +} + static void linux_synchronize_rcu_cb(ck_epoch_t *epoch __unused, ck_epoch_record_t *epoch_record, void *arg __unused) {