Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4772349rdb; Fri, 29 Dec 2023 12:58:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IETlC5laLmLsoVHF0mYkVqruY/FkJoy8SJwtToqzoH4p8ww/YZ0h39yUk4fTJ6ds6gIAKcz X-Received: by 2002:a05:6a20:13d5:b0:18a:d4ea:1678 with SMTP id ho21-20020a056a2013d500b0018ad4ea1678mr12662298pzc.15.1703883529042; Fri, 29 Dec 2023 12:58:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703883529; cv=none; d=google.com; s=arc-20160816; b=KJlashYToK307MjgDb8krKX63RvA0cP/gDlSfC/7zNIJpjReLYt3PQTG5ctWi7VPWN Y1pVJZ2tFexl1MypyUK+0/xmre5s1nlNWQKA9Gz3RTvvxR06DX5dwWdi49ffd9h3pO81 fQNmClEyXH4WaKgbWARJZTEatAAf5lPO3C415bTY5/mjaRoZYGlUmIKmF6I0rHm1tsCT HqFHLDr1jfw+PPU5dRe3U95X0vcJ+xCgWmH1w5xbLqq1XonkS4zLDOmliIgSS0txdTrY FC5vDrn1CQd4QhhvOO1UQ4MWODbMw/fsPJfKYYlQjRVcs1TFn5hx8LDMqmMyuodT3kyW spLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=A3jMVLyZgOGdwKLMkdWHQZWJAg73fPcpjyIjD5hEWIs=; fh=9mjHhe+fO0FRmfh1SiQmAZ16aLlCaXA7AOG78MgCddY=; b=raYhZfcGMT/8mDBz2leuvoXBAfAi8qHp2AvAIZloyk9YBR7Ud7gjEBVk0SPdJKEPa+ SyvsbJ50HQ61wwWAlaHgKnXfBY7sIB5Sb3hlV7eSIBsrWQwCsoqPi+fjhadS2deElVC/ QcpVea2VFCB8JT66u5ZeGQwvx7BKBfAciU+zk4AHj9RBQfCS0IH+7nOHRP0B+yW46pQ8 fTyxz85gdCD1mj0ehC3INoS9EU1W92XpOAaZJwSMGZoDu+QTl3WOi/fVioovbgFqBc9S nPdvIsa0CneOrcp0SkuX7zVKSC8JnD1rCbpTvuCQCQ0+rBY+zx5LnBJlJ1GnmWJYFGs+ 4/Gg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-13233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bj19-20020a056a02019300b005cdba90861asi15558203pgb.159.2023.12.29.12.58.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 12:58:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-13233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B21FD283C1E for ; Fri, 29 Dec 2023 20:58:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A4EAE1428E; Fri, 29 Dec 2023 20:58:43 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (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 ACEA91426F for ; Fri, 29 Dec 2023 20:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-202-bl5_I54CNsuf5Ujw93Ax2g-1; Fri, 29 Dec 2023 20:58:32 +0000 X-MC-Unique: bl5_I54CNsuf5Ujw93Ax2g-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 29 Dec 2023 20:58:15 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Fri, 29 Dec 2023 20:58:15 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'peterz@infradead.org'" , "'longman@redhat.com'" 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' Subject: [PATCH next 5/5] locking/osq_lock: Optimise vcpu_is_preempted() check. Thread-Topic: [PATCH next 5/5] locking/osq_lock: Optimise vcpu_is_preempted() check. Thread-Index: Ado6mcFsTi5k8LaETrKavOOIB4in0Q== Date: Fri, 29 Dec 2023 20:58:15 +0000 Message-ID: <23cef5ac49494b9087953f529ae5df16@AcuMS.aculab.com> References: <73a4b31c9c874081baabad9e5f2e5204@AcuMS.aculab.com> In-Reply-To: <73a4b31c9c874081baabad9e5f2e5204@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable The vcpu_is_preempted() test stops osq_lock() spinning if a virtual cpu is no longer running. Although patched out for bare-metal the code still needs the cpu number. Reading this from 'prev->cpu' is a pretty much guaranteed have a cache miss when osq_unlock() is waking up the next cpu. Instead save 'prev->cpu' in 'node->prev_cpu' and use that value instead. Update in the osq_lock() 'unqueue' path when 'node->prev' is changed. This is simpler than checking for 'node->prev' changing and caching 'prev->cpu'. Signed-off-by: David Laight --- kernel/locking/osq_lock.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c index b60b0add0161..89be63627434 100644 --- a/kernel/locking/osq_lock.c +++ b/kernel/locking/osq_lock.c @@ -14,8 +14,9 @@ =20 struct optimistic_spin_node { =09struct optimistic_spin_node *self, *next, *prev; -=09int locked; /* 1 if lock acquired */ -=09int cpu; /* encoded CPU # + 1 value */ +=09int locked; /* 1 if lock acquired */ +=09int cpu; /* encoded CPU # + 1 value */ +=09int prev_cpu; /* actual CPU # for vpcu_is_preempted() */ }; =20 static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node= ); @@ -29,11 +30,6 @@ static inline int encode_cpu(int cpu_nr) =09return cpu_nr + 1; } =20 -static inline int node_cpu(struct optimistic_spin_node *node) -{ -=09return node->cpu - 1; -} - static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) { =09int cpu_nr =3D encoded_cpu_val - 1; @@ -114,6 +110,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) =09if (old =3D=3D OSQ_UNLOCKED_VAL) =09=09return true; =20 +=09node->prev_cpu =3D old - 1; =09prev =3D decode_cpu(old); =09node->prev =3D prev; =09node->locked =3D 0; @@ -148,7 +145,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) =09 * polling, be careful. =09 */ =09if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || -=09=09=09=09 vcpu_is_preempted(node_cpu(node->prev)))) +=09=09=09=09 vcpu_is_preempted(node->prev_cpu))) =09=09return true; =20 =09/* unqueue */ @@ -205,6 +202,7 @@ bool osq_lock(struct optimistic_spin_queue *lock) =09 * it will wait in Step-A. =09 */ =20 +=09WRITE_ONCE(next->prev_cpu, prev->cpu - 1); =09WRITE_ONCE(next->prev, prev); =09WRITE_ONCE(prev->next, next); =20 --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)