Skip to contentSkip to conversation

Cleanup and fixup for z3fold

PATCH Applied Miaohe Lin 3 participants 10 messages Jun 19, 2021 – Jun 22, 2021 Memory management

lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

cover Cleanup and fixup for z3fold

Hi all,
This series contains cleanups to remove unused function, redefine macro to improve readability and so on. Also this fixes several bugs in z3fold, such as memory leak in z3fold_destroy_pool(). More details can be found in the respective changelogs. Thanks!

Miaohe Lin (6):
mm/z3fold: define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS mm/z3fold: avoid possible underflow in z3fold_alloc() mm/z3fold: remove magic number in z3fold_create_pool() mm/z3fold: remove unused function handle_to_z3fold_header() mm/z3fold: fix potential memory leak in z3fold_destroy_pool() mm/z3fold: use release_z3fold_page_locked() to release locked z3fold
page

 mm/z3fold.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 1/6 mm/z3fold: define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS

To improve the code readability, we could define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS. No functional change intended.

---
 mm/z3fold.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/z3fold.c +1 -1
index 56321bf37d56..04d0e493bd2e 100644
@@ -62,7 +62,7 @@
 #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
 #define ZHDR_CHUNKS	(ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
 #define TOTAL_CHUNKS	(PAGE_SIZE >> CHUNK_SHIFT)
-#define NCHUNKS		((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
+#define NCHUNKS		(TOTAL_CHUNKS - ZHDR_CHUNKS)
 
 #define BUDDY_MASK	(0x3)
 #define BUDDY_SHIFT	2
Signature · 1 line
2.23.0
author
#2 lore raw

PATCH 2/6 mm/z3fold: avoid possible underflow in z3fold_alloc()

In reply to Miaohe Lin

It is not enough to just make sure the z3fold header is not larger than the page size. When z3fold header is equal to PAGE_SIZE, we would underflow when check alloc size against PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE in z3fold_alloc(). Make sure there has remaining spaces for its buddy to fix this theoretical issue.

---
This causes the below checkpatch warning:
WARNING: Comparisons should place the constant on the right side of the test
#31: FILE: mm/z3fold.c:1812:
+ BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE - CHUNK_SIZE);

But I think the error is false positives as all members are constant.

---
 mm/z3fold.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/z3fold.c +5 -2
index 04d0e493bd2e..e261e14b7753 100644
@@ -1805,8 +1805,11 @@ static int __init init_z3fold(void)
 {
 	int ret;
 
-	/* Make sure the z3fold header is not larger than the page size */
-	BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
+	/*
+	 * Make sure the z3fold header is not larger than the page size and
+	 * there has remaining spaces for its buddy.
+	 */
+	BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE - CHUNK_SIZE);
 	ret = z3fold_mount();
 	if (ret)
 		return ret;
Signature · 1 line
2.23.0
author
#3 lore raw

PATCH 3/6 mm/z3fold: remove magic number in z3fold_create_pool()

In reply to Miaohe Lin

It's meaningless to pass a magic number 2 to __alloc_percpu() as there is a minimum alignment size of PCPU_MIN_ALLOC_SIZE (> 2) in it. Also there is no special alignment requirement for unbuddied. So we could replace this magic number with nature alignment, i.e. __alignof__(struct list_head), to improve readability.

---
 mm/z3fold.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/z3fold.c +2 -1
index e261e14b7753..988d57c143fd 100644
@@ -1000,7 +1000,8 @@ static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp,
 		goto out_c;
 	spin_lock_init(&pool->lock);
 	spin_lock_init(&pool->stale_lock);
