Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3314283ybt; Mon, 29 Jun 2020 22:46:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyY3R0uGrl4un+GsbNsJrTjpCBQTiSnVH01rEAO56E3M/5gHbnvjxTIN6UfkzyWusWi6WnI X-Received: by 2002:aa7:c609:: with SMTP id h9mr21759301edq.43.1593495982700; Mon, 29 Jun 2020 22:46:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593495982; cv=none; d=google.com; s=arc-20160816; b=0CF+6fIVt4TBLIlxtTvh0QIwXnM4gHaY0HCwMxKH3sctDIpPHQHGBSk2OQrjeON3Pj IBmwbO5MSnJB/a+4w3g5LyktGwUPhEsZGKzn1Allt4xa/mAo3yy6WdQ7nABM8zqofoRw ZbCz+gEiYUj6w6QziGYk0bz96ovL5KWjUj1b4GIFjYJ4m/s523vu9SCTbGvHvwyQmIYl 3kurkkk1D6yOo0X6AB6hwHB8XVVnzTwpw3ZiphHJPo96I2DspZlhfAOu5FrASLuFQl+3 rV2irIxiPorT57wMemgaCRKpD9Jwh1nUDal6MJ3w3bd+8hjdNN7325x7zaWMuqHYgjPL X6BQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=lunRlhcxXkJk+oc9hwlO5PkFmWrHF2RhUujVoo87/FM=; b=NgSLHfzvWTvlzJw4SeKUCqcZQhYjmuITOOM4hPbXbnRcfVaTBvCqdteI9p+2lK84U5 bwfr21I/Ve3DwCI2Rpn2eCZPneOYPlMCN/Ai8Yu/gI4V2gEFnwr/rnvZD6is/XUnfBn2 IA7/qSUrgtUrto9o33FetDHXjlqME4YVhJHTY/ofYB2GUQZtCBHIitJ25beVf00emHVO m67hgY1o6SR4dzG9qYhRMMvtRzBamPDk/5bdb9C5e4J8hXww8HsrXC9jXs+E5Qm7+ZcF BEyg8QuV4o5FE3yak6zSCqfHlvzR4jjqzb28hI/w3P0Fdmk09IrGxUYKJBiKHLRsIfVo Ha5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZxAVa4to; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a15si1097338edn.210.2020.06.29.22.45.58; Mon, 29 Jun 2020 22:46:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZxAVa4to; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1729799AbgF3FpU (ORCPT + 99 others); Tue, 30 Jun 2020 01:45:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729615AbgF3FpS (ORCPT ); Tue, 30 Jun 2020 01:45:18 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EEC2C03E979 for ; Mon, 29 Jun 2020 22:45:18 -0700 (PDT) From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1593495916; 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=lunRlhcxXkJk+oc9hwlO5PkFmWrHF2RhUujVoo87/FM=; b=ZxAVa4to3pAig/0VmF03G3MMNTGgLHCCuu/o6G87J4rC8YbiSbY4rtPgRV6ZbqaAJi1/JO Y09XbLqti1N9d0XEktgqFm1lRSDJSEsQ1XCVH3p5KyWEnueZHODDXr2isZa9iRcu34sWlE whEX7sPURX3OL/9xowsVN1qARLhZx1gBeDtG7res4Qu4s7Ua+ZYzLvxoikGtHCsaXiVK2A bIykX0FHl0AfH4mh+7hhCMukglf5B6bh8LP7BJdxRCb3COw0disoQoI+GCQXuYM5rMc//8 rrzuZC632sRPA3KAd8UL3rm/16ecLOmki+HL3OSq1bKr0yToWQNL0s/7K2xxIg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1593495916; 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=lunRlhcxXkJk+oc9hwlO5PkFmWrHF2RhUujVoo87/FM=; b=lTx3kz5SZxcJ6hhWdhZMl4D3aIXlhfg52i+BjAB8LlawV9Pg5teFb+MMsSMDqSRark2LWC E1S67aHbVej/88Bg== To: Peter Zijlstra , Ingo Molnar , Will Deacon Cc: Thomas Gleixner , "Paul E. McKenney" , "Sebastian A. Siewior" , Steven Rostedt , LKML , "Ahmed S. Darwish" Subject: [PATCH v3 05/20] seqlock: lockdep assert non-preemptibility on seqcount_t write Date: Tue, 30 Jun 2020 07:44:37 +0200 Message-Id: <20200630054452.3675847-6-a.darwish@linutronix.de> In-Reply-To: <20200630054452.3675847-1-a.darwish@linutronix.de> References: <20200519214547.352050-1-a.darwish@linutronix.de> <20200630054452.3675847-1-a.darwish@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Preemption must be disabled before entering a sequence count write side critical section. Failing to do so, the seqcount read side can preempt the write side section and spin for the entire scheduler tick. If that reader belongs to a real-time scheduling class, it can spin forever and the kernel will livelock. Assert through lockdep that preemption is disabled for seqcount writers. Signed-off-by: Ahmed S. Darwish --- include/linux/seqlock.h | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h index 057f7326a877..679c440b17fe 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -419,12 +419,29 @@ static inline void raw_write_seqcount_latch(seqcount_t *s) smp_wmb(); /* increment "sequence" before following stores */ } -static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass) +static inline void __write_seqcount_begin_nested(seqcount_t *s, int subclass) { raw_write_seqcount_begin(s); seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_); } +static inline void write_seqcount_begin_nested(seqcount_t *s, int subclass) +{ + lockdep_assert_preemption_disabled(); + __write_seqcount_begin_nested(s, subclass); +} + +/* + * write_seqcount_begin() without lockdep non-preemptibility checks. + * + * Use for internal seqlock.h code where it's known that preemption is + * already disabled. For example, seqlock_t write side functions. + */ +static inline void __write_seqcount_begin(seqcount_t *s) +{ + __write_seqcount_begin_nested(s, 0); +} + /** * write_seqcount_begin() - start a seqcount_t write-side critical section * @s: Pointer to &typedef seqcount_t @@ -563,7 +580,7 @@ static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start) static inline void write_seqlock(seqlock_t *sl) { spin_lock(&sl->lock); - write_seqcount_begin(&sl->seqcount); + __write_seqcount_begin(&sl->seqcount); } /** @@ -591,7 +608,7 @@ static inline void write_sequnlock(seqlock_t *sl) static inline void write_seqlock_bh(seqlock_t *sl) { spin_lock_bh(&sl->lock); - write_seqcount_begin(&sl->seqcount); + __write_seqcount_begin(&sl->seqcount); } /** @@ -618,7 +635,7 @@ static inline void write_sequnlock_bh(seqlock_t *sl) static inline void write_seqlock_irq(seqlock_t *sl) { spin_lock_irq(&sl->lock); - write_seqcount_begin(&sl->seqcount); + __write_seqcount_begin(&sl->seqcount); } /** @@ -639,7 +656,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl) unsigned long flags; spin_lock_irqsave(&sl->lock, flags); - write_seqcount_begin(&sl->seqcount); + __write_seqcount_begin(&sl->seqcount); return flags; } -- 2.20.1