Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3492801ybz; Mon, 4 May 2020 04:10:42 -0700 (PDT) X-Google-Smtp-Source: APiQypKolS7dD+dfnt45+Rn55eSiPYvwlxAogvNu7jtcQcbAInx4/9WUiIdBMu3WStDc3vwwnb36 X-Received: by 2002:aa7:d892:: with SMTP id u18mr14206479edq.156.1588590641838; Mon, 04 May 2020 04:10:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588590641; cv=none; d=google.com; s=arc-20160816; b=SytWyoWmDgiuQr7v1c7WtNcGgp9C0hzeRQUlGwS9orpc855y1fM6nM4y+auvhuFZz9 WnqBbpCDwVtWsJLo9/v5oZt1F7l/e+fOPRdo/pq3xbdUfNomb+EyCLodPWXrDERuskYH j1wXUH00dZLnOoPTKrgmRpBoMTZVXSObXwzVjeEXRV/wvDvdXk6azvnFHO6NMwjt+9lW 7RZTy57NCOymhjLigbavKvM5ffpmiTQJQip3/g3OcJrQR8f5hGeZEdqKz/sg6llsBceG owby2u8fVgjt8wMnYltTIPNHYkocnb9I2kEptuCOqdJ2xIS980iyFfZ1hMe4AjgWIjxv riSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=09losalqRUJ7midVbFAi44IAzVHZ/Wi/7WIU6kwrX9U=; b=oa2AzxO5/cVx99JotwS2v91PEjwFtN5ikMQ/2+DBrjx8JR2ZUXAyILLXJDD+SZjKjo PqkMD/9wKDc7pAyD0HGNqBvrYpGhZXhBMeMuB1W9+ts8QI7feMWIPyfVJsfG8bYfUhOv J8WOy/yynJb6dOAb4XT5m4C03TOa+V7w0u+EmD1OJxRPb7By0pYl5s5tgXGkRdfl1Mc0 dNe9q0w3QwzMLwnu9Vvv0O8fcdTQlcg2D/VRoqFJlGQflW3oVPtdapTeWbEvOZrz8WMS qIN60KddAxT7x/XbPufuKPN8tk0rb+VRgKpYkwHDbpsH5Sf/e5RKp7+R6DX9hPkJv+kT bcoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Px3HqRrx; 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 f6si6223338edw.325.2020.05.04.04.09.53; Mon, 04 May 2020 04:10:41 -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=Px3HqRrx; 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 S1728519AbgEDLD6 (ORCPT + 99 others); Mon, 4 May 2020 07:03:58 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:47348 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728297AbgEDLD5 (ORCPT ); Mon, 4 May 2020 07:03:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588590235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=09losalqRUJ7midVbFAi44IAzVHZ/Wi/7WIU6kwrX9U=; b=Px3HqRrxhIQEBqoJODUfdvZOKYEbaPkqTxR1AB8J6KU/wKd/KwHuXZu3S6hj6wPPfR+mgk Mzi1HnCUJFKYTI+Xlk0GVmxv1HZ1Pb1ItyZXhjdtHcqtAQLxRj4YVXasAXCX9K2C9Hbvx2 1XO0wADAYa0t0upc2vA9t7I9Sb7D4h8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-194-ZgOQAa-8Pj-ulBl137wF8w-1; Mon, 04 May 2020 07:03:54 -0400 X-MC-Unique: ZgOQAa-8Pj-ulBl137wF8w-1 Received: by mail-wm1-f69.google.com with SMTP id 71so3299779wmb.8 for ; Mon, 04 May 2020 04:03:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=09losalqRUJ7midVbFAi44IAzVHZ/Wi/7WIU6kwrX9U=; b=fE3gBPitTM+d7Avy0SEFWqD5zgekfdWyDyov+mzeHzsM83/Z7S58idREMqkD8hoauU pvdIDiCeXdB49lVoI+TVv1UNk/Af8tnII1yzDIF1QzQOj8HmGgAJ+NPjqC6JHw5FEGR8 PXUun1ZHa5PMFOdshPwCU/eXH10YrZYWc7JkGGzpvQlQjppS3mgtZg2XZZyJxKiQLjG1 lDc/MNpLG2TmWgd0OlbTY1ttcFul3Hkbo2OpgoqMUNEh7X7sAx0e0749RqnuhT757CB6 lA0cyJ8poutFgEBvhBpxrctPHQ0L/ODOS01hXQ0PewE47Y3WnucVOIEaOy4sYzosf8/+ 4iqg== X-Gm-Message-State: AGi0PubgC45qBdh83McYDk8P+lvdTzw0HHL119RnXpEfD0mFmh6dF+ke cCqxU0JzrHQ32abcUjRVjarj3FWNNWXNBFTzf4l/jjDNJ0giTsfPEVt0u0mGU0WqTdkK1pDxTbl PJmd1bV1LEiaWm1vML9lZnDPz X-Received: by 2002:a5d:5261:: with SMTP id l1mr6739138wrc.24.1588590232923; Mon, 04 May 2020 04:03:52 -0700 (PDT) X-Received: by 2002:a5d:5261:: with SMTP id l1mr6739098wrc.24.1588590232648; Mon, 04 May 2020 04:03:52 -0700 (PDT) Received: from localhost.localdomain.com ([194.230.155.213]) by smtp.gmail.com with ESMTPSA id a13sm10885750wrv.67.2020.05.04.04.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 May 2020 04:03:52 -0700 (PDT) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Christian Borntraeger , David Hildenbrand , Cornelia Huck , Paolo Bonzini , Vitaly Kuznetsov , Jim Mattson , Alexander Viro , Emanuele Giuseppe Esposito , linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, kvm-ppc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-fsdevel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH v2 1/5] refcount, kref: add dec-and-test wrappers for rw_semaphores Date: Mon, 4 May 2020 13:03:40 +0200 Message-Id: <20200504110344.17560-2-eesposit@redhat.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200504110344.17560-1-eesposit@redhat.com> References: <20200504110344.17560-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Similar to the existing functions that take a mutex or spinlock if and only if a reference count is decremented to zero, these new function take an rwsem for writing just before the refcount reaches 0 (and call a user-provided function in the case of kref_put_rwsem). These will be used for stats_fs_source data structures, which are protected by an rw_semaphore to allow concurrent sysfs reads. Signed-off-by: Emanuele Giuseppe Esposito --- include/linux/kref.h | 11 +++++++++++ include/linux/refcount.h | 2 ++ lib/refcount.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/include/linux/kref.h b/include/linux/kref.h index d32e21a2538c..2dc935445f45 100644 --- a/include/linux/kref.h +++ b/include/linux/kref.h @@ -79,6 +79,17 @@ static inline int kref_put_mutex(struct kref *kref, return 0; } +static inline int kref_put_rwsem(struct kref *kref, + void (*release)(struct kref *kref), + struct rw_semaphore *rwsem) +{ + if (refcount_dec_and_down_write(&kref->refcount, rwsem)) { + release(kref); + return 1; + } + return 0; +} + static inline int kref_put_lock(struct kref *kref, void (*release)(struct kref *kref), spinlock_t *lock) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 0e3ee25eb156..a9d5038aec9a 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -99,6 +99,7 @@ #include struct mutex; +struct rw_semaphore; /** * struct refcount_t - variant of atomic_t specialized for reference counts @@ -313,6 +314,7 @@ static inline void refcount_dec(refcount_t *r) extern __must_check bool refcount_dec_if_one(refcount_t *r); extern __must_check bool refcount_dec_not_one(refcount_t *r); extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock); +extern __must_check bool refcount_dec_and_down_write(refcount_t *r, struct rw_semaphore *rwsem); extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock); extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r, spinlock_t *lock, diff --git a/lib/refcount.c b/lib/refcount.c index ebac8b7d15a7..03e113e1b43a 100644 --- a/lib/refcount.c +++ b/lib/refcount.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -94,6 +95,37 @@ bool refcount_dec_not_one(refcount_t *r) } EXPORT_SYMBOL(refcount_dec_not_one); +/** + * refcount_dec_and_down_write - return holding rwsem for writing if able to decrement + * refcount to 0 + * @r: the refcount + * @lock: the mutex to be locked + * + * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail + * to decrement when saturated at REFCOUNT_SATURATED. + * + * Provides release memory ordering, such that prior loads and stores are done + * before, and provides a control dependency such that free() must come after. + * See the comment on top. + * + * Return: true and hold rwsem for writing if able to decrement refcount to 0, false + * otherwise + */ +bool refcount_dec_and_down_write(refcount_t *r, struct rw_semaphore *lock) +{ + if (refcount_dec_not_one(r)) + return false; + + down_write(lock); + if (!refcount_dec_and_test(r)) { + up_write(lock); + return false; + } + + return true; +} +EXPORT_SYMBOL(refcount_dec_and_down_write); + /** * refcount_dec_and_mutex_lock - return holding mutex if able to decrement * refcount to 0 -- 2.25.2