md: scope memalloc_noio to allocation critical sections
PATCH v2 1/1 md: scope memalloc_noio to allocation critical sections
From: Chen Cheng <[email protected]>
Save token as mddev-scoped in mddev->noio_flags cause PF_MEMALLOC_NOIO leak into task A, while task B restores a token that it never saved.
scenario:
task A mddev task B ====== ======= ============
write suspend_lomddev_suspend()
suspended == 0
drain active_io
suspended = 1
A: noio_A = memalloc_noio_save()
A returns with PF_MEMALLOC_NOIO set
write suspend_hi mddev_suspend()
suspended == 1
suspended = 2
B returns (no save)
mddev_resume()
suspended = 1
not last resume
A returns
A still has PF_MEMALLOC_NOIO <-- leaked
mddev_resume()
suspended = 0
memalloc_noio_restore(noio_A) (restores A's token in B)
Fixed by:
call flags = memalloc_noio_save() directly in mdraid, where:
1) after array suspend succeed;
2) before allocating memory
Validation:
repeatedly updates the array's suspend_lo and suspend_hi sysfs from many concurrent userspace workers. That makes multiple tasks to call mddev_suspend()/mddev_resume() concurrently.
Each worker:
- reads its initial /proc/self/stat flags and verifies that PF_MEMALLOC_NOIO is not already set
- writes 0 to either suspend_lo or suspend_hi
- immediately reads its own task flags again
- reports success if flags & PF_MEMALLOC_NOIO is true after the write returns
---
rfc -> v1:
- dm-raid side just bypass because suspend and resume not in the same task context..
v1 -> v2:
- just call flags = memalloc_noio_save() directly in mdraid (suggest by Yu Kuai)
--- drivers/md/md-bitmap.c | 3 +++ drivers/md/md.c | 52 ++++++++++++++++++++++++++++-------------- drivers/md/md.h | 1 - drivers/md/raid5.c | 14 ++++++++---- 4 files changed, 47 insertions(+), 23 deletions(-)
drivers/md/md-bitmap.c +3 -0
@@ -2621,14 +2621,16 @@ location_show(struct mddev *mddev, char *page)static ssize_t location_store (struct mddev *mddev,const char *buf,size_t len) {int rv; +unsigned int noio_flags; rv =mddev_suspend_and_lock (mddev);if (rv)return rv; + noio_flags =memalloc_noio_save ();if (mddev->pers) {if (mddev->recovery || mddev->sync_thread) { rv = -EBUSY ; goto out;
@@ -2711,10 +2713,11 @@ location_store(struct mddev *mddev, const char *buf, size_t len)set_bit (MD_SB_CHANGE_DEVS , &mddev->sb_flags);md_wakeup_thread (mddev->thread); } rv =0 ; out: +memalloc_noio_restore (noio_flags);mddev_unlock_and_resume (mddev);if (rv)return rv;return len;
drivers/md/md.c +35 -17
277 lines not on this page · raw message
drivers/md/md.h +0 -1
@@ -619,11 +619,10 @@ struct mddev {mempool_t *serial_info_pool;void (*sync_super)(struct mddev *mddev,struct md_rdev *rdev);struct md_cluster_info *cluster_info;struct md_cluster_operations *cluster_ops;unsigned int good_device_nr;/* good device num within cluster raid */ -unsigned int noio_flag;/* for memalloc scope API */ /* * Temporarily store rdev that will be finally removed when * reconfig_mutex is unlocked, protected by reconfig_mutex. */
drivers/md/raid5.c +9 -5
@@ -2454,15 +2454,10 @@ static int scribble_alloc(struct raid5_percpu *percpu,sizeof (struct page *) * (num +2 ) +sizeof (addr_conv_t) * (num +2 ) +sizeof (unsigned int ) * (num +2 );void *scribble; -/* -* If here is in raid array suspend context, it is in memalloc noio -* context as well, there is no potential recursive memory reclaim -* I/Os with the GFP_KERNEL flag. -*/ scribble =kvmalloc_array (cnt, obj_size,GFP_KERNEL );if (!scribble)return -ENOMEM ;kvfree (percpu->scribble);
@@ -2473,18 +2468,20 @@ static int scribble_alloc(struct raid5_percpu *percpu, }static int resize_chunks (struct r5conf *conf,int new_disks,int new_sectors) {unsigned long cpu; +unsigned int noio_flags;int err =0 ;/* Never shrink. */ if (conf->scribble_disks >= new_disks && conf->scribble_sectors >= new_sectors)return 0 ;raid5_quiesce (conf->mddev, true); + noio_flags =memalloc_noio_save ();cpus_read_lock ();for_each_present_cpu (cpu) {struct raid5_percpu *percpu;
@@ -2494,10 +2491,11 @@ static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)if (err)break ; }cpus_read_unlock (); +memalloc_noio_restore (noio_flags);raid5_quiesce (conf->mddev, false);if (!err) { conf->scribble_disks = new_disks; conf->scribble_sectors = new_sectors;
@@ -6993,10 +6991,11 @@ raid5_show_stripe_size(struct mddev *mddev, char *page)static ssize_t raid5_store_stripe_size (struct mddev *mddev,const char *page,size_t len) {struct r5conf *conf;unsigned long new; +unsigned int noio_flags =0 ;int err;int size;if (len >=PAGE_SIZE )return -EINVAL ;
@@ -7033,10 +7032,11 @@ raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len) mddev->reshape_position != MaxSector || mddev->sysfs_active) { err = -EBUSY ; goto out_unlock; } + noio_flags =memalloc_noio_save ();mutex_lock (&conf->cache_size_mutex); size = conf->max_nr_stripes;shrink_stripes (conf);
@@ -7049,10 +7049,11 @@ raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len) err = -ENOMEM ; }mutex_unlock (&conf->cache_size_mutex); out_unlock: +memalloc_noio_restore (noio_flags);mddev_unlock_and_resume (mddev);return err ?: len; } static struct md_sysfs_entry
@@ -8940,10 +8941,11 @@ static void *raid6_takeover(struct mddev *mddev) }static int raid5_change_consistency_policy (struct mddev *mddev,const char *buf) {struct r5conf *conf; +unsigned int noio_flags;int err; err =mddev_suspend_and_lock (mddev);if (err)return err;
@@ -8951,10 +8953,11 @@ static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)if (!conf) {mddev_unlock_and_resume (mddev);return -ENODEV ; } + noio_flags =memalloc_noio_save ();if (strncmp (buf,"ppl" ,3 ) ==0 ) {/* ppl only works with RAID 5 */ if (!raid5_has_ppl (conf) && conf->level ==5 ) { err =log_init (conf,NULL , true);if (!err) {
@@ -8990,10 +8993,11 @@ static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) }if (!err)md_update_sb (mddev,1 ); +memalloc_noio_restore (noio_flags);mddev_unlock_and_resume (mddev);return err; }
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] In backlog_store(), an error path unlocks the mddev but fails to resume it, leaving the array permanently suspended. --
--- Patch [1]: [PATCH v2] md: scope memalloc_noio to allocation critical sections --- commit 6df6cebcdd6ace34da5732a820e0ad95df14d0fc
Author: Chen Cheng <[email protected]>
md: scope memalloc_noio to allocation critical sections
This patch fixes a token leak issue where PF_MEMALLOC_NOIO could leak across tasks when saving the token in mddev_suspend() and restoring it in mddev_resume(). It refactors the code to call memalloc_noio_save() directly before memory allocation in mdraid operations after the array has been successfully suspended.
Quoted 5 lines
drivers/md/md-bitmap.c +0
-0
drivers/md/md-bitmap.c static ssize_t · line 2637
Quoted 9 lines
drivers/md/md-bitmap.c +2
-0@@ -2629,10 +2629,12 @@ static ssize_t location_store(struct mddev *mddev, const char *buf, size_t len) { int rv; +unsigned int noio_flags; rv =mddev_suspend_and_lock (mddev);if (rv)return rv; + noio_flags =memalloc_noio_save ();
[ ... ]
While reviewing this file, I noticed a pre-existing issue in backlog_store():
drivers/md/md-bitmap.c:backlog_store() {
...
rv = mddev_suspend_and_lock(mddev);
if (rv)
return rv;
/*
* Without write mostly device, it doesn't make sense to set
* backlog for max_write_behind.
*/
rdev_for_each(rdev, mddev) {
if (test_bit(WriteMostly, &rdev->flags)) {
has_write_mostly = true;
break;
}
}
if (!has_write_mostly) {
pr_warn_ratelimited("%s: can't set backlog, no write mostly device available\n",
mdname(mddev));
mddev_unlock(mddev);
return -EINVAL;
}
...
}[Severity: High]
This is a pre-existing issue, but the error path for !has_write_mostly in backlog_store() calls mddev_unlock(mddev) instead of mddev_unlock_and_resume(mddev).
If a privileged user writes to the bitmap/backlog sysfs file when there are no write-mostly devices in the array, mddev_suspend_and_lock(mddev) is called, but the array is never resumed on this error path.
Does this permanently leave mddev->suspended elevated, blocking normal I/O processing and any future suspend/resume operations on the array?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 65 lines · Chen Cheng
From: Chen Cheng<[email protected]>
Save token as mddev-scoped in
mddev->noio_flagscausePF_MEMALLOC_NOIOleak into task A, while task B restores a token that it never saved.scenario:
task A mddev task B ====== ======= ============write
suspend_lomddev_suspend()
suspended == 0
drainactive_io
suspended = 1
A:noio_A=memalloc_noio_save()
A returns withPF_MEMALLOC_NOIOsetwrite
suspend_himddev_suspend()
suspended == 1
suspended = 2
B returns (no save)
mddev_resume()
suspended = 1
not last resume
A returns
A still hasPF_MEMALLOC_NOIO<-- leaked
mddev_resume()
suspended = 0
memalloc_noio_restore(noio_A)(restores A's token in B)Fixed by:
call flags =memalloc_noio_save()directly in mdraid, where:
1) after array suspend succeed;
2) before allocating memoryValidation:
repeatedly updates the array'ssuspend_loandsuspend_hisysfs from many concurrent userspace workers. That makes multiple tasks to call mddev_suspend()/mddev_resume() concurrently.Each worker:
- reads its initial/proc/self/statflags and verifies thatPF_MEMALLOC_NOIOis not already set
- writes 0 to eithersuspend_loorsuspend_hi
- immediately reads its own task flags again
- reports success if flags &PF_MEMALLOC_NOIOis true after the write returnsFixes 78f57ef9d50a ("md: use memalloc scope APIs in mddev_suspend()/mddev_resume()")Signed-off-by Chen Cheng<[email protected]>---
rfc -> v1:
- dm-raid side just bypass because suspend and resume not in the same task context..v1 -> v2:
- just call flags =memalloc_noio_save()directly in mdraid (suggest by Yu Kuai)
Quoted 6 lines
--- drivers/md/md-bitmap.c | 3 +++ drivers/md/md.c | 52 ++++++++++++++++++++++++++++-------------- drivers/md/md.h | 1 - drivers/md/raid5.c | 14 ++++++++---- 4 files changed, 47 insertions(+), 23 deletions(-)
Signature · 2 lines
Thanks, Kuai
yu kuai
maintainerQuoted 65 lines · Chen Cheng
From: Chen Cheng<[email protected]>
Save token as mddev-scoped in
mddev->noio_flagscausePF_MEMALLOC_NOIOleak into task A, while task B restores a token that it never saved.scenario:
task A mddev task B ====== ======= ============write
suspend_lomddev_suspend()
suspended == 0
drainactive_io
suspended = 1
A:noio_A=memalloc_noio_save()
A returns withPF_MEMALLOC_NOIOsetwrite
suspend_himddev_suspend()
suspended == 1
suspended = 2
B returns (no save)
mddev_resume()
suspended = 1
not last resume
A returns
A still hasPF_MEMALLOC_NOIO<-- leaked
mddev_resume()
suspended = 0
memalloc_noio_restore(noio_A)(restores A's token in B)Fixed by:
call flags =memalloc_noio_save()directly in mdraid, where:
1) after array suspend succeed;
2) before allocating memoryValidation:
repeatedly updates the array'ssuspend_loandsuspend_hisysfs from many concurrent userspace workers. That makes multiple tasks to call mddev_suspend()/mddev_resume() concurrently.Each worker:
- reads its initial/proc/self/statflags and verifies thatPF_MEMALLOC_NOIOis not already set
- writes 0 to eithersuspend_loorsuspend_hi
- immediately reads its own task flags again
- reports success if flags &PF_MEMALLOC_NOIOis true after the write returnsFixes 78f57ef9d50a ("md: use memalloc scope APIs in mddev_suspend()/mddev_resume()")Signed-off-by Chen Cheng<[email protected]>---
rfc -> v1:
- dm-raid side just bypass because suspend and resume not in the same task context..v1 -> v2:
- just call flags =memalloc_noio_save()directly in mdraid (suggest by Yu Kuai)
Quoted 6 lines
--- drivers/md/md-bitmap.c | 3 +++ drivers/md/md.c | 52 ++++++++++++++++++++++++++++-------------- drivers/md/md.h | 1 - drivers/md/raid5.c | 14 ++++++++---- 4 files changed, 47 insertions(+), 23 deletions(-)
Rebase and applied to md-7.3
Signature · 2 lines
Thanks, Kuai