-	pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2);
+	pool->unbuddied = __alloc_percpu(sizeof(struct list_head) * NCHUNKS,
+					 __alignof__(struct list_head));
 	if (!pool->unbuddied)
 		goto out_pool;
 	for_each_possible_cpu(cpu) {
Signature · 1 line
2.23.0
author
#4 lore raw

PATCH 4/6 mm/z3fold: remove unused function handle_to_z3fold_header()

In reply to Miaohe Lin

handle_to_z3fold_header() is unused now. So we can remove it. As a result, get_z3fold_header() becomes the only caller of __get_z3fold_header() and the argument lock is always true. Therefore we could further fold the __get_z3fold_header() into get_z3fold_header() with lock = true.

---
 mm/z3fold.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)
mm/z3fold.c +4 -18
index 988d57c143fd..bab08c08bf19 100644
@@ -255,9 +255,8 @@ static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
 	spin_unlock(&zhdr->page_lock);
 }
 
-
-static inline struct z3fold_header *__get_z3fold_header(unsigned long handle,
-							bool lock)
+/* return locked z3fold page if it's not headless */
+static inline struct z3fold_header *get_z3fold_header(unsigned long handle)
 {
 	struct z3fold_buddy_slots *slots;
 	struct z3fold_header *zhdr;
@@ -271,13 +270,12 @@ static inline struct z3fold_header *__get_z3fold_header(unsigned long handle,
 			read_lock(&slots->lock);
 			addr = *(unsigned long *)handle;
 			zhdr = (struct z3fold_header *)(addr & PAGE_MASK);
-			if (lock)
-				locked = z3fold_page_trylock(zhdr);
+			locked = z3fold_page_trylock(zhdr);
 			read_unlock(&slots->lock);
 			if (locked)
 				break;
 			cpu_relax();
-		} while (lock);
+		} while (true);
 	} else {
 		zhdr = (struct z3fold_header *)(handle & PAGE_MASK);
 	}
@@ -285,18 +283,6 @@ static inline struct z3fold_header *__get_z3fold_header(unsigned long handle,
 	return zhdr;
 }
 
-/* Returns the z3fold page where a given handle is stored */
-static inline struct z3fold_header *handle_to_z3fold_header(unsigned long h)
-{
-	return __get_z3fold_header(h, false);
-}
-
-/* return locked z3fold page if it's not headless */
-static inline struct z3fold_header *get_z3fold_header(unsigned long h)
-{
-	return __get_z3fold_header(h, true);
-}
-
 static inline void put_z3fold_header(struct z3fold_header *zhdr)
 {
 	struct page *page = virt_to_page(zhdr);
Signature · 1 line
2.23.0
author
#5 lore raw

PATCH 5/6 mm/z3fold: fix potential memory leak in z3fold_destroy_pool()

In reply to Miaohe Lin

There is a memoryleak in z3fold_destroy_pool() as it forgets to free_percpu pool->unbuddied. Call free_percpu for pool->unbuddied to fix this issue.

Fixes d30561c56f41 ("z3fold: use per-cpu unbuddied lists")
---
 mm/z3fold.c | 1 +
 1 file changed, 1 insertion(+)
mm/z3fold.c +1 -0
index bab08c08bf19..196d886a3436 100644
@@ -1048,6 +1048,7 @@ static void z3fold_destroy_pool(struct z3fold_pool *pool)
 	destroy_workqueue(pool->compact_wq);
 	destroy_workqueue(pool->release_wq);
 	z3fold_unregister_migration(pool);
+	free_percpu(pool->unbuddied);
 	kfree(pool);
 }
 
Signature · 1 line
2.23.0
author
#6 lore raw

PATCH 6/6 mm/z3fold: use release_z3fold_page_locked() to release locked z3fold page

In reply to Miaohe Lin

We should use release_z3fold_page_locked() to release z3fold page when it's locked, although it looks harmless to use release_z3fold_page() now.

Fixes dcf5aedb24f8 ("z3fold: stricter locking and more careful reclaim")
---
 mm/z3fold.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/z3fold.c +1 -1
index 196d886a3436..b3c0577b8095 100644
@@ -1372,7 +1372,7 @@ static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
 			if (zhdr->foreign_handles ||
 			    test_and_set_bit(PAGE_CLAIMED, &page->private)) {
 				if (kref_put(&zhdr->refcount,
-						release_z3fold_page))
+						release_z3fold_page_locked))
 					atomic64_dec(&pool->pages_nr);
 				else
 					z3fold_page_unlock(zhdr);
