Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp5023998rwb; Wed, 17 Aug 2022 09:42:51 -0700 (PDT) X-Google-Smtp-Source: AA6agR5TM98WPx7UwCuE4EttRaHAhQW4nQ4q5S41mty0xIFMnS+CmWBH+KcSsdG1s7AuPQhZmvMM X-Received: by 2002:a05:6402:190d:b0:43d:f64f:9a0e with SMTP id e13-20020a056402190d00b0043df64f9a0emr24328068edz.346.1660754571214; Wed, 17 Aug 2022 09:42:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660754571; cv=none; d=google.com; s=arc-20160816; b=iqh8j26pX+NwlJYlA4iqgfuoyLnnvfkr38Qa4xlrX1ApLQQzeOboUn8cBQt7Zt0jhW vIU4PKQsQP7mUnCrANqTQ1wScRhSQqWhXqQRt5Sb0Ba6R5/nkHpMTG+iRYfXOQqI5lzg 9BoVvPTb05CP+2QZ3Qeq9F+MGEotWmTSz0Q+V8wcJdeRK1c3RNb9ti1cH7PnYmhhqqn0 yk+8oiOzBx6yBQK/K+j7WHrucHRW966buBAAIiv6Y3Xfoqn607zfEE7jnwQasPyFjIfx qkN898aHujNrPyDsCBS8kXihWMUw2+bkEgW9sa+kaHl5zjuquL5yh/xvH87uzX5Z3TOP S6bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=M7LEgDvWC9pMSb+fAAezzd4bTaAwzJLio/iXmPvwcXo=; b=Qzn+BghBlbz7pMHNlPeRbJFqAuc4zgh//TA9+JK5fqZOEgDIrvCZqUQbBtchPZv4qI GTzaSdIzDAtO9sRp75O8oYy+JOcG3WGjuWbY40idSHWT1PHrnFRE4PU8S0SFkxgvBtur gfuwKK1mO0yPgInr72goKKinAhQgt7Dy+W0DNlycEVp8kyyT93Mu6NL5QD3QPu/tx7ln OICdcboDTCAKi8zTuNFJpuo/JZWa7JaEEvwqcwkmL6ICAcMQe7k5iFHkX9NgfLOIhsJU ApSEeHSojwOBDTtIGhdW+c4LesWJsD3+vmM5nR03swaV90pCf4afLPpdO8bO+YW1MPoz N0NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=iME31brV; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s8-20020a056402520800b0043bcb2650d9si14757631edd.42.2022.08.17.09.42.25; Wed, 17 Aug 2022 09:42:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=iME31brV; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241063AbiHQQ1r (ORCPT + 99 others); Wed, 17 Aug 2022 12:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240995AbiHQQ1k (ORCPT ); Wed, 17 Aug 2022 12:27:40 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90753A0271 for ; Wed, 17 Aug 2022 09:27:37 -0700 (PDT) From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1660753656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M7LEgDvWC9pMSb+fAAezzd4bTaAwzJLio/iXmPvwcXo=; b=iME31brV27Ze7Ihd6kO4vED1fq0XXc8Rn9TgL/6UNe0sMzAWSB0d2S8PIkGLPCx/qSl5Gl qYWRaltw6G4LDIxGAQi39/v5y1YzP86BQZ7gsXvV92N9HW2I/g6rbV9wR0z2V/YtOF/WrJ EnpF/cf3StG8EJxQEq4n8rQPsJGNGrSLVg6skimQyXFAuyQsT4T1omhf12eYuHkC5dnYUE oIncthSGAjC3+If5BtlQ9OLuePUffhy07V94s1t6CXeXVUkpYhtmvjopQZ4ZCxYZQOxVWu 3VHP4+8X58hZ5C5629f170ibQwx6VY8gOq4tTHC5KGvrNHj14/eL0xgfUDipUg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1660753656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M7LEgDvWC9pMSb+fAAezzd4bTaAwzJLio/iXmPvwcXo=; b=foxkcdp5EtApYp/yt81/8EWABj8aPcHHfJXX+/2mlAxvpUxUo3KsBMq2bgGWXSoPMZDHf1 YD4ok0+vadOj/5BA== To: linux-kernel@vger.kernel.org Cc: Thomas Gleixner , Peter Zijlstra , Steven Rostedt , Linus Torvalds , Matthew Wilcox , Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Ingo Molnar , Juri Lelli , Mel Gorman , Valentin Schneider , Vincent Guittot , Sebastian Andrzej Siewior Subject: [PATCH 2/9] preempt: Provide preempt_[dis|en]able_nested() Date: Wed, 17 Aug 2022 18:26:56 +0200 Message-Id: <20220817162703.728679-3-bigeasy@linutronix.de> In-Reply-To: <20220817162703.728679-1-bigeasy@linutronix.de> References: <20220817162703.728679-1-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Gleixner On PREEMPT_RT enabled kernels, spinlocks and rwlocks are neither disabling preemption nor interrupts. Though there are a few places which depend on the implicit preemption/interrupt disable of those locks, e.g. seqcount write sections, per CPU statistics updates etc. To avoid sprinkling CONFIG_PREEMPT_RT conditionals all over the place, add preempt_disable_nested() and preempt_enable_nested() which should be descriptive enough. Add a lockdep assertion for the !PREEMPT_RT case to catch callers which do not have preemption disabled. Cc: Ben Segall Cc: Daniel Bristot de Oliveira Cc: Dietmar Eggemann Cc: Ingo Molnar Cc: Juri Lelli Cc: Mel Gorman Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Valentin Schneider Cc: Vincent Guittot Suggested-by: Linus Torvalds Signed-off-by: Thomas Gleixner Signed-off-by: Sebastian Andrzej Siewior --- include/linux/preempt.h | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/linux/preempt.h b/include/linux/preempt.h index b4381f255a5ca..0df425bf9bd75 100644 --- a/include/linux/preempt.h +++ b/include/linux/preempt.h @@ -421,4 +421,46 @@ static inline void migrate_enable(void) { } =20 #endif /* CONFIG_SMP */ =20 +/** + * preempt_disable_nested - Disable preemption inside a normally preempt d= isabled section + * + * Use for code which requires preemption protection inside a critical + * section which has preemption disabled implicitly on non-PREEMPT_RT + * enabled kernels, by e.g.: + * - holding a spinlock/rwlock + * - soft interrupt context + * - regular interrupt handlers + * + * On PREEMPT_RT enabled kernels spinlock/rwlock held sections, soft + * interrupt context and regular interrupt handlers are preemptible and + * only prevent migration. preempt_disable_nested() ensures that preemption + * is disabled for cases which require CPU local serialization even on + * PREEMPT_RT. For non-PREEMPT_RT kernels this is a NOP. + * + * The use cases are code sequences which are not serialized by a + * particular lock instance, e.g.: + * - seqcount write side critical sections where the seqcount is not + * associated to a particular lock and therefore the automatic + * protection mechanism does not work. This prevents a live lock + * against a preempting high priority reader. + * - RMW per CPU variable updates like vmstat. + */ +/* Macro to avoid header recursion hell vs. lockdep */ +#define preempt_disable_nested() \ +do { \ + if (IS_ENABLED(CONFIG_PREEMPT_RT)) \ + preempt_disable(); \ + else \ + lockdep_assert_preemption_disabled(); \ +} while (0) + +/** + * preempt_enable_nested - Undo the effect of preempt_disable_nested() + */ +static __always_inline void preempt_enable_nested(void) +{ + if (IS_ENABLED(CONFIG_PREEMPT_RT)) + preempt_enable(); +} + #endif /* __LINUX_PREEMPT_H */ --=20 2.37.2