linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xfs: fix overflows when converting from a count of groups to blocks
@ 2025-10-27 14:04 Christoph Hellwig
  2025-10-27 16:07 ` Darrick J. Wong
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2025-10-27 14:04 UTC (permalink / raw)
  To: cem; +Cc: hans.holmberg, linux-xfs

Add a xfs_group helper and a xfs_rtgroup wrapper and use that to avoid
overflows when converting zone/rtg counts to block counts.

Fixes: 0bb2193056b5 ("xfs: add support for zoned space reservations")
Fixes: 080d01c41d44 ("xfs: implement zoned garbage collection")
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/xfs/libxfs/xfs_group.h    | 9 +++++++++
 fs/xfs/libxfs/xfs_rtgroup.h  | 8 ++++++++
 fs/xfs/xfs_zone_gc.c         | 3 +--
 fs/xfs/xfs_zone_space_resv.c | 8 +++-----
 4 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_group.h b/fs/xfs/libxfs/xfs_group.h
index 4423932a2313..a6eabe6da4fb 100644
--- a/fs/xfs/libxfs/xfs_group.h
+++ b/fs/xfs/libxfs/xfs_group.h
@@ -98,6 +98,15 @@ xfs_group_max_blocks(
 	return xg->xg_mount->m_groups[xg->xg_type].blocks;
 }
 
+static inline xfs_rfsblock_t
+xfs_groups_to_fsb(
+	struct xfs_mount	*mp,
+	uint32_t		nr_groups,
+	enum xfs_group_type	type)
+{
+	return (xfs_rfsblock_t)mp->m_groups[type].blocks * nr_groups;
+}
+
 static inline xfs_fsblock_t
 xfs_group_start_fsb(
 	struct xfs_group	*xg)
diff --git a/fs/xfs/libxfs/xfs_rtgroup.h b/fs/xfs/libxfs/xfs_rtgroup.h
index d36a6ae0abe5..a34da969bb6b 100644
--- a/fs/xfs/libxfs/xfs_rtgroup.h
+++ b/fs/xfs/libxfs/xfs_rtgroup.h
@@ -365,4 +365,12 @@ static inline int xfs_initialize_rtgroups(struct xfs_mount *mp,
 # define xfs_rtgroup_get_geometry(rtg, rgeo)	(-EOPNOTSUPP)
 #endif /* CONFIG_XFS_RT */
 
+static inline xfs_rfsblock_t
+xfs_rtgs_to_rtb(
+	struct xfs_mount	*mp,
+	uint32_t		nr_groups)
+{
+	return xfs_groups_to_fsb(mp, nr_groups, XG_TYPE_RTG);
+}
+
 #endif /* __LIBXFS_RTGROUP_H */
diff --git a/fs/xfs/xfs_zone_gc.c b/fs/xfs/xfs_zone_gc.c
index 109877d9a6bf..c43513531200 100644
--- a/fs/xfs/xfs_zone_gc.c
+++ b/fs/xfs/xfs_zone_gc.c
@@ -179,8 +179,7 @@ xfs_zoned_need_gc(
 	available = xfs_estimate_freecounter(mp, XC_FREE_RTAVAILABLE);
 
 	if (available <
-	    mp->m_groups[XG_TYPE_RTG].blocks *
-	    (mp->m_max_open_zones - XFS_OPEN_GC_ZONES))
+	    xfs_rtgs_to_rtb(mp, mp->m_max_open_zones - XFS_OPEN_GC_ZONES))
 		return true;
 
 	free = xfs_estimate_freecounter(mp, XC_FREE_RTEXTENTS);
diff --git a/fs/xfs/xfs_zone_space_resv.c b/fs/xfs/xfs_zone_space_resv.c
index 9cd38716fd25..3a1a363fc8ea 100644
--- a/fs/xfs/xfs_zone_space_resv.c
+++ b/fs/xfs/xfs_zone_space_resv.c
@@ -54,12 +54,10 @@ xfs_zoned_default_resblks(
 {
 	switch (ctr) {
 	case XC_FREE_RTEXTENTS:
-		return (uint64_t)XFS_RESERVED_ZONES *
-			mp->m_groups[XG_TYPE_RTG].blocks +
-			mp->m_sb.sb_rtreserved;
+		return xfs_rtgs_to_rtb(mp, XFS_RESERVED_ZONES) +
+				mp->m_sb.sb_rtreserved;
 	case XC_FREE_RTAVAILABLE:
-		return (uint64_t)XFS_GC_ZONES *
-			mp->m_groups[XG_TYPE_RTG].blocks;
+		return xfs_rtgs_to_rtb(mp, XFS_GC_ZONES);
 	default:
 		ASSERT(0);
 		return 0;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] xfs: fix overflows when converting from a count of groups to blocks
  2025-10-27 14:04 [PATCH] xfs: fix overflows when converting from a count of groups to blocks Christoph Hellwig
@ 2025-10-27 16:07 ` Darrick J. Wong
  2025-10-30  6:54   ` Christoph Hellwig
  0 siblings, 1 reply; 4+ messages in thread
From: Darrick J. Wong @ 2025-10-27 16:07 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: cem, hans.holmberg, linux-xfs

On Mon, Oct 27, 2025 at 03:04:39PM +0100, Christoph Hellwig wrote:
> Add a xfs_group helper and a xfs_rtgroup wrapper and use that to avoid
> overflows when converting zone/rtg counts to block counts.
> 
> Fixes: 0bb2193056b5 ("xfs: add support for zoned space reservations")
> Fixes: 080d01c41d44 ("xfs: implement zoned garbage collection")

Cc: <stable@vger.kernel.org> # v6.15

> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  fs/xfs/libxfs/xfs_group.h    | 9 +++++++++
>  fs/xfs/libxfs/xfs_rtgroup.h  | 8 ++++++++
>  fs/xfs/xfs_zone_gc.c         | 3 +--
>  fs/xfs/xfs_zone_space_resv.c | 8 +++-----
>  4 files changed, 21 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/xfs/libxfs/xfs_group.h b/fs/xfs/libxfs/xfs_group.h
> index 4423932a2313..a6eabe6da4fb 100644
> --- a/fs/xfs/libxfs/xfs_group.h
> +++ b/fs/xfs/libxfs/xfs_group.h
> @@ -98,6 +98,15 @@ xfs_group_max_blocks(
>  	return xg->xg_mount->m_groups[xg->xg_type].blocks;
>  }
>  
> +static inline xfs_rfsblock_t
> +xfs_groups_to_fsb(

Minor nitpicking: should these functions that return a raw fsblock count
(xfs_rfsblock_t) have a slightly different name?

xfs_groups_to_rfsb() ?

Just so that someone doesn't think the return value is a segmented
fsblock address based on a misreading of the function name...

> +	struct xfs_mount	*mp,
> +	uint32_t		nr_groups,
> +	enum xfs_group_type	type)
> +{
> +	return (xfs_rfsblock_t)mp->m_groups[type].blocks * nr_groups;
> +}
> +
>  static inline xfs_fsblock_t
>  xfs_group_start_fsb(
>  	struct xfs_group	*xg)
> diff --git a/fs/xfs/libxfs/xfs_rtgroup.h b/fs/xfs/libxfs/xfs_rtgroup.h
> index d36a6ae0abe5..a34da969bb6b 100644
> --- a/fs/xfs/libxfs/xfs_rtgroup.h
> +++ b/fs/xfs/libxfs/xfs_rtgroup.h
> @@ -365,4 +365,12 @@ static inline int xfs_initialize_rtgroups(struct xfs_mount *mp,
>  # define xfs_rtgroup_get_geometry(rtg, rgeo)	(-EOPNOTSUPP)
>  #endif /* CONFIG_XFS_RT */
>  
> +static inline xfs_rfsblock_t
> +xfs_rtgs_to_rtb(

...especially since we're really not returning an rtblock here.

The additional casting is correct though.

--D

> +	struct xfs_mount	*mp,
> +	uint32_t		nr_groups)
> +{
> +	return xfs_groups_to_fsb(mp, nr_groups, XG_TYPE_RTG);
> +}
> +
>  #endif /* __LIBXFS_RTGROUP_H */
> diff --git a/fs/xfs/xfs_zone_gc.c b/fs/xfs/xfs_zone_gc.c
> index 109877d9a6bf..c43513531200 100644
> --- a/fs/xfs/xfs_zone_gc.c
> +++ b/fs/xfs/xfs_zone_gc.c
> @@ -179,8 +179,7 @@ xfs_zoned_need_gc(
>  	available = xfs_estimate_freecounter(mp, XC_FREE_RTAVAILABLE);
>  
>  	if (available <
> -	    mp->m_groups[XG_TYPE_RTG].blocks *
> -	    (mp->m_max_open_zones - XFS_OPEN_GC_ZONES))
> +	    xfs_rtgs_to_rtb(mp, mp->m_max_open_zones - XFS_OPEN_GC_ZONES))
>  		return true;
>  
>  	free = xfs_estimate_freecounter(mp, XC_FREE_RTEXTENTS);
> diff --git a/fs/xfs/xfs_zone_space_resv.c b/fs/xfs/xfs_zone_space_resv.c
> index 9cd38716fd25..3a1a363fc8ea 100644
> --- a/fs/xfs/xfs_zone_space_resv.c
> +++ b/fs/xfs/xfs_zone_space_resv.c
> @@ -54,12 +54,10 @@ xfs_zoned_default_resblks(
>  {
>  	switch (ctr) {
>  	case XC_FREE_RTEXTENTS:
> -		return (uint64_t)XFS_RESERVED_ZONES *
> -			mp->m_groups[XG_TYPE_RTG].blocks +
> -			mp->m_sb.sb_rtreserved;
> +		return xfs_rtgs_to_rtb(mp, XFS_RESERVED_ZONES) +
> +				mp->m_sb.sb_rtreserved;
>  	case XC_FREE_RTAVAILABLE:
> -		return (uint64_t)XFS_GC_ZONES *
> -			mp->m_groups[XG_TYPE_RTG].blocks;
> +		return xfs_rtgs_to_rtb(mp, XFS_GC_ZONES);
>  	default:
>  		ASSERT(0);
>  		return 0;
> -- 
> 2.47.3
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xfs: fix overflows when converting from a count of groups to blocks
  2025-10-27 16:07 ` Darrick J. Wong