Signature · 1 line
2.23.0
#7 lore raw

Re: [PATCH 6/6] mm/z3fold: use release_z3fold_page_locked() to release locked z3fold page

Quoted 14 lines · On Sat, 19 Jun 2021 17:31:51 +0800 Miaohe Lin wrote:

We should use release_z3fold_page_locked() to release z3fold page when it's locked, although it looks harmless to use release_z3fold_page() now.

Fixes dcf5aedb24f8 ("z3fold: stricter locking and more careful reclaim")
---
 mm/z3fold.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/z3fold.c +0 -0
index 196d886a3436..b3c0577b8095 100644

mm/z3fold.c static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries) · line 1376

Quoted 6 lines
mm/z3fold.c +1 -1
@@ -1372,7 +1372,7 @@ static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
 			if (zhdr->foreign_handles ||
 			    test_and_set_bit(PAGE_CLAIMED, &page->private)) {
 				if (kref_put(&zhdr->refcount,
-						release_z3fold_page))
+						release_z3fold_page_locked))
 					atomic64_dec(&pool->pages_nr);

LGTM. JFYI other issue in z3fold was reported [1] and if the fix proposed there makes any sense to you feel free to pick it up and ask Mike for his tests.

[1] https://lore.kernel.org/linux-mm/[email protected]/

author
#8 lore raw

Re: [PATCH 6/6] mm/z3fold: use release_z3fold_page_locked() to release locked z3fold page

Quoted 10 lines · Hillf Danton
Quoted 9 lines · On Sat, 19 Jun 2021 17:31:51 +0800 Miaohe Lin wrote:

We should use release_z3fold_page_locked() to release z3fold page when it's locked, although it looks harmless to use release_z3fold_page() now.

Fixes dcf5aedb24f8 ("z3fold: stricter locking and more careful reclaim")
---
 mm/z3fold.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

mm/z3fold.c static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries) · line 1376

Quoted 11 lines
mm/z3fold.c +1 -1
index 196d886a3436..b3c0577b8095 100644
@@ -1372,7 +1372,7 @@ static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
 			if (zhdr->foreign_handles ||
 			    test_and_set_bit(PAGE_CLAIMED, &page->private)) {
 				if (kref_put(&zhdr->refcount,
-						release_z3fold_page))
+						release_z3fold_page_locked))
 					atomic64_dec(&pool->pages_nr);

LGTM. JFYI other issue in z3fold was reported [1] and if the fix proposed there makes any sense to you feel free to pick it up and ask Mike for his tests.

Thank you for review and reply.

I browsed [1] several times but I failed to figure out what's the root cause. And I found some bugs and possible race windows from previous code inspection. I think we can try fix these first and see whether [1] is (hopefully) fixed. :)
Thanks again.

Quoted 3 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
.

Reviewed-by on the v1 series
#9 lore raw
In reply to Miaohe Lin
Quoted 15 lines · Miaohe Lin

Hi all,
This series contains cleanups to remove unused function, redefine macro to improve readability and so on. Also this fixes several bugs in z3fold, such as memory leak in z3fold_destroy_pool(). More details can be found in the respective changelogs. Thanks!

Miaohe Lin (6):
mm/z3fold: define macro NCHUNKS as TOTAL_CHUNKS - ZHDR_CHUNKS mm/z3fold: avoid possible underflow in z3fold_alloc() mm/z3fold: remove magic number in z3fold_create_pool() mm/z3fold: remove unused function handle_to_z3fold_header() mm/z3fold: fix potential memory leak in z3fold_destroy_pool() mm/z3fold: use release_z3fold_page_locked() to release locked z3fold
page

Quoted 4 lines
 mm/z3fold.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)

Thanks for looking into this!