Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp151211lqh; Wed, 27 Mar 2024 18:53:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVjJPcxkN8Pw8PJe6ihvFkd0+3lq1unSOSK9dcahbKEeDFxs90klwMZUa/3+73TNqN7QbhDTu9gRglAivIen0PGwscc5fuxYx1Y1l0Lwg== X-Google-Smtp-Source: AGHT+IGO/X4s785Kr9t+2aSjJEwd173MEc3jn+34xtI+aLjAUiBZR3wqJE1lY6CoZAx0QtF61I0G X-Received: by 2002:a17:906:4ace:b0:a45:f209:d2cb with SMTP id u14-20020a1709064ace00b00a45f209d2cbmr826083ejt.28.1711590794341; Wed, 27 Mar 2024 18:53:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711590794; cv=pass; d=google.com; s=arc-20160816; b=arq0If1MOCr9E4Rpj0n1IMKZc16//Z0KXPSxSPdQVvUBS3iOYjDEfQxkeJW0UbfvKk jH+v9ET1CVAT4z+6FKYq/YQ+HpNfLNhIfp5K+uD2hcTXIazij1/KdWzkJ6nAEZ1e8F+u NK9nO4xVk09ion5cv+RNof4rZdXfK3+5ZCrlWmeiZocAQH/SW3OnhJCSM93oOt6LcHyL EPGFgMiG/jADTwBtJjVIiaq97U0Ov4sfDmxpBPXq0D+TxgVbp1/RdIg41j6lWFWrDqlA +VnZBLdGwOA8aU2AUvR+z9hmrYp1BsJpa62N+tRszifD7UurgneGwXLlh7+qcUvF45Ki efJQ== ARC-Message-Signature: i=2; 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=bGrCCIEqgGoPQ+c4NYkn0V6dFitHSmmoXt4A1C3TGLs=; fh=X77V4NNS0WZa+u+VDpai1ql4bNFoUuYyLi66SrKJQXk=; b=jqRjwrP6wlgPI4TYhIM46gB/ecgMIabX8sqx40azwTdfoqcT78orHlTeKd+JQV8vEq LJYqxrhR9VzDevE1oTwodYvW3Jys/09k3adhwbVAs2MTnyX6Bd3Ew+H83sJBaZ1xOhs3 XkogwbHgzg/P8rruVjkIFiBCMorGwTB7tT+gkHR1C//vtWNErGymIeOlCtW7BVO7OfK1 heDQdmhdWE3xceCSr9KM8T6EXa532vyB3P9oMSIN8jY0YMCeKMBSSSrj3+GWQOAXKIRK q63+0OSxnoc1i9mJtrxnnc4HcLl8M95aSAUMJO6e3MIqIikaPXlQR4FMKrE+WGw4Qn4G +tCA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J9UKqMWB; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-122308-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122308-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. [147.75.80.249]) by mx.google.com with ESMTPS id u27-20020a1709063b9b00b00a46ba6a8493si160326ejf.728.2024.03.27.18.53.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 18:53:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122308-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J9UKqMWB; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-122308-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122308-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 187C31F2B89A for ; Thu, 28 Mar 2024 01:53:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 92FA2219F0; Thu, 28 Mar 2024 01:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="J9UKqMWB" 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 3FB0A22092 for ; Thu, 28 Mar 2024 01:53:04 +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=1711590786; cv=none; b=hu8NWLXeGYBuqUhgGnrrJfVvwVVyZ4jA7bievyyyuaVqNtQWFRI+IU00ReQ6cw5EYb6085/7LUiNGKqI7juhtiYZwogzjCCmEBOlPXe7cwCeXD/7oazeaP/9haC4/LTfymRmYD5lXjCZIAw3ZMwoYOUEYT46W+m8dLcTFnbifEE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711590786; c=relaxed/simple; bh=AH3ZSLj4vhSj2IBP6+WKnZ5lWH4r9Ur13Q6fmrRBewo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dfDhnabOaz1C58H5OZ7+IVZpim93L7GHkuZvEDIE6mIMiu4yM0xJp9Kyr1t8sVONewLij8dxA91m/CswDvWXkStIu6osiRVWG6+P5znA6n1FDjxsoMyUmLDxTP3uVCMmrNoCZP38LOMJPFOe2V7IK1QLZghZuNiPnZMr+VLQQIo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=J9UKqMWB; arc=none smtp.client-ip=170.10.129.124 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=1711590784; 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=bGrCCIEqgGoPQ+c4NYkn0V6dFitHSmmoXt4A1C3TGLs=; b=J9UKqMWBYOQO8oMZ7fdSF9Hl3BwGSVNXJwk0M4PO7Q4x5qOEW7VxiS3IvVsiQXlBnZR7GP 3JL/A6cQnSHYwd/AG0TINBK2ReZCq+RWOSL6Ux2+15CEb2GMYllVp6Kk3uTz7CFi2DRKVz IOYho9rSciaVg7zBgD5HYWTnyoY3mTM= 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-623-lk96PMowNmi8VSOtzW0Kmw-1; Wed, 27 Mar 2024 21:52:56 -0400 X-MC-Unique: lk96PMowNmi8VSOtzW0Kmw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 500C385A58B; Thu, 28 Mar 2024 01:52:56 +0000 (UTC) Received: from [10.22.33.225] (unknown [10.22.33.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id 07C0F1C060D0; Thu, 28 Mar 2024 01:52:55 +0000 (UTC) Message-ID: <38ba7584-3e74-44e3-b1f5-6e42179856a5@redhat.com> Date: Wed, 27 Mar 2024 21:52:55 -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 1/2] locking/pvqspinlock: Use try_cmpxchg_acquire() in trylock_clear_pending() Content-Language: en-US To: Uros Bizjak , linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng References: <20240325140943.815051-1-ubizjak@gmail.com> From: Waiman Long In-Reply-To: <20240325140943.815051-1-ubizjak@gmail.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.7 On 3/25/24 10:09, Uros Bizjak wrote: > Use try_cmpxchg_acquire(*ptr, &old, new) instead of > cmpxchg_relaxed(*ptr, old, new) == old in trylock_clear_pending(). > x86 CMPXCHG instruction returns success in ZF flag, so this change > saves a compare after cmpxchg. > > Also change the return type of the function to bool. > > No functional change intended. > > Signed-off-by: Uros Bizjak > Cc: Peter Zijlstra > Cc: Ingo Molnar > Cc: Will Deacon > Cc: Waiman Long > Cc: Boqun Feng > --- > kernel/locking/qspinlock_paravirt.h | 31 ++++++++++++----------------- > 1 file changed, 13 insertions(+), 18 deletions(-) > > diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h > index 169950fe1aad..77ba80bd95f9 100644 > --- a/kernel/locking/qspinlock_paravirt.h > +++ b/kernel/locking/qspinlock_paravirt.h > @@ -116,11 +116,12 @@ static __always_inline void set_pending(struct qspinlock *lock) > * barrier. Therefore, an atomic cmpxchg_acquire() is used to acquire the > * lock just to be sure that it will get it. > */ > -static __always_inline int trylock_clear_pending(struct qspinlock *lock) > +static __always_inline bool trylock_clear_pending(struct qspinlock *lock) > { > + u16 old = _Q_PENDING_VAL; > + > return !READ_ONCE(lock->locked) && > - (cmpxchg_acquire(&lock->locked_pending, _Q_PENDING_VAL, > - _Q_LOCKED_VAL) == _Q_PENDING_VAL); > + try_cmpxchg_acquire(&lock->locked_pending, &old, _Q_LOCKED_VAL); > } > #else /* _Q_PENDING_BITS == 8 */ > static __always_inline void set_pending(struct qspinlock *lock) > @@ -128,27 +129,21 @@ static __always_inline void set_pending(struct qspinlock *lock) > atomic_or(_Q_PENDING_VAL, &lock->val); > } > > -static __always_inline int trylock_clear_pending(struct qspinlock *lock) > +static __always_inline bool trylock_clear_pending(struct qspinlock *lock) > { > - int val = atomic_read(&lock->val); > - > - for (;;) { > - int old, new; > - > - if (val & _Q_LOCKED_MASK) > - break; > + int old, new; > > + old = atomic_read(&lock->val); > + do { > + if (old & _Q_LOCKED_MASK) > + return false; > /* > * Try to clear pending bit & set locked bit > */ > - old = val; > - new = (val & ~_Q_PENDING_MASK) | _Q_LOCKED_VAL; > - val = atomic_cmpxchg_acquire(&lock->val, old, new); > + new = (old & ~_Q_PENDING_MASK) | _Q_LOCKED_VAL; > + } while (!atomic_try_cmpxchg_acquire (&lock->val, &old, new)); > > - if (val == old) > - return 1; > - } > - return 0; > + return true; > } > #endif /* _Q_PENDING_BITS == 8 */ > Reviewed-by: Waiman Long