All of lore.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: linux-fsdevel@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH][RFC] move close_range(2) into fs/file.c, fold __close_range() into it
Date: Sun, 2 Jun 2024 22:58:03 +0100	[thread overview]
Message-ID: <20240602215803.GE1629371@ZenIV> (raw)
In-Reply-To: <20240602204238.GD1629371@ZenIV>

On Sun, Jun 02, 2024 at 09:42:38PM +0100, Al Viro wrote:
> 	We never had callers for __close_range() except for close_range(2)
> itself.  Nothing of that sort has appeared in four years and if any users
> do show up, we can always separate those suckers again.

BTW, looking through close_range()...  We have

static unsigned int sane_fdtable_size(struct fdtable *fdt, unsigned int max_fds)
{
	unsigned int count;

	count = count_open_files(fdt);
	if (max_fds < NR_OPEN_DEFAULT)
		max_fds = NR_OPEN_DEFAULT;
	return ALIGN(min(count, max_fds), BITS_PER_LONG);
}

which decides how large a table would be needed for descriptors below max_fds
that are opened in fdt.  And we start with finding the last opened descriptor
in fdt (well, rounded up to BITS_PER_LONG, if you look at count_open_files()).

Why do we bother to look at _anything_ past the max_fds?  Does anybody have
objections to the following?

diff --git a/fs/file.c b/fs/file.c
index f9fcebc7c838..4976ede108e0 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -276,20 +276,6 @@ static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt)
 	return test_bit(fd, fdt->open_fds);
 }
 
-static unsigned int count_open_files(struct fdtable *fdt)
-{
-	unsigned int size = fdt->max_fds;
-	unsigned int i;
-
-	/* Find the last open fd */
-	for (i = size / BITS_PER_LONG; i > 0; ) {
-		if (fdt->open_fds[--i])
-			break;
-	}
-	i = (i + 1) * BITS_PER_LONG;
-	return i;
-}
-
 /*
  * Note that a sane fdtable size always has to be a multiple of
  * BITS_PER_LONG, since we have bitmaps that are sized by this.
@@ -305,12 +291,18 @@ static unsigned int count_open_files(struct fdtable *fdt)
  */
 static unsigned int sane_fdtable_size(struct fdtable *fdt, unsigned int max_fds)
 {
-	unsigned int count;
+	const unsigned int min_words = BITS_TO_LONGS(NR_OPEN_DEFAULT);  // 1
+	unsigned int words;
+
+	if (max_fds <= NR_OPEN_DEFAULT)
+		return NR_OPEN_DEFAULT;
+
+	words = BITS_TO_LONGS(min(max_fds, fdt->max_fds)); // >= min_words
+
+	while (words > min_words && !fdt->open_fds[words - 1])
+		words--;
 
-	count = count_open_files(fdt);
-	if (max_fds < NR_OPEN_DEFAULT)
-		max_fds = NR_OPEN_DEFAULT;
-	return ALIGN(min(count, max_fds), BITS_PER_LONG);
+	return words * BITS_PER_LONG;
 }
 
 /*

  reply	other threads:[~2024-06-02 21:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-02 20:42 [PATCH][RFC] move close_range(2) into fs/file.c, fold __close_range() into it Al Viro
2024-06-02 21:58 ` Al Viro [this message]
2024-06-03 13:59   ` Christian Brauner
2024-06-03 13:56 ` Christian Brauner

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=20240602215803.GE1629371@ZenIV \
    --to=viro@zeniv.linux.org.uk \
    --cc=brauner@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=torvalds@linux-foundation.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.