Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3082413pxy; Sun, 25 Apr 2021 13:21:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwP5JsXPXtumMtlNYRpdXHaianieHQC9gmiLQgkl6PDFcwGdlWarTycFiacRrFSTR2agwbp X-Received: by 2002:a17:906:d213:: with SMTP id w19mr14907615ejz.16.1619382071421; Sun, 25 Apr 2021 13:21:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619382071; cv=none; d=google.com; s=arc-20160816; b=n/CQ0r3XK8WkNKRkEWxjRNy+n/Pz1YI00vuj9kfaf1nHq8xDBSLN8HPufXedx9uAe1 QAaITmFJn+lTQEopB+qcE3FDk/JMPGynOqLidjAJHB3Q7AFjQyQQwDIX2Rbme9B5x1jO Wma/OISyCT8whzCX38dhlOsbL0aj1ZERWP9B74W3fTONLI6bIEtsoLFbQm2pJOfJjR7x wGnvDfco9uM5uXjlyt2tVDm5g72K+PL9RUpCbPLq0k8s0m/NzrPTuyqKfiXl/ZiRuKFb JdL2CKxyEa/1eCTOJ7OyGLp9KJR1dHiCVszL5wkk9wnRd9EwP/fApNK/mRYI2abpN67S phSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=WNLmXfNHHFxJs3CZDbz/p8vZ8D/qmUMYK+vF6Nbj3qY=; b=iF+kdtRXqDCofpvbkAjQA3XEvh9eo9SkUhk4uCjxgnMmoM1UMDORy3ELap2BjSCFu9 PqFFTuVuIvuNcY6h+tqXiyTvqCEH1+e594mbQo5d97jAYUTo8e3aAESJrrMpS6j03LIR anEJLmHeQo9Ns0fpPbBnnJraR/svpKLXliIClKZ14Wwe1MdrTSYG8va68uk6HNno2cp9 v3dEm1ul9I506ZKE7ihQOhf2CXR6/KiSobV24OBLL2/26M18PJxSBMriaEAcK/MsUD1q m9st07xekeYfVrePVrFLOGDL8DokjGaC2JTbcdrr7tsnimnPkcujRR/Kr2mZ3rtkCl+M SzDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SKL558aG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q2si12315270ejz.47.2021.04.25.13.20.47; Sun, 25 Apr 2021 13:21:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SKL558aG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231304AbhDYUHj (ORCPT + 99 others); Sun, 25 Apr 2021 16:07:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:27628 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231197AbhDYUHi (ORCPT ); Sun, 25 Apr 2021 16:07:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619381217; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc; bh=WNLmXfNHHFxJs3CZDbz/p8vZ8D/qmUMYK+vF6Nbj3qY=; b=SKL558aGsvZ21zVBZBOW4PFk/y+SKRZGx1L5RbCRwjhwvu7GWQKWktlXOJNLWgI20Qigwu ai3gvJXq5jyH3whAY/k29z3UDRPNQoQ6l+ZBpnFB3qpCFSbH7he2H0dDzmlXcEmxl1eJos /ah00ObSCs8vlWUEvPzFoDQWh+bP1Bo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-186-qBlmxkS1OcicwS1fDheDZQ-1; Sun, 25 Apr 2021 16:06:56 -0400 X-MC-Unique: qBlmxkS1OcicwS1fDheDZQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DB49A107ACCA; Sun, 25 Apr 2021 20:06:54 +0000 (UTC) Received: from llong.com (ovpn-112-118.rdu2.redhat.com [10.10.112.118]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0602719D80; Sun, 25 Apr 2021 20:06:51 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: linux-kernel@vger.kernel.org, Borislav Petkov , Ali Saidi , Steve Capper , Waiman Long Subject: [PATCH] locking/qrwlock: queued_write_lock_slowpath() cleanup Date: Sun, 25 Apr 2021 16:06:37 -0400 Message-Id: <20210425200637.31298-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Make the code more readable by replacing the atomic_cmpxchg_acquire() by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add() to atomic_or(). For architectures that use qrwlock, I do not find one that has an atomic_add() defined but not an atomic_or(). I guess it should be fine by changing atomic_add() to atomic_or(). I add a comment to state that we can change it back to atomic_add() if there is an architecture that has a more performant atomic_add() than an atomic_or(). Suggested-by: Linus Torvalds Signed-off-by: Waiman Long --- kernel/locking/qrwlock.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c index b94f3831e963..a1fa0f636b07 100644 --- a/kernel/locking/qrwlock.c +++ b/kernel/locking/qrwlock.c @@ -60,18 +60,26 @@ EXPORT_SYMBOL(queued_read_lock_slowpath); */ void queued_write_lock_slowpath(struct qrwlock *lock) { - int cnts; + int cnts = 0; /* Put the writer into the wait queue */ arch_spin_lock(&lock->wait_lock); /* Try to acquire the lock directly if no reader is present */ if (!atomic_read(&lock->cnts) && - (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0)) + atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED)) goto unlock; - /* Set the waiting flag to notify readers that a writer is pending */ - atomic_add(_QW_WAITING, &lock->cnts); + /* + * Set the waiting flag to notify readers that a writer is pending + * + * As only one writer who is the wait_lock owner can set the waiting + * flag which will be cleared later on when acquiring the write lock, + * we can easily replace atomic_or() by an atomic_add() if there is + * an architecture where an atomic_add() performs better than an + * atomic_or(). + */ + atomic_or(_QW_WAITING, &lock->cnts); /* When no more readers or writers, set the locked flag */ do { -- 2.18.1