From patchwork Wed Dec 22 23:16:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 527185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BAD0C433FE for ; Wed, 22 Dec 2021 23:16:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243137AbhLVXQ5 (ORCPT ); Wed, 22 Dec 2021 18:16:57 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:23212 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243129AbhLVXQ4 (ORCPT ); Wed, 22 Dec 2021 18:16:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1640215015; 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=RmTpHVG4w1DDSJVwbUXUDVRNW8n5nBor+VQEYFULplo=; b=CAQvwVbFmGASMHV/+NEX3Q1+OA3GCtgm+hdMZ/psm0EQMQbD/7mMzBn6WgmPJQw3IsLoMl Lszo5qzS8332fCHjjh/6Mw86KSxakg8TdlfN3FJqObGSuaTYk8BFPPdhJlIDeb9zAyKoze X6clTxGjzyR2MUsGkU4g5A5O/7Atqyw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-381-NwUgopGnNvytrQasUWrdNA-1; Wed, 22 Dec 2021 18:16:52 -0500 X-MC-Unique: NwUgopGnNvytrQasUWrdNA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3CFDE8042E1; Wed, 22 Dec 2021 23:16:50 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.165]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD95C798D1; Wed, 22 Dec 2021 23:16:40 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH v4 11/68] fscache: Implement cache-level access helpers From: David Howells To: linux-cachefs@redhat.com Cc: Jeff Layton , dhowells@redhat.com, Trond Myklebust , Anna Schumaker , Steve French , Dominique Martinet , Jeff Layton , Matthew Wilcox , Alexander Viro , Omar Sandoval , JeffleXu , Linus Torvalds , linux-afs@lists.infradead.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs-developer@lists.sourceforge.net, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 22 Dec 2021 23:16:39 +0000 Message-ID: <164021499995.640689.18286203753480287850.stgit@warthog.procyon.org.uk> In-Reply-To: <164021479106.640689.17404516570194656552.stgit@warthog.procyon.org.uk> References: <164021479106.640689.17404516570194656552.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.23 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org Add a pair of functions to pin/unpin a cache that we're wanting to do a high-level access to (such as creating or removing a volume): bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why); void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why); The way the access gate works/will work is: (1) If the cache tests as not live (state is not FSCACHE_CACHE_IS_ACTIVE), then we return false to indicate access was not permitted. (2) If the cache tests as live, then we increment the n_accesses count and then recheck the liveness, ending the access if it ceased to be live. (3) When we end the access, we decrement n_accesses and wake up the any waiters if it reaches 0. (4) Whilst the cache is caching, n_accesses is kept artificially incremented to prevent wakeups from happening. (5) When the cache is taken offline, the state is changed to prevent new accesses, n_accesses is decremented and we wait for n_accesses to become 0. Note that some of this is implemented in a later patch. Signed-off-by: David Howells Reviewed-by: Jeff Layton cc: linux-cachefs@redhat.com Link: https://lore.kernel.org/r/163819593239.215744.7537428720603638088.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/163906893368.143852.14164004598465617981.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/163967093977.1823006.6967886507023056409.stgit@warthog.procyon.org.uk/ # v3 --- fs/fscache/cache.c | 62 ++++++++++++++++++++++++++++++++++++++++ fs/fscache/internal.h | 2 + fs/fscache/main.c | 2 + include/trace/events/fscache.h | 41 ++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) diff --git a/fs/fscache/cache.c b/fs/fscache/cache.c index 8db77bb9f8e2..e867cff53a70 100644 --- a/fs/fscache/cache.c +++ b/fs/fscache/cache.c @@ -216,6 +216,68 @@ void fscache_relinquish_cache(struct fscache_cache *cache) } EXPORT_SYMBOL(fscache_relinquish_cache); +/** + * fscache_begin_cache_access - Pin a cache so it can be accessed + * @cache: The cache-level cookie + * @why: An indication of the circumstances of the access for tracing + * + * Attempt to pin the cache to prevent it from going away whilst we're + * accessing it and returns true if successful. This works as follows: + * + * (1) If the cache tests as not live (state is not FSCACHE_CACHE_IS_ACTIVE), + * then we return false to indicate access was not permitted. + * + * (2) If the cache tests as live, then we increment the n_accesses count and + * then recheck the liveness, ending the access if it ceased to be live. + * + * (3) When we end the access, we decrement n_accesses and wake up the any + * waiters if it reaches 0. + * + * (4) Whilst the cache is caching, n_accesses is kept artificially + * incremented to prevent wakeups from happening. + * + * (5) When the cache is taken offline, the state is changed to prevent new + * accesses, n_accesses is decremented and we wait for n_accesses to + * become 0. + */ +bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why) +{ + int n_accesses; + + if (!fscache_cache_is_live(cache)) + return false; + + n_accesses = atomic_inc_return(&cache->n_accesses); + smp_mb__after_atomic(); /* Reread live flag after n_accesses */ + trace_fscache_access_cache(cache->debug_id, refcount_read(&cache->ref), + n_accesses, why); + if (!fscache_cache_is_live(cache)) { + fscache_end_cache_access(cache, fscache_access_unlive); + return false; + } + return true; +} + +/** + * fscache_end_cache_access - Unpin a cache at the end of an access. + * @cache: The cache-level cookie + * @why: An indication of the circumstances of the access for tracing + * + * Unpin a cache after we've accessed it. The @why indicator is merely + * provided for tracing purposes. + */ +void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why) +{ + int n_accesses; + + smp_mb__before_atomic(); + n_accesses = atomic_dec_return(&cache->n_accesses); + trace_fscache_access_cache(cache->debug_id, refcount_read(&cache->ref), + n_accesses, why); + if (n_accesses == 0) + wake_up_var(&cache->n_accesses); +} + #ifdef CONFIG_PROC_FS static const char fscache_cache_states[NR__FSCACHE_CACHE_STATE] = "-PAEW"; diff --git a/fs/fscache/internal.h b/fs/fscache/internal.h index 71c897757d44..be29816b37ef 100644 --- a/fs/fscache/internal.h +++ b/fs/fscache/internal.h @@ -23,6 +23,8 @@ #ifdef CONFIG_PROC_FS extern const struct seq_operations fscache_caches_seq_ops; #endif +bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why); +void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why); struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache); void fscache_put_cache(struct fscache_cache *cache, enum fscache_cache_trace where); diff --git a/fs/fscache/main.c b/fs/fscache/main.c index ae493e9ca1c9..876f4bee5840 100644 --- a/fs/fscache/main.c +++ b/fs/fscache/main.c @@ -21,6 +21,8 @@ module_param_named(debug, fscache_debug, uint, MODULE_PARM_DESC(fscache_debug, "FS-Cache debugging mask"); +EXPORT_TRACEPOINT_SYMBOL(fscache_access_cache); + struct workqueue_struct *fscache_wq; EXPORT_SYMBOL(fscache_wq); diff --git a/include/trace/events/fscache.h b/include/trace/events/fscache.h index 9286e1c4b2ac..734966bc49e1 100644 --- a/include/trace/events/fscache.h +++ b/include/trace/events/fscache.h @@ -62,6 +62,12 @@ enum fscache_cookie_trace { fscache_cookie_see_work, }; +enum fscache_access_trace { + fscache_access_cache_pin, + fscache_access_cache_unpin, + fscache_access_unlive, +}; + #endif /* @@ -107,6 +113,11 @@ enum fscache_cookie_trace { EM(fscache_cookie_see_withdraw, "- x-wth") \ E_(fscache_cookie_see_work, "- work ") +#define fscache_access_traces \ + EM(fscache_access_cache_pin, "PIN cache ") \ + EM(fscache_access_cache_unpin, "UNPIN cache ") \ + E_(fscache_access_unlive, "END unlive ") + /* * Export enum symbols via userspace. */ @@ -118,6 +129,7 @@ enum fscache_cookie_trace { fscache_cache_traces; fscache_volume_traces; fscache_cookie_traces; +fscache_access_traces; /* * Now redefine the EM() and E_() macros to map the enums to the strings that @@ -204,6 +216,35 @@ TRACE_EVENT(fscache_cookie, __entry->ref) ); +TRACE_EVENT(fscache_access_cache, + TP_PROTO(unsigned int cache_debug_id, + int ref, + int n_accesses, + enum fscache_access_trace why), + + TP_ARGS(cache_debug_id, ref, n_accesses, why), + + TP_STRUCT__entry( + __field(unsigned int, cache ) + __field(int, ref ) + __field(int, n_accesses ) + __field(enum fscache_access_trace, why ) + ), + + TP_fast_assign( + __entry->cache = cache_debug_id; + __entry->ref = ref; + __entry->n_accesses = n_accesses; + __entry->why = why; + ), + + TP_printk("C=%08x %s r=%d a=%d", + __entry->cache, + __print_symbolic(__entry->why, fscache_access_traces), + __entry->ref, + __entry->n_accesses) + ); + TRACE_EVENT(fscache_acquire, TP_PROTO(struct fscache_cookie *cookie),