linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] xfs: check krealloc() return value and simplify code
@ 2020-11-24 10:45 Zhen Lei
  2020-11-24 10:45 ` [PATCH 1/2] xfs: check the return value of krealloc() Zhen Lei
  2020-11-24 10:45 ` [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc() Zhen Lei
  0 siblings, 2 replies; 7+ messages in thread
From: Zhen Lei @ 2020-11-24 10:45 UTC (permalink / raw)
  To: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel; +Cc: Zhen Lei


Zhen Lei (2):
  xfs: check the return value of krealloc()
  xfs: remove the extra processing of zero size in xfs_idata_realloc()

 fs/xfs/libxfs/xfs_inode_fork.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

-- 
2.26.0.106.g9fadedd



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

* [PATCH 1/2] xfs: check the return value of krealloc()
  2020-11-24 10:45 [PATCH 0/2] xfs: check krealloc() return value and simplify code Zhen Lei
@ 2020-11-24 10:45 ` Zhen Lei
  2020-11-24 11:51   ` Christoph Hellwig
  2020-11-24 10:45 ` [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc() Zhen Lei
  1 sibling, 1 reply; 7+ messages in thread
From: Zhen Lei @ 2020-11-24 10:45 UTC (permalink / raw)
  To: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel; +Cc: Zhen Lei

krealloc() may fail to expand the memory space. Add sanity checks to it,
and WARN() if that really happened.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 fs/xfs/libxfs/xfs_inode_fork.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_inode_fork.c b/fs/xfs/libxfs/xfs_inode_fork.c
index 7575de5cecb1..4e457aea8493 100644
--- a/fs/xfs/libxfs/xfs_inode_fork.c
+++ b/fs/xfs/libxfs/xfs_inode_fork.c
@@ -366,6 +366,8 @@ xfs_iroot_realloc(
 
 	ifp = XFS_IFORK_PTR(ip, whichfork);
 	if (rec_diff > 0) {
+		struct xfs_btree_block *if_broot;
+
 		/*
 		 * If there wasn't any memory allocated before, just
 		 * allocate it now and get out.
@@ -386,8 +388,13 @@ xfs_iroot_realloc(
 		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
 		new_max = cur_max + rec_diff;
 		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
-		ifp->if_broot = krealloc(ifp->if_broot, new_size,
-					 GFP_NOFS | __GFP_NOFAIL);
+		if_broot = krealloc(ifp->if_broot, new_size,
+				    GFP_NOFS | __GFP_NOFAIL);
+		if (!if_broot) {
+			WARN(1, "if_broot realloc failed\n");
+			return;
+		}
+		ifp->if_broot = if_broot;
 		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
 						     ifp->if_broot_bytes);
 		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
@@ -477,6 +484,7 @@ xfs_idata_realloc(
 {
 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
 	int64_t			new_size = ifp->if_bytes + byte_diff;
+	char *if_data;
 
 	ASSERT(new_size >= 0);
 	ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
@@ -496,8 +504,13 @@ xfs_idata_realloc(
 	 * in size so that it can be logged and stay on word boundaries.
 	 * We enforce that here.
 	 */
-	ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
+	if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
 				      GFP_NOFS | __GFP_NOFAIL);
+	if (!if_data) {
+		WARN(1, "if_data realloc failed\n");
+		return;
+	}
+	ifp->if_u1.if_data = if_data;
 	ifp->if_bytes = new_size;
 }
 
-- 
2.26.0.106.g9fadedd



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

* [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc()
  2020-11-24 10:45 [PATCH 0/2] xfs: check krealloc() return value and simplify code Zhen Lei
  2020-11-24 10:45 ` [PATCH 1/2] xfs: check the return value of krealloc() Zhen Lei
@ 2020-11-24 10:45 ` Zhen Lei
  2020-11-24 11:52   ` Christoph Hellwig
  1 sibling, 1 reply; 7+ messages in thread
From: Zhen Lei @ 2020-11-24 10:45 UTC (permalink / raw)
  To: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel; +Cc: Zhen Lei

krealloc() does the free operation when the parameter new_size is 0, with
ZERO_SIZE_PTR returned. Because all other places use NULL to check whether
if_data is available or not, so covert it from ZERO_SIZE_PTR to NULL.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 fs/xfs/libxfs/xfs_inode_fork.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_inode_fork.c b/fs/xfs/libxfs/xfs_inode_fork.c
index 4e457aea8493..518af4088e79 100644
--- a/fs/xfs/libxfs/xfs_inode_fork.c
+++ b/fs/xfs/libxfs/xfs_inode_fork.c
@@ -492,13 +492,6 @@ xfs_idata_realloc(
 	if (byte_diff == 0)
 		return;
 
-	if (new_size == 0) {
-		kmem_free(ifp->if_u1.if_data);
-		ifp->if_u1.if_data = NULL;
-		ifp->if_bytes = 0;
-		return;
-	}
-
 	/*
 	 * For inline data, the underlying buffer must be a multiple of 4 bytes
 	 * in size so that it can be logged and stay on word boundaries.
@@ -510,7 +503,7 @@ xfs_idata_realloc(
 		WARN(1, "if_data realloc failed\n");
 		return;
 	}
-	ifp->if_u1.if_data = if_data;
+	ifp->if_u1.if_data = ZERO_OR_NULL_PTR(if_data) ? NULL : if_data;
 	ifp->if_bytes = new_size;
 }
 
-- 
2.26.0.106.g9fadedd



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

* Re: [PATCH 1/2] xfs: check the return value of krealloc()
  2020-11-24 10:45 ` [PATCH 1/2] xfs: check the return value of krealloc() Zhen Lei
@ 2020-11-24 11:51   ` Christoph Hellwig
  2020-11-24 12:05     ` Leizhen (ThunderTown)
  0 siblings, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2020-11-24 11:51 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel

On Tue, Nov 24, 2020 at 06:45:30PM +0800, Zhen Lei wrote:
> krealloc() may fail to expand the memory space. Add sanity checks to it,
> and WARN() if that really happened.

What part of the __GFP_NOFAIL semantics isn't clear enough?

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

* Re: [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc()
  2020-11-24 10:45 ` [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc() Zhen Lei
@ 2020-11-24 11:52   ` Christoph Hellwig
  2020-11-24 12:05     ` Leizhen (ThunderTown)
  0 siblings, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2020-11-24 11:52 UTC (permalink / raw)
  To: Zhen Lei; +Cc: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel

On Tue, Nov 24, 2020 at 06:45:31PM +0800, Zhen Lei wrote:
> krealloc() does the free operation when the parameter new_size is 0, with
> ZERO_SIZE_PTR returned. Because all other places use NULL to check whether
> if_data is available or not, so covert it from ZERO_SIZE_PTR to NULL.

This new code looks much harder to read than the version it replaced.

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

* Re: [PATCH 1/2] xfs: check the return value of krealloc()
  2020-11-24 11:51   ` Christoph Hellwig
@ 2020-11-24 12:05     ` Leizhen (ThunderTown)
  0 siblings, 0 replies; 7+ messages in thread
From: Leizhen (ThunderTown) @ 2020-11-24 12:05 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel



On 2020/11/24 19:51, Christoph Hellwig wrote:
> On Tue, Nov 24, 2020 at 06:45:30PM +0800, Zhen Lei wrote:
>> krealloc() may fail to expand the memory space. Add sanity checks to it,
>> and WARN() if that really happened.
> 
> What part of the __GFP_NOFAIL semantics isn't clear enough?

Oh, sorry, I didn't notice __GFP_NOFAIL flag.

> 
> 


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

* Re: [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc()
  2020-11-24 11:52   ` Christoph Hellwig
@ 2020-11-24 12:05     ` Leizhen (ThunderTown)
  0 siblings, 0 replies; 7+ messages in thread
From: Leizhen (ThunderTown) @ 2020-11-24 12:05 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Darrick J . Wong, Brian Foster, linux-xfs, linux-kernel



On 2020/11/24 19:52, Christoph Hellwig wrote:
> On Tue, Nov 24, 2020 at 06:45:31PM +0800, Zhen Lei wrote:
>> krealloc() does the free operation when the parameter new_size is 0, with
>> ZERO_SIZE_PTR returned. Because all other places use NULL to check whether
>> if_data is available or not, so covert it from ZERO_SIZE_PTR to NULL.
> 
> This new code looks much harder to read than the version it replaced.

OK

> 
> 


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

end of thread, other threads:[~2020-11-24 12:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-24 10:45 [PATCH 0/2] xfs: check krealloc() return value and simplify code Zhen Lei
2020-11-24 10:45 ` [PATCH 1/2] xfs: check the return value of krealloc() Zhen Lei
2020-11-24 11:51   ` Christoph Hellwig
2020-11-24 12:05     ` Leizhen (ThunderTown)
2020-11-24 10:45 ` [PATCH 2/2] xfs: remove the extra processing of zero size in xfs_idata_realloc() Zhen Lei
2020-11-24 11:52   ` Christoph Hellwig
2020-11-24 12:05     ` Leizhen (ThunderTown)

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).