[PATCH] erofs: simplify z_erofs_gbuf_growsize()

Gao Xiang <[email protected]>
Newsgroups org.ozlabs.lists.linux-erofs,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
 - Use guard(mutex)() to manage gbuf_resize_mutex;

 - Nullify tmp_pages after successful allocation, so that the error
   path becomes clearer.

Signed-off-by: Gao Xiang <[email protected]>
---
 fs/erofs/zutil.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/fs/erofs/zutil.c b/fs/erofs/zutil.c
index 6b9bfa2ef3b6..960cfe86eeb6 100644
--- a/fs/erofs/zutil.c
+++ b/fs/erofs/zutil.c
@@ -70,12 +70,10 @@ int z_erofs_gbuf_growsize(unsigned int nrpages)
 	void *ptr, *old_ptr;
 	int last, i, j;
 
-	mutex_lock(&gbuf_resize_mutex);
+	guard(mutex)(&gbuf_resize_mutex);
 	/* avoid shrinking gbufs, since no idea how many fses rely on */
-	if (nrpages <= z_erofs_gbuf_nrpages) {
-		mutex_unlock(&gbuf_resize_mutex);
+	if (nrpages <= z_erofs_gbuf_nrpages)
 		return 0;
-	}
 
 	for (i = 0; i < z_erofs_gbuf_count; ++i) {
 		gbuf = &z_erofs_gbufpool[i];
@@ -89,8 +87,7 @@ int z_erofs_gbuf_growsize(unsigned int nrpages)
 			tmp_pages[j] = gbuf->pages[j];
 		do {
 			last = j;
-			j = alloc_pages_bulk(GFP_KERNEL, nrpages,
-					     tmp_pages);
+			j = alloc_pages_bulk(GFP_KERNEL, nrpages, tmp_pages);
 			if (last == j)
 				goto out;
 		} while (j != nrpages);
@@ -101,24 +98,23 @@ int z_erofs_gbuf_growsize(unsigned int nrpages)
 
 		spin_lock(&gbuf->lock);
 		kfree(gbuf->pages);
-		gbuf->pages = tmp_pages;
 		old_ptr = gbuf->ptr;
+		gbuf->pages = tmp_pages;
 		gbuf->ptr = ptr;
 		gbuf->nrpages = nrpages;
 		spin_unlock(&gbuf->lock);
-		if (old_ptr)
-			vunmap(old_ptr);
+		vunmap(old_ptr);
+		tmp_pages = NULL;
 	}
 	z_erofs_gbuf_nrpages = nrpages;
 out:
-	if (i < z_erofs_gbuf_count && tmp_pages) {
+	if (unlikely(tmp_pages)) {
 		for (j = 0; j < nrpages; ++j)
 			if (tmp_pages[j] && (j >= gbuf->nrpages ||
 					     tmp_pages[j] != gbuf->pages[j]))
 				__free_page(tmp_pages[j]);
 		kfree(tmp_pages);
 	}
-	mutex_unlock(&gbuf_resize_mutex);
 	return i < z_erofs_gbuf_count ? -ENOMEM : 0;
 }
 
-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.