From nobody Tue Nov 26 10:48:09 2024 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DA0B17CA03; Fri, 18 Oct 2024 05:52:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230722; cv=none; b=KXtCIBbvz5a5YcVfbArUWGP2Ox6jfepaKMV7ZZSBAhodnZ8k4E4yFSnIoLhT9CzzsRGRrj8ST948DgRraOvr8dtppnOcnAsPLsUSNx27zNN+pAweUzcV9S73h7Gmvn0XLZJAcrU2QuzrdVwX1x+nT/z9GfsgfUu8p6mZ3jS418I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230722; c=relaxed/simple; bh=sgxh/WHiLpybhwyMhrRAlSI5uIMmb/7HK3IJyI+qd5o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OzCGBPsUIDvBbIPuHOwQuAel9CEcOUq40huheRvHHvXs9mn+v1GQIJWl8+jEYmHFTnurL7U4ZXKCD02AGHmMirMxiR6CyH2NbnhBcpTr5yrQw1Dzb8akm2T86xqURsLMYuvcxlo8UPGnAK2zk+gynkR5D/pg6AGi0Q6YW1srPkk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FSKZILIW; arc=none smtp.client-ip=209.85.160.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FSKZILIW" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-4609967ab7eso13632321cf.3; Thu, 17 Oct 2024 22:52:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230719; x=1729835519; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=eFPesI8y6EBdRmBeoozqipL4P2PTKsdAJwTlUVr16qI=; b=FSKZILIWltP4hqTsKzKbjV1r1gwSITqyttyq3A3vfJ6RVkLclvQMSY67JwLq3SO3Xb UHbtnudXOXTBORi0NcpYMUAyMoh7J3eA0gBpm4Pno5N1FlQpOL2b2nQiZK0ypn0G3ahA e1pjgM4hQ/Azes09cESth5O43jo8mML+L9zXy9mknm86RP4vH88XjJ2E78cYurd1zAse R2rj3+veC/8pVPrCo752xj9hGJPKdcl70i5xLHsmX4P0MVlM+vIL913sT8XhrddQk2jK /tgigHtZJ/z36jVu9sX7OHQyiiXdgOSvZn/9wC44W6TCovhK2GovI0LZiYE4JSSbKlmq FMYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230719; x=1729835519; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=eFPesI8y6EBdRmBeoozqipL4P2PTKsdAJwTlUVr16qI=; b=GUQDo7Y7sOLt4ezHvkxV01T7ur3afjGUMhEEznvH/0y8PUUAJnYt5tQg22hwvkCPiQ OqqSdTWsEjRqT4RPQwnsHspxiiHfS4HjQjvKtVoV63Y9cxEB1Dg6uTabM+6eqyO1NijS DutuJJ0DL6oWXek6dmv39FEPRD+UpGo/L+t0XV0wICT9zTJAKMXUqrnAIIDMElaDEURo /V3EVHIrhs1ikWCHIW44Ne+CCE3agzijgbn6nuWzZN7SXkmt/OrZ/uueTSux22fICR9C T9p99pmEl2mCbjZ0fapzT8B4wjfoJhmAuL1Q0lLoUSuPtEcmtHDogPaypfUMmJang3YT rXKQ== X-Forwarded-Encrypted: i=1; AJvYcCUkGKT/gOd6D8dz8rCRXcHHcNfBXJepsMcBdLUSC0v67AFLX/1xFAuF0GrhXyIFIhHYLL3DfaOmRTFjAzo=@vger.kernel.org, AJvYcCVOv2JybUHxIbH5NDiJp+WWbWSQgkPcb4RAZZtDp3IuH/NHzcs8nBSmvStWI6vYmiZeARShn1Pca2QQda3R6Ug=@vger.kernel.org X-Gm-Message-State: AOJu0Yxts6tCxfaFkeXvoszwGtWWrO9DoYgnLa0hilo8mzWO+hP1apEn bRe8AgslGc4KeNnKMB3MQw8+uOAK0bMICiZiaJmVLy4Y0lTJ0DnjQ5FSNw== X-Google-Smtp-Source: AGHT+IHNqFF39yGW5Isneevii6EHRqn159vrnCCrDQpMh6Ez0SYrHXWlQ5pzlHBj9XglnMdC0ZnZuA== X-Received: by 2002:ac8:5f89:0:b0:456:8b5e:bcf3 with SMTP id d75a77b69052e-460aee6fa78mr18494111cf.52.1729230719396; Thu, 17 Oct 2024 22:51:59 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460aea63322sm3958191cf.95.2024.10.17.22.51.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:51:59 -0700 (PDT) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfauth.phl.internal (Postfix) with ESMTP id 745721200073; Fri, 18 Oct 2024 01:51:58 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Fri, 18 Oct 2024 01:51:58 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddgleelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhephffvvefufffkofgjfhgggfestdekredtredttden ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeegleejiedthedvheeggfejveefjeejkefgveff ieeujefhueeigfegueehgeeggfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepvddupdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehtghhlgieslhhinhhuthhrohhnihigrdguvg dprhgtphhtthhopeguihhrkhdrsggvhhhmvgesghhmrghilhdrtghomhdprhgtphhtthho pehlhihuuggvsehrvgguhhgrthdrtghomhdprhgtphhtthhopehruhhsthdqfhhorhdqlh hinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegurghkrhesrhgv ughhrghtrdgtohhmpdhrtghpthhtoheprghirhhlihgvugesrhgvughhrghtrdgtohhmpd hrtghpthhtohepmhhinhhgohesrhgvughhrghtrdgtohhmpdhrtghpthhtohepfihilhhl sehkvghrnhgvlhdrohhrghdprhgtphhtthhopehlohhnghhmrghnsehrvgguhhgrthdrtg homh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:51:57 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Date: Thu, 17 Oct 2024 22:51:20 -0700 Message-ID: <20241018055125.2784186-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently the nested interrupt disabling and enabling is present by _irqsave() and _irqrestore() APIs, which are relatively unsafe, for example: spin_lock_irqsave(l1, flag1); spin_lock_irqsave(l2, flag2); spin_unlock_irqrestore(l1, flags1); // accesses to interrupt-disable protect data will cause races. This is even easier to triggered with guard facilities: unsigned long flag2; scoped_guard(spin_lock_irqsave, l1) { spin_lock_irqsave(l2, flag2); } // l2 locked but interrupts are enabled. spin_unlock_irqrestore(l2, flag2); (Hand-to-hand locking critical sections are not uncommon for a fine-grained lock design) And because this unsafety, Rust cannot easily wrap the interrupt-disabling locks in a safe API, which complicates the design. To resolve this, introduce a new set of interrupt disabling APIs: * local_interrupt_disalbe(); * local_interrupt_enable(); They work like local_irq_save() and local_irq_restore() except that 1) the outermost local_interrupt_disable() call save the interrupt state into a percpu variable, so that the outermost local_interrupt_enable() can restore the state, and 2) a percpu counter is added to record the nest level of these calls, so that interrupts are not accidentally enabled inside the outermost critical section. Also add the corresponding spin_lock primitives: spin_lock_irq_disable() and spin_unlock_irq_enable(), as a result, code as follow: spin_lock_irq_disable(l1); spin_lock_irq_disable(l2); spin_unlock_irq_enable(l1); // Interrupts are still disabled. spin_unlock_irq_enable(l2); doesn't have the issue that interrupts are accidentally enabled. This also makes the wrapper of interrupt-disabling locks on Rust easier to design. Signed-off-by: Boqun Feng Reviewed-by: Lyude Paul Tested-by: Andreas Hindborg Tested-by: Dirk Behme --- include/linux/irqflags.h | 32 +++++++++++++++++++++++++++++++- include/linux/irqflags_types.h | 6 ++++++ include/linux/spinlock.h | 13 +++++++++++++ include/linux/spinlock_api_smp.h | 29 +++++++++++++++++++++++++++++ include/linux/spinlock_rt.h | 10 ++++++++++ kernel/locking/spinlock.c | 16 ++++++++++++++++ kernel/softirq.c | 3 +++ 7 files changed, 108 insertions(+), 1 deletion(-) diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h index 3f003d5fde53..7840f326514b 100644 --- a/include/linux/irqflags.h +++ b/include/linux/irqflags.h @@ -225,7 +225,6 @@ extern void warn_bogus_irq_restore(void); raw_safe_halt(); \ } while (0) =20 - #else /* !CONFIG_TRACE_IRQFLAGS */ =20 #define local_irq_enable() do { raw_local_irq_enable(); } while (0) @@ -254,6 +253,37 @@ extern void warn_bogus_irq_restore(void); #define irqs_disabled() raw_irqs_disabled() #endif /* CONFIG_TRACE_IRQFLAGS_SUPPORT */ =20 +DECLARE_PER_CPU(struct interrupt_disable_state, local_interrupt_disable_st= ate); + +static inline void local_interrupt_disable(void) +{ + unsigned long flags; + long new_count; + + local_irq_save(flags); + + new_count =3D raw_cpu_inc_return(local_interrupt_disable_state.count); + + if (new_count =3D=3D 1) + raw_cpu_write(local_interrupt_disable_state.flags, flags); +} + +static inline void local_interrupt_enable(void) +{ + long new_count; + + new_count =3D raw_cpu_dec_return(local_interrupt_disable_state.count); + + if (new_count =3D=3D 0) { + unsigned long flags; + + flags =3D raw_cpu_read(local_interrupt_disable_state.flags); + local_irq_restore(flags); + } else if (unlikely(new_count < 0)) { + /* XXX: BUG() here? */ + } +} + #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags) =20 DEFINE_LOCK_GUARD_0(irq, local_irq_disable(), local_irq_enable()) diff --git a/include/linux/irqflags_types.h b/include/linux/irqflags_types.h index c13f0d915097..277433f7f53e 100644 --- a/include/linux/irqflags_types.h +++ b/include/linux/irqflags_types.h @@ -19,4 +19,10 @@ struct irqtrace_events { =20 #endif =20 +/* Per-cpu interrupt disabling state for local_interrupt_{disable,enable}(= ) */ +struct interrupt_disable_state { + unsigned long flags; + long count; +}; + #endif /* _LINUX_IRQFLAGS_TYPES_H */ diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h index 63dd8cf3c3c2..c1cbf5d5ebe0 100644 --- a/include/linux/spinlock.h +++ b/include/linux/spinlock.h @@ -272,9 +272,11 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *= lock) __releases(lock) #endif =20 #define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock) +#define raw_spin_lock_irq_disable(lock) _raw_spin_lock_irq_disable(lock) #define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock) #define raw_spin_unlock(lock) _raw_spin_unlock(lock) #define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock) +#define raw_spin_unlock_irq_enable(lock) _raw_spin_unlock_irq_enable(lock) =20 #define raw_spin_unlock_irqrestore(lock, flags) \ do { \ @@ -376,6 +378,11 @@ static __always_inline void spin_lock_irq(spinlock_t *= lock) raw_spin_lock_irq(&lock->rlock); } =20 +static __always_inline void spin_lock_irq_disable(spinlock_t *lock) +{ + raw_spin_lock_irq_disable(&lock->rlock); +} + #define spin_lock_irqsave(lock, flags) \ do { \ raw_spin_lock_irqsave(spinlock_check(lock), flags); \ @@ -401,6 +408,12 @@ static __always_inline void spin_unlock_irq(spinlock_t= *lock) raw_spin_unlock_irq(&lock->rlock); } =20 +static __always_inline void spin_unlock_irq_enable(spinlock_t *lock) +{ + raw_spin_unlock_irq_enable(&lock->rlock); +} + + static __always_inline void spin_unlock_irqrestore(spinlock_t *lock, unsig= ned long flags) { raw_spin_unlock_irqrestore(&lock->rlock, flags); diff --git a/include/linux/spinlock_api_smp.h b/include/linux/spinlock_api_= smp.h index 89eb6f4c659c..e96482c23044 100644 --- a/include/linux/spinlock_api_smp.h +++ b/include/linux/spinlock_api_smp.h @@ -28,6 +28,8 @@ _raw_spin_lock_nest_lock(raw_spinlock_t *lock, struct loc= kdep_map *map) void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) __acquires(lock); void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock) __acquires(lock); +void __lockfunc _raw_spin_lock_irq_disable(raw_spinlock_t *lock) + __acquires(lock); =20 unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock) __acquires(lock); @@ -39,6 +41,7 @@ int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock); void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock) __releases(lock); void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) __releases(lock); void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock) __releases(lock= ); +void __lockfunc _raw_spin_unlock_irq_enable(raw_spinlock_t *lock) __releas= es(lock); void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags) __releases(lock); @@ -55,6 +58,11 @@ _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsign= ed long flags) #define _raw_spin_lock_irq(lock) __raw_spin_lock_irq(lock) #endif =20 +/* Use the same config as spin_lock_irq() temporarily. */ +#ifdef CONFIG_INLINE_SPIN_LOCK_IRQ +#define _raw_spin_lock_irq_disable(lock) __raw_spin_lock_irq_disable(lock) +#endif + #ifdef CONFIG_INLINE_SPIN_LOCK_IRQSAVE #define _raw_spin_lock_irqsave(lock) __raw_spin_lock_irqsave(lock) #endif @@ -79,6 +87,11 @@ _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsign= ed long flags) #define _raw_spin_unlock_irq(lock) __raw_spin_unlock_irq(lock) #endif =20 +/* Use the same config as spin_unlock_irq() temporarily. */ +#ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQ +#define _raw_spin_unlock_irq_enable(lock) __raw_spin_unlock_irq_enable(loc= k) +#endif + #ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE #define _raw_spin_unlock_irqrestore(lock, flags) __raw_spin_unlock_irqrest= ore(lock, flags) #endif @@ -120,6 +133,14 @@ static inline void __raw_spin_lock_irq(raw_spinlock_t = *lock) LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock); } =20 +static inline void __raw_spin_lock_irq_disable(raw_spinlock_t *lock) +{ + local_interrupt_disable(); + preempt_disable(); + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_); + LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock); +} + static inline void __raw_spin_lock_bh(raw_spinlock_t *lock) { __local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET); @@ -160,6 +181,14 @@ static inline void __raw_spin_unlock_irq(raw_spinlock_= t *lock) preempt_enable(); } =20 +static inline void __raw_spin_unlock_irq_enable(raw_spinlock_t *lock) +{ + spin_release(&lock->dep_map, _RET_IP_); + do_raw_spin_unlock(lock); + local_interrupt_enable(); + preempt_enable(); +} + static inline void __raw_spin_unlock_bh(raw_spinlock_t *lock) { spin_release(&lock->dep_map, _RET_IP_); diff --git a/include/linux/spinlock_rt.h b/include/linux/spinlock_rt.h index 61c49b16f69a..c05be2cb4564 100644 --- a/include/linux/spinlock_rt.h +++ b/include/linux/spinlock_rt.h @@ -94,6 +94,11 @@ static __always_inline void spin_lock_irq(spinlock_t *lo= ck) rt_spin_lock(lock); } =20 +static __always_inline void spin_lock_irq_disable(spinlock_t *lock) +{ + rt_spin_lock(lock); +} + #define spin_lock_irqsave(lock, flags) \ do { \ typecheck(unsigned long, flags); \ @@ -117,6 +122,11 @@ static __always_inline void spin_unlock_irq(spinlock_t= *lock) rt_spin_unlock(lock); } =20 +static __always_inline void spin_unlock_irq_enable(spinlock_t *lock) +{ + rt_spin_unlock(lock); +} + static __always_inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags) { diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c index 7685defd7c52..a2e01ec4a0c8 100644 --- a/kernel/locking/spinlock.c +++ b/kernel/locking/spinlock.c @@ -172,6 +172,14 @@ noinline void __lockfunc _raw_spin_lock_irq(raw_spinlo= ck_t *lock) EXPORT_SYMBOL(_raw_spin_lock_irq); #endif =20 +#ifndef CONFIG_INLINE_SPIN_LOCK_IRQ +noinline void __lockfunc _raw_spin_lock_irq_disable(raw_spinlock_t *lock) +{ + __raw_spin_lock_irq_disable(lock); +} +EXPORT_SYMBOL_GPL(_raw_spin_lock_irq_disable); +#endif + #ifndef CONFIG_INLINE_SPIN_LOCK_BH noinline void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) { @@ -204,6 +212,14 @@ noinline void __lockfunc _raw_spin_unlock_irq(raw_spin= lock_t *lock) EXPORT_SYMBOL(_raw_spin_unlock_irq); #endif =20 +#ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ +noinline void __lockfunc _raw_spin_unlock_irq_enable(raw_spinlock_t *lock) +{ + __raw_spin_unlock_irq_enable(lock); +} +EXPORT_SYMBOL_GPL(_raw_spin_unlock_irq_enable); +#endif + #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH noinline void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) { diff --git a/kernel/softirq.c b/kernel/softirq.c index b756d6b3fd09..fcbf700963c4 100644 --- a/kernel/softirq.c +++ b/kernel/softirq.c @@ -88,6 +88,9 @@ EXPORT_PER_CPU_SYMBOL_GPL(hardirqs_enabled); EXPORT_PER_CPU_SYMBOL_GPL(hardirq_context); #endif =20 +DEFINE_PER_CPU(struct interrupt_disable_state, local_interrupt_disable_sta= te); +EXPORT_PER_CPU_SYMBOL_GPL(local_interrupt_disable_state); + /* * SOFTIRQ_OFFSET usage: * --=20 2.45.2 From nobody Tue Nov 26 10:48:09 2024 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43221181334; Fri, 18 Oct 2024 05:52:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230723; cv=none; b=LSWxQSvb/HXY6aFzEhn0AUM/rya9PU3gS4XigkaKl3a9zOfDfCIv84f5esgedxFMgSx6cN23xc/84nPkw3M4DIQMtIKd27LOamIPdy6SCsNBl92o6Mqz5umaVUZtBR63ws6gjq5ibkXJ33ZWgq2QvBiA3phrRud8wcWdUs0NHM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230723; c=relaxed/simple; bh=DN+57LIYkfMqYE05e0XVimoLNUjef/QEJKEggD7glyo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KrKs+dsEp4LJtbv7RDlzetYZF4/qwTUeFPLFUoM/BeZXtLn93MglW7x/UDiOq5qqS6Ka1+/driWGNMVIePCRN+stC1nb5eJYb0OonkhfF/8haSrIkPdYIvXjrs0UZnWoSsvjPJnxS8nBH2xH1DVfVLoEwciMgGBf02R6bc4hu50= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UJETTRBP; arc=none smtp.client-ip=209.85.219.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UJETTRBP" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6cbd550b648so12941836d6.0; Thu, 17 Oct 2024 22:52:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230721; x=1729835521; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=QchuT/lR++8UPbNj4oEBDAwX/3RB/ZO33V1EX16IN5o=; b=UJETTRBPsImh33IcOMXgU0HD87VFCRpkK3C8QC+X+Gv5UNRDrHIwF02SPmy/BtSuG/ jgyXx0Fry+Npin4F9cjfQL23O6D8ZoWqZpN4H+xWXYYPkgvXkImxN1DzvJAbTxquXmvt MHJxuN8bJ/sx1Ms05k9+iDGhNc1e5JJWoi/4O/8Y9beaPuesvSrrB0dDzQjBsAU00EC3 ynvwNqgZGMsfjzH39x0TNnzv0nDdq05YsjQ7+o+27LBpOMb6Osmf4JDp6lUvL7K1FCln FTWNKOL37P55RFJ9wmmIGg6dzeSl+92tDA1OLUTcpyOMw+Xe+cvZtniGH0rE1rcAkFF2 WxUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230721; x=1729835521; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QchuT/lR++8UPbNj4oEBDAwX/3RB/ZO33V1EX16IN5o=; b=L46ZqBKNzmlaEUACNl23XGtwXDqy9Jm6DYllYnDdifjgEVqEONTe/VQndL74wO1AQS PfQPGcPHI2er1Uxej262ncmeOXnJNgMWZDmHq3ceE5aLru1lwtmxhTCeiQbCwLZczNro HPL85arY4+I2cZl3PHAV/NvyzvJ25tgaVRtlTZMix3L+0YyLusIOdbV0Rp+xr2vWYqKg S2pk0qGzZHUCmjcSID7oMxb+Rz4iGLFLV5ljXtgDZyLDFAeRDIjdAG2B52Ykl62eUEeE 5f3TCtUzDFqMxqdY5qlaK/hAdSkP/vCMbRaGqT1Ap2qRBhAA533xo8FKIo0+B8+n0I+j OjsQ== X-Forwarded-Encrypted: i=1; AJvYcCWSleU3AnFIHrdvYiqdUdqdDLp14ihAmmJMueKNTSSwWwOE6UNz3WZrkV9abVAra6shlXrEBUbviR7FKlz1Bw8=@vger.kernel.org, AJvYcCXlYm9gl4nDSGMZDfIgOfYM7syRaZnswF3+QeTHEV3xyf2QZJKED+37CdtTa80iY+1vKkLlgcuftgrh6T0=@vger.kernel.org X-Gm-Message-State: AOJu0YxBN+cFEILNz85dL0RtME/oPqe/9mOrOtZNQ+HIIvZodJYCnvAw 3yXdCe4GcQrj1UtBU5HyVR6X5V33asiUX5GK5pURylpmbGhZABZ4 X-Google-Smtp-Source: AGHT+IGEJRJbnUwC2varYnTCZY5Ax/RNNDnrbQ6FJH44lwQk5RS/h12JEd/gNWvr3mj/kOn4LIxLeA== X-Received: by 2002:a05:6214:328e:b0:6cb:ea53:9c5f with SMTP id 6a1803df08f44-6cde162e841mr16687946d6.45.1729230721071; Thu, 17 Oct 2024 22:52:01 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cde114d782sm3903866d6.46.2024.10.17.22.52.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:52:00 -0700 (PDT) Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfauth.phl.internal (Postfix) with ESMTP id F14DE1200071; Fri, 18 Oct 2024 01:51:59 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Fri, 18 Oct 2024 01:51:59 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddt necuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilh drtghomheqnecuggftrfgrthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfev ffeiueejhfeuiefggeeuheeggefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepgh hmrghilhdrtghomhesfhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvuddpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidrug gvpdhrtghpthhtohepughirhhkrdgsvghhmhgvsehgmhgrihhlrdgtohhmpdhrtghpthht oheplhihuhguvgesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprhhushhtqdhfohhrqd hlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrkhhrsehr vgguhhgrthdrtghomhdprhgtphhtthhopegrihhrlhhivggusehrvgguhhgrthdrtghomh dprhgtphhtthhopehmihhnghhosehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhl lheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrd gtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:51:59 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 2/6] rust: Introduce interrupt module Date: Thu, 17 Oct 2024 22:51:21 -0700 Message-ID: <20241018055125.2784186-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Lyude Paul This introduces a module for dealing with interrupt-disabled contexts, including the ability to enable and disable interrupts along with the ability to annotate functions as expecting that IRQs are already disabled on the local CPU. [Boqun: This is based on Lyude's work on interrupt disable abstraction, I port to the new local_interrupt_disable() mechanism to make it work as a guard type. I cannot even take the credit of this design, since Lyude also brought up the same idea in zulip. Anyway, this is only for POC purpose, and of course all bugs are mine] Co-Developed-by: Lyude Paul Signed-off-by: Lyude Paul Signed-off-by: Boqun Feng Reviewed-by: Lyude Paul Tested-by: Andreas Hindborg Tested-by: Dirk Behme --- rust/helpers/helpers.c | 1 + rust/helpers/interrupt.c | 18 +++++++++++ rust/kernel/interrupt.rs | 64 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 4 files changed, 84 insertions(+) create mode 100644 rust/helpers/interrupt.c create mode 100644 rust/kernel/interrupt.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 30f40149f3a9..f6e5b33eaff8 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -12,6 +12,7 @@ #include "build_assert.c" #include "build_bug.c" #include "err.c" +#include "interrupt.c" #include "kunit.c" #include "mutex.c" #include "page.c" diff --git a/rust/helpers/interrupt.c b/rust/helpers/interrupt.c new file mode 100644 index 000000000000..e58da42916da --- /dev/null +++ b/rust/helpers/interrupt.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_local_interrupt_disable(void) +{ + local_interrupt_disable(); +} + +void rust_helper_local_interrupt_enable(void) +{ + local_interrupt_enable(); +} + +bool rust_helper_irqs_disabled(void) +{ + return irqs_disabled(); +} diff --git a/rust/kernel/interrupt.rs b/rust/kernel/interrupt.rs new file mode 100644 index 000000000000..fe5a36877538 --- /dev/null +++ b/rust/kernel/interrupt.rs @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Interrupt controls +//! +//! This module allows Rust code to control processor interrupts. [`with_i= nterrupt_disabled()`] may be +//! used for nested disables of interrupts, whereas [`InterruptDisabled`] = can be used for annotating code +//! that requires interrupts to be disabled. + +use bindings; +use core::marker::*; + +/// XXX: Temporarily definition for NotThreadSafe +pub type NotThreadSafe =3D PhantomData<*mut ()>; + +/// XXX: Temporarily const for NotThreadSafe +#[allow(non_upper_case_globals)] +pub const NotThreadSafe: NotThreadSafe =3D PhantomData; + +/// A guard that represent interrupt disable protection. +/// +/// [`InterruptDisabled`] is a guard type that represent interrupts have b= een disabled. Certain +/// functions take an immutable reference of [`InterruptDisabled`] in orde= r to require that they may +/// only be run in interrupt-disabled contexts. +/// +/// This is a marker type; it has no size, and is simply used as a compile= -time guarantee that +/// interrupts are disabled where required. +/// +/// This token can be created by [`with_interrupt_disabled`]. See [`with_i= nterrupt_disabled`] for examples and +/// further information. +/// +/// # Invariants +/// +/// Interrupts are disabled with `local_interrupt_disable()` when an objec= t of this type exists. +pub struct InterruptDisabled(NotThreadSafe); + +/// Disable interrupts. +pub fn interrupt_disable() -> InterruptDisabled { + // SAFETY: It's always safe to call `local_interrupt_disable()`. + unsafe { bindings::local_interrupt_disable() }; + + InterruptDisabled(NotThreadSafe) +} + +impl Drop for InterruptDisabled { + fn drop(&mut self) { + // SAFETY: Per type invariants, a `local_interrupt_disable()` must= be called to create this + // object, hence call the corresponding `local_interrupt_enable()`= is safe. + unsafe { bindings::local_interrupt_enable() }; + } +} + +impl InterruptDisabled { + const ASSUME_INTERRUPT_DISABLED: &'static InterruptDisabled =3D &Inter= ruptDisabled(NotThreadSafe); + + /// Assume that interrupts are disabled. + /// + /// # Safety + /// + /// For the whole life `'a`, interrupts must be considered disabled, f= or example an interrupt + /// handler. + pub unsafe fn assume_interrupt_disabled<'a>() -> &'a InterruptDisabled= { + Self::ASSUME_INTERRUPT_DISABLED + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index b5f4b3ce6b48..56ff464b7905 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -35,6 +35,7 @@ #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] pub mod firmware; pub mod init; +pub mod interrupt; pub mod ioctl; #[cfg(CONFIG_KUNIT)] pub mod kunit; --=20 2.45.2 From nobody Tue Nov 26 10:48:09 2024 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2B8A185B48; Fri, 18 Oct 2024 05:52:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230725; cv=none; b=eMtvNIVsdRmTRzrMsLIsF+CCxw79XP9GUEwVJFUiM05exdFUwFc1Vr8fIyrqkoLW+9DzKHbbY32cGIB4vPExgOwIb6MJ6Fy3mYq6NIWIGCEBOGZZwZpHnxOB6W1L3ujbw/+BqWLWeTsyk2oXxmFjePMyRxHnv1waDLgVHg01gtY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230725; c=relaxed/simple; bh=gsxvtbERea0O2/f0HBuFyQYqpO0yQOBbsIBU3bywGwo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nI5P5dqyzsUS+//3/FRZms+cISEbd5kgVWhKF3HJ/NKCooZKzZpMVWPyanP22xzWdRUM2NVfh5ga5Cb+BA6dr8sUwtBNhfJva2RK9A6IZ7Hf3KNcX61FCGiASdFZjp8cN8ZEih3AkKwiA/lH8X0ac4UfTVFZZGfhSB9Grm6cTPA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MqYKD/IV; arc=none smtp.client-ip=209.85.219.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MqYKD/IV" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-6cc1b20ce54so13993856d6.0; Thu, 17 Oct 2024 22:52:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230722; x=1729835522; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=UT9ol2eK6h7KmhUg/gBQ8DG/6hwW/ccjJZAMJOOdskQ=; b=MqYKD/IVgH9vWDyqQwy/HXt4I3Ve1pPN1Dc0PdRUHqOcak34J0JYGOEJWOAnNUeAps iK4flSyuaVh+Xb7y+H8GD6hC9zN7iu+2hLXUb9qWNaDTSMk4b+RBml4ktXHQB1hvXwQi Lpm3+Em2h9eyeVgukAdpVE21pPYT64wUP+/xPIDCixyMGYhaSp+/Yj6O5TiXX6nFS51O 4J+p4RjxXDBctZvtK4BR355MIrWrxLuAuD8JFWEZn1hLwRcHYvnE92MaGKmP9rdO8f7i tTi4iSHH7i3ISxGTRTFYC4SqsZXZJjXrnAQ8LJTDHBaAFza3EA9xFepMgWGXKdXTfdQh dBaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230722; x=1729835522; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=UT9ol2eK6h7KmhUg/gBQ8DG/6hwW/ccjJZAMJOOdskQ=; b=C3Cg8JipFqAWB+2fBJHUMQq7v3ruhirp5URuuk1aeMp54KyzAmJf9uRMFNRgml2n3w EKTGJ3RaZGcpmzaqkWO7zGeMcp3p1R1ZSC+kQEpultC+yiGt8mSNJJ1GQcPUuyVAu4HM /h7rw+oMP8EKGKptCydtvbUKQTiCelFrqeVEvgARC1vFVd1RBZT11SdiJweLm4uWH0xS Z30emxLqb1L90TOJ266tbX8z1SiF3QtAYmqTUFTCO23NTh1+cPi5KFVp0AMDgr+E42L5 brDuLpS1aO3bxU1pO47dypDS/AgSvln03kW7CPAMu7ZilZZZkTMIJhI/cfH6wv/n3L+W EcYA== X-Forwarded-Encrypted: i=1; AJvYcCUDKTgYffcWxArPSZRwJFzYTVyEkMcLOZJ0iTL2Fa4ckVgNZnTzD3X2ZXOFxup1l3yedHkDX0I2GlnIaGA=@vger.kernel.org, AJvYcCW2+l3pEdXUQTA1nmyFX0ks5c9eaKbInIhttRIXrfYYJlXi1TGXuzR3nH24u+R89WPCDez6TeU7KeWQNzPA9wg=@vger.kernel.org X-Gm-Message-State: AOJu0YzUi45w3blVtEreuu4GbZYO29BH47OIy63vFy6T1h4kzdKbx4eg txAgzLsO61flrN2IiQsHTMZdFpPzO7BLaT1xGzAJs/vZufOfUvBagtrxyg== X-Google-Smtp-Source: AGHT+IFEm/N07MN60Ld4bt4U9ttOtYMClhZ0XeRACytbdencWeQlsD/bTX6iRkYxMYVMBHediE+4mg== X-Received: by 2002:a05:6214:3187:b0:6c3:5b9e:699d with SMTP id 6a1803df08f44-6cde14bd49emr17870076d6.2.1729230722417; Thu, 17 Oct 2024 22:52:02 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cde136cb69sm3775376d6.106.2024.10.17.22.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:52:01 -0700 (PDT) Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfauth.phl.internal (Postfix) with ESMTP id 583C71200071; Fri, 18 Oct 2024 01:52:01 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Fri, 18 Oct 2024 01:52:01 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddt necuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilh drtghomheqnecuggftrfgrthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfev ffeiueejhfeuiefggeeuheeggefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepgh hmrghilhdrtghomhesfhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvuddpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidrug gvpdhrtghpthhtohepughirhhkrdgsvghhmhgvsehgmhgrihhlrdgtohhmpdhrtghpthht oheplhihuhguvgesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprhhushhtqdhfohhrqd hlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrkhhrsehr vgguhhgrthdrtghomhdprhgtphhtthhopegrihhrlhhivggusehrvgguhhgrthdrtghomh dprhgtphhtthhopehmihhnghhosehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhl lheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrd gtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:52:00 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 3/6] rust: helper: Add spin_{un,}lock_irq_{enable,disable}() helpers Date: Thu, 17 Oct 2024 22:51:22 -0700 Message-ID: <20241018055125.2784186-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" spin_lock_irq_disable() and spin_unlock_irq_enable() are inline functions, to use them in Rust, helpers are introduced. This is for interrupt disabling lock abstraction in Rust. Signed-off-by: Boqun Feng Reviewed-by: Lyude Paul Tested-by: Andreas Hindborg Tested-by: Dirk Behme --- rust/helpers/spinlock.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/helpers/spinlock.c b/rust/helpers/spinlock.c index acc1376b833c..8ae0e8f9372c 100644 --- a/rust/helpers/spinlock.c +++ b/rust/helpers/spinlock.c @@ -22,3 +22,13 @@ void rust_helper_spin_unlock(spinlock_t *lock) { spin_unlock(lock); } + +void rust_helper_spin_lock_irq_disable(spinlock_t *lock) +{ + spin_lock_irq_disable(lock); +} + +void rust_helper_spin_unlock_irq_enable(spinlock_t *lock) +{ + spin_unlock_irq_enable(lock); +} --=20 2.45.2 From nobody Tue Nov 26 10:48:09 2024 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C01761865EE; Fri, 18 Oct 2024 05:52:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230726; cv=none; b=MJAjs1R6tUfaGBM3rYG6ItSdllNk57dhmBtuGP2J1sGdf6BdZAswPAgTdGnC4tFf8WzFjv/uF+rNRW6h0GDfpkPC8m0I/SqqZDtuojgn7qVbGWlN6u47pwxdf1HrpyskwuCxFIW9JKgo3n5EqhWOUkqqObvBW9L/4hNmZDRz734= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230726; c=relaxed/simple; bh=Zz/XCdMoetiDJ3KgZ7uJO+07jo81iMjuuKEUb0AVVVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ntECaYg8eUWhcWhki6FA5OMiLHWjiu8lNpmsx8Kx7DJqCzS7nzB4vWlHq4nKhGUk6xRZjDtFl8lSulBXR2YgNhvif/t1XmlBg6BjzRqMsz6w0S0XyeZW2x6VZx3bHnTSQw+pV//oVkKmlWb7gCRCYJieSP/5AkeFkDAWfVcWJPQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Bkx2DoP6; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Bkx2DoP6" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-7b1363c7977so173242985a.1; Thu, 17 Oct 2024 22:52:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230723; x=1729835523; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=Abd9MR76gPMr7HW1p52lgGxqPWUVwdyQxqLxRSgnZYA=; b=Bkx2DoP6EUF5bxpo9U1gX55O/3aXJegCldAfY1WqoFHtcnorc3lBbEuLc1Y8GwHwki +/xGhP6R4SUtC5JwrmOqzGVOGcqtueMcvvSDUe2FavzuNg1eM4u4ML2wj0ZUvJhh3ZMB 05dMoICrzVOHlzxnpC+Mo0ZEU90XqPNH2zMQQPSK7ybNyOP5WSrL20W8mgLp2ZLZ+M+t fPYPE49rzXEJ+2SmXvCmdAVBpNyEFeUHwPi686F+nfeQjd67wu3xuGyjziRCoLHWRvQl Bp1+E9YR+AGxIWf429U7Im0TBBMNC2Ap0Ek7pj4B1AvLnHXmdGXHS1Me8lbLTkg21695 2oWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230723; x=1729835523; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Abd9MR76gPMr7HW1p52lgGxqPWUVwdyQxqLxRSgnZYA=; b=njprNDg2nKG8aOyM2SdvYkTU2tMO9aS5u9i20JU2EnEmfYl20eMs+FLjg2Tu2BkZIW GMwIgWukHDe07//S7QwRpBdC+ZsB6g/giGR5D5c8xK/W8+P29txrmNskRvhQtlqNYRwi am5Tl+wuM5rt+urDpSmGC6j2Cgz9JJPHnVi3ccw38V0hijcRwyPpZAkkofRmoKp6/m2e NpCBGSIcZZt6+YnrH6LTAssMjpqtM17pvTNY8fGgERjVHE2ZDHTGc5uK5PDPbhaJzgqY dHsiCRjNlT90Jz9kOPfMp/mUpSY3yRe76CuUqgPujc2BSZzl9D2ZI7Z6/8o8u3ah+zVG fAHg== X-Forwarded-Encrypted: i=1; AJvYcCVpabAlYrqIT4W/8n8nDvn9q+f/IRXKRXmAFw+X5fD+jmabaKbI3TBOsWGcvZi+aDr9vbNyfNAmgVOtdrzDigs=@vger.kernel.org, AJvYcCWCv9P1BCAWmWkX8/1of9CRVRDWm3ropnHfNCep88m4dXl3PfyxuaM53rq3Rn2krMQY0a3NUtaIgSFme+k=@vger.kernel.org X-Gm-Message-State: AOJu0YwJAuL+7AQ+K1X8zvr4f4+IymEF/IVY5EF8dIOQ24oQOscpTssn iiFc5+jOezkgH79Yv0Ijh3mC3LhTSKRbMq9d2jbIyJTQVlx2WN6f X-Google-Smtp-Source: AGHT+IFa5OrjzRklHkcP+21YQNpIXKSgcGSZqIkBGX4gOTx4WgLSdQHRwBCiNOm1oUuGjLOSCiZBYg== X-Received: by 2002:a05:6214:4403:b0:6cb:ef1b:33d2 with SMTP id 6a1803df08f44-6cde14b965emr18362336d6.3.1729230723530; Thu, 17 Oct 2024 22:52:03 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cde114ea1bsm3881556d6.51.2024.10.17.22.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:52:03 -0700 (PDT) Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfauth.phl.internal (Postfix) with ESMTP id B3DB71200071; Fri, 18 Oct 2024 01:52:02 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Fri, 18 Oct 2024 01:52:02 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddt necuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilh drtghomheqnecuggftrfgrthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfev ffeiueejhfeuiefggeeuheeggefgnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrg hmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepgh hmrghilhdrtghomhesfhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvuddpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidrug gvpdhrtghpthhtohepughirhhkrdgsvghhmhgvsehgmhgrihhlrdgtohhmpdhrtghpthht oheplhihuhguvgesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprhhushhtqdhfohhrqd hlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrkhhrsehr vgguhhgrthdrtghomhdprhgtphhtthhopegrihhrlhhivggusehrvgguhhgrthdrtghomh dprhgtphhtthhopehmihhnghhosehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhl lheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrd gtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:52:02 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 4/6] rust: sync: Add SpinLockIrq Date: Thu, 17 Oct 2024 22:51:23 -0700 Message-ID: <20241018055125.2784186-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Lyude Paul A variant of SpinLock that is expected to be used in noirq contexts, so lock() will disable interrupts and unlock() (i.e. `Guard::drop()` will undo the interrupt disable. [Boqun: Port to use spin_lock_irq_disable() and spin_unlock_irq_enable()] Co-developed-by: Lyude Paul Signed-off-by: Lyude Paul Signed-off-by: Boqun Feng Reviewed-by: Lyude Paul Tested-by: Andreas Hindborg Tested-by: Dirk Behme --- rust/kernel/sync.rs | 2 +- rust/kernel/sync/lock/spinlock.rs | 91 +++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 0ab20975a3b5..b028ee325f2a 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -15,7 +15,7 @@ pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult}; pub use lock::mutex::{new_mutex, Mutex}; -pub use lock::spinlock::{new_spinlock, SpinLock}; +pub use lock::spinlock::{new_spinlock, new_spinlock_irq, SpinLock, SpinLoc= kIrq}; pub use locked_by::LockedBy; =20 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`. diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.rs index ea5c5bc1ce12..884d4d1cbf23 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -115,3 +115,94 @@ unsafe fn unlock(ptr: *mut Self::State, _guard_state: = &Self::GuardState) { unsafe { bindings::spin_unlock(ptr) } } } + +/// Creates a [`SpinLockIrq`] initialiser with the given name and a newly-= created lock class. +/// +/// It uses the name if one is given, otherwise it generates one based on = the file name and line +/// number. +#[macro_export] +macro_rules! new_spinlock_irq { + ($inner:expr $(, $name:literal)? $(,)?) =3D> { + $crate::sync::SpinLockIrq::new( + $inner, $crate::optional_name!($($name)?), $crate::static_lock= _class!()) + }; +} +pub use new_spinlock_irq; + +/// A spinlock that may be acquired when interrupts are disabled. +/// +/// A version of [`SpinLock`] that can only be used in contexts where inte= rrupts for the local CPU +/// are disabled. It requires that the user acquiring the lock provide pro= of that interrupts are +/// disabled through [`IrqDisabled`]. +/// +/// For more info, see [`SpinLock`]. +/// +/// # Examples +/// +/// The following example shows how to declare, allocate initialise and ac= cess a struct (`Example`) +/// that contains an inner struct (`Inner`) that is protected by a spinloc= k. +/// +/// ``` +/// use kernel::sync::{new_spinlock_irq, SpinLockIrq}; +/// +/// struct Inner { +/// a: u32, +/// b: u32, +/// } +/// +/// #[pin_data] +/// struct Example { +/// c: u32, +/// #[pin] +/// d: SpinLockIrq, +/// } +/// +/// impl Example { +/// fn new() -> impl PinInit { +/// pin_init!(Self { +/// c: 10, +/// d <- new_spinlock_irq!(Inner { a: 20, b: 30 }), +/// }) +/// } +/// } +/// +/// // Allocate a boxed `Example` +/// let e =3D Box::pin_init(Example::new(), GFP_KERNEL)?; +/// +/// // Accessing an `Example` from a context where IRQs may not be disable= d already. +/// let b =3D e.d.lock().b; +/// +/// assert_eq!(b, 30); +/// # Ok::<(), Error>(()) +/// ``` +pub type SpinLockIrq =3D super::Lock; + +/// A kernel `spinlock_t` lock backend that is acquired in interrupt disab= led contexts. +pub struct SpinLockIrqBackend; + +unsafe impl super::Backend for SpinLockIrqBackend { + type State =3D bindings::spinlock_t; + type GuardState =3D (); + + unsafe fn init( + ptr: *mut Self::State, + name: *const core::ffi::c_char, + key: *mut bindings::lock_class_key, + ) { + // SAFETY: The safety requirements ensure that `ptr` is valid for = writes, and `name` and + // `key` are valid for read indefinitely. + unsafe { bindings::__spin_lock_init(ptr, name, key) } + } + + unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { + // SAFETY: The safety requirements of this function ensure that `p= tr` points to valid + // memory, and that it has been initialised before. + unsafe { bindings::spin_lock_irq_disable(ptr) } + } + + unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardStat= e) { + // SAFETY: The safety requirements of this function ensure that `p= tr` is valid and that the + // caller is the owner of the spinlock. + unsafe { bindings::spin_unlock_irq_enable(ptr) } + } +} --=20 2.45.2 From nobody Tue Nov 26 10:48:09 2024 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2907187FEB; Fri, 18 Oct 2024 05:52:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230728; cv=none; b=E4+zRPZJf8awBbBHa2PfxX4lcao9Nrv6PUy81J8HYiYZHlyyGxGLv4i+0gSzd/K/hxzkHBnPzBSpBHCLcdfjvzw1RiiV+vLw/47woPohDmZ3NMzsY8ABGS8o2TWzTQcM3oiZYdvIeKKdrBYwF2ybmjdUIgFD+uWCaQWQ2QTkWpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230728; c=relaxed/simple; bh=tbSrVABp3PaXJkMn+Cm+M441qbWN9NPoBe7KufBm1QA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oZ+JNfkbd40gVqh8/ywohU9xLUy57xEhPuFkFZUtVge8LDUz+9qqu2ktIiWWz3rpQYsUC7scDTVuPIB1NCiMUXNMpp35EQJOEHUtAkW7BwCZVtC/IujEgjtyZt3taQjwY2Yy1c44EVSIJKzaCUFYUQcEPn2/UORSzeWNnCgkIEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ei5mxr0Y; arc=none smtp.client-ip=209.85.219.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ei5mxr0Y" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6cbd092f7f0so11489386d6.0; Thu, 17 Oct 2024 22:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230726; x=1729835526; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=PiO8wSvLJP3CfC2CFEiFL3JHXiCvMRVN6nfnfklyn+E=; b=Ei5mxr0YqKaU2EWnfeJhBuY9eExmE4+a5alfG8X1OiSZTzzt9dIzgCPLPH6ZxQqy2N x+LHAhR3ABXKY4DJ3pOyPzOtjqTgboiV69U+4M7zhJSYRBzGAYn0EJHQITiAlHDnHx8y 806YbVwbCcbd9p8cfbmISj0fof346aj7kOqiMppbn5MvOosh32a4iClMoZfSk0uXneMC H3GfuPYUpvpK9yviPZQw1QWtRXMjjJ3gnm8p4DkNw540BZOE5SGD1YbL+GTZu96S0G4T MD54TKC1lpdeH6syq3IOoCFFoC8Ai0ICDzoXJIzbPmTca8Bi9dt4nHLHNzvsKD27tPg6 JdFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230726; x=1729835526; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=PiO8wSvLJP3CfC2CFEiFL3JHXiCvMRVN6nfnfklyn+E=; b=xDLDlR15Lf9eVbW0w4tyFFA/bsFJ0YxlwYb5sGIOhgTvNtfeC9zKf/IK3T21t2lJE2 TuKeMk9oNaiX+ZIaaE9mPgL/Cn753yU14GqnSBI4WWMXnoy2ksg5m1sCa5Fs8LaRfbgr /klolBquDVnMXKwlFHRMK/dw2YxQ7ULxtjJeuQbgRXfrLFHTx2j1YR+vqNL48AFWzzCN GTmybu2ILNYXlmGDy7Mw0fup9yYK6ak0ZgIoUSSNDi/dlWQz9YMVbA+3tNiNwrP8pggu rAT0vKY6t2fcyMwpkiLHf1rUQtm5lMVaoEdewGLA2COrH7aXcjgK2Ge2XUzVPXSnlaXK ohQA== X-Forwarded-Encrypted: i=1; AJvYcCU1wXAfnWoGebijpEI5g6u++hzJpenRxAiwR+TEBduwE/5CPZJfm14hmAaQJ1wMtq6MPHo2+LkIE6Rh84m7kBw=@vger.kernel.org, AJvYcCVS6kGylnon3ngHgDf5fXdZ2T0jgiq6cQgm07IJyVdPBgKyMDmpTJKzbxNUJOZXmmdZVU/sEgp8Fk3ggB8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw9/ojAUd2bOIcQZLhPDuNHpKP8MWFKgR0nDL1XxoSA9zNlN8+/ mwYay9hKLoDycO5ENX2LEL/WaMILkitoLRB16Z6y3AVAYakNWuTy X-Google-Smtp-Source: AGHT+IF4dS/pPKXEPCvmpweymPZSUAD0UOsQ1pCKGWPLMt2tDJw3rXN9Sx/soWflN4T5oAgrXMUObg== X-Received: by 2002:a05:6214:3b83:b0:6cc:42d:bbb with SMTP id 6a1803df08f44-6cde1695f11mr12452786d6.53.1729230725599; Thu, 17 Oct 2024 22:52:05 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cde114d0d7sm3899996d6.40.2024.10.17.22.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:52:05 -0700 (PDT) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfauth.phl.internal (Postfix) with ESMTP id 15B7D1200071; Fri, 18 Oct 2024 01:52:04 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-10.internal (MEProxy); Fri, 18 Oct 2024 01:52:04 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddt necuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilh drtghomheqnecuggftrfgrthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfev ffeiueejhfeuiefggeeuheeggefgnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrg hmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepgh hmrghilhdrtghomhesfhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvuddpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidrug gvpdhrtghpthhtohepughirhhkrdgsvghhmhgvsehgmhgrihhlrdgtohhmpdhrtghpthht oheplhihuhguvgesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprhhushhtqdhfohhrqd hlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrkhhrsehr vgguhhgrthdrtghomhdprhgtphhtthhopegrihhrlhhivggusehrvgguhhgrthdrtghomh dprhgtphhtthhopehmihhnghhosehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhl lheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrd gtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:52:03 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 5/6] rust: sync: Introduce lock::Backend::Context Date: Thu, 17 Oct 2024 22:51:24 -0700 Message-ID: <20241018055125.2784186-6-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Lyude Paul Now that we've introduced an `InterruptDisabled` token for marking contexts in which IRQs are disabled, we can have a way to avoid `SpinLockIrq` disabling interrupts if the interrupts have already been disabled. Basically, a `SpinLockIrq` should work like a `SpinLock` if interrupts are disabled. So a function: (&'a SpinLockIrq, &'a InterruptDisabled) -> Guard<'a, .., SpinLockBackend> makes senses. Note that due to `Guard` and `InterruptDisabled` has the same lifetime, interrupts cannot be enabled whiel the Guard exists. Add a `lock_with()` interface for `Lock`, and an associate type of `Backend` to describe the context. [Boqun: Change the interface a lot, now `SpinLockIrq` can use the `lock()` function, but it always disable the interrupts, reuse the `lock_with()` method to provide a way for locking if interrupts are already disabled. `lock_with()` implementation will be added later.] Co-developed-by: Lyude Paul Signed-off-by: Lyude Paul Signed-off-by: Boqun Feng Reviewed-by: Lyude Paul Tested-by: Andreas Hindborg Tested-by: Dirk Behme --- rust/kernel/sync/lock.rs | 12 +++++++++++- rust/kernel/sync/lock/mutex.rs | 1 + rust/kernel/sync/lock/spinlock.rs | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f6c34ca4d819..49b53433201c 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -38,6 +38,9 @@ pub unsafe trait Backend { /// [`unlock`]: Backend::unlock type GuardState; =20 + /// The context which can be provided to acquire the lock with a diffe= rent backend. + type Context<'a>; + /// Initialises the lock. /// /// # Safety @@ -120,8 +123,15 @@ pub fn new(t: T, name: &'static CStr, key: &'static Lo= ckClassKey) -> impl PinIni } =20 impl Lock { + /// Acquires the lock with the given context and gives the caller acce= ss to the data protected + /// by it. + pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, = T, B> { + todo!() + } + /// Acquires the lock and gives the caller access to the data protecte= d by it. - pub fn lock(&self) -> Guard<'_, T, B> { + #[inline] + pub fn lock<'a>(&'a self) -> Guard<'a, T, B> { // SAFETY: The constructor of the type calls `init`, so the existe= nce of the object proves // that `init` was called. let state =3D unsafe { B::lock(self.state.get()) }; diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 30632070ee67..7c2c23994493 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -93,6 +93,7 @@ macro_rules! new_mutex { unsafe impl super::Backend for MutexBackend { type State =3D bindings::mutex; type GuardState =3D (); + type Context<'a> =3D (); =20 unsafe fn init( ptr: *mut Self::State, diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.rs index 884d4d1cbf23..8f9e1b27e474 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -3,6 +3,7 @@ //! A kernel spinlock. //! //! This module allows Rust code to use the kernel's `spinlock_t`. +use crate::interrupt::InterruptDisabled; =20 /// Creates a [`SpinLock`] initialiser with the given name and a newly-cre= ated lock class. /// @@ -92,6 +93,7 @@ macro_rules! new_spinlock { unsafe impl super::Backend for SpinLockBackend { type State =3D bindings::spinlock_t; type GuardState =3D (); + type Context<'a> =3D (); =20 unsafe fn init( ptr: *mut Self::State, @@ -183,6 +185,7 @@ macro_rules! new_spinlock_irq { unsafe impl super::Backend for SpinLockIrqBackend { type State =3D bindings::spinlock_t; type GuardState =3D (); + type Context<'a> =3D &'a InterruptDisabled; =20 unsafe fn init( ptr: *mut Self::State, --=20 2.45.2 From nobody Tue Nov 26 10:48:09 2024 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0322618870E; Fri, 18 Oct 2024 05:52:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230729; cv=none; b=lt1TE37MwuwaffBhskN4CUfaLcO3aFgxll6ebqki1bJFAINS0QDR36PeKYaJ8DAKwFXj9mxvZOymbUcKYSGQL0L7imUjUa5wEYCFbtNX3nXM7uPuI20D4xTM87suObCGEp6jujyAm6Bv8ucCQrb88hj6M4mQgu0zFzgKbOSeIpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230729; c=relaxed/simple; bh=Olilde3fU9liAfVYtogjPSf9m7opIVIiHuPznn1iveg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZRTQYMBwOh94vRCaQfb/yJyGWJopQ3HUxtBVRUEglvb1vtjR+alfC5YKUvssrsohyr4mrRT6/9pZe9h8xbqDZUEsBk+opfw9p9q+rfST8pdEtQMthDQKL7yqE+1clTvBv6KqMLY3yFLGLSRnQko6SUb5BZ6VPXfxaZWVa3/V01U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Z2MyUtM9; arc=none smtp.client-ip=209.85.160.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z2MyUtM9" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-460a415633fso7220161cf.2; Thu, 17 Oct 2024 22:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230727; x=1729835527; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=zCrCyE2+SZF83j5kss/ADEwfWXSUmxKxUyHtwgYvegQ=; b=Z2MyUtM9ipdWZCde2qx13DpM5Mre+F7do/paM8ZXzmR7qXDXeBax4NF8CH9eQSCHaN dMu0G5l9ekMqh+rwegeApZP0SY8UT2mUDoQ6v4dGPf5yFXT+ebOUfBFJ/NlNGzjkHrp1 FA/QdQ/vv9uVuvJ/lc8+s8eu0XjWDDN7qxX3rN20D0WcclLX2s5nD9wUy7l3+lZOX6Ha 0sZDuFWw4aSIZe7etgKioALbaWs6r908+BBcAKEzxOgcuO1FqPjBw/bwLaIxs5OqNDiG vjdPqdoqsw+7S1EvTPs8AVEcrFHX7OKQJn5mTwxlmenc/sm5czCrIqJ0VNSHHJMxoXsc Exzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230727; x=1729835527; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zCrCyE2+SZF83j5kss/ADEwfWXSUmxKxUyHtwgYvegQ=; b=bo0PS9ldlLs/ga3A563CRXUBs1eboE92Wv3aLciQwADAmmx47R2m8VRKTxsQvwnQz2 V89nJIsabRPfFJDAOSD5wImywUbbH1jPxDDHvbx7LlKvPdF/ye9s4LuqAeZgwGUeCPaX NUNb81qqK7hmPZ8Gdm8Yciy0rEBSBAj4rsNC42MVBGZupIBgAACnNCNssdMHGdjoUae3 6VrAY67PUTF//40+kmDrfepNn4VNLOfu5aJ0qA5uOV5InFcmi0esV3NbNvr8vLoeWzd6 Cltxcqt7NcshgeerdBEMtpei9aovwPEtwHqVt8UYRjKmrO4vHwJ2jaFJHTQmS/EQqhFL e7jA== X-Forwarded-Encrypted: i=1; AJvYcCU7jaeojrd6ok8T2pG7NMz9mSrdhZLqw9JTElIqPzLkIlDpQWRr1aph5X78Wo6fZEVG8azim6in5CTTOEU=@vger.kernel.org, AJvYcCVZmcj80BeF4oa6/w6JRMvVwl0vipnEQs9K1Su7LUE55Ffl8HT/eHmjEYLBtyctk7WubNg2yrW5ELWRdCTCZbE=@vger.kernel.org X-Gm-Message-State: AOJu0YyfiuryPtbJnk17Jd4igyfZD+K4PKiKcPw+tITOsHcsBFKkzpE+ HYufBU1a6V2CqT6qJmkyMjAhJMijvnm+Q+uqAOqKvzSrbeXNlT5q X-Google-Smtp-Source: AGHT+IH9e62snMfTi+nvdpvBORtjymUBqFymyxF5Th73SGnnUVY2SNOf+XhMUnHQxQZ10CIVYsCOsg== X-Received: by 2002:a05:622a:1992:b0:45d:969f:8031 with SMTP id d75a77b69052e-460aed4a163mr17747841cf.25.1729230726920; Thu, 17 Oct 2024 22:52:06 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460ae94751bsm4057681cf.15.2024.10.17.22.52.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:52:06 -0700 (PDT) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfauth.phl.internal (Postfix) with ESMTP id 73A6F1200073; Fri, 18 Oct 2024 01:52:05 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Fri, 18 Oct 2024 01:52:05 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddt necuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilh drtghomheqnecuggftrfgrthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfev ffeiueejhfeuiefggeeuheeggefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepgh hmrghilhdrtghomhesfhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvuddpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidrug gvpdhrtghpthhtohepughirhhkrdgsvghhmhgvsehgmhgrihhlrdgtohhmpdhrtghpthht oheplhihuhguvgesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprhhushhtqdhfohhrqd hlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrkhhrsehr vgguhhgrthdrtghomhdprhgtphhtthhopegrihhrlhhivggusehrvgguhhgrthdrtghomh dprhgtphhtthhopehmihhnghhosehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhl lheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrd gtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:52:04 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 6/6] rust: sync: lock: Add `Backend::BackendInContext` Date: Thu, 17 Oct 2024 22:51:25 -0700 Message-ID: <20241018055125.2784186-7-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" `SpinLock`'s backend can be used for `SpinLockIrq`, if the interrupts are disabled. And it actually provides performance gains since interrupts are not needed to be disabled anymore. So add `Backend::BackendInContext` to describe the case where one backend can be used for another. Use the it to implement the `lock_with()` so that `SpinLockIrq` can avoid disabling interrupts by using `SpinLock`'s backend. Signed-off-by: Boqun Feng Reviewed-by: Lyude Paul Tested-by: Andreas Hindborg Tested-by: Dirk Behme --- rust/kernel/sync/lock.rs | 25 +++++++++++++++++++++++-- rust/kernel/sync/lock/mutex.rs | 1 + rust/kernel/sync/lock/spinlock.rs | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 49b53433201c..4e3316feb497 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -24,10 +24,14 @@ /// is owned, that is, between calls to [`lock`] and [`unlock`]. /// - Implementers must also ensure that [`relock`] uses the same locking = method as the original /// lock operation. +/// - Implementers must ensure if [`BackendInContext`] is a [`Backend`], i= t's safe to acquire lock +/// under the [`Context`], the [`State`] of two backends must be the sam= e. /// /// [`lock`]: Backend::lock /// [`unlock`]: Backend::unlock /// [`relock`]: Backend::relock +/// [`BackendInContext`]: Backend::BackendInContext +/// [`Context`]: Backend::Context pub unsafe trait Backend { /// The state required by the lock. type State; @@ -41,6 +45,9 @@ pub unsafe trait Backend { /// The context which can be provided to acquire the lock with a diffe= rent backend. type Context<'a>; =20 + /// The alternative backend we can use if a [`Context`] is provided. + type BackendInContext: Sized; + /// Initialises the lock. /// /// # Safety @@ -125,8 +132,22 @@ pub fn new(t: T, name: &'static CStr, key: &'static Lo= ckClassKey) -> impl PinIni impl Lock { /// Acquires the lock with the given context and gives the caller acce= ss to the data protected /// by it. - pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, = T, B> { - todo!() + pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, = T, B::BackendInContext> + where + B::BackendInContext: Backend, + { + // SAFETY: Per the safety guarantee of `Backend`, if `B::BackendIn= context` and `B` should + // have the same state, therefore the layout of the lock is the sa= me so it's safe the + // convert one to another. + let lock =3D unsafe { &*(self as *const _ as *const Lock) }; + // SAFETY: The constructor of the type calls `init`, so the existe= nce of the object proves + // that `init` was called. Plus the safety guarantee of `Backend` = guarantees that `B::state` + // is the same as `B::BackendInContext::state`, also it's safe to = call another backend + // because there is `B::Context<'a>`. + let state =3D unsafe { B::BackendInContext::lock(lock.state.get())= }; + + // SAFETY: The lock was just acquired. + unsafe { Guard::new(lock, state) } } =20 /// Acquires the lock and gives the caller access to the data protecte= d by it. diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 7c2c23994493..ddb7d06676f7 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -94,6 +94,7 @@ unsafe impl super::Backend for MutexBackend { type State =3D bindings::mutex; type GuardState =3D (); type Context<'a> =3D (); + type BackendInContext =3D (); =20 unsafe fn init( ptr: *mut Self::State, diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.rs index 8f9e1b27e474..3bec25b65c55 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -94,6 +94,7 @@ unsafe impl super::Backend for SpinLockBackend { type State =3D bindings::spinlock_t; type GuardState =3D (); type Context<'a> =3D (); + type BackendInContext =3D (); =20 unsafe fn init( ptr: *mut Self::State, @@ -146,6 +147,7 @@ macro_rules! new_spinlock_irq { /// /// ``` /// use kernel::sync::{new_spinlock_irq, SpinLockIrq}; +/// use kernel::interrupt::InterruptDisabled; /// /// struct Inner { /// a: u32, @@ -168,6 +170,12 @@ macro_rules! new_spinlock_irq { /// } /// } /// +/// // Accessing an `Example` from a function that can only be called in n= o-irq contexts +/// fn noirq_work(e: &Example, irq: &InterruptDisabled) { +/// assert_eq!(e.c, 10); +/// assert_eq!(e.d.lock_with(irq).a, 20); +/// } +/// /// // Allocate a boxed `Example` /// let e =3D Box::pin_init(Example::new(), GFP_KERNEL)?; /// @@ -186,6 +194,7 @@ unsafe impl super::Backend for SpinLockIrqBackend { type State =3D bindings::spinlock_t; type GuardState =3D (); type Context<'a> =3D &'a InterruptDisabled; + type BackendInContext =3D SpinLockBackend; =20 unsafe fn init( ptr: *mut Self::State, --=20 2.45.2