public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Dirk Behme" <dirk.behme@gmail.com>,
	"Lyude Paul" <lyude@redhat.com>,
	rust-for-linux@vger.kernel.org,
	"Danilo Krummrich" <dakr@redhat.com>,
	airlied@redhat.com, "Ingo Molnar" <mingo@redhat.com>,
	will@kernel.org, "Waiman Long" <longman@redhat.com>,
	linux-kernel@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	wedsonaf@gmail.com, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	aliceryhl@google.com, "Trevor Gross" <tmgross@umich.edu>
Subject: Re: [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling
Date: Thu, 24 Oct 2024 14:57:02 -0700	[thread overview]
Message-ID: <ZxrCrlg1XvaTtJ1I@boqun-archlinux> (raw)
In-Reply-To: <87bjz95sc4.ffs@tglx>

On Thu, Oct 24, 2024 at 07:22:19PM +0200, Thomas Gleixner wrote:
> On Thu, Oct 24 2024 at 12:05, Peter Zijlstra wrote:
> > On Wed, Oct 23, 2024 at 10:38:38PM +0200, Thomas Gleixner wrote:
> >> But if we want to support insanity then we make preempt count 64 bit and
> >> be done with it. But no, I don't think that encouraging insanity is a
> >> good thing.
> >
> > The problem is that in most release builds the overflow will be silent
> > and cause spurious weirdness that is a pain in the arse to debug :/
> >
> > That is my only concern -- making insane code crash hard is good, making
> > it silently mostly work but cause random weirdness is not.
> 
> I wish we could come up with a lightweight check for that.
> 

Since the preempt part takes exactly one byte in the preempt counter,
maybe we could use a "incb + jo"?

For example as below, note that since I used OF here, so it will try the
byte as s8 therefore overflow at 128, so 127 is the max level of
nesting.

Would this be a relatively lightweight check?

Regards,
Boqun

--------------------------->8
diff --git a/arch/x86/include/asm/current.h b/arch/x86/include/asm/current.h
index bf5953883ec3..c233b7703194 100644
--- a/arch/x86/include/asm/current.h
+++ b/arch/x86/include/asm/current.h
@@ -16,7 +16,10 @@ struct pcpu_hot {
 	union {
 		struct {
 			struct task_struct	*current_task;
-			int			preempt_count;
+			union {
+				int		preempt_count;
+				u8		preempt_bytes[4];
+			};
 			int			cpu_number;
 #ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
 			u64			call_depth;
diff --git a/arch/x86/include/asm/percpu.h b/arch/x86/include/asm/percpu.h
index c55a79d5feae..8d3725f8f2c7 100644
--- a/arch/x86/include/asm/percpu.h
+++ b/arch/x86/include/asm/percpu.h
@@ -251,6 +251,17 @@ do {									\
 		percpu_binary_op(size, qual, "add", var, val);		\
 } while (0)
 
+#define percpu_check_inc(size, qual, _var)				\
+({									\
+	bool overflow;							\
+									\
+	asm qual (__pcpu_op1_##size("inc", __percpu_arg([var]))		\
+		  CC_SET(o)						\
+	    : CC_OUT(o) (overflow), [var] "+m" (__my_cpu_var(_var)));	\
+									\
+	overflow;							\
+})
+
 /*
  * Add return operation
  */
@@ -488,6 +499,7 @@ do {									\
 #define this_cpu_read_stable_4(pcp)			__raw_cpu_read_stable(4, pcp)
 
 #define raw_cpu_add_1(pcp, val)				percpu_add_op(1, , (pcp), val)
+#define raw_cpu_check_inc_1(pcp)			percpu_check_inc(1, , (pcp))
 #define raw_cpu_add_2(pcp, val)				percpu_add_op(2, , (pcp), val)
 #define raw_cpu_add_4(pcp, val)				percpu_add_op(4, , (pcp), val)
 #define raw_cpu_and_1(pcp, val)				percpu_binary_op(1, , "and", (pcp), val)
diff --git a/arch/x86/include/asm/preempt.h b/arch/x86/include/asm/preempt.h
index 919909d8cb77..a39cf8c0fc8b 100644
--- a/arch/x86/include/asm/preempt.h
+++ b/arch/x86/include/asm/preempt.h
@@ -2,6 +2,7 @@
 #ifndef __ASM_PREEMPT_H
 #define __ASM_PREEMPT_H
 
+#include <asm/bug.h>
 #include <asm/rmwcc.h>
 #include <asm/percpu.h>
 #include <asm/current.h>
@@ -76,7 +77,12 @@ static __always_inline bool test_preempt_need_resched(void)
 
 static __always_inline void __preempt_count_add(int val)
 {
-	raw_cpu_add_4(pcpu_hot.preempt_count, val);
+	if (__builtin_constant_p(val) && val == 1) {
+		/* Panic if overflow */
+		BUG_ON(raw_cpu_check_inc_1(pcpu_hot.preempt_bytes[0]));
+	} else {
+		raw_cpu_add_4(pcpu_hot.preempt_count, val);
+	}
 }
 
 static __always_inline void __preempt_count_sub(int val)

  reply	other threads:[~2024-10-24 21:57 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-16 21:28 [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Lyude Paul
2024-09-16 21:28 ` [PATCH v6 1/3] rust: Introduce irq module Lyude Paul
2024-09-29 20:36   ` Trevor Gross
2024-09-29 23:45   ` Boqun Feng
2024-10-02 20:20   ` Thomas Gleixner
2024-10-04  8:58     ` Benno Lossin
2024-10-04 17:18       ` Lyude Paul
2024-10-17 18:51       ` Lyude Paul
2024-10-04 17:02     ` Lyude Paul
2024-10-10 21:00   ` Daniel Almeida
2024-09-16 21:28 ` [PATCH v6 2/3] rust: sync: Introduce lock::Backend::Context Lyude Paul
2024-09-29 20:40   ` Trevor Gross
2024-09-29 23:52   ` Boqun Feng
2024-09-16 21:28 ` [PATCH v6 3/3] rust: sync: Add SpinLockIrq Lyude Paul
2024-09-29 20:50   ` Trevor Gross
2024-09-29 23:59   ` Boqun Feng
2024-10-02 20:53   ` Thomas Gleixner
2024-10-03 12:51     ` Boqun Feng
2024-10-04 18:48     ` Lyude Paul
2024-10-05 18:19       ` Lyude Paul
2024-10-07 12:42         ` Boqun Feng
2024-10-07 18:13           ` Lyude Paul
2024-10-15 12:57           ` Andreas Hindborg
2024-10-15 20:17             ` Boqun Feng
2024-10-15 20:21               ` Boqun Feng
2024-10-16 20:57                 ` Lyude Paul
2024-10-17 13:34                   ` Andreas Hindborg
2024-10-07 12:01       ` Thomas Gleixner
2024-10-07 18:30         ` Lyude Paul
2024-10-08 15:21           ` Thomas Gleixner
2024-10-12  8:01             ` Boqun Feng
2024-10-10 16:39 ` [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Daniel Almeida
2024-10-12  5:29 ` Dirk Behme
2024-10-13 19:06   ` Thomas Gleixner
2024-10-13 21:43     ` Boqun Feng
2024-10-16 21:00       ` Thomas Gleixner
2024-10-16 21:31         ` Boqun Feng
2024-10-17 20:49           ` Lyude Paul
2024-10-17 22:27             ` Boqun Feng
2024-10-18  5:51           ` [POC 0/6] Allow SpinLockIrq to use a normal Guard interface Boqun Feng
2024-10-18  5:51             ` [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Boqun Feng
2024-10-21  7:04               ` kernel test robot
2024-10-21  7:35               ` kernel test robot
2024-10-21 20:44               ` Lyude Paul
2024-10-24 16:18                 ` Peter Zijlstra
2024-10-23 19:34               ` Thomas Gleixner
2024-10-23 19:51                 ` Peter Zijlstra
2024-10-23 20:38                   ` Thomas Gleixner
2024-10-24 10:05                     ` Peter Zijlstra
2024-10-24 17:22                       ` Thomas Gleixner
2024-10-24 21:57                         ` Boqun Feng [this message]
2024-10-25 15:04                           ` Thomas Gleixner
2024-10-25 18:28                             ` Peter Zijlstra
2024-10-24 19:12                       ` Lyude Paul
2025-07-24 20:36                   ` w/r/t "irq & spin_lock: Add counted interrupt disabling/enabling": holes in pcpu_hot? Lyude Paul
2025-07-24 21:59                     ` Thomas Gleixner
2024-10-24  5:05                 ` [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Boqun Feng
2024-10-24  8:17                   ` Thomas Gleixner
2024-10-24 16:20                     ` Boqun Feng
2024-10-18  5:51             ` [POC 2/6] rust: Introduce interrupt module Boqun Feng
2024-10-31 20:45               ` Lyude Paul
2024-10-31 20:47                 ` Lyude Paul
2024-10-18  5:51             ` [POC 3/6] rust: helper: Add spin_{un,}lock_irq_{enable,disable}() helpers Boqun Feng
2024-10-18  5:51             ` [POC 4/6] rust: sync: Add SpinLockIrq Boqun Feng
2024-10-18 19:23               ` Lyude Paul
2024-10-18 20:22                 ` Boqun Feng
2024-10-18  5:51             ` [POC 5/6] rust: sync: Introduce lock::Backend::Context Boqun Feng
2024-10-31 20:54               ` Lyude Paul
2024-10-18  5:51             ` [POC 6/6] rust: sync: lock: Add `Backend::BackendInContext` Boqun Feng
2024-10-18 10:22             ` [POC 0/6] Allow SpinLockIrq to use a normal Guard interface Andreas Hindborg
2024-10-18 12:42               ` Boqun Feng
2024-10-18 11:16             ` Andreas Hindborg
2024-10-18 16:05               ` Dirk Behme
2024-10-31 20:56             ` Lyude Paul
2024-10-17 20:42         ` [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Lyude Paul

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZxrCrlg1XvaTtJ1I@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@samsung.com \
    --cc=airlied@redhat.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dakr@redhat.com \
    --cc=dirk.behme@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=lyude@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tmgross@umich.edu \
    --cc=wedsonaf@gmail.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox