Skip to contentSkip to conversation

xfs: prevent 32bit overflow in space reservation

PATCH In review Dave Chinner 3 participants 5 messages Sep 2, 2010 – Sep 3, 2010 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: prevent 32bit overflow in space reservation

From: Dave Chinner <[email protected]>

If we attempt to preallocate more than 2^32 blocks of space in a single syscall, the transaction block reservation will overflow leading to a hangs in the superblock block accounting code. This is trivially reproduced with xfs_io. Fix the problem by capping the allocation reservation to the maximum number of blocks a single xfs_bmapi() call can allocate (2^21 blocks).

---
 fs/xfs/xfs_vnodeops.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
fs/xfs/xfs_vnodeops.c +9 -3
index 66d585c..91dd9c8 100644
@@ -2299,15 +2299,21 @@ xfs_alloc_file_space(
 			e = allocatesize_fsb;
 		}
 
+		/*
+		 * we can't allocate more than @nimaps extents at a time,
+		 * so prevent a 32bit overflow on the transaction reserve
+		 * by trying to reserve > 16TB worth of blocks for the
+		 * preallocation.
+		 */
+		resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
 		if (unlikely(rt)) {
-			resrtextents = qblocks = (uint)(e - s);
+			resrtextents = qblocks = resblks;
 			resrtextents /= mp->m_sb.sb_rextsize;
 			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
 			quota_flag = XFS_QMOPT_RES_RTBLKS;
 		} else {
 			resrtextents = 0;
-			resblks = qblocks = \
-				XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
+			resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
 			quota_flag = XFS_QMOPT_RES_REGBLKS;
 		}
 
Signature · 6 lines
1.7.1

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Dave Chinner

From: Dave Chinner <[email protected]>

If we attempt to preallocate more than 2^32 blocks of space in a single syscall, the transaction block reservation will overflow leading to a hangs in the superblock block accounting code. This is trivially reproduced with xfs_io. Fix the problem by capping the allocation reservation to the maximum number of blocks a single xfs_bmapi() call can allocate (2^21 blocks).

Looks good,

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
In reply to Dave Chinner
Quoted 8 lines · Dave Chinner

From: Dave Chinner <[email protected]>

If we attempt to preallocate more than 2^32 blocks of space in a single syscall, the transaction block reservation will overflow leading to a hangs in the superblock block accounting code. This is trivially reproduced with xfs_io. Fix the problem by capping the allocation reservation to the maximum number of blocks a single xfs_bmapi() call can allocate (2^21 blocks).

This looks OK, but I have two comments, below.

Quoted 10 lines
---
 fs/xfs/xfs_vnodeops.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
fs/xfs/xfs_vnodeops.c +0 -0
index 66d585c..91dd9c8 100644

fs/xfs/xfs_vnodeops.c xfs_alloc_file_space( · line 2307

Quoted 9 lines
fs/xfs/xfs_vnodeops.c +6 -0
@@ -2299,15 +2299,21 @@ xfs_alloc_file_space(
 			e = allocatesize_fsb;
 		}
 
+		/*
+		 * we can't allocate more than @nimaps extents at a time,
+		 * so prevent a 32bit overflow on the transaction reserve
+		 * by trying to reserve > 16TB worth of blocks for the
+		 * preallocation.
+

This comment could use rewording. How about something like:

A 32-bit block count limits the amount of space that can be reserved in a transaction, so we need to limit the number of blocks reserved to avoid overflow. We can't allocate more than @nimaps extents (whose size won't exceed 32 bits) at a time anyway, so use that to enforce the limit.

fs/xfs/xfs_vnodeops.c xfs_alloc_file_space( · line 2309

Quoted 2 lines
fs/xfs/xfs_vnodeops.c +1 -0
 		 */
+		resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));

I guess it's clear that MAXEXTLEN fits in 32 bits because of sizeof (xfs_extlen_t). And inspection shows that nimaps is just 1, so this does the 32-bit limiting. But that just seems indirect. (Actually, now that I've written this I updated the above comment and it's better...)

-Alex

