From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CDBD328243 for ; Mon, 18 May 2026 15:00:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779116459; cv=none; b=LXi0fol0HTBb5N57h0hO+cUqsncS15MZJNHLpQcMKgkc3u+gBkEMBJ5GzG79iDFzwAi9EAY9x79RzHDXKLjYciGei2tpdBX0Hbkgkmeo2C9eg9C5EIvlQVaXCfBAxGf+O1WY4Wj09GYh2BqFFeZdxsqTKbVQRDkwcNwuToqFqzw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779116459; c=relaxed/simple; bh=I4jZ9TK5Swq44Efm/wOy7lZUIeZSXZvJaalElK+fZFU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=HQqN2cnlWPx+hbSr3UHKi5pZolJDOvZ+XO6FAcaVgDE3kR2QT8II7ZzypeIkAxA6JVhUmxU8cVWIlkXk8IFkVkQTsVxTHNNcxxN65FIuulNECqbqNfGHvxoVvmanhjELrPTl7XlmzdUpMLXA11mRnDt4h6zkSDa3RG12WTA61A8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=M8E7y+3u; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="M8E7y+3u" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779116453; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LAX8YH75jf5k1zT0qjqD3u1/EQDP3GLAeVLUujmN9O4=; b=M8E7y+3ul4/FQksBtLINgI85+vAZPFHNwA2bplWh/QIHU3NjiCcwmw3nGw/AC5GaWWONGY hEv76kEeXFY8daEveLzVFxQYt4TPTvMThVjTFIPQYgc53uQplLEtskNfoV7Sql7ywRlgIR uoljHKyEFIZxISzyDkE7vzEDbTTDasU= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-335-E8d34SkANTGaYF3No5XdOQ-1; Mon, 18 May 2026 11:00:49 -0400 X-MC-Unique: E8d34SkANTGaYF3No5XdOQ-1 X-Mimecast-MFC-AGG-ID: E8d34SkANTGaYF3No5XdOQ_1779116448 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 27F081800616; Mon, 18 May 2026 15:00:48 +0000 (UTC) Received: from [10.22.81.89] (unknown [10.22.81.89]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 59AA419560A2; Mon, 18 May 2026 15:00:46 +0000 (UTC) Message-ID: <02ed4276-4a56-444d-af07-4e86df8150ed@redhat.com> Date: Mon, 18 May 2026 11:00:45 -0400 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] locking/osq_lock: Use READ_ONCE() for node->prev To: David Laight , Will Deacon Cc: Yu Peng , Peter Zijlstra , Ingo Molnar , Boqun Feng , linux-kernel@vger.kernel.org References: <20260330013255.25937-1-pengyu@kylinos.cn> <20260518152925.10ab8fab@pumpkin> Content-Language: en-US From: Waiman Long In-Reply-To: <20260518152925.10ab8fab@pumpkin> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 On 5/18/26 10:29 AM, David Laight wrote: > On Mon, 18 May 2026 11:29:53 +0100 > Will Deacon wrote: > >> On Mon, Mar 30, 2026 at 09:32:55AM +0800, Yu Peng wrote: >>> osq_lock() consults node->prev in the vcpu_is_preempted() heuristic while >>> a concurrent predecessor may update it via WRITE_ONCE(next->prev, prev) >>> during unqueue. >>> >>> This read only affects the decision to abort optimistic spinning; stale >>> values do not affect queue linkage or lock correctness. Use READ_ONCE() >>> to mark the shared read and match the concurrent WRITE_ONCE() update. >>> >>> No functional change intended. >>> >>> Signed-off-by: Yu Peng >>> --- >>> kernel/locking/osq_lock.c | 2 +- >>> 1 file changed, 1 insertion(+), 1 deletion(-) >>> >>> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c >>> index b4233dc2c2b04..db4545e7bb72c 100644 >>> --- a/kernel/locking/osq_lock.c >>> +++ b/kernel/locking/osq_lock.c >>> @@ -144,7 +144,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) >>> * polling, be careful. >>> */ >>> if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || >>> - vcpu_is_preempted(node_cpu(node->prev)))) >>> + vcpu_is_preempted(node_cpu(READ_ONCE(node->prev))))) >>> return true; >> Hmm, I wonder whether this is actually sufficient... >> >> Architectures with relaxed memory models won't order plain reads to >> different addresses, so the read of 'node->locked' is unordered wrt the >> read of 'node->prev' in this condition. Given that we're using >> smp_cond_load_relaxed(), can we end up using a value of 'node->prev' >> that was loaded in a previous iteration of the loop? >> >> I'd be much more comfortable if this was smp_cond_load_acquire(), in >> addition to the READ_ONCE() that you are proposing. > I've got a patch 'pending' for this file that tidied some things up. > In particular it saves the cpu numbers not the per-cpu addresses. > So the above check doesn't bounce a cache line. > Perhaps it is time to resend it. > > Note that the vpu_is_preempted() path is horribly expensive and > can't actually work reliably. > And can't work at all on arm where 'mwait' (equivalent) is used. I believe the vcpu_is_preempted() check is only enabled in x86, loongarch, powerpc and s390. It is disabled (always returns false) in other arches. Yes, the vcpu_is_preempted() check, if implemented, can be expensive. Perhaps we can reduce the frequency that it is being called. Cheers, Longman