md/linear: add fault-tolerant mode for unraid-like setups
Subsystems (5)
PATCH 1/2 md/linear: add fault-tolerant mode for unraid-like setups
From: Yu Kuai <[email protected]>
Add a module parameter 'fault_tolerant' that changes how md-linear handles disk failures. When enabled:
- Disk failures are isolated instead of failing the entire array
- I/O to failed disks returns -EIO while healthy disks continue
- The array remains operational with reduced capacity
- Failed disk count is tracked and shown in /proc/mdstat
This enables unraid-like functionality where individual disk failures don't bring down the entire array, allowing continued access to data on healthy disks.
The fault_tolerant parameter can be set at module load time or dynamically via /sys/module/md_linear/parameters/fault_tolerant.
--- drivers/md/md-linear.c | 63 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 8 deletions(-)
drivers/md/md-linear.c +51 -7
@@ -2,6 +2,10 @@/* * linear.c : Multiple Devices driver for Linux Copyright (C) 1994-96 Marc * ZYNGIER <[email protected]> or <[email protected]> +* +* Fault-tolerant mode added for unraid-like setups. +* When fault_tolerant=1, disk failures are isolated - I/O to failed disks +* returns -EIO while healthy disks continue operating normally. */ #include <linux/blkdev.h>
@@ -21,9 +25,15 @@ struct linear_conf {sector_t array_sectors;/* a copy of mddev->raid_disks */ int raid_disks; +atomic_t failed_disks;/* count of failed disks */ struct dev_info disks[] __counted_by(raid_disks); }; +static bool fault_tolerant; +module_param (fault_tolerant, bool,0644 ); +MODULE_PARM_DESC (fault_tolerant, +"Enable fault-tolerant mode: isolate disk failures instead of failing array (default: false)" ); +/* * find which device holds a particular offset */
@@ -96,6 +106,8 @@ static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)if (!conf)return ERR_PTR (-ENOMEM ); +atomic_set (&conf->failed_disks,0 ); +/* * conf->raid_disks is copy of mddev->raid_disks. The reason to * keep a copy of mddev->raid_disks in struct linear_conf is,
@@ -251,7 +263,8 @@ static bool linear_make_request(struct mddev *mddev, struct bio *bio) bio_sector < start_sector)) goto out_of_bounds; -if (unlikely (is_rdev_broken (tmp_dev->rdev))) { +if (unlikely (is_rdev_broken (tmp_dev->rdev) || +test_bit (Faulty, &tmp_dev->rdev->flags))) {md_error (mddev, tmp_dev->rdev);bio_io_error (bio);return true;
@@ -296,16 +309,47 @@ static bool linear_make_request(struct mddev *mddev, struct bio *bio)static void linear_status (struct seq_file *seq,struct mddev *mddev) { +struct linear_conf *conf = mddev->private; +seq_printf (seq," %dk rounding" , mddev->chunk_sectors /2 ); +if (fault_tolerant) { +int failed =atomic_read (&conf->failed_disks); + +seq_puts (seq," fault-tolerant" ); +if (failed) +seq_printf (seq," [%d failed]" , failed); + } }static void linear_error (struct mddev *mddev,struct md_rdev *rdev) { -if (!test_and_set_bit (MD_BROKEN , &mddev->flags)) { -char *md_name =mdname (mddev); - -pr_crit ("md/linear%s: Disk failure on %pg detected, failing array.\n" , - md_name, rdev->bdev); +char *md_name =mdname (mddev); + +if (fault_tolerant) { +/* +* Fault-tolerant mode: isolate the failed disk instead of +* failing the entire array. I/O to this disk will return -EIO +* but other disks continue operating normally. +*/ +if (!test_and_set_bit (Faulty, &rdev->flags)) { +struct linear_conf *conf = mddev->private; + +atomic_inc (&conf->failed_disks); +pr_warn ("md/linear%s: Disk failure on %pg detected, isolating device (fault-tolerant mode).\n" , + md_name, rdev->bdev); +pr_warn ("md/linear%s: %d disk(s) now failed, array continues with reduced capacity.\n" , + md_name,atomic_read (&conf->failed_disks)); +/* Notify userspace about the state change */ +sysfs_notify_dirent_safe (rdev->sysfs_state); + } + }else { +/* +* Standard mode: fail the entire array on any disk failure. +*/ +if (!test_and_set_bit (MD_BROKEN , &mddev->flags)) { +pr_crit ("md/linear%s: Disk failure on %pg detected, failing array.\n" , + md_name, rdev->bdev); + } } }
@@ -344,7 +388,7 @@ static void linear_exit(void)module_init (linear_init);module_exit (linear_exit);MODULE_LICENSE ("GPL" ); -MODULE_DESCRIPTION ("Linear device concatenation personality for MD (deprecated)" ); +MODULE_DESCRIPTION ("Linear device concatenation personality for MD with optional fault-tolerant mode" );MODULE_ALIAS ("md-personality-1" );/* LINEAR - deprecated*/ MODULE_ALIAS ("md-linear" );MODULE_ALIAS ("md-level--1" );
--
2.43.0
PATCH 2/2 ext4: add unraid mount option for single-disk-per-group mode
From: Yu Kuai <[email protected]>
Add support for an "unraid" mount option that enables a special mode designed for use with fault-tolerant md-linear arrays. In this mode:
1. Variable block groups: Each block group can have a different size, allowing one physical disk per group. Lookup tables are used for block-to-group mapping instead of fixed-size calculations.
2. Distributed metadata: Every block group has its own superblock and group descriptor table copy, enabling the filesystem to remain accessible even if some disks fail.
3. Single-group allocation: Files are allocated entirely within a single block group. If a group doesn't have enough space, the allocation fails with -ENOSPC instead of trying other groups. This ensures each file resides on a single physical disk.
4. Inode locality: Inodes are allocated in the same group as their parent directory, keeping files and their metadata on the same disk.
This enables unraid-like functionality where:
- Each disk is independent and can be read separately
- Disk failures only affect files on that specific disk
- The filesystem continues operating with reduced capacity
Usage:
mount -t ext4 -o unraid /dev/md0 /mnt
Note: This requires a specially formatted filesystem where each block group corresponds to one physical disk. A future mkfs.ext4 extension will support creating such filesystems.
--- fs/ext4/balloc.c | 45 ++++++++++++++++++++++++++++++++++++++++----- fs/ext4/ext4.h | 15 ++++++++++++++- fs/ext4/ialloc.c | 13 +++++++++++++ fs/ext4/mballoc.c | 8 ++++++++ fs/ext4/super.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 6 deletions(-)
fs/ext4/balloc.c +43 -5
@@ -54,17 +54,43 @@ ext4_group_t ext4_get_group_number(struct super_block *sb,void ext4_get_group_no_and_offset (struct super_block *sb,ext4_fsblk_t blocknr,ext4_group_t *blockgrpp,ext4_grpblk_t *offsetp) { -struct ext4_super_block *es =EXT4_SB (sb)->s_es; +struct ext4_sb_info *sbi =EXT4_SB (sb); +struct ext4_super_block *es = sbi->s_es;ext4_grpblk_t offset; blocknr = blocknr -le32_to_cpu (es->s_first_data_block); + +/* Unraid mode: binary search through variable-size groups */ +if (sbi->s_group_first_block) { +ext4_group_t lo =0 , hi = sbi->s_groups_count -1 ; +ext4_fsblk_t first_data =le32_to_cpu (es->s_first_data_block); + + blocknr += first_data;/* restore original block number */ + +while (lo < hi) { +ext4_group_t mid = (lo + hi +1 ) /2 ; + +if (blocknr < sbi->s_group_first_block[mid]) + hi = mid -1 ; +else + lo = mid; + } +if (blockgrpp) + *blockgrpp = lo; +if (offsetp) { + offset = (blocknr - sbi->s_group_first_block[lo]) >> + sbi->s_cluster_bits; + *offsetp = offset; + } +return ; + } + offset =do_div (blocknr,EXT4_BLOCKS_PER_GROUP (sb)) >> -EXT4_SB (sb)->s_cluster_bits; + sbi->s_cluster_bits;if (offsetp) *offsetp = offset;if (blockgrpp) *blockgrpp = blocknr; - }/*
@@ -162,8 +188,13 @@ static unsigned ext4_num_overhead_clusters(struct super_block *sb,static unsigned int num_clusters_in_group (struct super_block *sb,ext4_group_t block_group) { +struct ext4_sb_info *sbi =EXT4_SB (sb);unsigned int blocks; +/* Unraid mode: use per-group blocks count */ +if (sbi->s_group_blocks_count) +return EXT4_NUM_B2C (sbi, sbi->s_group_blocks_count[block_group]); +if (block_group ==ext4_get_groups_count (sb) -1 ) {/* * Even though mke2fs always initializes the first and
@@ -171,11 +202,11 @@ static unsigned int num_clusters_in_group(struct super_block *sb,* we need to make sure we calculate the right free * blocks. */ - blocks =ext4_blocks_count (EXT4_SB (sb)->s_es) - + blocks =ext4_blocks_count (sbi->s_es) -ext4_group_first_block_no (sb, block_group); }else blocks =EXT4_BLOCKS_PER_GROUP (sb); -return EXT4_NUM_B2C (EXT4_SB (sb), blocks); +return EXT4_NUM_B2C (sbi, blocks); }/* Initializes an uninitialized block bitmap */
@@ -855,6 +886,13 @@ int ext4_bg_has_super(struct super_block *sb, ext4_group_t group) {struct ext4_super_block *es =EXT4_SB (sb)->s_es; +/* +* Unraid mode: every group has a superblock copy for fault tolerance. +* This allows mounting the filesystem even if some disks fail. +*/ +if (test_opt2 (sb,UNRAID )) +return 1 ; +if (group ==0 )return 1 ;if (ext4_has_feature_sparse_super2 (sb)) {
fs/ext4/ext4.h +14 -1
@@ -1295,6 +1295,9 @@ struct ext4_inode_info {* scanning in mballoc */ #define EXT4_MOUNT2_ABORT 0x00000100/* Abort filesystem */ +#define EXT4_MOUNT2_UNRAID 0x00000200/* Unraid mode: one disk per +* group, single-group alloc +*/ #define clear_opt (sb, opt) EXT4_SB(sb)->s_mount_opt &= \ ~EXT4_MOUNT_##opt
@@ -1687,6 +1690,10 @@ struct ext4_sb_info {struct flex_groups *__rcu *s_flex_groups;ext4_group_t s_flex_groups_allocated; +/* Unraid mode: variable block groups (one disk per group) */ +ext4_fsblk_t *s_group_first_block;/* First block of each group */ +ext4_grpblk_t *s_group_blocks_count;/* Blocks count per group */ +/* workqueue for reserved extent conversions (buffered io) */ struct workqueue_struct *rsv_conversion_wq;
@@ -2627,8 +2634,14 @@ struct dir_private_info {static inline ext4_fsblk_t ext4_group_first_block_no (struct super_block *sb,ext4_group_t group_no) { +struct ext4_sb_info *sbi =EXT4_SB (sb); + +/* Unraid mode: variable block groups, use lookup table */ +if (sbi->s_group_first_block) +return sbi->s_group_first_block[group_no]; +return group_no * (ext4_fsblk_t )EXT4_BLOCKS_PER_GROUP (sb) + -le32_to_cpu (EXT4_SB (sb)->s_es->s_first_data_block); +le32_to_cpu (sbi->s_es->s_first_data_block); }/*
fs/ext4/ialloc.c +13 -0
@@ -438,6 +438,19 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,int flex_size =ext4_flex_bg_size (sbi);struct dx_hash_info hinfo; +/* +* Unraid mode: always allocate inode in parent's group. +* This ensures files and their inodes stay on the same disk. +*/ +if (test_opt2 (sb,UNRAID )) { + desc =ext4_get_group_desc (sb, parent_group,NULL ); +if (desc &&ext4_free_inodes_count (sb, desc) >0 ) { + *group = parent_group; +return 0 ; + } +return -1 ;/* No free inodes in parent's group */ + } + ngroups = real_ngroups;if (flex_size >1 ) { ngroups = (real_ngroups + flex_size - 1) >>
fs/ext4/mballoc.c +8 -0
@@ -2997,6 +2997,14 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)if (err || ac->ac_status ==AC_STATUS_FOUND ) goto out; +/* +* Unraid mode: files must be allocated entirely within a single group. +* If the goal group doesn't have enough space, fail with -ENOSPC +* instead of trying other groups. +*/ +if (test_opt2 (sb,UNRAID )) + goto out; +if (unlikely (ac->ac_flags &EXT4_MB_HINT_GOAL_ONLY )) goto out;
fs/ext4/super.c +70 -1
107 lines not on this page · raw message
--
2.43.0
v2 cover blk-cgroup: fix blkg list and policy data races
In reply to Yu KuaiFrom: Yu Kuai <[email protected]>
Hi,
This series fixes races around q->blkg_list and blkg policy data lifetime.
Patch 1 protects blkg_destroy_all()'s q->blkg_list walk with blkcg_mutex.
Patches 2-3 fix races between blkcg_activate_policy() and concurrent blkg destruction.
Patch 4 factors the policy data teardown loop into a helper after the race fixes.
Changes since v1:
- Drop the BFQ q->blkg_list patch because the current block tree already has a stronger fix in commit 17b2d950a3c0 ("block, bfq: protect async queue reset with blkcg locks").
- Add Reviewed-by tags from Tang Yizhou.
Yu Kuai (1):
blk-cgroup: protect q->blkg_list iteration in blkg_destroy_all() with
blkcg_mutex
Zheng Qixing (3):
blk-cgroup: fix race between policy activation and blkg destruction
blk-cgroup: skip dying blkg in blkcg_activate_policy()
blk-cgroup: factor policy pd teardown loop into helper
block/blk-cgroup.c | 65 +++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 30 deletions(-)
Signature · 1 line
2.51.0
PATCH v2 1/4 blk-cgroup: protect q->blkg_list iteration in blkg_destroy_all() with blkcg_mutex
In reply to Yu KuaiFrom: Yu Kuai <[email protected]>
blkg_destroy_all() iterates q->blkg_list without holding blkcg_mutex, which can race with blkg_free_workfn() that removes blkgs from the list while holding blkcg_mutex.
Add blkcg_mutex protection around the q->blkg_list iteration to prevent potential list corruption or use-after-free issues.
--- block/blk-cgroup.c | 3 +++ 1 file changed, 3 insertions(+)
block/blk-cgroup.c +3 -0
@@ -574,10 +574,11 @@ static void blkg_destroy_all(struct gendisk *disk)struct blkcg_gq *blkg;int count =BLKG_DESTROY_BATCH_SIZE ;int i; restart: +mutex_lock (&q->blkcg_mutex);spin_lock_irq (&q->queue_lock);list_for_each_entry (blkg, &q->blkg_list, q_node) {struct blkcg *blkcg = blkg->blkcg;if (hlist_unhashed (&blkg->blkcg_node))
@@ -592,10 +593,11 @@ static void blkg_destroy_all(struct gendisk *disk)* it when a batch of blkgs are destroyed. */ if (!(--count)) { count =BLKG_DESTROY_BATCH_SIZE ;spin_unlock_irq (&q->queue_lock); +mutex_unlock (&q->blkcg_mutex);cond_resched (); goto restart; } }
@@ -611,10 +613,11 @@ static void blkg_destroy_all(struct gendisk *disk)__clear_bit (pol->plid, q->blkcg_pols); } q->root_blkg =NULL ;spin_unlock_irq (&q->queue_lock); +mutex_unlock (&q->blkcg_mutex);wake_up_var (&q->root_blkg); }static void blkg_iostat_set (struct blkg_iostat *dst,struct blkg_iostat *src)
Signature · 1 line
2.51.0
PATCH v2 2/4 blk-cgroup: fix race between policy activation and blkg destruction
In reply to Yu KuaiFrom: Zheng Qixing <[email protected]>
When switching an IO scheduler on a block device, blkcg_activate_policy() allocates blkg_policy_data (pd) for all blkgs attached to the queue. However, blkcg_activate_policy() may race with concurrent blkcg deletion, leading to use-after-free and memory leak issues.
The use-after-free occurs in the following race:
T1 (blkcg_activate_policy):
- Successfully allocates pd for blkg1 (loop0->queue, blkcgA)
- Fails to allocate pd for blkg2 (loop0->queue, blkcgB)
- Enters the enomem rollback path to release blkg1 resources
T2 (blkcg deletion):
- blkcgA is deleted concurrently
- blkg1 is freed via blkg_free_workfn()
- blkg1->pd is freed
T1 (continued):
- Rollback path accesses blkg1->pd->online after pd is freed
- Triggers use-after-free
In addition, blkg_free_workfn() frees pd before removing the blkg from q->blkg_list. This allows blkcg_activate_policy() to allocate a new pd for a blkg that is being destroyed, leaving the newly allocated pd unreachable when the blkg is finally freed.
Fix these races by extending blkcg_mutex coverage to serialize blkcg_activate_policy() rollback and blkg destruction, ensuring pd lifecycle is synchronized with blkg list visibility.
--- block/blk-cgroup.c | 3 +++ 1 file changed, 3 insertions(+)
block/blk-cgroup.c +3 -0
@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)if (WARN_ON_ONCE (!pol->pd_alloc_fn || !pol->pd_free_fn))return -EINVAL ;if (queue_is_mq (q)) memflags =blk_mq_freeze_queue (q); + +mutex_lock (&q->blkcg_mutex); retry:spin_lock_irq (&q->queue_lock);/* blkg_list is pushed at the head, reverse walk to initialize parents first */ list_for_each_entry_reverse (blkg, &q->blkg_list, q_node) {
@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)__set_bit (pol->plid, q->blkcg_pols); ret =0 ;spin_unlock_irq (&q->queue_lock); out: +mutex_unlock (&q->blkcg_mutex);if (queue_is_mq (q))blk_mq_unfreeze_queue (q, memflags);if (pinned_blkg)blkg_put (pinned_blkg);if (pd_prealloc)
Signature · 1 line
2.51.0
Re: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Quoted 19 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)if (WARN_ON_ONCE (!pol->pd_alloc_fn || !pol->pd_free_fn))return -EINVAL ;if (queue_is_mq (q)) memflags =blk_mq_freeze_queue (q); + +mutex_lock (&q->blkcg_mutex); retry:spin_lock_irq (&q->queue_lock);/* blkg_list is pushed at the head, reverse walk to initialize parents first */ list_for_each_entry_reverse (blkg, &q->blkg_list, q_node) {@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)__set_bit (pol->plid, q->blkcg_pols);
block/blk-cgroup.c int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) · line 1637
Quoted 10 lines
block/blk-cgroup.c +1
-0ret =0 ;spin_unlock_irq (&q->queue_lock); out: +mutex_unlock (&q->blkcg_mutex);if (queue_is_mq (q))blk_mq_unfreeze_queue (q, memflags);if (pinned_blkg)blkg_put (pinned_blkg);if (pd_prealloc)
If the policy allocation fails, we jump to the lable enomem: and teardown pds. But I see this path still only acquires ->queue_lock. Don't we also need to protect it with ->blkcg_mutex?
Moreover I still see race between blkg insertion in blkg_create() which still doesn't use ->blkcg_mutex and so list traversal in bfq_end_wr_async() may still race with blkg_create(), isn't it? I remember you once told this will be handled in another series but I couldn't find that yet.
Thanks,
--Nilay
yu kuai
maintainerRe: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Hi,
Quoted 27 lines · Nilay Shroff
Quoted 26 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn)) return -EINVAL; if (queue_is_mq(q)) memflags = blk_mq_freeze_queue(q); + + mutex_lock(&q->blkcg_mutex);retry:
spin_lock_irq(&q->queue_lock);
/*blkg_listis pushed at the head, reverse walk to initialize parents first */list_for_each_entry_reverse(blkg, &q->blkg_list, q_node){block/blk-cgroup.c +0
-0@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) __set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock);out:
+mutex_unlock(&q->blkcg_mutex);
if (queue_is_mq(q))
blk_mq_unfreeze_queue(q, memflags);
Quoted 8 lines
Quoted 3 lines
if (
pinned_blkg)
blkg_put(pinned_blkg);
if (pd_prealloc)If the policy allocation fails, we jump to the lable enomem: and teardown pds.
But I see this path still only acquires ->queue_lock. Don't we also need to protect it with ->blkcg_mutex?
Yes, I agree we should protect it as well.
Quoted 6 lines
Moreover I still see race between blkg insertion in
blkg_create()which still doesn't use ->blkcg_mutex and so list traversal inbfq_end_wr_async()
may still race withblkg_create(), isn't it? I remember you once told this will be handled in another series but I couldn't find that yet.
This is the set:
[PATCH 0/8] blk-cgroup: remove queue_lock nesting from blkcg paths - Yu Kuai <https://lore.kernel.org/all/[email protected]/>
Noted that set just make sure queue_lock is not nested under other atomic context, and that set do not acquire blkcg_mutex for blkg_create() yet. Howerver, with that set it'll be easy to convert all queue_lock to blkcg_mtuex for blkg protection, and together with lots of blk-cgroup code cleanups.
Quoted 7 lines
Thanks,
--Nilay
Signature · 2 lines
Thanks, Kuai
yu kuai
maintainerRe: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Hi,
Quoted 37 lines · yu kuai
Hi,
Quoted 26 lines · Nilay Shroff
Quoted 25 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn)) return -EINVAL; if (queue_is_mq(q)) memflags = blk_mq_freeze_queue(q); + + mutex_lock(&q->blkcg_mutex); retry: spin_lock_irq(&q->queue_lock); /* blkg_list is pushed at the head, reverse walk toinitialize parents first */
list_for_each_entry_reverse(blkg, &q->blkg_list, q_node){block/blk-cgroup.c +1
-0@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) __set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock); out: + mutex_unlock(&q->blkcg_mutex); if (queue_is_mq(q))Quoted 8 lines
block/blk-cgroup.c int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) · line 1637
Quoted 4 lines
block/blk-cgroup.c +0
-0blk_mq_unfreeze_queue(q, memflags); if (pinned_blkg) blkg_put(pinned_blkg); if (pd_prealloc)If the policy allocation fails, we jump to the lable enomem: and teardown pds.
But I see this path still only acquires ->queue_lock. Don't we also need to protect it with ->blkcg_mutex?
Quoted 1 line
Yes, I agree we should protect it as well.
Just take a closer look at the code, the enomem is already protected by blkcg_mutex :)
Quoted 23 lines
Quoted 6 lines
Moreover I still see race between blkg insertion in
blkg_create()which still doesn't use ->blkcg_mutex and so list traversal inbfq_end_wr_async()
may still race withblkg_create(), isn't it? I remember you once told this will be handled in another series but I couldn't find that yet.This is the set:
[PATCH 0/8] blk-cgroup: remove
queue_locknesting from blkcg paths - Yu Kuai <https://lore.kernel.org/all/[email protected]/>Noted that set just make sure
queue_lockis not nested under other atomic context, and that set do not acquireblkcg_mutexforblkg_create()yet. Howerver, with that set it'll be easy to convert allqueue_locktoblkcg_mtuexfor blkg protection, and together with lots of blk-cgroup code cleanups.Quoted 6 lines
Thanks,
--Nilay
Signature · 2 lines
Thanks, Kuai
Re: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Quoted 40 lines · yu kuai
Hi,
Quoted 37 lines · yu kuai
Hi,
Quoted 34 lines · Nilay Shroff
Quoted 19 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn)) return -EINVAL; if (queue_is_mq(q)) memflags = blk_mq_freeze_queue(q); + + mutex_lock(&q->blkcg_mutex); retry: spin_lock_irq(&q->queue_lock); /* blkg_list is pushed at the head, reverse walk toinitialize parents first */
list_for_each_entry_reverse(blkg, &q->blkg_list, q_node){block/blk-cgroup.c +0
-0@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)block/blk-cgroup.c int blkcg_activate_policy(struct gendisk · line 1637
Quoted 10 lines
block/blk-cgroup.c +1
-0__set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock); out: + mutex_unlock(&q->blkcg_mutex); if (queue_is_mq(q)) blk_mq_unfreeze_queue(q, memflags); if (pinned_blkg) blkg_put(pinned_blkg); if (pd_prealloc)If the policy allocation fails, we jump to the lable enomem: and teardown pds.
But I see this path still only acquires ->queue_lock. Don't we also need to protect it with ->blkcg_mutex?
Quoted 5 lines
Quoted 1 line
Yes, I agree we should protect it as well.
Just take a closer look at the code, the enomem is already protected by
blkcg_mutex:)
Oh yes, but the ->blkcg_mutex is never released if we jump to enomem. So that may potentially cause deadlock. We need to release ->blkcg_mutex once blkcg_policy_teardown_pds() returns. Or may be refactor code (or add comment) so that it's easy to realize or spot the ->blkcg_mutex is acquired and then released around blkcg_policy_teardown_pds().
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Moreover I still see race between blkg insertion in
blkg_create()which still doesn't use ->blkcg_mutex and so list traversal inbfq_end_wr_async()
may still race withblkg_create(), isn't it? I remember you once told this will be handled in another series but I couldn't find that yet.This is the set:
Quoted 8 lines
Quoted 8 lines
[PATCH 0/8] blk-cgroup: remove
queue_locknesting from blkcg paths - Yu Kuai <https://lore.kernel.org/all/[email protected]/>Noted that set just make sure
queue_lockis not nested under other atomic context, and that set do not acquireblkcg_mutexforblkg_create()yet. Howerver, with that set it'll be easy to convert allqueue_locktoblkcg_mtuexfor blkg protection, and together with lots of blk-cgroup code cleanups.
Okay, so are you planning to send a follow-up patchset that replaces ->queue_lock with ->blkcg_mutex for protecting the blkg_list? If so, I'd still prefer acquiring ->blkcg_mutex around blkg_create() in this patchset. That would address the race between blkg_create() and the blkg_list traversal in bfq_end_wr_async(), while the subsequent series can focus on cleaning up and removing the remaining ->queue_lock usage for blkg protection.
Thanks,
--Nilay
yu kuai
maintainerRe: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Hi,
Quoted 48 lines · Nilay Shroff
Quoted 41 lines · yu kuai
Hi,
Quoted 38 lines · yu kuai
Hi,
Quoted 35 lines · Nilay Shroff
Quoted 22 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn)) return -EINVAL; if (queue_is_mq(q)) memflags = blk_mq_freeze_queue(q); + + mutex_lock(&q->blkcg_mutex);retry:
spin_lock_irq(&q->queue_lock);
/*blkg_listis pushed at the head, reverse walk to initialize parents first */list_for_each_entry_reverse(blkg, &q->blkg_list, q_node){block/blk-cgroup.c +0
-0@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) __set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock);Quoted 7 lines
out:
+mutex_unlock(&q->blkcg_mutex);
if (queue_is_mq(q))
blk_mq_unfreeze_queue(q, memflags);
if (pinned_blkg)
blkg_put(pinned_blkg);
if (pd_prealloc)If the policy allocation fails, we jump to the lable enomem: and teardown pds.
But I see this path still only acquires ->queue_lock. Don't we also need
to protect it with ->blkcg_mutex?Quoted 5 lines
Quoted 1 line
Yes, I agree we should protect it as well.
Just take a closer look at the code, the enomem is already protected by
blkcg_mutex:)
Quoted 6 lines
Oh yes, but the ->blkcg_mutex is never released if we jump to enomem. So that may potentially cause deadlock. We need to release ->blkcg_mutex once
blkcg_policy_teardown_pds()returns. Or may be refactor code (or add comment) so that it's easy to realize or spot the ->blkcg_mutex is acquired
and then released aroundblkcg_policy_teardown_pds().
the enomem will goto out at last, and blkcg_mutex do released. The code is a bit hacky.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Moreover I still see race between blkg insertion in
blkg_create()which
still doesn't use ->blkcg_mutex and so list traversal inbfq_end_wr_async()
may still race withblkg_create(), isn't it? I remember you once told this will be handled in another series but I couldn't find that yet.This is the set:
[PATCH 0/8] blk-cgroup: remove
queue_locknesting from blkcg paths - Yu Kuai <https://lore.kernel.org/all/[email protected]/>Quoted 8 lines
Quoted 8 lines
Noted that set just make sure
queue_lockis not nested under other atomic
context, and that set do not acquireblkcg_mutexforblkg_create()yet. Howerver,
with that set it'll be easy to convert allqueue_locktoblkcg_mtuexfor blkg
protection, and together with lots of blk-cgroup code cleanups.
Quoted 11 lines
Okay, so are you planning to send a follow-up patchset that replaces ->queue_lock
with ->blkcg_mutex for protecting theblkg_list? If so, I'd still prefer acquiring
->blkcg_mutex aroundblkg_create()in this patchset. That would address the race
betweenblkg_create()and theblkg_listtraversal inbfq_end_wr_async(), while the
subsequent series can focus on cleaning up and removing the remaining ->queue_lock
usage for blkg protection.
Yes, there is a follow-up patchset. When this set was posted, blkg_create is still called with queue_lock held, so I can't do that. However, not that the other set is already applied, I can hold blkcg_mutex for blkg_create() now.
Quoted 4 lines
Thanks,
--Nilay
Signature · 2 lines
Thanks, Kuai
Re: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Quoted 95 lines · yu kuai
Hi,
Quoted 48 lines · Nilay Shroff
Quoted 41 lines · yu kuai
Hi,
Quoted 38 lines · yu kuai
Hi,
Quoted 35 lines · Nilay Shroff
Quoted 19 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn)) return -EINVAL; if (queue_is_mq(q)) memflags = blk_mq_freeze_queue(q); + + mutex_lock(&q->blkcg_mutex); retry: spin_lock_irq(&q->queue_lock); /* blkg_list is pushed at the head, reverse walk toinitialize parents first */
list_for_each_entry_reverse(blkg, &q->blkg_list, q_node){block/blk-cgroup.c +0
-0@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)block/blk-cgroup.c int blkcg_activate_policy(struct gendisk · line 1637
Quoted 10 lines
block/blk-cgroup.c +1
-0__set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock); out: + mutex_unlock(&q->blkcg_mutex); if (queue_is_mq(q)) blk_mq_unfreeze_queue(q, memflags); if (pinned_blkg) blkg_put(pinned_blkg); if (pd_prealloc)If the policy allocation fails, we jump to the lable enomem: and teardown pds.
But I see this path still only acquires ->queue_lock. Don't we also need
to protect it with ->blkcg_mutex?Quoted 5 lines
Quoted 1 line
Yes, I agree we should protect it as well.
Just take a closer look at the code, the enomem is already protected by
blkcg_mutex:)Quoted 6 lines
Oh yes, but the ->blkcg_mutex is never released if we jump to enomem. So that may potentially cause deadlock. We need to release ->blkcg_mutex once
blkcg_policy_teardown_pds()returns. Or may be refactor code (or add comment) so that it's easy to realize or spot the ->blkcg_mutex is acquired
and then released aroundblkcg_policy_teardown_pds().the enomem will goto out at last, and
blkcg_mutexdo released. The code is a bit hacky.Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Moreover I still see race between blkg insertion in
blkg_create()which
still doesn't use ->blkcg_mutex and so list traversal inbfq_end_wr_async()
may still race withblkg_create(), isn't it? I remember you once told this will be handled in another series but I couldn't find that yet.This is the set:
[PATCH 0/8] blk-cgroup: remove
queue_locknesting from blkcg paths - Yu Kuai <https://lore.kernel.org/all/[email protected]/>Quoted 8 lines
Quoted 8 lines
Noted that set just make sure
queue_lockis not nested under other atomic
context, and that set do not acquireblkcg_mutexforblkg_create()yet. Howerver,
with that set it'll be easy to convert allqueue_locktoblkcg_mtuexfor blkg
protection, and together with lots of blk-cgroup code cleanups.Quoted 11 lines
Okay, so are you planning to send a follow-up patchset that replaces ->queue_lock
with ->blkcg_mutex for protecting theblkg_list? If so, I'd still prefer acquiring
->blkcg_mutex aroundblkg_create()in this patchset. That would address the race
betweenblkg_create()and theblkg_listtraversal inbfq_end_wr_async(), while the
subsequent series can focus on cleaning up and removing the remaining ->queue_lock
usage for blkg protection.
Quoted 4 lines
Yes, there is a follow-up patchset. When this set was posted,
blkg_createis still called withqueue_lockheld, so I can't do that. However, not that the other set is already applied, I can holdblkcg_mutexforblkg_create()now.
If you already have a follow-up patchset that replaces ->queue_lock with ->blkcg_mutex for protecting blkg_list, then I think it might make sense to send that out first (if you haven't already) and hold off this series for now. That way, the blkg_create() race can be addressed first, and this series can build on top of those changes.
BTW, if that follow-up series is merged first, I suspect the first two patches in this series may no longer be necessary, leaving only patches 3/4 and 4/4.
Thanks,
--Nilay
yu kuai
maintainerRe: [PATCH v2 2/4] blk-cgroup: fix race between policy activation and blkg destruction
Hi,
Quoted 110 lines · Nilay Shroff
Quoted 102 lines · yu kuai
Hi,
Quoted 49 lines · Nilay Shroff
Quoted 41 lines · yu kuai
Hi,
Quoted 38 lines · yu kuai
Hi,
Quoted 35 lines · Nilay Shroff
Quoted 22 lines · Yu Kuai
block/blk-cgroup.c +2
-0@@ -1563,10 +1563,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn)) return -EINVAL; if (queue_is_mq(q)) memflags = blk_mq_freeze_queue(q); + + mutex_lock(&q->blkcg_mutex);retry:
spin_lock_irq(&q->queue_lock);
/*blkg_listis pushed at the head, reverse walk to initialize parents first */list_for_each_entry_reverse(blkg, &q->blkg_list, q_node){block/blk-cgroup.c +0
-0@@ -1625,10 +1627,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol) __set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock);Quoted 7 lines
out:
+mutex_unlock(&q->blkcg_mutex);
if (queue_is_mq(q))
blk_mq_unfreeze_queue(q, memflags);
if (pinned_blkg)
blkg_put(pinned_blkg);
if (pd_prealloc)If the policy allocation fails, we jump to the lable enomem: and teardown pds.
But I see this path still only acquires ->queue_lock. Don't we also need
to protect it with ->blkcg_mutex?Quoted 6 lines
Quoted 1 line
Yes, I agree we should protect it as well.
Just take a closer look at the code, the enomem is already protected by
blkcg_mutex:)Quoted 8 lines
Oh yes, but the ->blkcg_mutex is never released if we jump to enomem. So that may potentially cause deadlock. We need to release ->blkcg_mutex
onceblkcg_policy_teardown_pds()returns. Or may be refactor code (or add
comment) so that it's easy to realize or spot the ->blkcg_mutex is acquired
and then released aroundblkcg_policy_teardown_pds().the enomem will goto out at last, and
blkcg_mutexdo released. The code is
a bit hacky.Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
Quoted 9 lines
Moreover I still see race between blkg insertion in
blkg_create()which
still doesn't use ->blkcg_mutex and so list traversal inbfq_end_wr_async()
may still race withblkg_create(), isn't it? I remember you once told
this will be handled in another series but I couldn't find that yet.This is the set:
[PATCH 0/8] blk-cgroup: remove
queue_locknesting from blkcg paths
- Yu
Kuai
<https://lore.kernel.org/all/[email protected]/>Quoted 8 lines
Quoted 8 lines
Noted that set just make sure
queue_lockis not nested under other atomic
context, and that set do not acquireblkcg_mutexforblkg_create()yet. Howerver,
with that set it'll be easy to convert allqueue_locktoblkcg_mtuexfor blkg
protection, and together with lots of blk-cgroup code cleanups.Quoted 11 lines
Okay, so are you planning to send a follow-up patchset that replaces ->queue_lock
with ->blkcg_mutex for protecting theblkg_list? If so, I'd still prefer acquiring
->blkcg_mutex aroundblkg_create()in this patchset. That would address the race
betweenblkg_create()and theblkg_listtraversal inbfq_end_wr_async(), while the
subsequent series can focus on cleaning up and removing the remaining ->queue_lock
usage for blkg protection.Quoted 6 lines
Yes, there is a follow-up patchset. When this set was posted,
blkg_createis still
called withqueue_lockheld, so I can't do that. However, not that the other set
is already applied, I can holdblkcg_mutexforblkg_create()now.
Quoted 6 lines
If you already have a follow-up patchset that replaces ->queue_lock with ->blkcg_mutex for protecting
blkg_list, then I think it might make sense to send that out first (if you haven't already) and hold off this series for
now. That way, theblkg_create()race can be addressed first, and this series can build on top of those changes.
Yes, I already have a follow-up patchset that is build after this set. I agree send that first will make sense, I'll rebase and send soon.
Quoted 4 lines
BTW, if that follow-up series is merged first, I suspect the first two patches in
this series may no longer be necessary, leaving only patches 3/4 and 4/4.
Yes, the first two patches will be folded into the patch to replace queue_lock with blkcg_mutex.
Quoted 6 lines
Thanks,
--Nilay
Signature · 2 lines
Thanks, Kuai
PATCH v2 3/4 blk-cgroup: skip dying blkg in blkcg_activate_policy()
In reply to Yu KuaiFrom: Zheng Qixing <[email protected]>
When switching IO schedulers on a block device, blkcg_activate_policy() can race with concurrent blkcg deletion, leading to a use-after-free in rcu_accelerate_cbs.
T1: T2:
blkg_destroy
kill(&blkg->refcnt) // blkg->refcnt=1->0
blkg_release // call_rcu(__blkg_release)
...
blkg_free_workfn
->pd_free_fn(pd)elv_iosched_storeelevator_switch
...
iterate blkg listblkg_get(blkg) // blkg->refcnt=0->1
list_del_init(&blkg->q_node)
blkg_put(pinned_blkg) // blkg->refcnt=1->0blkg_release // call_rcu againrcu_accelerate_cbs // uaf
Fix this by checking hlist_unhashed(&blkg->blkcg_node) before getting a reference to the blkg. This is the same check used in blkg_destroy() to detect if a blkg has already been destroyed. If the blkg is already unhashed, skip processing it since it's being destroyed.
--- block/blk-cgroup.c | 2 ++ 1 file changed, 2 insertions(+)
block/blk-cgroup.c +2 -0
@@ -1574,10 +1574,12 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)list_for_each_entry_reverse (blkg, &q->blkg_list, q_node) {struct blkg_policy_data *pd;if (blkg->pd[pol->plid])continue ; +if (hlist_unhashed (&blkg->blkcg_node)) +continue ;/* If prealloc matches, use it; otherwise try GFP_NOWAIT */ if (blkg == pinned_blkg) { pd = pd_prealloc; pd_prealloc =NULL ;
Signature · 1 line
2.51.0
PATCH v2 4/4 blk-cgroup: factor policy pd teardown loop into helper
In reply to Yu KuaiFrom: Zheng Qixing <[email protected]>
Move the teardown sequence which offlines and frees per-policy blkg_policy_data (pd) into a helper for readability.
No functional change intended.
--- block/blk-cgroup.c | 57 ++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 30 deletions(-)
block/blk-cgroup.c +27 -30
@@ -1526,10 +1526,35 @@ struct cgroup_subsys io_cgrp_subsys = { .depends_on = 1 << memory_cgrp_id,#endif };EXPORT_SYMBOL_GPL (io_cgrp_subsys); +/* +* Tear down per-blkg policy data for @pol on @q. +*/ +static void blkcg_policy_teardown_pds (struct request_queue *q, +const struct blkcg_policy *pol) +{ +struct blkcg_gq *blkg; + +list_for_each_entry (blkg, &q->blkg_list, q_node) { +struct blkcg *blkcg = blkg->blkcg; +struct blkg_policy_data *pd; + +spin_lock (&blkcg->lock); + pd = blkg->pd[pol->plid]; +if (pd) { +if (pd->online && pol->pd_offline_fn) + pol->pd_offline_fn (pd); + pd->online = false; + pol->pd_free_fn (pd); + blkg->pd[pol->plid] =NULL ; + } +spin_unlock (&blkcg->lock); + } +} +/** * blkcg_activate_policy - activate a blkcg policy on a gendisk * @disk: gendisk of interest * @pol: blkcg policy to activate *
@@ -1641,25 +1666,11 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)return ret; enomem:/* alloc failed, take down everything */ spin_lock_irq (&q->queue_lock); -list_for_each_entry (blkg, &q->blkg_list, q_node) { -struct blkcg *blkcg = blkg->blkcg; -struct blkg_policy_data *pd; - -spin_lock (&blkcg->lock); - pd = blkg->pd[pol->plid]; -if (pd) { -if (pd->online && pol->pd_offline_fn) - pol->pd_offline_fn (pd); - pd->online = false; - pol->pd_free_fn (pd); - blkg->pd[pol->plid] =NULL ; - } -spin_unlock (&blkcg->lock); - } +blkcg_policy_teardown_pds (q, pol);spin_unlock_irq (&q->queue_lock); ret = -ENOMEM ; goto out; }EXPORT_SYMBOL_GPL (blkcg_activate_policy);
@@ -1674,11 +1685,10 @@ EXPORT_SYMBOL_GPL(blkcg_activate_policy);*/ void blkcg_deactivate_policy (struct gendisk *disk,const struct blkcg_policy *pol) {struct request_queue *q = disk->queue; -struct blkcg_gq *blkg;unsigned int memflags;if (!blkcg_policy_enabled (q, pol))return ;
@@ -1687,24 +1697,11 @@ void blkcg_deactivate_policy(struct gendisk *disk,mutex_lock (&q->blkcg_mutex);spin_lock_irq (&q->queue_lock);__clear_bit (pol->plid, q->blkcg_pols); - -list_for_each_entry (blkg, &q->blkg_list, q_node) { -struct blkcg *blkcg = blkg->blkcg; - -spin_lock (&blkcg->lock); -if (blkg->pd[pol->plid]) { -if (blkg->pd[pol->plid]->online && pol->pd_offline_fn) - pol->pd_offline_fn (blkg->pd[pol->plid]); - pol->pd_free_fn (blkg->pd[pol->plid]); - blkg->pd[pol->plid] =NULL ; - } -spin_unlock (&blkcg->lock); - } - +blkcg_policy_teardown_pds (q, pol);spin_unlock_irq (&q->queue_lock);mutex_unlock (&q->blkcg_mutex);if (queue_is_mq (q))blk_mq_unfreeze_queue (q, memflags);
Signature · 1 line
2.51.0
yu kuai
maintainerHi,
Please ignore this patch, this patch is supposed only used downstream. Ai somehow generate the cmd to send it together with the patchset:
blk-cgroup: fix blkg list and policy data races
Same for the other ext4 patch.
Sorry for the noise. :(
Quoted 23 lines · Yu Kuai
From: Yu Kuai<[email protected]>
Add a module parameter '
fault_tolerant' that changes how md-linear handles disk failures. When enabled:- Disk failures are isolated instead of failing the entire array
- I/O to failed disks returns-EIOwhile healthy disks continue
- The array remains operational with reduced capacity
- Failed disk count is tracked and shown in/proc/mdstatThis enables unraid-like functionality where individual disk failures don't bring down the entire array, allowing continued access to data on healthy disks.
The
fault_tolerantparameter can be set at module load time or dynamically via/sys/module/md_linear/parameters/fault_tolerant.Signed-off-by Yu Kuai<[email protected]>--- drivers/md/md-linear.c | 63 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 8 deletions(-)drivers/md/md-linear.c +0
-0
Signature · 2 lines
Thanks, Kuai