Skip to content

Commit ba02508

Browse files
htejunLinus Torvalds
authored andcommitted
[PATCH] blk: fix tag shrinking (revive real_max_size)
My patch in commit fa72b90 incorrectly removed blk_queue_tag->real_max_depth. The original resize implementation was incorrect in the following points. * actual allocation size of tag_index was shorter than real_max_size, but assumed to be of the same size, possibly causing memory access beyond the allocated area. * bits in tag_map between max_deptn and real_max_depth were initialized to 1's, making the tags permanently reserved. In an attempt to fix above two bugs, I had removed allocation optimization in init_tag_map and real_max_size. Tag map/index were allocated and freed immediately during resize. Unfortunately, I wasn't considering that tag map/index can be resized dynamically with tags beyond new_depth active. This led to accessing freed area after shrinking tags and led to the following bug reporting thread on linux-scsi. http://marc.theaimsgroup.com/?l=linux-scsi&m=112319898111885&w=2 To fix the problem, I've revived real_max_depth without allocation optimization in init_tag_map, and Andrew Vasquez confirmed that the problem was fixed. As Jens is not going to be available for a week, he asked me to make sure that this patch reaches you. http://marc.theaimsgroup.com/?l=linux-scsi&m=112325778530886&w=2 Also, a comment was added to make sure that real_max_size is needed for dynamic shrinking. Signed-off-by: Tejun Heo <htejun@gmail.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent c7546f8 commit ba02508

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎drivers/block/ll_rw_blk.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ struct request *blk_queue_find_tag(request_queue_t *q, int tag)
719719
{
720720
struct blk_queue_tag *bqt = q->queue_tags;
721721

722-
if (unlikely(bqt == NULL || tag >= bqt->max_depth))
722+
if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
723723
return NULL;
724724

725725
return bqt->tag_index[tag];
@@ -798,6 +798,7 @@ init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
798798

799799
memset(tag_index, 0, depth * sizeof(struct request *));
800800
memset(tag_map, 0, nr_ulongs * sizeof(unsigned long));
801+
tags->real_max_depth = depth;
801802
tags->max_depth = depth;
802803
tags->tag_index = tag_index;
803804
tags->tag_map = tag_map;
@@ -871,12 +872,23 @@ int blk_queue_resize_tags(request_queue_t *q, int new_depth)
871872
if (!bqt)
872873
return -ENXIO;
873874

875+
/*
876+
* if we already have large enough real_max_depth. just
877+
* adjust max_depth. *NOTE* as requests with tag value
878+
* between new_depth and real_max_depth can be in-flight, tag
879+
* map can not be shrunk blindly here.
880+
*/
881+
if (new_depth <= bqt->real_max_depth) {
882+
bqt->max_depth = new_depth;
883+
return 0;
884+
}
885+
874886
/*
875887
* save the old state info, so we can copy it back
876888
*/
877889
tag_index = bqt->tag_index;
878890
tag_map = bqt->tag_map;
879-
max_depth = bqt->max_depth;
891+
max_depth = bqt->real_max_depth;
880892

881893
if (init_tag_map(q, bqt, new_depth))
882894
return -ENOMEM;
@@ -913,7 +925,7 @@ void blk_queue_end_tag(request_queue_t *q, struct request *rq)
913925

914926
BUG_ON(tag == -1);
915927

916-
if (unlikely(tag >= bqt->max_depth))
928+
if (unlikely(tag >= bqt->real_max_depth))
917929
/*
918930
* This can happen after tag depth has been reduced.
919931
* FIXME: how about a warning or info message here?

‎include/linux/blkdev.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ struct blk_queue_tag {
301301
struct list_head busy_list; /* fifo list of busy tags */
302302
int busy; /* current depth */
303303
int max_depth; /* what we will send to device */
304+
int real_max_depth; /* what the array can hold */
304305
atomic_t refcnt; /* map can be shared */
305306
};
306307

0 commit comments

Comments
 (0)