Skip to contentSkip to conversation

mm/zsmalloc: fix release order of locks in zs_page_migrate()

PATCH Applied Richard Chang 3 participants 5 messages Jul 28 – Jul 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/zsmalloc: fix release order of locks in zs_page_migrate()

In zs_page_migrate(), locks are acquired in the following order:
1. write_lock(&pool->lock)
2. spin_lock(&class->lock)
3. zspage_write_trylock(zspage)

However, upon successful page migration, they were being released in forward acquisition (FIFO) order:
1. write_unlock(&pool->lock)
2. spin_unlock(&class->lock)
3. zspage_write_unlock(zspage)

Fix the unlocking order to release locks in strict reverse (LIFO) order of acquisition:
3. zspage_write_unlock(zspage)
2. spin_unlock(&class->lock)
1. write_unlock(&pool->lock)

Releasing locks in reverse order of acquisition adheres to standard kernel locking hygiene, prevents potential lock ordering and lockdep inconsistencies.

---
 mm/zsmalloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/zsmalloc.c +2 -2
index 83f5820c45f9..b09299115886 100644
@@ -1772,9 +1772,9 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
 	 * Since we complete the data copy and set up new zspage structure,
 	 * it's okay to release migration_lock.
 	 */
-	write_unlock(&pool->lock);
-	spin_unlock(&class->lock);
 	zspage_write_unlock(zspage);
+	spin_unlock(&class->lock);
+	write_unlock(&pool->lock);
 
 	zpdesc_get(newzpdesc);
 	if (zpdesc_zone(newzpdesc) != zpdesc_zone(zpdesc)) {
Signature · 1 line
2.55.0.229.g6434b31f56-goog
maintainer Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 17 lines · Richard Chang

In zs_page_migrate(), locks are acquired in the following order:
1. write_lock(&pool->lock)
2. spin_lock(&class->lock)
3. zspage_write_trylock(zspage)

However, upon successful page migration, they were being released in forward acquisition (FIFO) order:
1. write_unlock(&pool->lock)
2. spin_unlock(&class->lock)
3. zspage_write_unlock(zspage)

Fix the unlocking order to release locks in strict reverse (LIFO) order of acquisition:
3. zspage_write_unlock(zspage)
2. spin_unlock(&class->lock)
1. write_unlock(&pool->lock)

Quoted 5 lines

Releasing locks in reverse order of acquisition adheres to standard kernel locking hygiene, prevents potential lock ordering and lockdep inconsistencies.

maintainer
#2 lore raw
In reply to Richard Chang
Quoted 17 lines · On Tue, 28 Jul 2026 05:53:33 +0000 Richard Chang <[email protected]> wrote:

In zs_page_migrate(), locks are acquired in the following order:
1. write_lock(&pool->lock)
2. spin_lock(&class->lock)
3. zspage_write_trylock(zspage)

However, upon successful page migration, they were being released in forward acquisition (FIFO) order:
1. write_unlock(&pool->lock)
2. spin_unlock(&class->lock)
3. zspage_write_unlock(zspage)

Fix the unlocking order to release locks in strict reverse (LIFO) order of acquisition:
3. zspage_write_unlock(zspage)
2. spin_unlock(&class->lock)
1. write_unlock(&pool->lock)

Quoted 4 lines

Releasing locks in reverse order of acquisition adheres to standard kernel locking hygiene, prevents potential lock ordering and lockdep inconsistencies.

Thanks.

Does this actually fix a lockdep splat? If so, is a link available?

#3 lore raw

Hi Andrew,

Quoted 5 lines · Andrew Morton

Thanks.

Does this actually fix a lockdep splat? If so, is a link available?

No, this does not fix an active lockdep splat or a reported runtime deadlock. It is just a code hygiene to release locks in LIFO order.

#4 lore raw
Quoted 9 lines · Richard Chang
Quoted 5 lines · Andrew Morton

Thanks.

Does this actually fix a lockdep splat? If so, is a link available?

No, this does not fix an active lockdep splat or a reported runtime deadlock. It is just a code hygiene to release locks in LIFO order.

Right, surprisingly enough lockdep was never unhappy with the original code (I run tests with CONFIG_PROVE_LOCKING=y.)