@ 2025-10-30  6:54   ` Christoph Hellwig
  2025-10-30 16:32     ` Darrick J. Wong
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2025-10-30  6:54 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: Christoph Hellwig, cem, hans.holmberg, linux-xfs

On Mon, Oct 27, 2025 at 09:07:26AM -0700, Darrick J. Wong wrote:
> >  
> > +static inline xfs_rfsblock_t
> > +xfs_rtgs_to_rtb(
> 
> ...especially since we're really not returning an rtblock here.

What would be a good name here?

xfs_rtgs_to_rfsb() ?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xfs: fix overflows when converting from a count of groups to blocks
  2025-10-30  6:54   ` Christoph Hellwig
@ 2025-10-30 16:32     ` Darrick J. Wong
  0 siblings, 0 replies; 4+ messages in thread
From: Darrick J. Wong @ 2025-10-30 16:32 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: cem, hans.holmberg, linux-xfs

On Thu, Oct 30, 2025 at 07:54:39AM +0100, Christoph Hellwig wrote:
> On Mon, Oct 27, 2025 at 09:07:26AM -0700, Darrick J. Wong wrote:
> > >  
> > > +static inline xfs_rfsblock_t
> > > +xfs_rtgs_to_rtb(
> > 
> > ...especially since we're really not returning an rtblock here.
> 
> What would be a good name here?
> 
> xfs_rtgs_to_rfsb() ?

xfs_rtgs_to_rfsbs?

(rtg[roups]s is plural, so rfsb[lock]s should be too)

[this is all bs, yuk yuk, I'll be at this club all week :P]

--D

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-10-30 16:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-27 14:04 [PATCH] xfs: fix overflows when converting from a count of groups to blocks Christoph Hellwig
2025-10-27 16:07 ` Darrick J. Wong
2025-10-30  6:54   ` Christoph Hellwig
2025-10-30 16:32     ` Darrick J. Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).