Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp5826567rdb; Sun, 31 Dec 2023 20:09:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IGrlWdBR1g//MXAQDy8aD/RdcHIBSA9BKbULms3G4kWX1mJ3A12V5H2AXAmB0RJrfuxDV1G X-Received: by 2002:a05:620a:1441:b0:781:c22:c097 with SMTP id i1-20020a05620a144100b007810c22c097mr17362148qkl.11.1704082194528; Sun, 31 Dec 2023 20:09:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704082194; cv=none; d=google.com; s=arc-20160816; b=C6rDrAbBUbMQMBYyOaCkF4dBIIqqJdaZa4RyKVqI+BVzoGQ3dlFR2qHNhYvQUxotIu v+FongxGI8mitmFibp39hgdtmhQ20xede/xCE/7DISjFHht0pKfsD3MmDeXLxArkWFRf kbfM35g95BloXv78aL2++Aa3uhbja17J/egAVb2HX4yrBTrhk8hDTlLQ7T+AD1XnFcaW fSJKd2DMP8QKDA02w+RXAUsqmqHdOuLPr89C/5jTC78tEEgUq3fzvuEDDbq6MVxe/CfJ b0XmaRCw+OSqHZrWw4w9AGS6icKhoB7J6ePMaGZpD3Ylxi3WnFIfzL7vFko22i+TJvIc NavQ== 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=TXBAlgGK7S8pQB0FG4Kct0qp1YZSDI7A8y3x9iGBDgc=; fh=/8TN5tpEqOkkoN73Yz9fFu8j5QUKph09dO732LvPqE4=; b=e31dCe5GzncTFyYx/QSsyyViYBc4i9hg+mB4gRXLLr6TdIRjhtz1w/DaTj0aaZb8OR YdEKm6B7g51DYSDrKRgKrEPe15MXOOIy2Hq3jhR5+R62FSA/o0dbgt4L84Iws7jb9kma qNUkBj3MS/NrFCgmh8TGa6WTRySRvQIZheZlIzw0nknXdkgMtJ5JO5GRBwZlCFcRDNfx LMXSbQLfBFl7ODOK5d9P2bcI00X1hM2wYu/y+7fpL3mvYrHyOTmeDlQcRoLebaVOtCN9 TZxr60eGBnMYKvOHHRvJntQS3EKGrJS9Z0JoeYOIt7tQG1R9sevhJjt8SPjmP9olzBRQ Doew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JEac30NC; spf=pass (google.com: domain of linux-kernel+bounces-13824-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13824-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. [147.75.199.223]) by mx.google.com with ESMTPS id u17-20020a05620a023100b0077f09ac9202si23930949qkm.230.2023.12.31.20.09.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Dec 2023 20:09:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13824-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JEac30NC; spf=pass (google.com: domain of linux-kernel+bounces-13824-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13824-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 4C4001C21C63 for ; Mon, 1 Jan 2024 04:09:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 067D32595; Mon, 1 Jan 2024 04:09:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JEac30NC" X-Original-To: linux-kernel@vger.kernel.org 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 9DAE31C01 for ; Mon, 1 Jan 2024 04:09:36 +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=1704082175; 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=TXBAlgGK7S8pQB0FG4Kct0qp1YZSDI7A8y3x9iGBDgc=; b=JEac30NCt6gyzyUxHx4z3d9DCoz6KNc087tTptHX5CjWGCbK3enUxdLzE/7t4bYvDonMdv 6unw7s6t4CjM9kDbRKrVdhB4PYxJI2KjkTXkjyphkunI/t3AShw9zIl0vs7wHOrbAl8VlQ SnUEM1u8jmAjrf3KXfXtRaiyfktinTc= 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-313-YiIT4ADlM7Siv7xUTevm2w-1; Sun, 31 Dec 2023 23:09:31 -0500 X-MC-Unique: YiIT4ADlM7Siv7xUTevm2w-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 97B4785A589; Mon, 1 Jan 2024 04:09:30 +0000 (UTC) Received: from [10.22.16.25] (unknown [10.22.16.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2DA5A51D5; Mon, 1 Jan 2024 04:09:30 +0000 (UTC) Message-ID: <82b6320e-cd5a-43a4-8a12-4d6d5484fc34@redhat.com> Date: Sun, 31 Dec 2023 23:09:30 -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 3/5] locking/osq_lock: Use node->prev_cpu instead of saving node->prev. 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> <7906aaa73f93493c873e6286c1f96645@AcuMS.aculab.com> From: Waiman Long In-Reply-To: <7906aaa73f93493c873e6286c1f96645@AcuMS.aculab.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 On 12/31/23 16:54, David Laight wrote: > node->prev is only used to update 'prev' in the unlikely case > of concurrent unqueues. > This can be replaced by a check for node->prev_cpu changing > and then calling decode_cpu() to get the changed 'prev' pointer. > > node->cpu (or more particularly) prev->cpu is only used for the > osq_wait_next() call in the unqueue path. > Normally this is exactly the value that the initial xchg() read > from lock->tail (used to obtain 'prev'), but can get updated > by concurrent unqueues. > > Both the 'prev' and 'cpu' members of optimistic_spin_node are > now unused and can be deleted. > > Signed-off-by: David Laight > --- > kernel/locking/osq_lock.c | 31 +++++++++++++++++-------------- > 1 file changed, 17 insertions(+), 14 deletions(-) > > diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c > index eb8a6dfdb79d..27324b509f68 100644 > --- a/kernel/locking/osq_lock.c > +++ b/kernel/locking/osq_lock.c > @@ -13,9 +13,8 @@ > */ > > struct optimistic_spin_node { > - struct optimistic_spin_node *next, *prev; > + struct optimistic_spin_node *next; > int locked; /* 1 if lock acquired */ > - int cpu; /* encoded CPU # + 1 value */ > int prev_cpu; /* encoded CPU # + 1 value */ > }; > > @@ -91,10 +90,9 @@ bool osq_lock(struct optimistic_spin_queue *lock) > struct optimistic_spin_node *node = this_cpu_ptr(&osq_node); > struct optimistic_spin_node *prev, *next; > int curr = encode_cpu(smp_processor_id()); > - int old; > + int prev_cpu; > > node->next = NULL; > - node->cpu = curr; > > /* > * We need both ACQUIRE (pairs with corresponding RELEASE in > @@ -102,13 +100,12 @@ bool osq_lock(struct optimistic_spin_queue *lock) > * the node fields we just initialised) semantics when updating > * the lock tail. > */ > - old = atomic_xchg(&lock->tail, curr); > - if (old == OSQ_UNLOCKED_VAL) > + prev_cpu = atomic_xchg(&lock->tail, curr); > + if (prev_cpu == OSQ_UNLOCKED_VAL) > return true; > > - node->prev_cpu = old; > - prev = decode_cpu(old); > - node->prev = prev; > + node->prev_cpu = prev_cpu; > + prev = decode_cpu(prev_cpu); > node->locked = 0; > > /* > @@ -174,9 +171,16 @@ bool osq_lock(struct optimistic_spin_queue *lock) > > /* > * Or we race against a concurrent unqueue()'s step-B, in which > - * case its step-C will write us a new @node->prev pointer. > + * case its step-C will write us a new @node->prev_cpu value. > */ > - prev = READ_ONCE(node->prev); > + { > + int new_prev_cpu = READ_ONCE(node->prev_cpu); > + > + if (new_prev_cpu == prev_cpu) > + continue; > + prev_cpu = new_prev_cpu; > + prev = decode_cpu(prev_cpu); > + } Just a minor nit. It is not that common in the kernel to add another nesting level just to reduce the scope of  new_prev_cpu auto variable. Anyway, Reviewed-by: Waiman Long