mm/zsmalloc.c: close race window between zs_pool_dec_isolated() and zs_unregister_migration()
Subsystems (2)
PATCH v2 1/1 mm/zsmalloc.c: close race window between zs_pool_dec_isolated() and zs_unregister_migration()
There has one possible race window between zs_pool_dec_isolated() and zs_unregister_migration() because wait_for_isolated_drain() checks the isolated count without holding class->lock and there is no order inside zs_pool_dec_isolated(). Thus the below race window could be possible:
zs_pool_dec_isolated zs_unregister_migration
check pool->destroying != 0
pool->destroying = true; smp_mb(); wait_for_isolated_drain() wait for pool->isolated_pages == 0
atomic_long_dec(&pool->isolated_pages);
atomic_long_read(&pool->isolated_pages) == 0
Since we observe the pool->destroying (false) before atomic_long_dec() for pool->isolated_pages, waking pool->migration_wait up is missed.
Fix this by ensure checking pool->destroying is happened after the atomic_long_dec(&pool->isolated_pages).
---v1->v2:
Fix potential race window rather than simply combine atomic_long_dec
and atomic_long_read.
Hi Andrew,
This patch is the version 2 of
mm-zsmallocc-combine-two-atomic-ops-in-zs_pool_dec_isolated.patch. Many thanks.
--- mm/zsmalloc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/zsmalloc.c +4 -3
@@ -1830,10 +1830,11 @@ static inline void zs_pool_dec_isolated(struct zs_pool *pool)VM_BUG_ON (atomic_long_read (&pool->isolated_pages) <=0 );atomic_long_dec (&pool->isolated_pages);/* -* There's no possibility of racing, since wait_for_isolated_drain() -* checks the isolated count under &class->lock after enqueuing -* on migration_wait. +* Checking pool->destroying must happen after atomic_long_dec() +* for pool->isolated_pages above. Paired with the smp_mb() in +* zs_unregister_migration(). */ +smp_mb__after_atomic ();if (atomic_long_read (&pool->isolated_pages) ==0 && pool->destroying)wake_up_all (&pool->migration_wait); }
Signature · 1 line
2.23.0
Sorry for the disturbs! Please ignore this duplicated one...
Quoted 55 lines · Miaohe Lin
There has one possible race window between
zs_pool_dec_isolated()andzs_unregister_migration()becausewait_for_isolated_drain()checks the isolated count without holdingclass->lockand there is no order insidezs_pool_dec_isolated(). Thus the below race window could be possible:
zs_pool_dec_isolatedzs_unregister_migration
checkpool->destroying!= 0pool->destroying = true; smp_mb(); wait_for_isolated_drain() wait for pool->isolated_pages == 0
atomic_long_dec(&pool->isolated_pages);
atomic_long_read(&pool->isolated_pages)== 0Since we observe the
pool->destroying(false) beforeatomic_long_dec()forpool->isolated_pages, wakingpool->migration_waitup is missed.Fix this by ensure checking
pool->destroyingis happened after theatomic_long_dec(&pool->isolated_pages).Fixes 701d678599d0 ("mm/zsmalloc.c: fix race condition in zs_destroy_pool")Signed-off-by Miaohe Lin <[email protected]>---
v1->v2:
Fix potential race window rather than simply combineatomic_long_dec
andatomic_long_read.Hi Andrew,
This patch is the version 2 of
mm-zsmallocc-combine-two-atomic-ops-in-zs_pool_dec_isolated.patch. Many thanks.--- mm/zsmalloc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/zsmalloc.c +4
-3@@ -1830,10 +1830,11 @@ static inline void zs_pool_dec_isolated(struct zs_pool *pool)VM_BUG_ON (atomic_long_read (&pool->isolated_pages) <=0 );atomic_long_dec (&pool->isolated_pages);/* -* There's no possibility of racing, since wait_for_isolated_drain() -* checks the isolated count under &class->lock after enqueuing -* on migration_wait. +* Checking pool->destroying must happen after atomic_long_dec() +* for pool->isolated_pages above. Paired with the smp_mb() in +* zs_unregister_migration(). */ +smp_mb__after_atomic ();if (atomic_long_read (&pool->isolated_pages) ==0 && pool->destroying)wake_up_all (&pool->migration_wait); }