Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1404986pxk; Fri, 4 Sep 2020 08:35:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9S7FmMOoH/qIDV303Czyub7nD1WQE4pYcKJpUIG5uVa4u2QpagbdIPVlWRQFWDVaayhzh X-Received: by 2002:a50:ed16:: with SMTP id j22mr9354537eds.104.1599233720123; Fri, 04 Sep 2020 08:35:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599233720; cv=none; d=google.com; s=arc-20160816; b=ZBnogF7ghj/WAmpG/t42YFYveFboJ8SgP7eWDH+rpG6w+KUxVMtUX4RqdMRRWdFfaX 5x1LNGYG60Jf3OBaRsLS/cUgicUhSLDnPQKQeOGvHJBDoLpjCFFLYdpZP1oPWn+O1n2Y WVnl2TrEETRp+eC/QLYK1h2mkA0PxBGfDCMnOK1RXF34n4PRyzBxP+xZOLf9ky91yNpL lfHGarELOqGGuYO1yLiWktyo7FJL/Yzlme5mjN5kSzwMhM21qGiF2+nZ235zm+wvYZxk I6T/wFgEAfGYO/V8hiEgjxdAEEGaBWrcG7Eq4guejzraYOcD6IBhvd7Ps0DrWlEsD/05 uCwQ== 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=t3aTXUWOFUY6rMm9X7d1EuCalQEcR/WTioCR4C7q4F8=; b=y8ZqOzS9ZhDBL6bho73xSBNtn1LTk4u6encwpQg5gfRfS0Kmn0QicAnatHRVPT6Eg8 BikpkDe9+d8eVbjBI0aRJ67LD6zF6plbYxfPHCEACGXzhCMLaIPzCU/dqLW/yri2x6+S W4g9JpTPSS6+tkLr0rruYexsRv+Gu87Bu7tHf5S5gM9D9AswSUdrAOJ1PUpt6PH17AUy GVk5eZzBez1z117vHGQ7bbTb3yWqKPvgOq4ZRhdOpWr+0dw1bcmh0a9O3xjNhYE3v8Vu UAjV5+SVZP3bnWLFz11MlgtS2lWjIGUXRT7PRtMXor5RFBkx2145zxm/NzEijaaLSbzk eStg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=kHVNSAsv; dkim=neutral (no key) header.i=@linutronix.de; 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 n17si2223814edq.422.2020.09.04.08.34.56; Fri, 04 Sep 2020 08:35:20 -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=kHVNSAsv; dkim=neutral (no key) header.i=@linutronix.de; 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 S1726722AbgIDPc6 (ORCPT + 99 others); Fri, 4 Sep 2020 11:32:58 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:33754 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726406AbgIDPca (ORCPT ); Fri, 4 Sep 2020 11:32:30 -0400 From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1599233547; 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=t3aTXUWOFUY6rMm9X7d1EuCalQEcR/WTioCR4C7q4F8=; b=kHVNSAsvvteInNP5oNANH57rTRn36oQ/5zodjwhLfpDeIqhmzg2kRUK87iBeOKqn16zvcC I/aWwvBv048I09iPKWK0tOonrB60UfJQyjQyOoj6dBXNZuxTZDZhRv/B/XRqnwj4CAsJJu uLELOxWRiVAmGmVu1XFGjYvjHZ8uT73KuHnQDOBqYTIg2A+LrW7d2yn41tVhVUKHW1o2dx 8vXDd6cb/BrOorv2/GbLDnr4wZ3eEeCZXXO1rJjB6xye7bnzzLxIVJc+/thp3BOaAfqkPV 0IrIbe0FNMxwTkSAx/LlMVxkGPvOTEYKijnW3SYAnT3X2/kr8aftiiw8J+hGgg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1599233548; 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=t3aTXUWOFUY6rMm9X7d1EuCalQEcR/WTioCR4C7q4F8=; b=v8Ek/F4lB/eyVfaLzdCxREoKbWISzJ7xACajorteCh1heIv2M0H3+o0UveCs+DR3qJOfz+ 9te4GHzFirp0k/AA== To: Peter Zijlstra , Ingo Molnar , Will Deacon Cc: Thomas Gleixner , "Sebastian A. Siewior" , "Paul E. McKenney" , Steven Rostedt , LKML , "Ahmed S. Darwish" Subject: [PATCH v2 5/5] seqlock: PREEMPT_RT: Do not starve seqlock_t writers Date: Fri, 4 Sep 2020 17:32:31 +0200 Message-Id: <20200904153231.11994-6-a.darwish@linutronix.de> In-Reply-To: <20200904153231.11994-1-a.darwish@linutronix.de> References: <20200904153231.11994-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 On PREEMPT_RT, seqlock_t is transformed to a sleeping lock that do not disable preemption. A seqlock_t reader can thus preempt its write side section and spin for the enter scheduler tick. If that reader belongs to a real-time scheduling class, it can spin forever and the kernel will livelock. To break this livelock possibility on PREEMPT_RT, implement seqlock_t in terms of "seqcount_spinlock_t" instead of plain "seqcount_t". Beside its pure annotational value, this will leverage the existing seqcount_LOCKNAME_T PREEMPT_RT anti-livelock mechanisms, without adding any extra code. Signed-off-by: Ahmed S. Darwish --- include/linux/seqlock.h | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h index 6ac5a63fc536..06a339355c3a 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -757,13 +757,17 @@ static inline void raw_write_seqcount_t_latch(seqcount_t *s) * - Documentation/locking/seqlock.rst */ typedef struct { - struct seqcount seqcount; + /* + * Make sure that readers don't starve writers on PREEMPT_RT: use + * seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK(). + */ + seqcount_spinlock_t seqcount; spinlock_t lock; } seqlock_t; #define __SEQLOCK_UNLOCKED(lockname) \ { \ - .seqcount = SEQCNT_ZERO(lockname), \ + .seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \ .lock = __SPIN_LOCK_UNLOCKED(lockname) \ } @@ -773,8 +777,8 @@ typedef struct { */ #define seqlock_init(sl) \ do { \ - seqcount_init(&(sl)->seqcount); \ spin_lock_init(&(sl)->lock); \ + seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \ } while (0) /** @@ -821,6 +825,12 @@ static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start) return read_seqcount_retry(&sl->seqcount, start); } +/* + * For all seqlock_t write side functions, use write_seqcount_*t*_begin() + * instead of the generic write_seqcount_begin(). This way, no redundant + * lockdep_assert_held() checks are added. + */ + /** * write_seqlock() - start a seqlock_t write side critical section * @sl: Pointer to seqlock_t @@ -837,7 +847,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_t_begin(&sl->seqcount); + write_seqcount_t_begin(&sl->seqcount.seqcount); } /** @@ -849,7 +859,7 @@ static inline void write_seqlock(seqlock_t *sl) */ static inline void write_sequnlock(seqlock_t *sl) { - write_seqcount_t_end(&sl->seqcount); + write_seqcount_t_end(&sl->seqcount.seqcount); spin_unlock(&sl->lock); } @@ -863,7 +873,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_t_begin(&sl->seqcount); + write_seqcount_t_begin(&sl->seqcount.seqcount); } /** @@ -876,7 +886,7 @@ static inline void write_seqlock_bh(seqlock_t *sl) */ static inline void write_sequnlock_bh(seqlock_t *sl) { - write_seqcount_t_end(&sl->seqcount); + write_seqcount_t_end(&sl->seqcount.seqcount); spin_unlock_bh(&sl->lock); } @@ -890,7 +900,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_t_begin(&sl->seqcount); + write_seqcount_t_begin(&sl->seqcount.seqcount); } /** @@ -902,7 +912,7 @@ static inline void write_seqlock_irq(seqlock_t *sl) */ static inline void write_sequnlock_irq(seqlock_t *sl) { - write_seqcount_t_end(&sl->seqcount); + write_seqcount_t_end(&sl->seqcount.seqcount); spin_unlock_irq(&sl->lock); } @@ -911,7 +921,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl) unsigned long flags; spin_lock_irqsave(&sl->lock, flags); - write_seqcount_t_begin(&sl->seqcount); + write_seqcount_t_begin(&sl->seqcount.seqcount); return flags; } @@ -940,7 +950,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl) static inline void write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags) { - write_seqcount_t_end(&sl->seqcount); + write_seqcount_t_end(&sl->seqcount.seqcount); spin_unlock_irqrestore(&sl->lock, flags); } -- 2.28.0