From: "Darrick J. Wong" <djwong@kernel.org>
To: cem@kernel.org
Cc: linux-xfs@vger.kernel.org
Subject: Re: [PATCH 12/14] xfs: don't allow sorting sparse arrays
Date: Tue, 22 Sep 2026 11:07:42 -0700 [thread overview]
Message-ID: <20260922180742.GY2705364@frogsfrogsfrogs> (raw)
In-Reply-To: <178996120807.181988.9510302652927544095.stgit@frogsfrogsfrogs>
On Sun, Sep 20, 2026 at 11:18:06PM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <djwong@kernel.org>
>
> Now that we've reduced the functionality of xfarray_unset, let's add a
> new safeguard: no sorting of xfarrays with sparse holes in them. It's
> not clear what that even means, and nobody actually does this, so we're
> really just eliminating subtle logic bombs.
>
> Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
> ---
> fs/xfs/scrub/xfarray.h | 3 +++
> fs/xfs/scrub/xfarray.c | 11 +++++++++++
> 2 files changed, 14 insertions(+)
>
>
> diff --git a/fs/xfs/scrub/xfarray.h b/fs/xfs/scrub/xfarray.h
> index d55225c7885b25..05ff65b09fcf41 100644
> --- a/fs/xfs/scrub/xfarray.h
> +++ b/fs/xfs/scrub/xfarray.h
> @@ -32,6 +32,9 @@ struct xfarray {
>
> /* log2 of array element size, if possible. */
> int obj_size_log;
> +
> + /* Might there be sparse holes in this array? */
> + bool possibly_sparse;
> };
>
> int xfarray_create(const char *descr, unsigned long long required_capacity,
> diff --git a/fs/xfs/scrub/xfarray.c b/fs/xfs/scrub/xfarray.c
> index ee3f0bf87433f1..78274ad53097e1 100644
> --- a/fs/xfs/scrub/xfarray.c
> +++ b/fs/xfs/scrub/xfarray.c
> @@ -193,6 +193,8 @@ xfarray_store(
> if (ret)
> return ret;
>
> + if (idx > array->nr)
> + array->possibly_sparse = true;
> array->nr = max(array->nr, idx + 1);
> return 0;
> }
> @@ -844,6 +846,14 @@ xfarray_sort(
> return 0;
> if (array->nr >= QSORT_MAX_RECS)
> return -E2BIG;
> + if (array->possibly_sparse) {
> + /*
> + * What does it mean to sort an array with holes in it?
> + * Currently none of the users need this ability.
> + */
> + ASSERT(array->possibly_sparse);
Shashiko says:
"Will this assertion always evaluate to true?
Since it is located inside the if (array->possibly_sparse) block, it acts
as a no-op instead of catching sparse array sorting in debug builds. Should
this be ASSERT(!array->possibly_sparse) before the condition, or ASSERT(0)
inside it?"
Yes. The logic is backwards. Will fix.
> + return -EINVAL;
> + }
>
> error = xfarray_sortinfo_alloc(array, cmp_fn, flags, &si);
> if (error)
> @@ -997,4 +1007,5 @@ xfarray_truncate(
> {
> xfile_discard(array->xfile, 0, MAX_LFS_FILESIZE);
> array->nr = 0;
> + array->possibly_sparse = false;
> }
"Since the new possibly_sparse flag is cleared here when the array is
truncated to 0 elements, does xfarray_trim() need a similar update?
Looking at fs/xfs/scrub/xfarray.c:xfarray_trim(), it can also reduce
the array to 0 elements:
int
xfarray_trim(
struct xfarray *array,
unsigned long long nr)
{
if (nr > array->nr)
return -ENODATA;
array->nr -= nr;
xfile_discard(array->xfile, xfarray_pos(array, array->nr),
MAX_LFS_FILESIZE);
return 0;
}
If an array is sparse, completely trimmed to 0 elements via
xfarray_trim(array, array->nr), and then populated densely, it appears
the possibly_sparse flag would remain true. Could this cause future
calls to xfarray_sort() on the dense array to fail with a spurious
-EINVAL?"
Yes, the trim function needs it too. Nobody does that, but I agree not
to leave a logic bomb.
--D
next prev parent reply other threads:[~2026-09-22 18:07 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-21 6:13 [PATCHSET] xfs: LLM-inspired bug fixes, part 16 Darrick J. Wong
2026-09-21 6:15 ` [PATCH 01/14] xfs: fix missing xfs_qm_adjust_dqlimits call in quotacheck repair Darrick J. Wong
2026-09-22 5:14 ` Christoph Hellwig
2026-09-21 6:15 ` [PATCH 02/14] xfs: online quotacheck must dirty dquot if enforcement adjustments needed Darrick J. Wong
2026-09-22 5:14 ` Christoph Hellwig
2026-09-22 18:02 ` Darrick J. Wong
2026-09-21 6:15 ` [PATCH 03/14] xfs: fix buffer overruns in xfs_ioc_attr_list Darrick J. Wong
2026-09-22 5:15 ` Christoph Hellwig
2026-09-22 17:31 ` Darrick J. Wong
2026-09-21 6:16 ` [PATCH 04/14] xfs: clean up after failed metafile relinking Darrick J. Wong
2026-09-22 5:17 ` Christoph Hellwig
2026-09-22 17:33 ` Darrick J. Wong
2026-09-21 6:16 ` [PATCH 05/14] xfs: pass xfs_trans_resv object to reservation calculation helpers Darrick J. Wong
2026-09-22 5:18 ` Christoph Hellwig
2026-09-22 17:29 ` Darrick J. Wong
2026-09-22 17:34 ` Darrick J. Wong
2026-09-21 6:16 ` [PATCH 06/14] xfs: fix xfs_rename_space_res for non-pptr filesystems Darrick J. Wong
2026-09-22 5:20 ` Christoph Hellwig
2026-09-22 17:36 ` Darrick J. Wong
2026-09-21 6:16 ` [PATCH 07/14] xfs: fix ondisk symlink target validation in xrep_dinode_check_dfork Darrick J. Wong
2026-09-22 5:20 ` Christoph Hellwig
2026-09-22 20:42 ` Dave Chinner
2026-09-21 6:17 ` [PATCH 08/14] xfs: fix maximum atomic cow length computation Darrick J. Wong
2026-09-22 5:21 ` Christoph Hellwig
2026-09-22 6:53 ` Darrick J. Wong
2026-09-21 6:17 ` [PATCH 09/14] xfs: add missing healthmon trace strings Darrick J. Wong
2026-09-22 5:21 ` Christoph Hellwig
2026-09-21 6:17 ` [PATCH 10/14] xfarray: don't crash when sorting if array element crosses a folio Darrick J. Wong
2026-09-22 5:22 ` Christoph Hellwig
2026-09-22 17:53 ` Darrick J. Wong
2026-09-23 4:39 ` Christoph Hellwig
2026-09-21 6:17 ` [PATCH 11/14] xfarray: don't allow users to unset in the middle of an array Darrick J. Wong
2026-09-22 5:23 ` Christoph Hellwig
2026-09-22 6:47 ` Darrick J. Wong
2026-09-22 18:14 ` Darrick J. Wong
2026-09-21 6:18 ` [PATCH 12/14] xfs: don't allow sorting sparse arrays Darrick J. Wong
2026-09-22 5:24 ` Christoph Hellwig
2026-09-22 18:07 ` Darrick J. Wong [this message]
2026-09-21 6:18 ` [PATCH 13/14] xfs: simply the free space btree repair code Darrick J. Wong
2026-09-22 5:24 ` Christoph Hellwig
2026-09-21 6:18 ` [PATCH 14/14] xfarray: warn against sorting arrays with identical elements Darrick J. Wong
2026-09-22 5:25 ` Christoph Hellwig
2026-09-22 18:10 ` Darrick J. Wong
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=20260922180742.GY2705364@frogsfrogsfrogs \
--to=djwong@kernel.org \
--cc=cem@kernel.org \
--cc=linux-xfs@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox