All of lore.kernel.org
 help / color / mirror / Atom feed
From: trondmy@kernel.org
To: linux-nfs@vger.kernel.org
Subject: [PATCH v6 05/13] NFS: Improve algorithm for falling back to uncached readdir
Date: Mon, 21 Feb 2022 11:08:43 -0500	[thread overview]
Message-ID: <20220221160851.15508-6-trondmy@kernel.org> (raw)
In-Reply-To: <20220221160851.15508-5-trondmy@kernel.org>

From: Trond Myklebust <trond.myklebust@hammerspace.com>

When reading a very large directory, we want to try to keep the page
cache up to date if doing so is inexpensive. Right now, we will try to
refill the page cache if it is non-empty, irrespective of whether or not
doing so is going to take a long time.

Replace that algorithm with something that looks at how many times we've
refilled the page cache without seeing a cache hit.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
---
 fs/nfs/dir.c           | 51 +++++++++++++++++++++---------------------
 include/linux/nfs_fs.h |  1 +
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index a7f25fef890a..d0707e63ce45 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -71,19 +71,16 @@ const struct address_space_operations nfs_dir_aops = {
 
 #define NFS_INIT_DTSIZE PAGE_SIZE
 
-static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir)
+static struct nfs_open_dir_context *
+alloc_nfs_open_dir_context(struct inode *dir)
 {
 	struct nfs_inode *nfsi = NFS_I(dir);
 	struct nfs_open_dir_context *ctx;
-	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
+
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
 	if (ctx != NULL) {
-		ctx->duped = 0;
 		ctx->attr_gencount = nfsi->attr_gencount;
-		ctx->dir_cookie = 0;
-		ctx->dup_cookie = 0;
-		ctx->page_index = 0;
 		ctx->dtsize = NFS_INIT_DTSIZE;
-		ctx->eof = false;
 		spin_lock(&dir->i_lock);
 		if (list_empty(&nfsi->open_files) &&
 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
@@ -170,6 +167,7 @@ struct nfs_readdir_descriptor {
 	unsigned long	timestamp;
 	unsigned long	gencount;
 	unsigned long	attr_gencount;
+	unsigned int	page_fill_misses;
 	unsigned int	cache_entry_index;
 	unsigned int	buffer_fills;
 	unsigned int	dtsize;
@@ -925,6 +923,18 @@ nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
 					   desc->last_cookie);
 }
 
+#define NFS_READDIR_PAGE_FILL_MISS_MAX 5
+/*
+ * If we've tried to refill the page cache more than 5 times, and
+ * still not found our cookie, then we should stop and fall back
+ * to uncached readdir
+ */
+static bool nfs_readdir_may_fill_pagecache(struct nfs_readdir_descriptor *desc)
+{
+	return desc->dir_cookie == 0 ||
+	       desc->page_fill_misses < NFS_READDIR_PAGE_FILL_MISS_MAX;
+}
+
 /*
  * Returns 0 if desc->dir_cookie was found on page desc->page_index
  * and locks the page to prevent removal from the page cache.
@@ -940,6 +950,8 @@ static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
 	if (!desc->page)
 		return -ENOMEM;
 	if (nfs_readdir_page_needs_filling(desc->page)) {
+		if (!nfs_readdir_may_fill_pagecache(desc))
+			return -EBADCOOKIE;
 		desc->page_index_max = desc->page_index;
 		res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
 					       &desc->page, 1);
@@ -958,36 +970,22 @@ static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
 		if (desc->page_index == 0)
 			memcpy(nfsi->cookieverf, verf,
 			       sizeof(nfsi->cookieverf));
+		desc->page_fill_misses++;
 	}
 	res = nfs_readdir_search_array(desc);
-	if (res == 0)
+	if (res == 0) {
+		desc->page_fill_misses = 0;
 		return 0;
+	}
 	nfs_readdir_page_unlock_and_put_cached(desc);
 	return res;
 }
 
-static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc)
-{
-	struct address_space *mapping = desc->file->f_mapping;
-	struct inode *dir = file_inode(desc->file);
-	unsigned int dtsize = NFS_SERVER(dir)->dtsize;
-	loff_t size = i_size_read(dir);
-
-	/*
-	 * Default to uncached readdir if the page cache is empty, and
-	 * we're looking for a non-zero cookie in a large directory.
-	 */
-	return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize;
-}
-
 /* Search for desc->dir_cookie from the beginning of the page cache */
 static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
 {
 	int res;
 
-	if (nfs_readdir_dont_search_cache(desc))
-		return -EBADCOOKIE;
-
 	do {
 		if (desc->page_index == 0) {
 			desc->current_index = 0;
@@ -1149,6 +1147,7 @@ static int nfs_readdir(struct file *file, struct dir_context *ctx)
 	page_index = dir_ctx->page_index;
 	desc->attr_gencount = dir_ctx->attr_gencount;
 	desc->eof = dir_ctx->eof;
+	desc->page_fill_misses = dir_ctx->page_fill_misses;
 	nfs_set_dtsize(desc, dir_ctx->dtsize);
 	memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
 	spin_unlock(&file->f_lock);
@@ -1204,6 +1203,7 @@ static int nfs_readdir(struct file *file, struct dir_context *ctx)
 	dir_ctx->duped = desc->duped;
 	dir_ctx->attr_gencount = desc->attr_gencount;
 	dir_ctx->page_index = desc->page_index;
+	dir_ctx->page_fill_misses = desc->page_fill_misses;
 	dir_ctx->eof = desc->eof;
 	dir_ctx->dtsize = desc->dtsize;
 	memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
@@ -1247,6 +1247,7 @@ static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
 			dir_ctx->dir_cookie = offset;
 		else
 			dir_ctx->dir_cookie = 0;
+		dir_ctx->page_fill_misses = 0;
 		if (offset == 0)
 			memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
 		dir_ctx->duped = 0;
diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
index d27f7e788624..3165927048e4 100644
--- a/include/linux/nfs_fs.h
+++ b/include/linux/nfs_fs.h
@@ -106,6 +106,7 @@ struct nfs_open_dir_context {
 	__u64 dir_cookie;
 	__u64 dup_cookie;
 	pgoff_t page_index;
+	unsigned int page_fill_misses;
 	unsigned int dtsize;
 	signed char duped;
 	bool eof;
-- 
2.35.1


  reply	other threads:[~2022-02-21 16:16 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21 16:08 [PATCH v6 00/13] Readdir improvements trondmy
2022-02-21 16:08 ` [PATCH v6 01/13] NFS: constify nfs_server_capable() and nfs_have_writebacks() trondmy
2022-02-21 16:08   ` [PATCH v6 02/13] NFS: Trace lookup revalidation failure trondmy
2022-02-21 16:08     ` [PATCH v6 03/13] NFS: Adjust the amount of readahead performed by NFS readdir trondmy
2022-02-21 16:08       ` [PATCH v6 04/13] NFS: Simplify nfs_readdir_xdr_to_array() trondmy
2022-02-21 16:08         ` trondmy [this message]
2022-02-21 16:08           ` [PATCH v6 06/13] NFS: Improve heuristic for readdirplus trondmy
2022-02-21 16:08             ` [PATCH v6 07/13] NFS: Don't ask for readdirplus unless it can help nfs_getattr() trondmy
2022-02-21 16:08               ` [PATCH v6 08/13] NFSv4: Ask for a full XDR buffer of readdir goodness trondmy
2022-02-21 16:08                 ` [PATCH v6 09/13] NFS: Readdirplus can't help lookup for case insensitive filesystems trondmy
2022-02-21 16:08                   ` [PATCH v6 10/13] NFS: Don't request readdirplus when revaldation was forced trondmy
2022-02-21 16:08                     ` [PATCH v6 11/13] NFS: Add basic readdir tracing trondmy
2022-02-21 16:08                       ` [PATCH v6 12/13] NFS: Trace effects of readdirplus on the dcache trondmy
2022-02-21 16:08                         ` [PATCH v6 13/13] NFS: Trace effects of the readdirplus heuristic trondmy
2022-02-23 13:40                           ` [PATCH v3 1/8] NFS: save the directory's change attribute on pagecache pages Benjamin Coddington
2022-02-23 13:40                             ` [PATCH v3 2/8] NFSv4: Send GETATTR with READDIR Benjamin Coddington
2022-02-23 13:40                               ` [PATCH v3 3/8] NFS: Add a struct to track readdir pagecache location Benjamin Coddington
2022-02-23 13:40                                 ` [PATCH v3 4/8] NFS: Keep the readdir pagecache cursor updated Benjamin Coddington
2022-02-23 13:40                                   ` [PATCH v3 5/8] NFS: readdir per-page cache validation Benjamin Coddington
2022-02-23 13:40                                     ` [PATCH v3 6/8] NFS: stash the readdir pagecache cursor on the open directory context Benjamin Coddington
2022-02-23 13:40                                       ` [PATCH v3 7/8] NFS: Support headless readdir pagecache pages Benjamin Coddington
2022-02-23 13:40                                         ` [PATCH v3 8/8] NFS: Revalidate the directory pagecache on every nfs_readdir() Benjamin Coddington
2022-02-21 16:45           ` [PATCH v6 05/13] NFS: Improve algorithm for falling back to uncached readdir Benjamin Coddington
2022-02-21 19:58             ` Trond Myklebust
2022-02-21 20:22               ` Benjamin Coddington
2022-02-21 20:55                 ` Trond Myklebust
2022-02-21 21:10                   ` Benjamin Coddington
2022-02-21 23:20                     ` Trond Myklebust
2022-02-22 12:50                       ` Benjamin Coddington
2022-02-22 20:11                         ` Trond Myklebust
2022-02-22 20:21                           ` Benjamin Coddington
2022-02-23 12:17                             ` Trond Myklebust
2022-02-23 13:34                               ` Benjamin Coddington
2022-02-23 21:31                                 ` Trond Myklebust

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220221160851.15508-6-trondmy@kernel.org \
    --to=trondmy@kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.