Skip to content

Commit ec821ac

Browse files
author
Ming Lei
committed
blk-mq: Defer freeing of tags page_list to SRCU callback
JIRA: https://issues.redhat.com/browse/RHEL-120078 commit ad0d05d Author: Ming Lei <ming.lei@redhat.com> Date: Sat Aug 30 10:18:21 2025 +0800 blk-mq: Defer freeing of tags page_list to SRCU callback Tag iterators can race with the freeing of the request pages(tags->page_list), potentially leading to use-after-free issues. Defer the freeing of the page list and the tags structure itself until after an SRCU grace period has passed. This ensures that any concurrent tag iterators have completed before the memory is released. With this way, we can replace the big tags->lock in tags iterator code path with srcu for solving the issue. This is achieved by: - Adding a new `srcu_struct tags_srcu` to `blk_mq_tag_set` to protect tag map iteration. - Adding an `rcu_head` to `struct blk_mq_tags` to be used with `call_srcu`. - Moving the page list freeing logic and the `kfree(tags)` call into a new callback function, `blk_mq_free_tags_callback`. - In `blk_mq_free_tags`, invoking `call_srcu` to schedule the new callback for deferred execution. The read-side protection for the tag iterators will be added in a subsequent patch. Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Yu Kuai <yukuai3@huawei.com> Signed-off-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Ming Lei <ming.lei@redhat.com>
1 parent 3c073d3 commit ec821ac

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

block/blk-mq-tag.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
#include <linux/kernel.h>
1010
#include <linux/module.h>
11+
#include <linux/slab.h>
12+
#include <linux/mm.h>
13+
#include <linux/kmemleak.h>
1114

1215
#include <linux/delay.h>
1316
#include "blk.h"
@@ -576,11 +579,30 @@ struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
576579
return NULL;
577580
}
578581

582+
static void blk_mq_free_tags_callback(struct rcu_head *head)
583+
{
584+
struct blk_mq_tags *tags = container_of(head, struct blk_mq_tags,
585+
rcu_head);
586+
struct page *page;
587+
588+
while (!list_empty(&tags->page_list)) {
589+
page = list_first_entry(&tags->page_list, struct page, lru);
590+
list_del_init(&page->lru);
591+
/*
592+
* Remove kmemleak object previously allocated in
593+
* blk_mq_alloc_rqs().
594+
*/
595+
kmemleak_free(page_address(page));
596+
__free_pages(page, page->private);
597+
}
598+
kfree(tags);
599+
}
600+
579601
void blk_mq_free_tags(struct blk_mq_tag_set *set, struct blk_mq_tags *tags)
580602
{
581603
sbitmap_queue_free(&tags->bitmap_tags);
582604
sbitmap_queue_free(&tags->breserved_tags);
583-
kfree(tags);
605+
call_srcu(&set->tags_srcu, &tags->rcu_head, blk_mq_free_tags_callback);
584606
}
585607

586608
int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,

block/blk-mq.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,7 +3450,6 @@ void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
34503450
unsigned int hctx_idx)
34513451
{
34523452
struct blk_mq_tags *drv_tags;
3453-
struct page *page;
34543453

34553454
if (list_empty(&tags->page_list))
34563455
return;
@@ -3474,17 +3473,10 @@ void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
34743473
}
34753474

34763475
blk_mq_clear_rq_mapping(drv_tags, tags);
3477-
3478-
while (!list_empty(&tags->page_list)) {
3479-
page = list_first_entry(&tags->page_list, struct page, lru);
3480-
list_del_init(&page->lru);
3481-
/*
3482-
* Remove kmemleak object previously allocated in
3483-
* blk_mq_alloc_rqs().
3484-
*/
3485-
kmemleak_free(page_address(page));
3486-
__free_pages(page, page->private);
3487-
}
3476+
/*
3477+
* Free request pages in SRCU callback, which is called from
3478+
* blk_mq_free_tags().
3479+
*/
34883480
}
34893481

34903482
void blk_mq_free_rq_map(struct blk_mq_tag_set *set, struct blk_mq_tags *tags)
@@ -4830,6 +4822,9 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
48304822
if (ret)
48314823
goto out_free_srcu;
48324824
}
4825+
ret = init_srcu_struct(&set->tags_srcu);
4826+
if (ret)
4827+
goto out_cleanup_srcu;
48334828

48344829
init_rwsem(&set->update_nr_hwq_lock);
48354830

@@ -4838,7 +4833,7 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
48384833
sizeof(struct blk_mq_tags *), GFP_KERNEL,
48394834
set->numa_node);
48404835
if (!set->tags)
4841-
goto out_cleanup_srcu;
4836+
goto out_cleanup_tags_srcu;
48424837

48434838
for (i = 0; i < set->nr_maps; i++) {
48444839
set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
@@ -4867,6 +4862,8 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
48674862
}
48684863
kfree(set->tags);
48694864
set->tags = NULL;
4865+
out_cleanup_tags_srcu:
4866+
cleanup_srcu_struct(&set->tags_srcu);
48704867
out_cleanup_srcu:
48714868
if (set->flags & BLK_MQ_F_BLOCKING)
48724869
cleanup_srcu_struct(set->srcu);
@@ -4912,6 +4909,9 @@ void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
49124909

49134910
kfree(set->tags);
49144911
set->tags = NULL;
4912+
4913+
srcu_barrier(&set->tags_srcu);
4914+
cleanup_srcu_struct(&set->tags_srcu);
49154915
if (set->flags & BLK_MQ_F_BLOCKING) {
49164916
cleanup_srcu_struct(set->srcu);
49174917
kfree(set->srcu);

include/linux/blk-mq.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ struct blk_mq_tag_set {
531531
struct mutex tag_list_lock;
532532
struct list_head tag_list;
533533
struct srcu_struct *srcu;
534+
struct srcu_struct tags_srcu;
534535

535536
struct rw_semaphore update_nr_hwq_lock;
536537
};
@@ -767,6 +768,7 @@ struct blk_mq_tags {
767768
* request pool
768769
*/
769770
spinlock_t lock;
771+
struct rcu_head rcu_head;
770772
};
771773

772774
static inline struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags,

0 commit comments

Comments
 (0)