Quoted 4 lines
fs/xfs/xfs_vnodeops.c +1 -1
 		if (unlikely(rt)) {
-			resrtextents = qblocks = (uint)(e - s);
+			resrtextents = qblocks = resblks;

fs/xfs/xfs_vnodeops.c xfs_alloc_file_space( · line 2318

Quoted 10 lines
fs/xfs/xfs_vnodeops.c +1 -2
 			resrtextents /= mp->m_sb.sb_rextsize;
 			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
 			quota_flag = XFS_QMOPT_RES_RTBLKS;
 		} else {
 			resrtextents = 0;
-			resblks = qblocks = \
-				XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
+			resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
 			quota_flag = XFS_QMOPT_RES_REGBLKS;
 		}

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw
Quoted 34 lines · Alex Elder
Quoted 8 lines · Dave Chinner

From: Dave Chinner <[email protected]>

If we attempt to preallocate more than 2^32 blocks of space in a single syscall, the transaction block reservation will overflow leading to a hangs in the superblock block accounting code. This is trivially reproduced with xfs_io. Fix the problem by capping the allocation reservation to the maximum number of blocks a single xfs_bmapi() call can allocate (2^21 blocks).

This looks OK, but I have two comments, below.

Quoted 10 lines
---
 fs/xfs/xfs_vnodeops.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
fs/xfs/xfs_vnodeops.c +0 -0
index 66d585c..91dd9c8 100644

fs/xfs/xfs_vnodeops.c xfs_alloc_file_space( · line 2307

Quoted 9 lines
fs/xfs/xfs_vnodeops.c +6 -0
@@ -2299,15 +2299,21 @@ xfs_alloc_file_space(
 			e = allocatesize_fsb;
 		}
 
+		/*
+		 * we can't allocate more than @nimaps extents at a time,
+		 * so prevent a 32bit overflow on the transaction reserve
+		 * by trying to reserve > 16TB worth of blocks for the
+		 * preallocation.
+

This comment could use rewording. How about something like:

Quoted 6 lines

A 32-bit block count limits the amount of space that can be reserved in a transaction, so we need to limit the number of blocks reserved to avoid overflow. We can't allocate more than @nimaps extents (whose size won't exceed 32 bits) at a time anyway, so use that to enforce the limit.

Ok, make sense - I'll reword it.

Quoted 5 lines

fs/xfs/xfs_vnodeops.c xfs_alloc_file_space( · line 2309

Quoted 2 lines
fs/xfs/xfs_vnodeops.c +1 -0
 		 */
+		resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));

I guess it's clear that MAXEXTLEN fits in 32 bits because of sizeof (xfs_extlen_t).

True, but if sizeof(xfs_extlen_t) was the limiting factor, then the mulitply could still cause 32bit overflows.

The real reason is that MAXEXTLEN defines the maximum extent length supported by the on disk bmap btree record format. The record format defines the extent length in FSBs to be:

#define MAXEXTLEN ((xfs_extlen_t)0x001fffff) /* 21 bits */

and as such fits easily into the 32 bit limit.

Quoted 3 lines

And inspection shows that nimaps is
just 1, so this does the 32-bit limiting. But that just seems indirect.

nimaps can be up to:

#define XFS_BMAP_MAX_NMAP 4

So if we change the loop to do more allocations per loop, then the code will already handle it correctly. :)

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#4 lore raw
Quoted 5 lines · Dave Chinner
Quoted 4 lines · Alex Elder
Quoted 3 lines · Dave Chinner

From: Dave Chinner <[email protected]>

If we attempt to preallocate more than 2^32 blocks of space in a

. . .

Quoted 8 lines
Quoted 4 lines

fs/xfs/xfs_vnodeops.c xfs_alloc_file_space( · line 2308

Quoted 1 line
fs/xfs/xfs_vnodeops.c +1 -0
+		resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));

I guess it's clear that MAXEXTLEN fits in 32 bits because of sizeof (xfs_extlen_t).

True, but if sizeof(xfs_extlen_t) was the limiting factor, then the mulitply could still cause 32bit overflows.

Quoted 7 lines

The real reason is that MAXEXTLEN defines the maximum extent length supported by the on disk bmap btree record format. The record format defines the extent length in FSBs to be:

#define MAXEXTLEN ((xfs_extlen_t)0x001fffff) /* 21 bits */

and as such fits easily into the 32 bit limit.

Yes, I recognized that but didn't mention it. However...

Quoted 7 lines
Quoted 3 lines

And inspection shows that nimaps is
just 1, so this does the 32-bit limiting. But that just seems indirect.

nimaps can be up to:

#define XFS_BMAP_MAX_NMAP 4

...I had not noticed that nimap could have been changed from its value 1 by the xfs_bmapi() call, so the point you make is important.

Quoted 2 lines

So if we change the loop to do more allocations per loop, then the code will already handle it correctly. :)

Yes. And like I said, just adjusting the comment explains why it is safe.

-Alex

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs