Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp589814pxb; Wed, 8 Sep 2021 08:01:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyv019RGm1YERTk3JGpF6toZ7wOmuEEoMpHc9mc/uqJdFTVI2nxZ5adesYgm1UYrB7FBGqS X-Received: by 2002:a17:906:4482:: with SMTP id y2mr300577ejo.484.1631113266855; Wed, 08 Sep 2021 08:01:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631113266; cv=none; d=google.com; s=arc-20160816; b=VoINV6XWIm12IZ0hnXoRxOMNuUdsltod6XQQoT8fKJVVz3NSq141g9pdFLPhKOCZ2Y pI38EX6+CzaykFk3Wwmc+/+pFhFnnZNPbNmUFe9l1jp8xJPjAiI5GEy4rm15beJgh4uw va3J8OK2HSa9JwGk+MqqDQHwTwUZBwGSh2xJfEOnfxwsTJs3mJWp8KhCYep4BfsJ9Hfb sV/+wlCf5viIWOspN/lnkQ7p9bPgvHSGX+9pMBEqZt5q+vqoxgXZ0HwH/7oHjADLofNt ESExSa6YdlUb09n0pnoNVZb0/fmbmXPWgpuTd+Jc5eod5fkGQ1JydU8/naP+9CLGyJkO cQng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=umPny3Ok0/zsN6Nvy2jYnqoJuWfVc2FWJg6UAYNRrPo=; b=s1XdrtG9dzh+DF1XICUxIVrmuUQzpKJklIeHkhr3F3OGVGbJ0vYluigezFnGcRsyFS 0PV0hT+oV+uLiWVMKmMd6O02tpwB9ka8nZgdZEwPaRz458dbL6IrlqcFat8ForWMAe/F MjKeuIE2z408ZXZFVfFhTR6fOqB49C1HCrd7RYM7qyg05TsybiIKdWiwX8GodcvtGqy5 6q8WiWamL5cVxr9Y+kqQGtINl2ZLkVTM5Bh49Hz+MbR8WaU56KBKekRtdcS95mFLg4qR X3Cem5gb0KYWzahjiwcaXVAhXy93oS1l4xC3WzCaoIPxsMtBcRaWVJDsSFttY55TptmT BdDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=g9hpn+iF; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bu15si2251667ejb.164.2021.09.08.08.00.41; Wed, 08 Sep 2021 08:01:06 -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=@infradead.org header.s=casper.20170209 header.b=g9hpn+iF; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231164AbhIHOnp (ORCPT + 99 others); Wed, 8 Sep 2021 10:43:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351932AbhIHOno (ORCPT ); Wed, 8 Sep 2021 10:43:44 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 600E3C061575 for ; Wed, 8 Sep 2021 07:42:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=umPny3Ok0/zsN6Nvy2jYnqoJuWfVc2FWJg6UAYNRrPo=; b=g9hpn+iFXn8HwbSWpCypvYf4wO 1OHpv4WsFFJrsS1z1BZt93Qbhu7Y46ZHNVfA57Qecf/dV7wT+gvVdoIIUbudm5pjs/J7xNik9h3KI kC3iwxIxC5UaiPy4yqbWXtfb/eMZ25YuNcPAISo/S5VvHw/iZIO5zf4N23ddON+uo1+RQ/t4t6vDH Hw0YOuyH48E0d6SVhi/JrP2ZYt6UgaDoqNMhmoDzqMUJaJhAJOMvlnm2PXpxaVg4AHg+34rx5M7jo sP7K+2hb2bOLDMb8J24soE8jCev6aWo3J0JBddyqmm15vV7bqOl6Wy/klEp62OGL2VgO1nspqGYK2 39yMtz0w==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1mNylD-008tSZ-Bf; Wed, 08 Sep 2021 14:41:20 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 5BA43300454; Wed, 8 Sep 2021 16:41:10 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 36FD3201736A5; Wed, 8 Sep 2021 16:41:10 +0200 (CEST) Date: Wed, 8 Sep 2021 16:41:10 +0200 From: Peter Zijlstra To: Boqun Feng Cc: Thomas Gleixner , Ingo Molnar , Juri Lelli , Steven Rostedt , Davidlohr Bueso , Will Deacon , Waiman Long , Sebastian Andrzej Siewior , Mike Galbraith , Daniel Bristot de Oliveira , LKML Subject: Re: [RFC] locking: rwbase: Take care of ordering guarantee for fastpath reader Message-ID: References: <20210901150627.620830-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 08, 2021 at 09:08:52PM +0800, Boqun Feng wrote: > On Wed, Sep 08, 2021 at 01:51:24PM +0200, Peter Zijlstra wrote: > [...] > > @@ -201,23 +207,30 @@ static int __sched rwbase_write_lock(struct rwbase_rt *rwb, > > { > > struct rt_mutex_base *rtm = &rwb->rtmutex; > > unsigned long flags; > > + int readers; > > > > /* Take the rtmutex as a first step */ > > if (rwbase_rtmutex_lock_state(rtm, state)) > > return -EINTR; > > > > /* Force readers into slow path */ > > - atomic_sub(READER_BIAS, &rwb->readers); > > + readers = atomic_sub_return_relaxed(READER_BIAS, &rwb->readers); > > > > - raw_spin_lock_irqsave(&rtm->wait_lock, flags); > > /* > > * set_current_state() for rw_semaphore > > * current_save_and_set_rtlock_wait_state() for rwlock > > */ > > rwbase_set_and_save_current_state(state); > > rwbase_set_and_save_current_state() may eventually call > current_save_and_set_rtlock_wait_state(), which requires being called > with irq-off, while rwbase_write_lock() may be called with irq-on. I > guess we can change the raw_spin_lock() to raw_spin_lock_irqsave() in > current_save_and_set_rtlock_wait_state() to solve this. Oh right... that's actually something I pointed out to Thomas during review, and I suppose we both forgot about it, or figured it didn't matter enough. Oooh, Thomas added that lockdep_assert.. still lemme change that to match set_special_state(). Also,... --- Subject: sched/wakeup: Strengthen current_save_and_set_rtlock_wait_state() While looking at current_save_and_set_rtlock_wait_state() I'm thinking it really ought to use smp_store_mb(), because something like: current_save_and_set_rtlock_wait_state(); for (;;) { if (try_lock()) break; raw_spin_unlock_irq(&lock->wait_lock); if (!cond) schedule(); raw_spin_lock_irq(&lock->wait_lock); set_current_state(TASK_RTLOCK_WAIT); } current_restore_rtlock_saved_state(); which is very close to the advertised usage in the comment, is actually broken I think: - try_lock() doesn't need to provide any ordering on failure; - raw_spin_unlock() only needs to provide RELEASE ordering; which gives that the above turns into something like: WRITE_ONCE(current->__state, TASK_RTLOCK_WAIT); raw_spin_unlock(¤t->pi_lock); raw_spin_unlock(&lock->wait_lock); if (!cond) and the load of @cond is then allowed to speculate right before the __state store, and we've got a missed wakeup -> BAD(tm). Fixes: 5f220be21418 ("sched/wakeup: Prepare for RT sleeping spin/rwlocks") Signed-off-by: Peter Zijlstra (Intel) --- diff --git a/include/linux/sched.h b/include/linux/sched.h index 1780260f237b..3d3246d7e87d 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -245,7 +245,8 @@ struct task_group; * if (try_lock()) * break; * raw_spin_unlock_irq(&lock->wait_lock); - * schedule_rtlock(); + * if (!cond) + * schedule_rtlock(); * raw_spin_lock_irq(&lock->wait_lock); * set_current_state(TASK_RTLOCK_WAIT); * } @@ -253,22 +254,24 @@ struct task_group; */ #define current_save_and_set_rtlock_wait_state() \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags; /* may shadow */ \ + \ + raw_spin_lock_irqsave(¤t->pi_lock, flags); \ current->saved_state = current->__state; \ debug_rtlock_wait_set_state(); \ - WRITE_ONCE(current->__state, TASK_RTLOCK_WAIT); \ - raw_spin_unlock(¤t->pi_lock); \ + smp_store_mb(current->__state, TASK_RTLOCK_WAIT); \ + raw_spin_unlock_irqrestore(¤t->pi_lock, flags); \ } while (0); #define current_restore_rtlock_saved_state() \ do { \ - lockdep_assert_irqs_disabled(); \ - raw_spin_lock(¤t->pi_lock); \ + unsigned long flags; /* may shadow */ \ + \ + raw_spin_lock_irqsave(¤t->pi_lock, flags); \ debug_rtlock_wait_restore_state(); \ WRITE_ONCE(current->__state, current->saved_state); \ current->saved_state = TASK_RUNNING; \ - raw_spin_unlock(¤t->pi_lock); \ + raw_spin_unlock_irqrestore(¤t->pi_lock, flags); \ } while (0); #define get_current_state() READ_ONCE(current->__state)