Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4904180rdb; Fri, 29 Dec 2023 19:20:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IF2XG8w1M1Ym2roffKOsPZ3WF8BKshj8KuR9Aou7IMJ1sXDHV8QoeqfQgE5Td2fLaywxCkJ X-Received: by 2002:a05:6402:1609:b0:555:f5e2:e447 with SMTP id f9-20020a056402160900b00555f5e2e447mr222857edv.12.1703906440033; Fri, 29 Dec 2023 19:20:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703906440; cv=none; d=google.com; s=arc-20160816; b=ibJIcM7xuoNuqjNkRxuiTiPgUVBR1B9P6bwyEBZ/mF1irJpNRBIbaDKjVqX3vVbs4Y 0QioiqEtk6Bc3e5QgUz/YUgTALmjQdu0MRc/Iyf0nZyMBSsp0xNiCh4enZlIBmeLuXqR wTHHc6KwigkjU1wQMe0LXxRWY1muEcj/jvL9Dw1EQhDE/Kmg4m1ympKNo+lqmDFtGN4l qJeqa8UFyfq2kLH5DfBBESgHTbjwOJZE8mZAPdZZDMe2Q2Q5lPCJsx/06bP+fh2RiSeh cVixnrBrwCvjJk5kcC3q8KZ0YFjNykXawJ42UKndwT9CoBTxfbfUQzrFAYQK0YQRYl8w xYeA== 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=LJA7Dz6XH3eb7dofNJf23l8/rgg/xnXgNticl1fyqpU=; fh=pvHai+PZt5LffIZ++I0WChek9Xa9cRTyDX+3LL+snXY=; b=V+J6Xtly0OWp/9Fxn3WTJ/k6Y0hlEVTXSZFi+ZnUW2Fw/YbUqCjaeTTctvcbsyor+c iC/PiZn6uz3lFyugudqR+g5/pi71jRQktuT6sa4+QeTjwGTse3dvZc36mPSa8xqjzKxQ eEN7L77vhFSYq1Yon+vzMnF+5Qks+/0vBdfpURzi9jT2e2EybpibQVo0BuXdfiP3QsSz jEjzU8xSbD51CRh6AYoXkWVLYpNZq8NJUkYDmGo1bO/84DBWx0JlVMJLf2aPAtOvcX67 3fLmuj+Fss5XQ4k0xKEzdkhwz+ya2fTUIvtuKE5fLlT42Z0Vr9cNpBxahhUIPrm/EGU0 MSCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WVPIekIs; spf=pass (google.com: domain of linux-kernel+bounces-13322-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13322-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t12-20020a056402524c00b005549e5f54edsi5880211edd.527.2023.12.29.19.20.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 19:20:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13322-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WVPIekIs; spf=pass (google.com: domain of linux-kernel+bounces-13322-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13322-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 am.mirrors.kernel.org (Postfix) with ESMTPS id B9F881F21A59 for ; Sat, 30 Dec 2023 03:20:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C6921138E; Sat, 30 Dec 2023 03:20:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WVPIekIs" 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 7C4CD10EB for ; Sat, 30 Dec 2023 03:20: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=1703906428; 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=LJA7Dz6XH3eb7dofNJf23l8/rgg/xnXgNticl1fyqpU=; b=WVPIekIsTaEONb3oyptBvXO3u75+DvkrST3jzwBsB2CcsmUdxHuGiCaUqx16DluSkWv1oY Vyahk/TYQeDAS4gMc1PAjU8AR9QkQuuXAJdmmYWVAzgQRy3zrPjq9UnUPn5vmgzTsocOx9 K+f235BL+S9x+5hDBp7aPHOAdwTOb+w= 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-573-jTpncnn7P_m9NnT-cmNEiA-1; Fri, 29 Dec 2023 22:20:26 -0500 X-MC-Unique: jTpncnn7P_m9NnT-cmNEiA-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 2FBA485A589; Sat, 30 Dec 2023 03:20:26 +0000 (UTC) Received: from [10.22.16.36] (unknown [10.22.16.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B18F2166B31; Sat, 30 Dec 2023 03:20:25 +0000 (UTC) Message-ID: <835f65d9-a041-4956-b89d-7cd3660c3ab8@redhat.com> Date: Fri, 29 Dec 2023 22:20: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 2/5] locking/osq_lock: Avoid dirtying the local cpu's 'node' 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' , "'xinhui.pan@linux.vnet.ibm.com'" , "'virtualization@lists.linux-foundation.org'" , 'Zeng Heng' References: <73a4b31c9c874081baabad9e5f2e5204@AcuMS.aculab.com> From: Waiman Long In-Reply-To: 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/29/23 17:11, David Laight wrote: > osq_lock() starts by setting node->next to NULL and node->locked to 0. > Careful analysis shows that node->next is always NULL on entry. > > node->locked is set non-zero by another cpu to force a wakeup. > This can only happen after the 'prev->next = node' assignment, > so locked can be set to zero just before that (along with the assignment > to node->prev). > > Only initialise node->cpu once, after that use its value instead > of smp_processor_id() - which is probably a real function call. > > Should reduce cache-line bouncing a little. > > Signed-off-by: David Laight > --- > > Re-send without the 'RE:' on the subject line. > kernel/locking/osq_lock.c | 13 ++++++------- > 1 file changed, 6 insertions(+), 7 deletions(-) > > diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c > index d414eef4bec6..55f5db896c02 100644 > --- a/kernel/locking/osq_lock.c > +++ b/kernel/locking/osq_lock.c > @@ -51,7 +51,7 @@ osq_wait_next(struct optimistic_spin_queue *lock, > struct optimistic_spin_node *prev) > { > struct optimistic_spin_node *next = NULL; > - int curr = encode_cpu(smp_processor_id()); > + int curr = node->cpu; > int old; > > /* > @@ -98,12 +98,10 @@ 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; > > - node->locked = 0; > - node->next = NULL; I have some concern about not clearing node->next at the beginning. I know that next is supposed to be cleared at the end. I do have some worry that there may exist a race condition that leaves next not cleared before it is used again. So you either have to prove that such a race does not exist, or initializing it to NULL at the beginning like it is today. Cheers, Longman > - node->cpu = curr; > + if (unlikely(node->cpu == OSQ_UNLOCKED_VAL)) > + node->cpu = encode_cpu(smp_processor_id()); > > /* > * We need both ACQUIRE (pairs with corresponding RELEASE in > @@ -111,12 +109,13 @@ 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); > + old = atomic_xchg(&lock->tail, node->cpu); > if (old == OSQ_UNLOCKED_VAL) > return true; > > prev = decode_cpu(old); > node->prev = prev; > + node->locked = 0; > > /* > * osq_lock() unqueue > @@ -214,7 +213,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) > void osq_unlock(struct optimistic_spin_queue *lock) > { > struct optimistic_spin_node *node, *next; > - int curr = encode_cpu(smp_processor_id()); > + int curr = raw_cpu_read(osq_node.cpu); > > /* > * Fast path for the uncontended case.