Revert "mm: zswap: fix race between [de]compression and CPU hotunplug"
Subsystems (3)
PATCH v2 1/2 Revert "mm: zswap: fix race between [de]compression and CPU hotunplug"
This reverts commit eaebeb93922ca6ab0dd92027b73d0112701706ef.
Commit eaebeb93922c ("mm: zswap: fix race between [de]compression and CPU hotunplug") used the CPU hotplug lock in zswap compress/decompress operations to protect against a race with CPU hotunplug making some per-CPU resources go away.
However, zswap compress/decompress can be reached through reclaim while
the lock is held, resulting in a potential deadlock as reported by
syzbot:
======================================================
WARNING: possible circular locking dependency detected 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Not tainted
------------------------------------------------------
kswapd0/89 is trying to acquire lock:
ffffffff8e7d2ed0 (cpu_hotplug_lock){++++}-{0:0}, at: acomp_ctx_get_cpu mm/zswap.c:886 [inline] ffffffff8e7d2ed0 (cpu_hotplug_lock){++++}-{0:0}, at: zswap_compress mm/zswap.c:908 [inline] ffffffff8e7d2ed0 (cpu_hotplug_lock){++++}-{0:0}, at: zswap_store_page mm/zswap.c:1439 [inline] ffffffff8e7d2ed0 (cpu_hotplug_lock){++++}-{0:0}, at: zswap_store+0xa74/0x1ba0 mm/zswap.c:1546
but task is already holding lock:
ffffffff8ea355a0 (fs_reclaim){+.+.}-{0:0}, at: balance_pgdat mm/vmscan.c:6871 [inline] ffffffff8ea355a0 (fs_reclaim){+.+.}-{0:0}, at: kswapd+0xb58/0x2f30 mm/vmscan.c:7253
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (fs_reclaim){+.+.}-{0:0}:
lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849
__fs_reclaim_acquire mm/page_alloc.c:3853 [inline]
fs_reclaim_acquire+0x88/0x130 mm/page_alloc.c:3867
might_alloc include/linux/sched/mm.h:318 [inline]
slab_pre_alloc_hook mm/slub.c:4070 [inline]
slab_alloc_node mm/slub.c:4148 [inline]
__kmalloc_cache_node_noprof+0x40/0x3a0 mm/slub.c:4337
kmalloc_node_noprof include/linux/slab.h:924 [inline]
alloc_worker kernel/workqueue.c:2638 [inline]
create_worker+0x11b/0x720 kernel/workqueue.c:2781
workqueue_prepare_cpu+0xe3/0x170 kernel/workqueue.c:6628
cpuhp_invoke_callback+0x48d/0x830 kernel/cpu.c:194
__cpuhp_invoke_callback_range kernel/cpu.c:965 [inline]
cpuhp_invoke_callback_range kernel/cpu.c:989 [inline]
cpuhp_up_callbacks kernel/cpu.c:1020 [inline]
_cpu_up+0x2b3/0x580 kernel/cpu.c:1690
cpu_up+0x184/0x230 kernel/cpu.c:1722
cpuhp_bringup_mask+0xdf/0x260 kernel/cpu.c:1788
cpuhp_bringup_cpus_parallel+0xf9/0x160 kernel/cpu.c:1878
bringup_nonboot_cpus+0x2b/0x50 kernel/cpu.c:1892
smp_init+0x34/0x150 kernel/smp.c:1009
kernel_init_freeable+0x417/0x5d0 init/main.c:1569
kernel_init+0x1d/0x2b0 init/main.c:1466
ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244-> #0 (cpu_hotplug_lock){++++}-{0:0}:
check_prev_add kernel/locking/lockdep.c:3161 [inline]
check_prevs_add kernel/locking/lockdep.c:3280 [inline]
validate_chain+0x18ef/0x5920 kernel/locking/lockdep.c:3904
__lock_acquire+0x1397/0x2100 kernel/locking/lockdep.c:5226
lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849
percpu_down_read include/linux/percpu-rwsem.h:51 [inline]
cpus_read_lock+0x42/0x150 kernel/cpu.c:490
acomp_ctx_get_cpu mm/zswap.c:886 [inline]
zswap_compress mm/zswap.c:908 [inline]
zswap_store_page mm/zswap.c:1439 [inline]
zswap_store+0xa74/0x1ba0 mm/zswap.c:1546
swap_writepage+0x647/0xce0 mm/page_io.c:279
shmem_writepage+0x1248/0x1610 mm/shmem.c:1579
pageout mm/vmscan.c:696 [inline]
shrink_folio_list+0x35ee/0x57e0 mm/vmscan.c:1374
shrink_inactive_list mm/vmscan.c:1967 [inline]
shrink_list mm/vmscan.c:2205 [inline]
shrink_lruvec+0x16db/0x2f30 mm/vmscan.c:5734
mem_cgroup_shrink_node+0x385/0x8e0 mm/vmscan.c:6575
mem_cgroup_soft_reclaim mm/memcontrol-v1.c:312 [inline]
memcg1_soft_limit_reclaim+0x346/0x810 mm/memcontrol-v1.c:362
balance_pgdat mm/vmscan.c:6975 [inline]
kswapd+0x17b3/0x2f30 mm/vmscan.c:7253
kthread+0x2f0/0x390 kernel/kthread.c:389
ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(fs_reclaim);
lock(cpu_hotplug_lock);
lock(fs_reclaim); rlock(cpu_hotplug_lock);
*** DEADLOCK ***
1 lock held by kswapd0/89:
#0: ffffffff8ea355a0 (fs_reclaim){+.+.}-{0:0}, at: balance_pgdat mm/vmscan.c:6871 [inline] #0: ffffffff8ea355a0 (fs_reclaim){+.+.}-{0:0}, at: kswapd+0xb58/0x2f30 mm/vmscan.c:7253
stack backtrace: CPU: 0 UID: 0 PID: 89 Comm: kswapd0 Not tainted 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024 Call Trace: <TASK> __dump_stack lib/dump_stack.c:94 [inline] dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120 print_circular_bug+0x13a/0x1b0 kernel/locking/lockdep.c:2074 check_noncircular+0x36a/0x4a0 kernel/locking/lockdep.c:2206 check_prev_add kernel/locking/lockdep.c:3161 [inline] check_prevs_add kernel/locking/lockdep.c:3280 [inline] validate_chain+0x18ef/0x5920 kernel/locking/lockdep.c:3904 __lock_acquire+0x1397/0x2100 kernel/locking/lockdep.c:5226 lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849 percpu_down_read include/linux/percpu-rwsem.h:51 [inline] cpus_read_lock+0x42/0x150 kernel/cpu.c:490 acomp_ctx_get_cpu mm/zswap.c:886 [inline] zswap_compress mm/zswap.c:908 [inline] zswap_store_page mm/zswap.c:1439 [inline] zswap_store+0xa74/0x1ba0 mm/zswap.c:1546 swap_writepage+0x647/0xce0 mm/page_io.c:279 shmem_writepage+0x1248/0x1610 mm/shmem.c:1579 pageout mm/vmscan.c:696 [inline] shrink_folio_list+0x35ee/0x57e0 mm/vmscan.c:1374 shrink_inactive_list mm/vmscan.c:1967 [inline] shrink_list mm/vmscan.c:2205 [inline] shrink_lruvec+0x16db/0x2f30 mm/vmscan.c:5734 mem_cgroup_shrink_node+0x385/0x8e0 mm/vmscan.c:6575 mem_cgroup_soft_reclaim mm/memcontrol-v1.c:312 [inline] memcg1_soft_limit_reclaim+0x346/0x810 mm/memcontrol-v1.c:362 balance_pgdat mm/vmscan.c:6975 [inline] kswapd+0x17b3/0x2f30 mm/vmscan.c:7253 kthread+0x2f0/0x390 kernel/kthread.c:389 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 </TASK>
Revert the change. A different fix for the race with CPU hotunplug will follow.
---
The patches apply on top of mm-hotfixes-unstable and are meant for v6.13.
Andrew, I am not sure what's the best way to handle this. This fix is already merged into Linus's tree and had CC:stable, so I thought it's best to revert it and replace it with a separate fix that would be easy to backport instead of the revert patch, especially that functionally the new fix is different anyway.
v1 -> v2:
- Disable migration as an alternative fix instead of SRCU, and explain why SRCU and cpus_read_lock() cannot be used in the commit log of patch 2.
--- mm/zswap.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)
mm/zswap.c +3 -16
@@ -880,18 +880,6 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } -/* Prevent CPU hotplug from freeing up the per-CPU acomp_ctx resources */ -static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) -{ -cpus_read_lock (); -return raw_cpu_ptr (acomp_ctx); -} - -static void acomp_ctx_put_cpu (void ) -{ -cpus_read_unlock (); -} -static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool) {
@@ -905,7 +893,8 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,gfp_t gfp;u8 *dst; - acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx); + acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); +mutex_lock (&acomp_ctx->mutex); dst = acomp_ctx->buffer;
@@ -961,7 +950,6 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); -acomp_ctx_put_cpu ();return comp_ret ==0 && alloc_ret ==0 ; }
@@ -972,7 +960,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx); + acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );
@@ -1002,7 +990,6 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); -acomp_ctx_put_cpu (); }/*********************************
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH v2 2/2 mm: zswap: disable migration while using per-CPU acomp_ctx
In zswap_compress() and zswap_decompress(), the per-CPU acomp_ctx of the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.
If the original CPU is hotunplugged while the acomp_ctx is still in use, we run into a UAF bug as the resources attached to the acomp_ctx are freed during hotunplug in zswap_cpu_comp_dead().
The problem was introduced in commit 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration") when the switch to the crypto_acomp API was made. Prior to that, the per-CPU crypto_comp was retrieved using get_cpu_ptr() which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with the crypto_acomp API as a sleepable context is needed.
Commit 8ba2f844f050 ("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.
This cannot be fixed by holding cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage of acomp_ctx in an SRCU critical section and using synchronize_srcu() in zswap_cpu_comp_dead(), because synchronize_srcu() is not allowed in CPU-hotplug notifiers (see Documentation/RCU/Design/Requirements/Requirements.rst).
This can be fixed by refcounting the acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.
Keep things simple for now and just disable migration while using the per-CPU acomp_ctx to block CPU hotunplug until the usage is over.
--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
mm/zswap.c +16 -3
@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } +/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable (); +return raw_cpu_ptr (acomp_ctx); +} + +static void acomp_ctx_put_cpu (void ) +{ +migrate_enable (); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool) {
@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - + acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); dst = acomp_ctx->buffer;
@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); +acomp_ctx_put_cpu ();return comp_ret ==0 && alloc_ret ==0 ; }
@@ -960,7 +972,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); + acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );
@@ -990,6 +1002,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); +acomp_ctx_put_cpu (); }/*********************************
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 54 lines · Yosry Ahmed
In
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration")Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Reported-by Sam Sun <[email protected]>--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) · line 886
Quoted 7 lines
mm/zswap.c +4
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } +/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable ();
I'm not entirely sure, but I feel it is quite unsafe. Allowing sleep during migrate_disable() andmigrate_enable() would require the entire scheduler, runqueue, waitqueue, and CPU
hotplug mechanisms to be aware that a task is pinned to a specific CPU.
If there is no sleep during this period, it seems to be only a runqueue issue—CPU hotplug can
wait for the task to be unpinned while it is always in runqueue. However, if sleep is involved,
the situation becomes significantly more complex.
If static data doesn't consume much memory, it could be the simplest solution.
Quoted 47 lines
mm/zswap.c +12
-4+return raw_cpu_ptr (acomp_ctx); +} + +static void acomp_ctx_put_cpu (void ) +{ +migrate_enable (); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool) {@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - + acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); dst = acomp_ctx->buffer;@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); +acomp_ctx_put_cpu ();return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,7 +972,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); + acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );@@ -990,6 +1002,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); +acomp_ctx_put_cpu (); }/********************************* --
Quoted 2 lines
2.47.1.613.gc27f4b7a9f-goog
Thanks
barry
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 70 lines · Barry Song
Quoted 54 lines · Yosry Ahmed
In
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration")Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Reported-by Sam Sun <[email protected]>--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) · line 886
Quoted 7 lines
mm/zswap.c +4
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } +/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable ();I'm not entirely sure, but I feel it is quite unsafe. Allowing sleep during
migrate_disable()andmigrate_enable()would require the entire scheduler, runqueue, waitqueue, and CPU
hotplug mechanisms to be aware that a task is pinned to a specific CPU.
Quoted 5 lines
If there is no sleep during this period, it seems to be only a runqueue issue—CPU hotplug can
wait for the task to be unpinned while it is always in runqueue. However, if sleep is involved,
the situation becomes significantly more complex.
After double-checking the scheduler's code, it seems fine. When a task is scheduled out, __schedule() will set its allowable cpu by:
migrate_disable_switch(rq, prev);
static void migrate_disable_switch(struct rq *rq, struct task_struct *p)
{
struct affinity_context ac = {
.new_mask = cpumask_of(rq->cpu),
.flags = SCA_MIGRATE_DISABLE,
};
if (likely(!p->migration_disabled))
return;
if (p->cpus_ptr != &p->cpus_mask)
return;
/*
* Violates locking rules! see comment in __do_set_cpus_allowed().
*/
__do_set_cpus_allowed(p, &ac);
}while woken-up, the previous cpu will be selected:
/*
* The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable. */
static inline
int select_task_rq(struct task_struct *p, int cpu, int wake_flags) {
lockdep_assert_held(&p->pi_lock);
if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p))
cpu = p->sched_class->select_task_rq(p, cpu, wake_flags);
else
cpu = cpumask_any(p->cpus_ptr);
...
}Anyway, not an expert :-) Hopefully, other experts can provide their input to confirm whether sleeping during migrate_disable() is all good.
Quoted 48 lines
If static data doesn't consume much memory, it could be the simplest solution.
Quoted 45 lines
mm/zswap.c +12
-3+return raw_cpu_ptr (acomp_ctx); +} + +static void acomp_ctx_put_cpu (void ) +{ +migrate_enable (); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool) {@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - + acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); dst = acomp_ctx->buffer;@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); +acomp_ctx_put_cpu ();return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,7 +972,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); + acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );@@ -990,6 +1002,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); +acomp_ctx_put_cpu (); }
Quoted 5 lines
Quoted 4 lines
mm/zswap.c +0
-1/********************************* -- 2.47.1.613.gc27f4b7a9f-goog
Thanks
Barry
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
In reply to Barry SongQuoted 59 lines · Barry Song
Quoted 57 lines · Yosry Ahmed
In
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration")Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Reported-by Sam Sun <[email protected]>--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; }
Quoted 10 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) · line 886
Quoted 4 lines
mm/zswap.c +4
-0+/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable ();I'm not entirely sure, but I feel it is quite unsafe. Allowing sleep during
migrate_disable()andmigrate_enable()would require the entire scheduler, runqueue, waitqueue, and CPU
hotplug mechanisms to be aware that a task is pinned to a specific CPU.
My understanding is that sleeping is already allowed when migration is disabled (unlike preemption). See delete_all_elements() in kernel/bpf/hashtab.c for example, or __bpf_prog_enter_sleepable() in kernel/bpf/trampoline.c. I am not sure exactly what you mean.
Quoted 8 lines
If there is no sleep during this period, it seems to be only a runqueue issue—CPU hotplug can
wait for the task to be unpinned while it is always in runqueue. However, if sleep is involved,
the situation becomes significantly more complex.If static data doesn't consume much memory, it could be the simplest solution.
Do you mean allocating the buffers and requests for all possible CPUs instead of allocating them dynamically in CPU hotplug notifiers? I am not sure how much more memory this would be. Seems like it depends on CONFIG options and the firmware.
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 72 lines · Yosry Ahmed
Quoted 59 lines · Barry Song
Quoted 57 lines · Yosry Ahmed
In
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration")Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Reported-by Sam Sun <[email protected]>--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; }Quoted 10 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) · line 886
Quoted 4 lines
mm/zswap.c +4
-0+/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable ();I'm not entirely sure, but I feel it is quite unsafe. Allowing sleep during
migrate_disable()andmigrate_enable()would require the entire scheduler, runqueue, waitqueue, and CPU
hotplug mechanisms to be aware that a task is pinned to a specific CPU.
Quoted 4 lines
My understanding is that sleeping is already allowed when migration is disabled (unlike preemption). See
delete_all_elements()inkernel/bpf/hashtab.cfor example, or__bpf_prog_enter_sleepable()inkernel/bpf/trampoline.c. I am not sure exactly what you mean.
Initially, I had some doubts about whether the scheduler handled this correctly, but after reviewing the scheduler code again, it seems fine. While a task is dequeued, its cpus_ptr is updated. When the task is woken up, it uses its cpus_ptr instead of selecting a suitable CPU (e.g., idle or wake-affine CPUs). Only after migrate_enable() restores this_rq()->nr_pinned to zero can CPU hotplug take down the CPU.
Therefore, I sent another email to correct myself:
https://lore.kernel.org/linux-mm/CAGsJ_4yb03yo6So-8wZwcy2fa-tURRrgJ+P-XhDL-RHgg1DvVA@mail.gmail.com/
Quoted 8 lines
Quoted 8 lines
If there is no sleep during this period, it seems to be only a runqueue issue—CPU hotplug can
wait for the task to be unpinned while it is always in runqueue. However, if sleep is involved,
the situation becomes significantly more complex.
Quoted 6 lines
Quoted 1 line
If static data doesn't consume much memory, it could be the simplest solution.
Do you mean allocating the buffers and requests for all possible CPUs instead of allocating them dynamically in CPU hotplug notifiers? I am not sure how much more memory this would be. Seems like it depends on CONFIG options and the firmware.
Correct, the firmware/devicetree will help identify all possible CPUs. Even if CONFIG is large, only those possible CPUs will be allocated memory. However, if migrate_disable() and migrate_enable() work as expected, we don't need to consider this option.
Thanks
Barry
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 104 lines · Barry Song
Quoted 72 lines · Yosry Ahmed
Quoted 59 lines · Barry Song
Quoted 57 lines · Yosry Ahmed
In
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration")Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Reported-by Sam Sun <[email protected]>--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; }Quoted 10 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) · line 886
Quoted 4 lines
mm/zswap.c +4
-0+/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable ();I'm not entirely sure, but I feel it is quite unsafe. Allowing sleep during
migrate_disable()andmigrate_enable()would require the entire scheduler, runqueue, waitqueue, and CPU
hotplug mechanisms to be aware that a task is pinned to a specific CPU.Quoted 4 lines
My understanding is that sleeping is already allowed when migration is disabled (unlike preemption). See
delete_all_elements()inkernel/bpf/hashtab.cfor example, or__bpf_prog_enter_sleepable()inkernel/bpf/trampoline.c. I am not sure exactly what you mean.Initially, I had some doubts about whether the scheduler handled this correctly, but after reviewing the scheduler code again, it seems fine. While a task is dequeued, its
cpus_ptris updated. When the task is woken up, it uses itscpus_ptrinstead of selecting a suitable CPU (e.g., idle or wake-affine CPUs). Only aftermigrate_enable()restores this_rq()->nr_pinned to zero can CPU hotplug take down the CPU.Therefore, I sent another email to correct myself:
https://lore.kernel.org/linux-mm/CAGsJ_4yb03yo6So-8wZwcy2fa-tURRrgJ+P-XhDL-RHgg1DvVA@mail.gmail.com/Quoted 8 lines
Quoted 8 lines
If there is no sleep during this period, it seems to be only a runqueue issue—CPU hotplug can
wait for the task to be unpinned while it is always in runqueue. However, if sleep is involved,
the situation becomes significantly more complex.Quoted 6 lines
Quoted 1 line
If static data doesn't consume much memory, it could be the simplest solution.
Do you mean allocating the buffers and requests for all possible CPUs instead of allocating them dynamically in CPU hotplug notifiers? I am not sure how much more memory this would be. Seems like it depends on CONFIG options and the firmware.
Quoted 4 lines
Correct, the
firmware/devicetreewill help identify all possible CPUs. Even if CONFIG is large, only those possible CPUs will be allocated memory. However, ifmigrate_disable()andmigrate_enable()work as expected, we don't need to consider this option.
By the way, there could be a slight performance regression if the previous CPU is occupied with other tasks, as we would have to wait for preemption. However, when migrate_disable() is not in effect, the woken-up task can quickly select a neighboring idle CPU and continue execution.
When the entire system is not busy, there might be no difference. However, when the system is packed with tasks, it could make a difference. Hopefully, the regression is small enough to be negligible :-)
Thanks
Barry
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 111 lines · Barry Song
Quoted 104 lines · Barry Song
Quoted 72 lines · Yosry Ahmed
Quoted 59 lines · Barry Song
Quoted 57 lines · Yosry Ahmed
In
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API for hardware acceleration")Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Reported-by Sam Sun <[email protected]>--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; }Quoted 10 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) · line 886
Quoted 4 lines
mm/zswap.c +4
-0+/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable ();I'm not entirely sure, but I feel it is quite unsafe. Allowing sleep during
migrate_disable()andmigrate_enable()would require the entire scheduler, runqueue, waitqueue, and CPU
hotplug mechanisms to be aware that a task is pinned to a specific CPU.Quoted 4 lines
My understanding is that sleeping is already allowed when migration is disabled (unlike preemption). See
delete_all_elements()inkernel/bpf/hashtab.cfor example, or__bpf_prog_enter_sleepable()inkernel/bpf/trampoline.c. I am not sure exactly what you mean.Initially, I had some doubts about whether the scheduler handled this correctly, but after reviewing the scheduler code again, it seems fine. While a task is dequeued, its
cpus_ptris updated. When the task is woken up, it uses itscpus_ptrinstead of selecting a suitable CPU (e.g., idle or wake-affine CPUs). Only aftermigrate_enable()restores this_rq()->nr_pinned to zero can CPU hotplug take down the CPU.Therefore, I sent another email to correct myself:
https://lore.kernel.org/linux-mm/CAGsJ_4yb03yo6So-8wZwcy2fa-tURRrgJ+P-XhDL-RHgg1DvVA@mail.gmail.com/Quoted 8 lines
Quoted 8 lines
If there is no sleep during this period, it seems to be only a runqueue issue—CPU hotplug can
wait for the task to be unpinned while it is always in runqueue. However, if sleep is involved,
the situation becomes significantly more complex.Quoted 6 lines
Quoted 1 line
If static data doesn't consume much memory, it could be the simplest solution.
Do you mean allocating the buffers and requests for all possible CPUs instead of allocating them dynamically in CPU hotplug notifiers? I am not sure how much more memory this would be. Seems like it depends on CONFIG options and the firmware.
Quoted 4 lines
Correct, the
firmware/devicetreewill help identify all possible CPUs. Even if CONFIG is large, only those possible CPUs will be allocated memory. However, ifmigrate_disable()andmigrate_enable()work as expected, we don't need to consider this option.
Quoted 8 lines
By the way, there could be a slight performance regression if the previous CPU is occupied with other tasks, as we would have to wait for preemption. However, when
migrate_disable()is not in effect, the woken-up task can quickly select a neighboring idle CPU and continue execution.When the entire system is not busy, there might be no difference. However, when the system is packed with tasks, it could make a difference. Hopefully, the regression is small enough to be negligible :-)
Hopefully the robot will let us know if it isn't :)
RE: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
In reply to Yosry AhmedHi Yosry,
Quoted 106 lines
-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, January 7, 2025 2:23 PM
To: Andrew Morton <[email protected]>
Cc: Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Vitaly Wool <[email protected]>; Barry Song <[email protected]>; Sam Sun <[email protected]>; Sridhar, Kanchana P <[email protected]>; [email protected]; linux- [email protected]; Yosry Ahmed <[email protected]>; [email protected]
Subject: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPUacomp_ctxIn
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the
current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API forhardware acceleration")
Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Closes:
https://lore.kernel.org/lkml/[email protected]/Reported-by Sam Sun <[email protected]>Closes:
https://lore.kernel.org/lkml/CAEkJfYMtSdM5HceNsXUDf5haghD5+o2e7Qv4O
[email protected]/--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +12
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } +/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable (); +return raw_cpu_ptr (acomp_ctx); +} + +static void acomp_ctx_put_cpu (void ) +{ +migrate_enable (); +} +static bool zswap_compress(struct page *page, struct
zswap_entry*entry,mm/zswap.c +1
-2struct zswap_pool *pool) {@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page, structzswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - + acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex);mm/zswap.c +0
-0dst = acomp_ctx->buffer;
mm/zswap.c static bool zswap_compress(struct page *page, struct · line 965
Quoted 5 lines
mm/zswap.c +1
-0@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); +acomp_ctx_put_cpu ();
I have observed that disabling/enabling preemption in this portion of the code can result in scheduling while atomic errors. Is the same possible while disabling/enabling migration?
Couple of possibly related thoughts:
1) I have been thinking some more about the purpose of this per-cpu acomp_ctx mutex. It appears that the main benefit is it causes task blocked errors (which are useful to detect problems) if any computes in the code section it covers take a long duration. Other than that, it does not protect a resource, nor prevent cpu offlining from deleting its containing structure.
2) Seems like the overall problem appears to be applicable to any per-cpu data that is being used by a process, vis-a-vis cpu hotunplug. Could it be that a solution in cpu hotunplug can safe-guard more generally? Really not sure about the specifics of any solution, but it occurred to me that this may not be a problem unique to zswap.
Thanks,
Kanchana
Quoted 24 lines
mm/zswap.c +1
-1return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,7 +972,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); + acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex);mm/zswap.c +1
-0src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );@@ -990,6 +1002,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); +acomp_ctx_put_cpu (); }/*********************************
--
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 112 lines · Kanchana P
Hi Yosry,
Quoted 109 lines
-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, January 7, 2025 2:23 PM
To: Andrew Morton <[email protected]>
Cc: Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Vitaly Wool <[email protected]>; Barry Song <[email protected]>; Sam Sun <[email protected]>; Sridhar, Kanchana P <[email protected]>; [email protected]; linux- [email protected]; Yosry Ahmed <[email protected]>; [email protected]
Subject: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPUacomp_ctxIn
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the
current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration are disabled, it is possible that the operation continues on a different CPU.If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxare freed during hotunplug inzswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: move to usecrypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas retrieved usingget_cpu_ptr()which disables preemption and makes sure the CPU cannot go away from under us. Preemption cannot be disabled with thecrypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed from under zswap compression/decompression by CPU hotunplug.This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API forhardware acceleration")
Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Closes:
https://lore.kernel.org/lkml/[email protected]/Reported-by Sam Sun <[email protected]>Closes:
https://lore.kernel.org/lkml/CAEkJfYMtSdM5HceNsXUDf5haghD5+o2e7Qv4O
[email protected]/--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +12
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } +/* Remain on the CPU while using its acomp_ctx to stop it from going offline */ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable (); +return raw_cpu_ptr (acomp_ctx); +} + +static void acomp_ctx_put_cpu (void ) +{ +migrate_enable (); +} +static bool zswap_compress(struct page *page, struct
zswap_entry*entry,
structzswap_pool*pool)
{mm/zswap.c +1
-2@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page, structzswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - + acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex);dst = acomp_ctx->buffer;mm/zswap.c +0
-0@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;
Quoted 6 lines
mm/zswap.c static bool zswap_compress(struct page *page, struct zswap_entry *entry, · line 964
Quoted 2 lines
mm/zswap.c +1
-0mutex_unlock (&acomp_ctx->mutex); +acomp_ctx_put_cpu ();I have observed that disabling/enabling preemption in this portion of the code can result in scheduling while atomic errors. Is the same possible while disabling/enabling migration?
IIUC no, migration disabled is not an atomic context.
Quoted 8 lines
Couple of possibly related thoughts:
1) I have been thinking some more about the purpose of this per-cpu
acomp_ctxmutex. It appears that the main benefit is it causes task blocked errors (which are useful to detect problems) if any computes in the code section it covers take a long duration. Other than that, it does not protect a resource, nor prevent cpu offlining from deleting its containing structure.
It does protect resources. Consider this case:
- Process A runs on CPU #1, gets the acomp_ctx, and locks it, then is migrated to CPU #2.
- Process B runs on CPU #1, gets the same acomp_ctx, and tries to lock it then waits for process A. Without the lock they would be using the same lock.
It is also possible that process A simply gets preempted while running on CPU #1 by another task that also tries to use the acomp_ctx. The mutex also protects against this case.
Quoted 6 lines
2) Seems like the overall problem appears to be applicable to any per-cpu data that is being used by a process, vis-a-vis cpu hotunplug. Could it be that a solution in cpu hotunplug can safe-guard more generally? Really not sure about the specifics of any solution, but it occurred to me that this may not be a problem unique to zswap.
Not really. Static per-CPU data and data allocated with alloc_percpu() should be available for all possible CPUs, regardless of whether they are online or not, so CPU hotunplug is not relevant. It is relevant here because we allocate the memory dynamically for online CPUs only to save memory. I am not sure how important this is as I am not aware what the difference between the number of online and possible CPUs can be in real life deployments.
Quoted 29 lines
Thanks,
KanchanaQuoted 24 lines
mm/zswap.c +1
-1return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,7 +972,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); + acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex);src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);mm/zswap.c +1
-0@@ -990,6 +1002,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); +acomp_ctx_put_cpu (); }/*********************************
--
2.47.1.613.gc27f4b7a9f-goog
RE: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 146 lines
-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, January 7, 2025 4:12 PM
To: Sridhar, Kanchana P <[email protected]>
Cc: Andrew Morton <[email protected]>; Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Vitaly Wool <[email protected]>; Barry Song <[email protected]>; Sam Sun <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2 2/2] mm: zswap: disable migration while using per- CPUacomp_ctxQuoted 10 lines · Kanchana P
Hi Yosry,
Quoted 7 lines
-----Original Message-----
From: Yosry Ahmed <[email protected]>
Sent: Tuesday, January 7, 2025 2:23 PM
To: Andrew Morton <[email protected]>
Cc: Johannes Weiner <[email protected]>; Nhat Pham <[email protected]>; Chengming Zhou <[email protected]>; Vitaly Wool <[email protected]>; Barry Song <[email protected]>;Sam
Quoted 5 lines
Quoted 5 lines
Sun <[email protected]>; Sridhar, Kanchana P <[email protected]>; [email protected]; linux- [email protected]; Yosry Ahmed <[email protected]>; [email protected]
Subject: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU
Quoted 6 lines
Quoted 6 lines
acomp_ctxIn
zswap_compress()andzswap_decompress(), the per-CPUacomp_ctxof the
current CPU at the beginning of the operation is retrieved and used throughout. However, since neither preemption nor migration aredisabled,
Quoted 4 lines
Quoted 4 lines
it is possible that the operation continues on a different CPU.
If the original CPU is hotunplugged while the
acomp_ctxis still in use, we run into a UAF bug as the resources attached to theacomp_ctxarefreed
Quoted 3 lines
Quoted 3 lines
during hotunplug in
zswap_cpu_comp_dead().The problem was introduced in commit
1ec3b5fe6eec("mm/zswap: moveto
Quoted 3 lines
Quoted 3 lines
use
crypto_acompAPI for hardware acceleration") when the switch to thecrypto_acompAPI was made. Prior to that, the per-CPUcrypto_compwas
Quoted 1 line
Quoted 1 line
retrieved using
get_cpu_ptr()which disables preemption and makes surethe
Quoted 1 line
Quoted 1 line
CPU cannot go away from under us. Preemption cannot be disabled with
the
Quoted 5 lines
Quoted 5 lines
crypto_acompAPI as a sleepable context is needed.Commit
8ba2f844f050("mm/zswap: change per-cpu mutex and buffer to per-acomp_ctx") increased the UAF surface area by making the per-CPU buffers dynamic, adding yet another resource that can be freed fromunder
Quoted 18 lines
Quoted 18 lines
zswap compression/decompression by CPU hotunplug.
This cannot be fixed by holding
cpus_read_lock(), as it is possible for code already holding the lock to fall into reclaim and enter zswap (causing a deadlock). It also cannot be fixed by wrapping the usage ofacomp_ctxin an SRCU critical section and usingsynchronize_srcu()inzswap_cpu_comp_dead(), becausesynchronize_srcu()is not allowed in CPU-hotplug notifiers (seeDocumentation/RCU/Design/Requirements/Requirements.rst).This can be fixed by refcounting the
acomp_ctx, but it involves complexity in handling the race between the refcount dropping to zero in zswap_[de]compress() and the refcount being re-initialized when the CPU is onlined.Keep things simple for now and just disable migration while using the per-CPU
acomp_ctxto block CPU hotunplug until the usage is over.Quoted 7 lines
Quoted 7 lines
Fixes 1ec3b5fe6eec ("mm/zswap: move to use crypto_acomp API forhardware acceleration")
Cc <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>Reported-by Johannes Weiner <[email protected]>Closes:
https://lore.kernel.org/lkml/[email protected]/
Quoted 3 lines
Quoted 3 lines
Reported-by Sam Sun <[email protected]>Closes:
https://lore.kernel.org/lkml/CAEkJfYMtSdM5HceNsXUDf5haghD5+o2e7Qv4O
Quoted 10 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int
Quoted 10 lines
--- mm/zswap.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)mm/zswap.c +0
-0@@ -880,6 +880,18 @@ static int zswap_cpu_comp_dead(unsigned intcpu,
Quoted 5 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
Quoted 5 lines
mm/zswap.c +1
-0struct hlist_node *node)return 0 ; } +/* Remain on the CPU while using its acomp_ctx to stop it from going offline
Quoted 6 lines
Quoted 6 lines
mm/zswap.c +4
-0*/ +static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +migrate_disable (); +return raw_cpu_ptr (acomp_ctx);Quoted 8 lines
Quoted 8 lines
mm/zswap.c +7
-0+} + +static void acomp_ctx_put_cpu (void ) +{ +migrate_enable (); +} +static bool zswap_compress(struct page *page, struct
zswap_entry*entry,
Quoted 3 lines
mm/zswap.c static bool zswap_compress(struct page *page,
Quoted 3 lines
struct
zswap_pool*pool)
{mm/zswap.c +0
-0@@ -893,8 +905,7 @@ static bool zswap_compress(struct page *page,struct
Quoted 11 lines
mm/zswap.c static bool zswap_compress(struct page *page,
Quoted 11 lines
mm/zswap.c +1
-2zswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - + acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex);dst = acomp_ctx->buffer;mm/zswap.c +0
-0@@ -950,6 +961,7 @@ static bool zswap_compress(struct page *page,struct
Quoted 6 lines
Quoted 5 lines
mm/zswap.c +1
-0zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); +acomp_ctx_put_cpu ();
Quoted 6 lines
Quoted 1 line
I have observed that disabling/enabling preemption in this portion of the
code
Quoted 2 lines
can result in scheduling while atomic errors. Is the same possible while disabling/enabling migration?
IIUC no, migration disabled is not an atomic context.
Ok, thanks.
Quoted 21 lines
Quoted 5 lines
Couple of possibly related thoughts:
1) I have been thinking some more about the purpose of this per-cpu
acomp_ctxQuoted 1 line
mutex. It appears that the main benefit is it causes task blocked errors
(which are
Quoted 1 line
useful to detect problems) if any computes in the code section it covers
take a
Quoted 1 line
long duration. Other than that, it does not protect a resource, nor
prevent
Quoted 1 line
cpu offlining from deleting its containing structure.
It does protect resources. Consider this case:
- Process A runs on CPU #1, gets theacomp_ctx, and locks it, then is migrated to CPU #2.
- Process B runs on CPU #1, gets the sameacomp_ctx, and tries to lock it then waits for process A. Without the lock they would be using the same lock.
Quoted 3 lines
It is also possible that process A simply gets preempted while running on CPU #1 by another task that also tries to use the
acomp_ctx. The mutex also protects against this case.
Got it, thanks for the explanations. It seems with this patch, the mutex would be redundant in the first example. Would this also be true of the second example where process A gets preempted? If not, is it worth figuring out a solution that works for both migration and preemption?
Quoted 10 lines
Quoted 3 lines
2) Seems like the overall problem appears to be applicable to any per-cpu
data
Quoted 3 lines
that is being used by a process, vis-a-vis cpu hotunplug. Could it be that a solution in cpu hotunplug can safe-guard more generally? Really not sure about the specifics of any solution, but it occurred to me that this may
not
Quoted 1 line
be a problem unique to zswap.
Quoted 7 lines
Not really. Static per-CPU data and data allocated with
alloc_percpu()should be available for all possible CPUs, regardless of whether they are online or not, so CPU hotunplug is not relevant. It is relevant here because we allocate the memory dynamically for online CPUs only to save memory. I am not sure how important this is as I am not aware what the difference between the number of online and possible CPUs can be in real life deployments.
Thought I would clarify what I meant: the problem of per-cpu data that gets allocated dynamically using cpu hotplug and deleted even while in use by cpu hotunplug may not be unique to zswap. If so, I was wondering if a more generic solution in the cpu hotunplug code would be feasible/worth exploring.
Thanks,
Kanchana
Quoted 32 lines
Quoted 9 lines
Thanks,
Kanchanamm/zswap.c static void zswap_decompress(struct
Quoted 4 lines
mm/zswap.c +0
-0return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,7 +972,7 @@ static void zswap_decompress(struct
zswap_entryQuoted 10 lines
mm/zswap.c static void zswap_decompress(struct
Quoted 10 lines
mm/zswap.c +1
-1*entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); + acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex);src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);mm/zswap.c +0
-0@@ -990,6 +1002,7 @@ static void zswap_decompress(struct
zswap_entryQuoted 11 lines
Quoted 10 lines
mm/zswap.c +1
-0*entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); +acomp_ctx_put_cpu (); }/*********************************
--
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
[..]
Quoted 19 lines
Quoted 19 lines
Quoted 3 lines
Couple of possibly related thoughts:
1) I have been thinking some more about the purpose of this per-cpu
acomp_ctxQuoted 1 line
mutex. It appears that the main benefit is it causes task blocked errors
(which are
Quoted 1 line
useful to detect problems) if any computes in the code section it covers
take a
Quoted 1 line
long duration. Other than that, it does not protect a resource, nor
prevent
Quoted 1 line
cpu offlining from deleting its containing structure.
It does protect resources. Consider this case:
- Process A runs on CPU #1, gets theacomp_ctx, and locks it, then is migrated to CPU #2.
- Process B runs on CPU #1, gets the sameacomp_ctx, and tries to lock it then waits for process A. Without the lock they would be using the same lock.
Quoted 7 lines
Quoted 3 lines
It is also possible that process A simply gets preempted while running on CPU #1 by another task that also tries to use the
acomp_ctx. The mutex also protects against this case.Got it, thanks for the explanations. It seems with this patch, the mutex would be redundant in the first example. Would this also be true of the second example where process A gets preempted?
I think the mutex is still required in the second example. Migration being disabled does not prevent other processes from running on the same CPU and attempting to use the same acomp_ctx.
Quoted 2 lines
If not, is it worth
figuring out a solution that works for both migration and preemption?
Not sure exactly what you mean here. I suspect you mean have a single mechanism to protect against concurrent usage and CPU hotunplug rather than disabling migration and having a mutex. Yeah that would be ideal, but probably not for a hotfix.
Quoted 19 lines
Quoted 11 lines
Quoted 4 lines
2) Seems like the overall problem appears to be applicable to any per-cpu
data
Quoted 3 lines
that is being used by a process, vis-a-vis cpu hotunplug. Could it be that a solution in cpu hotunplug can safe-guard more generally? Really not sure about the specifics of any solution, but it occurred to me that this may
not
Quoted 1 line
be a problem unique to zswap.
Quoted 7 lines
Not really. Static per-CPU data and data allocated with
alloc_percpu()should be available for all possible CPUs, regardless of whether they are online or not, so CPU hotunplug is not relevant. It is relevant here because we allocate the memory dynamically for online CPUs only to save memory. I am not sure how important this is as I am not aware what the difference between the number of online and possible CPUs can be in real life deployments.
Quoted 5 lines
Thought I would clarify what I meant: the problem of per-cpu data that gets allocated dynamically using cpu hotplug and deleted even while in use by cpu hotunplug may not be unique to zswap. If so, I was wondering if a more generic solution in the cpu hotunplug code would be feasible/worth exploring.
I didn't look too closely, if there's something out there or something can be easily developed I'd be open to updating the zswap code accordingly, but I don't have time to look into it tbh, and it's too late in the release cycle to get creative imo.
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 33 lines · Yosry Ahmed
[..]
Quoted 19 lines
Quoted 19 lines
Quoted 3 lines
Couple of possibly related thoughts:
1) I have been thinking some more about the purpose of this per-cpu
acomp_ctxQuoted 1 line
mutex. It appears that the main benefit is it causes task blocked errors
(which are
Quoted 1 line
useful to detect problems) if any computes in the code section it covers
take a
Quoted 1 line
long duration. Other than that, it does not protect a resource, nor
prevent
Quoted 1 line
cpu offlining from deleting its containing structure.
It does protect resources. Consider this case:
- Process A runs on CPU #1, gets theacomp_ctx, and locks it, then is migrated to CPU #2.
- Process B runs on CPU #1, gets the sameacomp_ctx, and tries to lock it then waits for process A. Without the lock they would be using the same lock.Quoted 7 lines
Quoted 3 lines
It is also possible that process A simply gets preempted while running on CPU #1 by another task that also tries to use the
acomp_ctx. The mutex also protects against this case.Got it, thanks for the explanations. It seems with this patch, the mutex would be redundant in the first example. Would this also be true of the second example where process A gets preempted?
I think the mutex is still required in the second example. Migration being disabled does not prevent other processes from running on the same CPU and attempting to use the same
acomp_ctx.
Quoted 7 lines
Quoted 2 lines
If not, is it worth
figuring out a solution that works for both migration and preemption?Not sure exactly what you mean here. I suspect you mean have a single mechanism to protect against concurrent usage and CPU hotunplug rather than disabling migration and having a mutex. Yeah that would be ideal, but probably not for a hotfix.
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
mm/zswap.c +0 -0
@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu,
struct hlist_node *node)
struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);
struct crypto_acomp_ctx *acomp_ctx = per_cpu_ptr(pool->acomp_ctx, cpu);+ mutex_lock(&acomp_ctx->mutex);
if (!IS_ERR_OR_NULL(acomp_ctx)) {
if (!IS_ERR_OR_NULL(acomp_ctx->req))
acomp_request_free(acomp_ctx->req);
+ acomp_ctx->req = NULL;
if (!IS_ERR_OR_NULL(acomp_ctx->acomp))
crypto_free_acomp(acomp_ctx->acomp);
kfree(acomp_ctx->buffer);
}+ mutex_unlock(&acomp_ctx->mutex);
return 0;
}
+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked(
+ struct crypto_acomp_ctx __percpu *acomp_ctx)
+{
+ struct crypto_acomp_ctx *ctx;
+
+ for (;;) {
+ ctx = raw_cpu_ptr(acomp_ctx); + mutex_lock(&ctx->mutex); + if (likely(ctx->req))
+ return ctx;
+ /* Raced with zswap_cpu_comp_dead() on CPU hotunplug */
+ mutex_unlock(&ctx->mutex);
+ }
+}
+
+static void acomp_ctx_put_unlock(struct crypto_acomp_ctx *ctx) +{
+ mutex_unlock(&ctx->mutex);
+}
+
static bool zswap_compress(struct page *page, struct zswap_entry *entry,
struct zswap_pool *pool)
{
@@ -893,10 +916,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
gfp_t gfp;
u8 *dst;- acomp_ctx = raw_cpu_ptr(pool->acomp_ctx);
-
- mutex_lock(&acomp_ctx->mutex);
-
+ acomp_ctx = acomp_ctx_get_cpu_locked(pool->acomp_ctx);
dst = acomp_ctx->buffer;
sg_init_table(&input, 1);
sg_set_page(&input, page, PAGE_SIZE, 0);@@ -949,7 +969,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
else if (alloc_ret)
zswap_reject_alloc_fail++;
- mutex_unlock(&acomp_ctx->mutex);
+ acomp_ctx_put_unlock(acomp_ctx);
return comp_ret == 0 && alloc_ret == 0;}
@@ -960,9 +980,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)
struct crypto_acomp_ctx *acomp_ctx;
u8 *src;- acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
- mutex_lock(&acomp_ctx->mutex);
-
+ acomp_ctx = acomp_ctx_get_cpu_locked(entry->pool->acomp_ctx);
src = zpool_map_handle(zpool, entry->handle, ZPOOL_MM_RO);
/*Nhat Pham
maintainerRe: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 22 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ;
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 10 lines
mm/zswap.c +8
-0} +static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx; + +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);
I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
If it is somehow protected, then yeah this seems quite elegant :)
Quoted 49 lines
mm/zswap.c +12
-0+if (likely (ctx->req)) +return ctx; +/* Raced with zswap_cpu_comp_dead() on CPU hotunplug */ +mutex_unlock (&ctx->mutex); + } +} + +static void acomp_ctx_put_unlock (struct crypto_acomp_ctx *ctx) +{ +mutex_unlock (&ctx->mutex); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool){
mm/zswap.c +1
-4@@ -893,10 +916,7 @@ static bool zswap_compress(struct page *page,struct zswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (pool->acomp_ctx); dst = acomp_ctx->buffer;sg_init_table (&input,1 );sg_set_page(&input, page, PAGE_SIZE, 0);mm/zswap.c +2
-4@@ -949,7 +969,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, else if (alloc_ret) zswap_reject_alloc_fail++; - mutex_unlock(&acomp_ctx->mutex); + acomp_ctx_put_unlock(acomp_ctx);return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,9 +980,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (entry->pool->acomp_ctx); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );/*
Chengming Zhou
reviewerRe: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 34 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 24 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ; }
Quoted 12 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 8 lines
mm/zswap.c +8
-0+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx; + +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Yeah, it's not safe, we can only use this_cpu_ptr(), which will disable preempt (so cpu offline can't kick in), and get refcount of ctx. Since we can't mutex_lock in the preempt disabled section.
Thanks.
Quoted 52 lines
If it is somehow protected, then yeah this seems quite elegant :)
Quoted 49 lines
mm/zswap.c +12
-0+if (likely (ctx->req)) +return ctx; +/* Raced with zswap_cpu_comp_dead() on CPU hotunplug */ +mutex_unlock (&ctx->mutex); + } +} + +static void acomp_ctx_put_unlock (struct crypto_acomp_ctx *ctx) +{ +mutex_unlock (&ctx->mutex); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool){mm/zswap.c +1
-4@@ -893,10 +916,7 @@ static bool zswap_compress(struct page *page,struct zswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (pool->acomp_ctx); dst = acomp_ctx->buffer;sg_init_table (&input,1 );sg_set_page(&input, page, PAGE_SIZE, 0);mm/zswap.c +2
-4@@ -949,7 +969,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, else if (alloc_ret) zswap_reject_alloc_fail++; - mutex_unlock(&acomp_ctx->mutex); + acomp_ctx_put_unlock(acomp_ctx);return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,9 +980,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (entry->pool->acomp_ctx); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );/*
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 36 lines · Chengming Zhou
Quoted 34 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 24 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ; }
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
mm/zswap.c +4
-0+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx;Quoted 8 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 4 lines
mm/zswap.c +4
-0+ +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Please correct me if I am wrong, but my understanding is that memory allocated with alloc_percpu() is allocated for each *possible* CPU, and does not go away when CPUs are offlined. We allocate the per-CPU crypto_acomp_ctx structs with alloc_percpu() (including the mutex), so they should not go away with CPU offlining.
OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, and crypto_acomp_ctx.buffer only for online CPUs through the CPU hotplug notifiers (i.e. zswap_cpu_comp_prepare() and zswap_cpu_comp_dead()). These are the resources that can go away with CPU offlining, and what we need to protect about.
The approach I am taking here is to hold the per-CPU mutex in the CPU offlining code while we free these resources, and set crypto_acomp_ctx.req to NULL. In acomp_ctx_get_cpu_locked(), we hold the mutex of the current CPU, and check if crypto_acomp_ctx.req is NULL.
If it is NULL, then the CPU is offlined between raw_cpu_ptr() and acquiring the mutex, and we retry on the new CPU that we end up on. If it is not NULL, then we are guaranteed that the resources will not be freed by CPU offlining until acomp_ctx_put_unlock() is called and the mutex is unlocked.
Quoted 4 lines
Yeah, it's not safe, we can only use
this_cpu_ptr(), which will disable preempt (so cpu offline can't kick in), and get refcount of ctx. Since we can'tmutex_lockin the preempt disabled section.
My understanding is that the purpose of this_cpu_ptr() disabling preemption is to prevent multiple CPUs accessing per-CPU data of a single CPU concurrently. In the zswap case, we don't really need that because we use the mutex to protect against it (and we cannot disable preemption anyway).
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 57 lines · Yosry Ahmed
Quoted 36 lines · Chengming Zhou
Quoted 34 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 24 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ; }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
mm/zswap.c +4
-0+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx;Quoted 8 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 4 lines
mm/zswap.c +4
-0+ +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Please correct me if I am wrong, but my understanding is that memory allocated with
alloc_percpu()is allocated for each *possible* CPU, and does not go away when CPUs are offlined. We allocate the per-CPUcrypto_acomp_ctxstructs withalloc_percpu()(including the mutex), so they should not go away with CPU offlining.
Quoted 5 lines
OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, and crypto_acomp_ctx.buffer only for online CPUs through the CPU hotplug notifiers (i.e.
zswap_cpu_comp_prepare()andzswap_cpu_comp_dead()). These are the resources that can go away with CPU offlining, and what we need to protect about.
..and now that I explain all of this I am wondering if the complexity is warranted in the first place. It goes back all the way to the first zswap commit, so I can't tell the justification for it.
I am not sure if they are setups that have significantly different numbers of online and possible CPUs. Maybe we should just bite the bullet and just allocate everything with alloc_percpu() and rip out the hotplug callbacks completely.
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 65 lines · Yosry Ahmed
Quoted 57 lines · Yosry Ahmed
Quoted 36 lines · Chengming Zhou
Quoted 34 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 24 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ; }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
mm/zswap.c +4
-0+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx;Quoted 8 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 4 lines
mm/zswap.c +4
-0+ +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Please correct me if I am wrong, but my understanding is that memory allocated with
alloc_percpu()is allocated for each *possible* CPU, and does not go away when CPUs are offlined. We allocate the per-CPUcrypto_acomp_ctxstructs withalloc_percpu()(including the mutex), so they should not go away with CPU offlining.Quoted 5 lines
OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, and crypto_acomp_ctx.buffer only for online CPUs through the CPU hotplug notifiers (i.e.
zswap_cpu_comp_prepare()andzswap_cpu_comp_dead()). These are the resources that can go away with CPU offlining, and what we need to protect about.
Quoted 3 lines
..and now that I explain all of this I am wondering if the complexity is warranted in the first place. It goes back all the way to the first zswap commit, so I can't tell the justification for it.
Personally, I would vote for the added complexity, as it avoids the potential negative side effects of reverting the scheduler's optimization for selecting a suitable CPU for a woken-up task and I have been looking for an approach to resolve it by cpuhotplug lock (obviously quite hacky and more complex than using this mutex)
for (;;) in acomp_ctx_get_cpu_locked() is a bit tricky but correct and really interesting, maybe it needs some comments.
Quoted 5 lines
I am not sure if they are setups that have significantly different numbers of online and possible CPUs. Maybe we should just bite the bullet and just allocate everything with
alloc_percpu()and rip out the hotplug callbacks completely.
Thanks
Barry
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 71 lines · Barry Song
Quoted 65 lines · Yosry Ahmed
Quoted 57 lines · Yosry Ahmed
Quoted 36 lines · Chengming Zhou
Quoted 34 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 24 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ; }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
mm/zswap.c +4
-0+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx;Quoted 8 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 4 lines
mm/zswap.c +4
-0+ +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Please correct me if I am wrong, but my understanding is that memory allocated with
alloc_percpu()is allocated for each *possible* CPU, and does not go away when CPUs are offlined. We allocate the per-CPUcrypto_acomp_ctxstructs withalloc_percpu()(including the mutex), so they should not go away with CPU offlining.Quoted 5 lines
OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, and crypto_acomp_ctx.buffer only for online CPUs through the CPU hotplug notifiers (i.e.
zswap_cpu_comp_prepare()andzswap_cpu_comp_dead()). These are the resources that can go away with CPU offlining, and what we need to protect about.Quoted 3 lines
..and now that I explain all of this I am wondering if the complexity is warranted in the first place. It goes back all the way to the first zswap commit, so I can't tell the justification for it.
Quoted 5 lines
Personally, I would vote for the added complexity, as it avoids the potential negative side effects of reverting the scheduler's optimization for selecting a suitable CPU for a woken-up task and I have been looking for an approach to resolve it by cpuhotplug lock (obviously quite hacky and more complex than using this mutex)
Oh, I was not talking about my proposed diff, but the existing logic that allocates the requests and buffers in the hotplug callbacks instead of just using alloc_percpu() to allocate them once for each possible CPU. I was wondering if there are actual setups where this matters and a significant amount of memory is being saved. Otherwise we should simplify things and just rip out the hotplug callbacks.
Anyway, for now I will cleanup and send the mutex diff as a new patch.
Quoted 12 lines
for (;;) in
acomp_ctx_get_cpu_locked()is a bit tricky but correct and really interesting, maybe it needs some comments.Quoted 5 lines
I am not sure if they are setups that have significantly different numbers of online and possible CPUs. Maybe we should just bite the bullet and just allocate everything with
alloc_percpu()and rip out the hotplug callbacks completely.Thanks
Barry
Nhat Pham
maintainerRe: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 8 lines · Yosry Ahmed
Oh, I was not talking about my proposed diff, but the existing logic that allocates the requests and buffers in the hotplug callbacks instead of just using
alloc_percpu()to allocate them once for each possible CPU. I was wondering if there are actual setups where this matters and a significant amount of memory is being saved. Otherwise we should simplify things and just rip out the hotplug callbacks.
My vote is for ripping the hotplug callbacks (eventually) :) In addition to the discrepancy in the number of possible and online CPUs, we also need a relatively smaller memory size for the discrepancy to matter, no? Systems with hundreds of CPUs (hopefully) should have hundreds of GBs worth of memory available (if not more).
Anyhow, we can just go with the diff you sent for now (and for past kernels). Seems simple enough, and wouldn't get in the way of the eventual hotplug logic removal (if you decide to pursue it).
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 17 lines · Nhat Pham
Quoted 8 lines · Yosry Ahmed
Oh, I was not talking about my proposed diff, but the existing logic that allocates the requests and buffers in the hotplug callbacks instead of just using
alloc_percpu()to allocate them once for each possible CPU. I was wondering if there are actual setups where this matters and a significant amount of memory is being saved. Otherwise we should simplify things and just rip out the hotplug callbacks.My vote is for ripping the hotplug callbacks (eventually) :) In addition to the discrepancy in the number of possible and online CPUs, we also need a relatively smaller memory size for the discrepancy to matter, no? Systems with hundreds of CPUs (hopefully) should have hundreds of GBs worth of memory available (if not more).
Quoted 3 lines
Anyhow, we can just go with the diff you sent for now (and for past kernels). Seems simple enough, and wouldn't get in the way of the eventual hotplug logic removal (if you decide to pursue it).
Yeah I sent that out just now:
https://lore.kernel.org/lkml/[email protected]/
I will look into sending an RFC to rip out the hotplug callbacks later.
Chengming Zhou
reviewerRe: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
In reply to Yosry AhmedQuoted 50 lines · Yosry Ahmed
Quoted 36 lines · Chengming Zhou
Quoted 34 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 24 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ; }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
mm/zswap.c +4
-0+static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx;Quoted 8 lines
mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 4 lines
mm/zswap.c +4
-0+ +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Quoted 5 lines
Please correct me if I am wrong, but my understanding is that memory allocated with
alloc_percpu()is allocated for each *possible* CPU, and does not go away when CPUs are offlined. We allocate the per-CPUcrypto_acomp_ctxstructs withalloc_percpu()(including the mutex), so they should not go away with CPU offlining.
Ah, right! I missed that only buffer and req is dynamically allocated by the cpu online callback.
Then your fix is safe to me, thanks for your explanation!
Quoted 24 lines
OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, and crypto_acomp_ctx.buffer only for online CPUs through the CPU hotplug notifiers (i.e.
zswap_cpu_comp_prepare()andzswap_cpu_comp_dead()). These are the resources that can go away with CPU offlining, and what we need to protect about.The approach I am taking here is to hold the per-CPU mutex in the CPU offlining code while we free these resources, and set crypto_acomp_ctx.req to NULL. In
acomp_ctx_get_cpu_locked(), we hold the mutex of the current CPU, and check if crypto_acomp_ctx.req is NULL.If it is NULL, then the CPU is offlined between
raw_cpu_ptr()and acquiring the mutex, and we retry on the new CPU that we end up on. If it is not NULL, then we are guaranteed that the resources will not be freed by CPU offlining untilacomp_ctx_put_unlock()is called and the mutex is unlocked.Quoted 4 lines
Yeah, it's not safe, we can only use
this_cpu_ptr(), which will disable preempt (so cpu offline can't kick in), and get refcount of ctx. Since we can'tmutex_lockin the preempt disabled section.
Quoted 5 lines
My understanding is that the purpose of
this_cpu_ptr()disabling preemption is to prevent multiple CPUs accessing per-CPU data of a single CPU concurrently. In the zswap case, we don't really need that because we use the mutex to protect against it (and we cannot disable preemption anyway).
Yes, your fix is correct, preemption disable is not needed in this case.
Thanks.
Nhat Pham
maintainerRe: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
In reply to Yosry AhmedQuoted 20 lines · Yosry Ahmed
On Tue, Jan 7, 2025 at 9:00 PM Chengming Zhou <[email protected]> wrote:
Please correct me if I am wrong, but my understanding is that memory allocated withalloc_percpu()is allocated for each *possible* CPU, and does not go away when CPUs are offlined. We allocate the per-CPUcrypto_acomp_ctxstructs withalloc_percpu()(including the mutex), so they should not go away with CPU offlining.OTOH, we allocate the crypto_acomp_ctx.acompx, crypto_acomp_ctx.req, and crypto_acomp_ctx.buffer only for online CPUs through the CPU hotplug notifiers (i.e.
zswap_cpu_comp_prepare()andzswap_cpu_comp_dead()). These are the resources that can go away with CPU offlining, and what we need to protect about.The approach I am taking here is to hold the per-CPU mutex in the CPU offlining code while we free these resources, and set crypto_acomp_ctx.req to NULL. In
acomp_ctx_get_cpu_locked(), we hold the mutex of the current CPU, and check if crypto_acomp_ctx.req is NULL.
Quoted 6 lines
If it is NULL, then the CPU is offlined between
raw_cpu_ptr()and acquiring the mutex, and we retry on the new CPU that we end up on. If it is not NULL, then we are guaranteed that the resources will not be freed by CPU offlining untilacomp_ctx_put_unlock()is called and the mutex is unlocked.
Ah you're right, that makes a lot of sense now :)
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
In reply to Nhat PhamQuoted 48 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 22 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ;mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 10 lines
mm/zswap.c +8
-0} +static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx; + +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Quoted 1 line
If it is somehow protected, then yeah this seems quite elegant :)
thought about this again. Could it be the following?
bool cpus_is_read_locked(void)
{
return percpu_is_read_locked(&cpu_hotplug_lock);
}in zswap:
bool locked = cpus_is_read_locked();
if (!locked)
cpus_read_lock();
.... // do our job
if (!locked)
cpus_read_unlock();
This seems to resolve all three problems:
1. if our context has held read lock, we won't hold it again;
2. if other contexts are holding write lock, we wait for the completion of cpuhotplug
by acquiring read lock
3. if our context hasn't held a read lock, we hold it.
Quoted 44 lines
Quoted 44 lines
mm/zswap.c +12
-0+if (likely (ctx->req)) +return ctx; +/* Raced with zswap_cpu_comp_dead() on CPU hotunplug */ +mutex_unlock (&ctx->mutex); + } +} + +static void acomp_ctx_put_unlock (struct crypto_acomp_ctx *ctx) +{ +mutex_unlock (&ctx->mutex); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool){
mm/zswap.c +1
-4@@ -893,10 +916,7 @@ static bool zswap_compress(struct page *page,struct zswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (pool->acomp_ctx); dst = acomp_ctx->buffer;sg_init_table (&input,1 );sg_set_page(&input, page, PAGE_SIZE, 0);mm/zswap.c +1
-1@@ -949,7 +969,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, else if (alloc_ret) zswap_reject_alloc_fail++; - mutex_unlock(&acomp_ctx->mutex); + acomp_ctx_put_unlock(acomp_ctx);return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,9 +980,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src;
Quoted 6 lines
mm/zswap.c static void zswap_decompress(struct zswap_entry · line 986
Quoted 6 lines
mm/zswap.c +1
-3- acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (entry->pool->acomp_ctx); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );/*
Thanks
Barry
Re: [PATCH v2 2/2] mm: zswap: disable migration while using per-CPU acomp_ctx
Quoted 69 lines · Barry Song
Quoted 48 lines · Nhat Pham
Quoted 6 lines · Yosry Ahmed
Actually, using the mutex to protect against CPU hotunplug is not too complicated. The following diff is one way to do it (lightly tested). Johannes, Nhat, any preferences between this patch (disabling migration) and the following diff?
I mean if this works, this over migration diasbling any day? :)
Quoted 22 lines
mm/zswap.c +3
-0@@ -869,17 +869,40 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node);struct crypto_acomp_ctx *acomp_ctx =per_cpu_ptr (pool->acomp_ctx, cpu); +mutex_lock (&acomp_ctx->mutex);if (!IS_ERR_OR_NULL (acomp_ctx)) {if (!IS_ERR_OR_NULL (acomp_ctx->req))acomp_request_free (acomp_ctx->req); + acomp_ctx->req =NULL ;if (!IS_ERR_OR_NULL (acomp_ctx->acomp))crypto_free_acomp (acomp_ctx->acomp);kfree (acomp_ctx->buffer); } +mutex_unlock (&acomp_ctx->mutex);return 0 ;mm/zswap.c static int zswap_cpu_comp_dead(unsigned int cpu, · line 894
Quoted 10 lines
mm/zswap.c +8
-0} +static struct crypto_acomp_ctx *acomp_ctx_get_cpu_locked ( +struct crypto_acomp_ctx __percpu *acomp_ctx) +{ +struct crypto_acomp_ctx *ctx; + +for (;;) { + ctx =raw_cpu_ptr (acomp_ctx); +mutex_lock (&ctx->mutex);I'm a bit confused. IIUC, ctx is per-cpu right? What's protecting this cpu-local data (including the mutex) from being invalidated under us while we're sleeping and waiting for the mutex?
Quoted 1 line
If it is somehow protected, then yeah this seems quite elegant :)
thought about this again. Could it be the following?
bool
cpus_is_read_locked(void){ return percpu_is_read_locked(&cpu_hotplug_lock); }in zswap:
bool locked =
cpus_is_read_locked();if (!locked)
cpus_read_lock();.... // do our job
if (!locked)
Quoted 9 lines
cpus_read_unlock();This seems to resolve all three problems:
1. if our context has held read lock, we won't hold it again;
2. if other contexts are holding write lock, we wait for the completion of cpuhotplug
by acquiring read lock
3. if our context hasn't held a read lock, we hold it.
sorry for the noise.
This won't work because percpu_is_read_locked() is a sum:
bool percpu_is_read_locked(struct percpu_rw_semaphore *sem)
{
return per_cpu_sum(*sem->read_count) != 0 && !atomic_read(&sem->block);
}EXPORT_SYMBOL_GPL(percpu_is_read_locked);
If other CPUs hold the read lock, it will also return true. However, once those CPUs release the lock, our data might still be released by CPU hotplug.
This approach would require something like percpu_is_read_locked_by_me() :-(
Quoted 53 lines
Quoted 44 lines
Quoted 44 lines
mm/zswap.c +12
-0+if (likely (ctx->req)) +return ctx; +/* Raced with zswap_cpu_comp_dead() on CPU hotunplug */ +mutex_unlock (&ctx->mutex); + } +} + +static void acomp_ctx_put_unlock (struct crypto_acomp_ctx *ctx) +{ +mutex_unlock (&ctx->mutex); +} +static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool){
mm/zswap.c +1
-4@@ -893,10 +916,7 @@ static bool zswap_compress(struct page *page,struct zswap_entry *entry, gfp_t gfp;u8 *dst; - acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); - -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (pool->acomp_ctx); dst = acomp_ctx->buffer;sg_init_table (&input,1 );sg_set_page(&input, page, PAGE_SIZE, 0);mm/zswap.c +1
-1@@ -949,7 +969,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, else if (alloc_ret) zswap_reject_alloc_fail++; - mutex_unlock(&acomp_ctx->mutex); + acomp_ctx_put_unlock(acomp_ctx);return comp_ret ==0 && alloc_ret ==0 ; }@@ -960,9 +980,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src;Quoted 6 lines
mm/zswap.c static void zswap_decompress(struct zswap_entry · line 986
Quoted 6 lines
mm/zswap.c +1
-3- acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx); -mutex_lock (&acomp_ctx->mutex); - + acomp_ctx =acomp_ctx_get_cpu_locked (entry->pool->acomp_ctx); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );/* Thanks
Barry
Quoted 21 lines · Yosry Ahmed
This reverts commit
eaebeb93922ca6ab0dd92027b73d0112701706ef.Commit
eaebeb93922c("mm: zswap: fix race between [de]compression and CPU hotunplug") used the CPU hotplug lock in zswap compress/decompress operations to protect against a race with CPU hotunplug making some per-CPU resources go away.However, zswap compress/decompress can be reached through reclaim while
the lock is held, resulting in a potential deadlock as reported by
syzbot:
======================================================
WARNING: possible circular locking dependency detected 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Not tainted
------------------------------------------------------
kswapd0/89 is trying to acquire lock:
ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:acomp_ctx_get_cpumm/zswap.c:886[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_compressmm/zswap.c:908[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store_pagemm/zswap.c:1439[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store+0xa74/0x1ba0mm/zswap.c:1546
Quoted 5 lines
but task is already holding lock:
ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:balance_pgdatmm/vmscan.c:6871[inline]ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:kswapd+0xb58/0x2f30mm/vmscan.c:7253which lock already depends on the new lock.
We have functions like percpu_is_write_locked(), percpu_is_read_locked(), and cpus_read_trylock().
Could they help prevent circular locking dependencies if we perform a check before acquiring the lock?
Quoted 196 lines
the existing dependency chain (in reverse order) is:
-> #1 (fs_reclaim){+.+.}-{0:0}:
lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849 __fs_reclaim_acquire mm/page_alloc.c:3853 [inline] fs_reclaim_acquire+0x88/0x130 mm/page_alloc.c:3867 might_alloc include/linux/sched/mm.h:318 [inline] slab_pre_alloc_hook mm/slub.c:4070 [inline] slab_alloc_node mm/slub.c:4148 [inline] __kmalloc_cache_node_noprof+0x40/0x3a0 mm/slub.c:4337 kmalloc_node_noprof include/linux/slab.h:924 [inline] alloc_worker kernel/workqueue.c:2638 [inline] create_worker+0x11b/0x720 kernel/workqueue.c:2781 workqueue_prepare_cpu+0xe3/0x170 kernel/workqueue.c:6628 cpuhp_invoke_callback+0x48d/0x830 kernel/cpu.c:194 __cpuhp_invoke_callback_range kernel/cpu.c:965 [inline] cpuhp_invoke_callback_range kernel/cpu.c:989 [inline] cpuhp_up_callbacks kernel/cpu.c:1020 [inline] _cpu_up+0x2b3/0x580 kernel/cpu.c:1690 cpu_up+0x184/0x230 kernel/cpu.c:1722 cpuhp_bringup_mask+0xdf/0x260 kernel/cpu.c:1788 cpuhp_bringup_cpus_parallel+0xf9/0x160 kernel/cpu.c:1878 bringup_nonboot_cpus+0x2b/0x50 kernel/cpu.c:1892 smp_init+0x34/0x150 kernel/smp.c:1009 kernel_init_freeable+0x417/0x5d0 init/main.c:1569 kernel_init+0x1d/0x2b0 init/main.c:1466 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244-> #0 (cpu_hotplug_lock){++++}-{0:0}:
check_prev_add kernel/locking/lockdep.c:3161 [inline] check_prevs_add kernel/locking/lockdep.c:3280 [inline] validate_chain+0x18ef/0x5920 kernel/locking/lockdep.c:3904 __lock_acquire+0x1397/0x2100 kernel/locking/lockdep.c:5226 lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849 percpu_down_read include/linux/percpu-rwsem.h:51 [inline] cpus_read_lock+0x42/0x150 kernel/cpu.c:490 acomp_ctx_get_cpu mm/zswap.c:886 [inline] zswap_compress mm/zswap.c:908 [inline] zswap_store_page mm/zswap.c:1439 [inline] zswap_store+0xa74/0x1ba0 mm/zswap.c:1546 swap_writepage+0x647/0xce0 mm/page_io.c:279 shmem_writepage+0x1248/0x1610 mm/shmem.c:1579 pageout mm/vmscan.c:696 [inline] shrink_folio_list+0x35ee/0x57e0 mm/vmscan.c:1374 shrink_inactive_list mm/vmscan.c:1967 [inline] shrink_list mm/vmscan.c:2205 [inline] shrink_lruvec+0x16db/0x2f30 mm/vmscan.c:5734 mem_cgroup_shrink_node+0x385/0x8e0 mm/vmscan.c:6575 mem_cgroup_soft_reclaim mm/memcontrol-v1.c:312 [inline] memcg1_soft_limit_reclaim+0x346/0x810 mm/memcontrol-v1.c:362 balance_pgdat mm/vmscan.c:6975 [inline] kswapd+0x17b3/0x2f30 mm/vmscan.c:7253 kthread+0x2f0/0x390 kernel/kthread.c:389 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244other info that might help us debug this:
Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(fs_reclaim); lock(cpu_hotplug_lock); lock(fs_reclaim);
rlock(cpu_hotplug_lock);*** DEADLOCK ***
1 lock held by kswapd0/89:
#0:ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:balance_pgdatmm/vmscan.c:6871[inline] #0:ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:kswapd+0xb58/0x2f30mm/vmscan.c:7253stack backtrace: CPU: 0 UID: 0 PID: 89 Comm: kswapd0 Not tainted 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 09/13/2024 Call Trace: <TASK> __dump_stack lib/dump_stack.c:94 [inline] dump_stack_lvl+0x241/0x360 lib/dump_stack.c:120 print_circular_bug+0x13a/0x1b0 kernel/locking/lockdep.c:2074 check_noncircular+0x36a/0x4a0 kernel/locking/lockdep.c:2206 check_prev_add kernel/locking/lockdep.c:3161 [inline] check_prevs_add kernel/locking/lockdep.c:3280 [inline] validate_chain+0x18ef/0x5920 kernel/locking/lockdep.c:3904 __lock_acquire+0x1397/0x2100 kernel/locking/lockdep.c:5226 lock_acquire+0x1ed/0x550 kernel/locking/lockdep.c:5849 percpu_down_read include/linux/percpu-rwsem.h:51 [inline] cpus_read_lock+0x42/0x150 kernel/cpu.c:490 acomp_ctx_get_cpu mm/zswap.c:886 [inline] zswap_compress mm/zswap.c:908 [inline] zswap_store_page mm/zswap.c:1439 [inline] zswap_store+0xa74/0x1ba0 mm/zswap.c:1546 swap_writepage+0x647/0xce0 mm/page_io.c:279 shmem_writepage+0x1248/0x1610 mm/shmem.c:1579 pageout mm/vmscan.c:696 [inline] shrink_folio_list+0x35ee/0x57e0 mm/vmscan.c:1374 shrink_inactive_list mm/vmscan.c:1967 [inline] shrink_list mm/vmscan.c:2205 [inline] shrink_lruvec+0x16db/0x2f30 mm/vmscan.c:5734 mem_cgroup_shrink_node+0x385/0x8e0 mm/vmscan.c:6575 mem_cgroup_soft_reclaim mm/memcontrol-v1.c:312 [inline] memcg1_soft_limit_reclaim+0x346/0x810 mm/memcontrol-v1.c:362 balance_pgdat mm/vmscan.c:6975 [inline] kswapd+0x17b3/0x2f30 mm/vmscan.c:7253 kthread+0x2f0/0x390 kernel/kthread.c:389 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 </TASK>Revert the change. A different fix for the race with CPU hotunplug will follow.
Reported-by syzbot <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>---
The patches apply on top of mm-hotfixes-unstable and are meant for v6.13.
Andrew, I am not sure what's the best way to handle this. This fix is already merged into Linus's tree and had CC:stable, so I thought it's best to revert it and replace it with a separate fix that would be easy to backport instead of the revert patch, especially that functionally the new fix is different anyway.
v1 -> v2:
- Disable migration as an alternative fix instead of SRCU, and explain why SRCU andcpus_read_lock()cannot be used in the commit log of patch 2.--- mm/zswap.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)mm/zswap.c +3
-17@@ -880,18 +880,6 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)return 0 ; } -/* Prevent CPU hotplug from freeing up the per-CPU acomp_ctx resources */ -static struct crypto_acomp_ctx *acomp_ctx_get_cpu (struct crypto_acomp_ctx __percpu *acomp_ctx) -{ -cpus_read_lock (); -return raw_cpu_ptr (acomp_ctx); -} - -static void acomp_ctx_put_cpu (void ) -{ -cpus_read_unlock (); -} -static bool zswap_compress (struct page *page,struct zswap_entry *entry,struct zswap_pool *pool) {@@ -905,7 +893,8 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,gfp_t gfp;u8 *dst; - acomp_ctx =acomp_ctx_get_cpu (pool->acomp_ctx); + acomp_ctx =raw_cpu_ptr (pool->acomp_ctx); +mutex_lock (&acomp_ctx->mutex); dst = acomp_ctx->buffer;@@ -961,7 +950,6 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, zswap_reject_alloc_fail++;mutex_unlock (&acomp_ctx->mutex); -acomp_ctx_put_cpu ();return comp_ret ==0 && alloc_ret ==0 ; }@@ -972,7 +960,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct crypto_acomp_ctx *acomp_ctx;u8 *src; - acomp_ctx =acomp_ctx_get_cpu (entry->pool->acomp_ctx); + acomp_ctx =raw_cpu_ptr (entry->pool->acomp_ctx);mutex_lock (&acomp_ctx->mutex); src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO );@@ -1002,7 +990,6 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)if (src != acomp_ctx->buffer)zpool_unmap_handle (zpool, entry->handle); -acomp_ctx_put_cpu (); }/********************************* --
Quoted 2 lines
2.47.1.613.gc27f4b7a9f-goog
Thanks
barry
Quoted 27 lines · Barry Song
Quoted 25 lines · Yosry Ahmed
This reverts commit
eaebeb93922ca6ab0dd92027b73d0112701706ef.Commit
eaebeb93922c("mm: zswap: fix race between [de]compression and CPU hotunplug") used the CPU hotplug lock in zswap compress/decompress operations to protect against a race with CPU hotunplug making some per-CPU resources go away.However, zswap compress/decompress can be reached through reclaim while
the lock is held, resulting in a potential deadlock as reported by
syzbot:
======================================================
WARNING: possible circular locking dependency detected 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Not tainted
------------------------------------------------------
kswapd0/89 is trying to acquire lock:
ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:acomp_ctx_get_cpumm/zswap.c:886[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_compressmm/zswap.c:908[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store_pagemm/zswap.c:1439[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store+0xa74/0x1ba0mm/zswap.c:1546but task is already holding lock:
ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:balance_pgdatmm/vmscan.c:6871[inline]ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:kswapd+0xb58/0x2f30mm/vmscan.c:7253
Quoted 6 lines
Quoted 1 line
which lock already depends on the new lock.
We have functions like
percpu_is_write_locked(),percpu_is_read_locked(), andcpus_read_trylock().
Could they help prevent circular locking dependencies if we perform a check before acquiring the lock?
Yeah we can do that but it feels a bit hacky, we may have to unnecessarily fail the operation in some cases, right? Not sure tbh.
Quoted 31 lines · Yosry Ahmed
Quoted 29 lines · Barry Song
Quoted 26 lines · Yosry Ahmed
This reverts commit
eaebeb93922ca6ab0dd92027b73d0112701706ef.Commit
eaebeb93922c("mm: zswap: fix race between [de]compression and CPU hotunplug") used the CPU hotplug lock in zswap compress/decompress operations to protect against a race with CPU hotunplug making some per-CPU resources go away.However, zswap compress/decompress can be reached through reclaim while
the lock is held, resulting in a potential deadlock as reported by
syzbot:
======================================================
WARNING: possible circular locking dependency detected 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Not tainted
------------------------------------------------------
kswapd0/89 is trying to acquire lock:
ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:acomp_ctx_get_cpumm/zswap.c:886[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_compressmm/zswap.c:908[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store_pagemm/zswap.c:1439[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store+0xa74/0x1ba0mm/zswap.c:1546but task is already holding lock:
ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:balance_pgdatmm/vmscan.c:6871[inline]ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:kswapd+0xb58/0x2f30mm/vmscan.c:7253which lock already depends on the new lock.
Quoted 7 lines
Quoted 4 lines
We have functions like
percpu_is_write_locked(),percpu_is_read_locked(), andcpus_read_trylock().
Could they help prevent circular locking dependencies if we perform a check before acquiring the lock?Yeah we can do that but it feels a bit hacky, we may have to unnecessarily fail the operation in some cases, right? Not sure tbh.
Not sure if it can be as simple as the following:
locked = cpus_read_trylock();
....
if (locked)
cpus_read_unlock();If this works, it seems better than migrate_disable(), which could affect the scheduler's select_rq especially given that swap is a hot path :-)
Thanks
Barry
Quoted 43 lines · Barry Song
Quoted 31 lines · Yosry Ahmed
Quoted 29 lines · Barry Song
Quoted 26 lines · Yosry Ahmed
This reverts commit
eaebeb93922ca6ab0dd92027b73d0112701706ef.Commit
eaebeb93922c("mm: zswap: fix race between [de]compression and CPU hotunplug") used the CPU hotplug lock in zswap compress/decompress operations to protect against a race with CPU hotunplug making some per-CPU resources go away.However, zswap compress/decompress can be reached through reclaim while
the lock is held, resulting in a potential deadlock as reported by
syzbot:
======================================================
WARNING: possible circular locking dependency detected 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Not tainted
------------------------------------------------------
kswapd0/89 is trying to acquire lock:
ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:acomp_ctx_get_cpumm/zswap.c:886[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_compressmm/zswap.c:908[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store_pagemm/zswap.c:1439[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store+0xa74/0x1ba0mm/zswap.c:1546but task is already holding lock:
ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:balance_pgdatmm/vmscan.c:6871[inline]ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:kswapd+0xb58/0x2f30mm/vmscan.c:7253which lock already depends on the new lock.
Quoted 7 lines
Quoted 4 lines
We have functions like
percpu_is_write_locked(),percpu_is_read_locked(), andcpus_read_trylock().
Could they help prevent circular locking dependencies if we perform a check before acquiring the lock?Yeah we can do that but it feels a bit hacky, we may have to unnecessarily fail the operation in some cases, right? Not sure tbh.
Not sure if it can be as simple as the following:
Quoted 7 lines
locked = cpus_read_trylock(); .... if (locked) cpus_read_unlock();If this works, it seems better than
migrate_disable(), which could affect the scheduler'sselect_rqespecially given that swap is a hot path :-)
I didn't look too closely into this, but I'd prefer the simpler fix unless it causes any noticeable regressions. Unless others are also concerned about disabling migration..
Quoted 3 lines
Thanks
Barry
Quoted 50 lines · Yosry Ahmed
Quoted 48 lines · Barry Song
Quoted 31 lines · Yosry Ahmed
Quoted 29 lines · Barry Song
Quoted 26 lines · Yosry Ahmed
This reverts commit
eaebeb93922ca6ab0dd92027b73d0112701706ef.Commit
eaebeb93922c("mm: zswap: fix race between [de]compression and CPU hotunplug") used the CPU hotplug lock in zswap compress/decompress operations to protect against a race with CPU hotunplug making some per-CPU resources go away.However, zswap compress/decompress can be reached through reclaim while
the lock is held, resulting in a potential deadlock as reported by
syzbot:
======================================================
WARNING: possible circular locking dependency detected 6.13.0-rc6-syzkaller-00006-g5428dc1906dd #0 Not tainted
------------------------------------------------------
kswapd0/89 is trying to acquire lock:
ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:acomp_ctx_get_cpumm/zswap.c:886[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_compressmm/zswap.c:908[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store_pagemm/zswap.c:1439[inline]ffffffff8e7d2ed0(cpu_hotplug_lock){++++}-{0:0}, at:zswap_store+0xa74/0x1ba0mm/zswap.c:1546but task is already holding lock:
ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:balance_pgdatmm/vmscan.c:6871[inline]ffffffff8ea355a0(fs_reclaim){+.+.}-{0:0}, at:kswapd+0xb58/0x2f30mm/vmscan.c:7253which lock already depends on the new lock.
Quoted 7 lines
Quoted 4 lines
We have functions like
percpu_is_write_locked(),percpu_is_read_locked(), andcpus_read_trylock().
Could they help prevent circular locking dependencies if we perform a check before acquiring the lock?Yeah we can do that but it feels a bit hacky, we may have to unnecessarily fail the operation in some cases, right? Not sure tbh.
Not sure if it can be as simple as the following:
locked = cpus_read_trylock(); .... if (locked) cpus_read_unlock();
Quoted 6 lines
Quoted 2 lines
If this works, it seems better than
migrate_disable(), which could affect the scheduler'sselect_rqespecially given that swap is a hot path :-)I didn't look too closely into this, but I'd prefer the simpler fix unless it causes any noticeable regressions. Unless others are also concerned about disabling migration..
Okay, fair enough. It could be hacky, as there's a chance that a write lock could be acquired by someone else. Waiman's initial patchset to fix the same issue had an ugly sleep/retry mechanism:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Quoted 2 lines
Thanks
Barry