public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: Dave Chinner <david@fromorbit.com>
Cc: linux-xfs@vger.kernel.org, hch@lst.de
Subject: Re: [PATCH 04/10] xfs: replace shouty XFS_BM{BT,DR} macros
Date: Thu, 29 Aug 2024 15:10:29 -0700	[thread overview]
Message-ID: <20240829221029.GR6224@frogsfrogsfrogs> (raw)
In-Reply-To: <Zs/WSw6fm4SyyyW4@dread.disaster.area>

On Thu, Aug 29, 2024 at 12:00:43PM +1000, Dave Chinner wrote:
> On Tue, Aug 27, 2024 at 04:34:45PM -0700, Darrick J. Wong wrote:
> > From: Darrick J. Wong <djwong@kernel.org>
> > 
> > Replace all the shouty bmap btree and bmap disk root macros with actual
> > functions, and fix a type handling error in the xattr code that the
> > macros previously didn't care about.
> 
> I don't see a type handling fix in the xattr code in the patch.
> 
> If there is one, can you please split it out to make it obvious?

No, I think that's just debris from an old iteration of this years-old
patch.  The xattr fixes got merged in the xattr state machine series
that Allison merged a long time ago.

> ....
> 
> > -#define XFS_BMDR_REC_ADDR(block, index) \
> > -	((xfs_bmdr_rec_t *) \
> > -		((char *)(block) + \
> > -		 sizeof(struct xfs_bmdr_block) + \
> > -	         ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
> 
> > +
> > +static inline struct xfs_bmbt_rec *
> > +xfs_bmdr_rec_addr(
> > +	struct xfs_bmdr_block	*block,
> > +	unsigned int		index)
> > +{
> > +	return (struct xfs_bmbt_rec *)
> > +		((char *)(block + 1) +
> > +		 (index - 1) * sizeof(struct xfs_bmbt_rec));
> > +}
> 
> There's a logic change in these BMDR conversions - why does the new
> version use (block + 1) and the old one use a (block + sizeof())
> calculation?

Pointer arithmetic works great inside C functions where you know the
type of the operand.  That's why I used it.

That doesn't work at all in a macro where someone could pass in any
random pointer and then the math is wrong.  That's why the macro casts
to a byte-sized pointer type and then adds exactly the sizeof() bytes,
or at least I assume that's the motivation of the authors.

> I *think* they are equivalent, but now as I read the code I have to
> think about casts and pointer arithmetic and work out what structure
> we are taking the size of in my head rather than it being straight
> forward and obvious from the code.

The function argument declaration is four lines up.

> It doesn't change the code that is generated, so I think that the
> existing "+ sizeof()" variants is better than this mechanism because
> everyone is familiar with the existing definitions....

I disagree.  With your change, to validate this function, everyone must
to check that the argument type matches the sizeof argument to confirm
that the pointer arithmetic is correct.

static inline struct xfs_bmbt_rec *
xfs_bmdr_rec_addr(
	struct xfs_bmdr_block	*block,
	unsigned int		index)
{
	return (struct xfs_bmbt_rec *)
		((char *)(block) +
		 sizeof(struct xfs_btree_block) +
		 (index - 1) * sizeof(struct xfs_bmbt_rec));
}

Oops, this function is broken, when we could have trusted the compiler
to get the types and the math correct for us.

> > +static inline struct xfs_bmbt_key *
> > +xfs_bmdr_key_addr(
> > +	struct xfs_bmdr_block	*block,
> > +	unsigned int		index)
> > +{
> > +	return (struct xfs_bmbt_key *)
> > +		((char *)(block + 1) +
> > +		 (index - 1) * sizeof(struct xfs_bmbt_key));
> > +}
> > +
> > +static inline xfs_bmbt_ptr_t *
> > +xfs_bmdr_ptr_addr(
> > +	struct xfs_bmdr_block	*block,
> > +	unsigned int		index,
> > +	unsigned int		maxrecs)
> > +{
> > +	return (xfs_bmbt_ptr_t *)
> > +		((char *)(block + 1) +
> > +		 maxrecs * sizeof(struct xfs_bmbt_key) +
> > +		 (index - 1) * sizeof(xfs_bmbt_ptr_t));
> > +}
> 
> Same for these.
> 
> > +/*
> > + * Compute the space required for the incore btree root containing the given
> > + * number of records.
> > + */
> > +static inline size_t
> > +xfs_bmap_broot_space_calc(
> > +	struct xfs_mount	*mp,
> > +	unsigned int		nrecs)
> > +{
> > +	return xfs_bmbt_block_len(mp) + \
> > +	       (nrecs * (sizeof(struct xfs_bmbt_key) + sizeof(xfs_bmbt_ptr_t)));
> > +}
> 
> stray '\' remains in that conversion.

Will fix, thanks.

--D

> -Dave.
> -- 
> Dave Chinner
> david@fromorbit.com
> 

  reply	other threads:[~2024-08-29 22:10 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-27 23:33 [PATCHSET v4.1] xfs: cleanups for inode rooted btree code Darrick J. Wong
2024-08-27 23:33 ` [PATCH 01/10] xfs: fix C++ compilation errors in xfs_fs.h Darrick J. Wong
2024-08-28  4:09   ` Christoph Hellwig
2024-08-29  1:29   ` Dave Chinner
2024-09-02 15:20   ` Carlos Maiolino
2024-08-27 23:34 ` [PATCH 02/10] xfs: fix FITRIM reporting again Darrick J. Wong
2024-08-28  4:10   ` Christoph Hellwig
2024-08-27 23:34 ` [PATCH 03/10] xfs: fix a sloppy memory handling bug in xfs_iroot_realloc Darrick J. Wong
2024-08-28  4:10   ` Christoph Hellwig
2024-08-27 23:34 ` [PATCH 04/10] xfs: replace shouty XFS_BM{BT,DR} macros Darrick J. Wong
2024-08-28  4:11   ` Christoph Hellwig
2024-08-29  2:00   ` Dave Chinner
2024-08-29 22:10     ` Darrick J. Wong [this message]
2024-08-30  3:43       ` Christoph Hellwig
2024-08-27 23:35 ` [PATCH 05/10] xfs: move the zero records logic into xfs_bmap_broot_space_calc Darrick J. Wong
2024-08-28  4:14   ` Christoph Hellwig
2024-08-29  1:15     ` Darrick J. Wong
2024-08-29  3:51       ` Christoph Hellwig
2024-08-29  2:05     ` Dave Chinner
2024-08-29 22:34       ` Darrick J. Wong
2024-08-27 23:35 ` [PATCH 06/10] xfs: refactor the allocation and freeing of incore inode fork btree roots Darrick J. Wong
2024-08-28  4:15   ` Christoph Hellwig
2024-08-28  4:17   ` Christoph Hellwig
2024-08-28 21:50     ` Darrick J. Wong
2024-08-27 23:35 ` [PATCH 07/10] xfs: refactor creation of bmap " Darrick J. Wong
2024-08-28  4:19   ` Christoph Hellwig
2024-08-29  2:13   ` Dave Chinner
2024-08-29 22:46     ` Darrick J. Wong
2024-08-27 23:35 ` [PATCH 08/10] xfs: hoist the code that moves the incore inode fork broot memory Darrick J. Wong
2024-08-28  4:20   ` Christoph Hellwig
2024-08-29  2:54   ` Dave Chinner
2024-08-29 23:35     ` Darrick J. Wong
2024-08-27 23:36 ` [PATCH 09/10] xfs: rearrange xfs_iroot_realloc a bit Darrick J. Wong
2024-08-28  4:21   ` Christoph Hellwig
2024-08-27 23:36 ` [PATCH 10/10] xfs: standardize the btree maxrecs function parameters Darrick J. Wong
2024-08-28  4:21   ` Christoph Hellwig
2024-08-27 23:45 ` [RFC PATCH] libxfs: compile with a C++ compiler Darrick J. Wong
2024-08-28  0:01   ` Sam James
2024-08-28  0:10     ` Sam James
2024-08-28 23:53     ` Darrick J. Wong
2024-08-29  0:17       ` Sam James
2024-08-29  1:30         ` Kees Cook
2024-08-28  4:25   ` Christoph Hellwig

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=20240829221029.GR6224@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=david@fromorbit.com \
    --cc=hch@lst.de \
    --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