Skip to contentSkip to conversation

zsmalloc: return -EBUSY for zspage migration lock contention

PATCH Superseded Hui Zhu 2 participants 2 messages Mar 19

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH v2 1/1 zsmalloc: return -EBUSY for zspage migration lock contention

From: teawater <[email protected]>

movable_operations::migrate_page() should return an appropriate error code for temporary migration failures so the migration core can handle them correctly.

zs_page_migrate() currently returns -EINVAL when zspage_write_trylock() fails. That path reflects transient lock contention, not invalid input, so -EINVAL is clearly wrong.

However, -EAGAIN is also inappropriate here: the zspage's reader-lock owner may hold the lock for an unbounded duration due to slow decompression. Since migration retries are bounded by NR_MAX_MIGRATE_PAGES_RETRY and performed with virtually no delay between attempts, there is no guarantee the lock will be released in time for a retry to succeed. -EAGAIN implies "try again soon", which does not hold in this case.

Return -EBUSY instead, which more accurately conveys that the resource is occupied and migration cannot proceed at this time.

Changelog:
v2:
According to the comments of Sergey Senozhatsky, change from -EAGAIN to -EBUSY and add comments.

---
 mm/zsmalloc.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
mm/zsmalloc.c +13 -1
index 2c1430bf8d57..db42ec4ffcfa 100644
@@ -1727,7 +1727,19 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
 	if (!zspage_write_trylock(zspage)) {
 		spin_unlock(&class->lock);
 		write_unlock(&pool->lock);
-		return -EINVAL;
+		/*
+		 * Return -EBUSY but not -EAGAIN: the zspage's reader-lock
+		 * owner may hold the lock for an unbounded duration due to a
+		 * slow decompression.
+		 * Since migration retries are bounded by
+		 * NR_MAX_MIGRATE_PAGES_RETRY and performed with virtually no
+		 * delay between attempts, there is no guarantee the lock will
+		 * be released in time for a retry to succeed.
+		 * -EAGAIN implies "try again soon", which does not hold here.
+		 * -EBUSY more accurately conveys "resource is occupied,
+		 * migration cannot proceed".
+		 */
+		return -EBUSY;
 	}
 
 	/* We're committed, tell the world that this is a Zsmalloc page. */
Signature · 1 line
2.43.0
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 16 lines · Hui Zhu

movable_operations::migrate_page() should return an appropriate error code for temporary migration failures so the migration core can handle them correctly.

zs_page_migrate() currently returns -EINVAL when zspage_write_trylock() fails. That path reflects transient lock contention, not invalid input, so -EINVAL is clearly wrong.

However, -EAGAIN is also inappropriate here: the zspage's reader-lock owner may hold the lock for an unbounded duration due to slow decompression. Since migration retries are bounded by NR_MAX_MIGRATE_PAGES_RETRY and performed with virtually no delay between attempts, there is no guarantee the lock will be released in time for a retry to succeed. -EAGAIN implies "try again soon", which does not hold in this case.

Quoted 7 lines

Return -EBUSY instead, which more accurately conveys that the resource is occupied and migration cannot proceed at this time.

Changelog:
v2:
According to the comments of Sergey Senozhatsky, change from -EAGAIN to -EBUSY and add comments.

I assume this Changelog was supposed to be under "---".

mm/zsmalloc.c static int zs_page_migrate(struct page *newpage, struct page *page, · line 1733

Quoted 4 lines
mm/zsmalloc.c +4 -0
+		/*
+		 * Return -EBUSY but not -EAGAIN: the zspage's reader-lock
+		 * owner may hold the lock for an unbounded duration due to a
+		 * slow decompression.

^^^ or reader-lock owner preemption

mm/zsmalloc.c static int zs_page_migrate(struct page *newpage, struct page *page, · line 1743

Quoted 10 lines
mm/zsmalloc.c +9 -0
+		 * Since migration retries are bounded by
+		 * NR_MAX_MIGRATE_PAGES_RETRY and performed with virtually no
+		 * delay between attempts, there is no guarantee the lock will
+		 * be released in time for a retry to succeed.
+		 * -EAGAIN implies "try again soon", which does not hold here.
+		 * -EBUSY more accurately conveys "resource is occupied,
+		 * migration cannot proceed".
+		 */
+		return -EBUSY;
 	}

Otherwise,