Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp5827559rdb; Sun, 31 Dec 2023 20:13:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IG7hoH0RWFl7Zf+ucukmSFhOjmG5cCK2cZqX4eaZNhVen4YvsY2bfqUppAxH4rsQzdiU0sR X-Received: by 2002:ae9:f718:0:b0:781:2abd:2451 with SMTP id s24-20020ae9f718000000b007812abd2451mr19050083qkg.8.1704082421057; Sun, 31 Dec 2023 20:13:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704082421; cv=none; d=google.com; s=arc-20160816; b=OKsXfII7/wA5KvpumXxij+t3xmH3jbT7VQDq4A9efNtoqPQxLfzMHhYEdf+baDYkxL 2uy1H7kGf7h853yD+g+eOAPE1Qv50lCS0X7KZWBnDKewMsEEnTWyyQLAj3Jlt51FHPq4 s7BIKPvZcc3pXQnKzB/2DAX/yRukdAH8pAv7gMUqdAbqIJai6pL/eYzTr2sf8UARvkiF 5eHMHF24AWDuYAnzR9FF9BkLNxuZ7dyOcXz/Y1I0uTgp2a0z4SRV6Cbx3gqrYLHgIMJ+ ni/E/92L6t67JZ9I/sDY7FesZB4G88oA12qFf96/XueIjELnoTV3AIhP4E2nhDCAun88 pxvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=b2w6gP/r1kZh4IA5V7Iwl9YCEdVnK3Y0Y/TP+Gf1xTU=; fh=/8TN5tpEqOkkoN73Yz9fFu8j5QUKph09dO732LvPqE4=; b=zGcYxqXtTz+UM2yV7J+sVBfCyGygbfYr511z3eYsD9BEwM/91ZpAPedThC9aAVRvt6 3AFdy/4BW5or/KSI/0Qf20YZwmHzCxvssCVjVwg+xkKn8TJRMsCT9rCjXyv7lnLAhJL/ dU1fyRP+sWzC18Ao/+Vcv2as6S5rte7nqdCKg7luT12IJPCOz6lY/3/P0DjwtdyXhLsP 6a6iMau+zC/YVIZth41nGhNue8Q/0ssviMOTpQUfyyqZIAcwHvx3wpH9vznpWqy9Eq2o Fi6fq7/y3jSkMNfOnK85/hcfYGD1EY8K66DtgbIEbc0y85yKuL5JsJy0xz8Kj7YSP9Q3 NziA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FuQOV7V8; spf=pass (google.com: domain of linux-kernel+bounces-13826-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13826-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f10-20020a05620a408a00b007815e202a65si12046748qko.574.2023.12.31.20.13.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Dec 2023 20:13:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13826-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FuQOV7V8; spf=pass (google.com: domain of linux-kernel+bounces-13826-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13826-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 839251C21C8C for ; Mon, 1 Jan 2024 04:13:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 144D78C07; Mon, 1 Jan 2024 04:13:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FuQOV7V8" X-Original-To: linux-kernel@vger.kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 C32A98BE0 for ; Mon, 1 Jan 2024 04:13:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704082408; 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=b2w6gP/r1kZh4IA5V7Iwl9YCEdVnK3Y0Y/TP+Gf1xTU=; b=FuQOV7V8G0LGI//0iJ+8BDV/WfgQuIIKJUFJm+50h4qvV+u37Omt9mnXHyktHQ+u56hwdD 2Nc4SQRuCPne3e3fmHLV1qTlzwmboNVlLl1ClYY4Oz3BpLMIUVj1+RNReJNrkr8IkQItLE RjOIObGR8RV+Q/kSAPJen0S7a/lMPEk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-532-bK0lsNcVOzqeuZ2Qc6xYxQ-1; Sun, 31 Dec 2023 23:13:26 -0500 X-MC-Unique: bK0lsNcVOzqeuZ2Qc6xYxQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 615D7185A781; Mon, 1 Jan 2024 04:13:26 +0000 (UTC) Received: from [10.22.16.25] (unknown [10.22.16.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2ACC2166B31; Mon, 1 Jan 2024 04:13:25 +0000 (UTC) Message-ID: <544f928d-4444-4b26-9da6-bdf0c4d9831b@redhat.com> Date: Sun, 31 Dec 2023 23:13:25 -0500 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 next v2 4/5] locking/osq_lock: Avoid writing to node->next in the osq_lock() fast path. Content-Language: en-US To: David Laight , "'linux-kernel@vger.kernel.org'" , "'peterz@infradead.org'" Cc: "'mingo@redhat.com'" , "'will@kernel.org'" , "'boqun.feng@gmail.com'" , 'Linus Torvalds' , "'virtualization@lists.linux-foundation.org'" , 'Zeng Heng' References: <2b4e8a5816a742d2bd23fdbaa8498e80@AcuMS.aculab.com> <06a11b2c7d784f2d80dc8e81c7175c57@AcuMS.aculab.com> From: Waiman Long In-Reply-To: <06a11b2c7d784f2d80dc8e81c7175c57@AcuMS.aculab.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 On 12/31/23 16:54, David Laight wrote: > When osq_lock() returns false or osq_unlock() returns static > analysis shows that node->next should always be NULL. > This means that it isn't necessary to explicitly set it to NULL > prior to atomic_xchg(&lock->tail, curr) on extry to osq_lock(). > > Just in case there a non-obvious race condition that can leave it > non-NULL check with WARN_ON_ONCE() and NULL if set. > Note that without this check the fast path (adding at the list head) > doesn't need to to access the per-cpu osq_node at all. > > Signed-off-by: David Laight > --- > kernel/locking/osq_lock.c | 14 ++++++++++---- > 1 file changed, 10 insertions(+), 4 deletions(-) > > diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c > index 27324b509f68..35bb99e96697 100644 > --- a/kernel/locking/osq_lock.c > +++ b/kernel/locking/osq_lock.c > @@ -87,12 +87,17 @@ osq_wait_next(struct optimistic_spin_queue *lock, > > bool osq_lock(struct optimistic_spin_queue *lock) > { > - struct optimistic_spin_node *node = this_cpu_ptr(&osq_node); > - struct optimistic_spin_node *prev, *next; > + struct optimistic_spin_node *node, *prev, *next; > int curr = encode_cpu(smp_processor_id()); > int prev_cpu; > > - node->next = NULL; > + /* > + * node->next should be NULL on entry. > + * Check just in case there is a race somewhere. > + * Note that this is probably an unnecessary cache miss in the fast path. > + */ > + if (WARN_ON_ONCE(raw_cpu_read(osq_node.next) != NULL)) > + raw_cpu_write(osq_node.next, NULL); > > /* > * We need both ACQUIRE (pairs with corresponding RELEASE in > @@ -104,8 +109,9 @@ bool osq_lock(struct optimistic_spin_queue *lock) > if (prev_cpu == OSQ_UNLOCKED_VAL) > return true; > > - node->prev_cpu = prev_cpu; > + node = this_cpu_ptr(&osq_node); > prev = decode_cpu(prev_cpu); > + node->prev_cpu = prev_cpu; > node->locked = 0; > > /* Reviewed-by: Waiman Long