Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757121AbYKOFav (ORCPT ); Sat, 15 Nov 2008 00:30:51 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752224AbYKOFZX (ORCPT ); Sat, 15 Nov 2008 00:25:23 -0500 Received: from kroah.org ([198.145.64.141]:50652 "EHLO coco.kroah.org" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752874AbYKOFZJ (ORCPT ); Sat, 15 Nov 2008 00:25:09 -0500 Date: Fri, 14 Nov 2008 21:23:42 -0800 From: Greg KH To: linux-kernel@vger.kernel.org, stable@kernel.org, Greg KH Cc: Justin Forbes , Zwane Mwaikambo , "Theodore Ts'o" , Randy Dunlap , Dave Jones , Chuck Wolber , Chris Wedgwood , Michael Krufky , Chuck Ebbert , Domenico Andreoli , Willy Tarreau , Rodrigo Rubira Branco , Jake Edge , Eugene Teo , torvalds@linux-foundation.org, akpm@linux-foundation.org, alan@lxorguk.ukuu.org.uk, FUJITA Tomonori , Jens Axboe , Nikanth Karthikesan Subject: [patch 17/22] block: fix nr_phys_segments miscalculation bug Message-ID: <20081115052342.GR3710@kroah.com> References: <20081115051732.506914008@mini.kroah.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline; filename="block-fix-nr_phys_segments-miscalculation-bug.patch" In-Reply-To: <20081115052220.GA3710@kroah.com> User-Agent: Mutt/1.5.16 (2007-06-09) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4183 Lines: 124 2.6.27-stable review patch. If anyone has any objections, please let us know. ------------------ From: FUJITA Tomonori commit 8677142710516d986d932d6f1fba7be8382c1fec upstream backported by Nikanth Karthikesan to the 2.6.27.y tree. block: fix nr_phys_segments miscalculation bug This fixes the bug reported by Nikanth Karthikesan : http://lkml.org/lkml/2008/10/2/203 The root cause of the bug is that blk_phys_contig_segment miscalculates q->max_segment_size. blk_phys_contig_segment checks: req->biotail->bi_size + next_req->bio->bi_size > q->max_segment_size But blk_recalc_rq_segments might expect that req->biotail and the previous bio in the req are supposed be merged into one segment. blk_recalc_rq_segments might also expect that next_req->bio and the next bio in the next_req are supposed be merged into one segment. In such case, we merge two requests that can't be merged here. Later, blk_rq_map_sg gives more segments than it should. We need to keep track of segment size in blk_recalc_rq_segments and use it to see if two requests can be merged. This patch implements it in the similar way that we used to do for hw merging (virtual merging). Signed-off-by: FUJITA Tomonori Signed-off-by: Jens Axboe Cc: Nikanth Karthikesan Signed-off-by: Greg Kroah-Hartman --- block/blk-merge.c | 19 +++++++++++++++++-- include/linux/bio.h | 7 +++++++ 2 files changed, 24 insertions(+), 2 deletions(-) --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -95,6 +95,9 @@ new_hw_segment: nr_hw_segs++; } + if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size) + rq->bio->bi_seg_front_size = seg_size; + nr_phys_segs++; bvprv = bv; seg_size = bv->bv_len; @@ -106,6 +109,10 @@ new_hw_segment: rq->bio->bi_hw_front_size = hw_seg_size; if (hw_seg_size > rq->biotail->bi_hw_back_size) rq->biotail->bi_hw_back_size = hw_seg_size; + if (nr_phys_segs == 1 && seg_size > rq->bio->bi_seg_front_size) + rq->bio->bi_seg_front_size = seg_size; + if (seg_size > rq->biotail->bi_seg_back_size) + rq->biotail->bi_seg_back_size = seg_size; rq->nr_phys_segments = nr_phys_segs; rq->nr_hw_segments = nr_hw_segs; } @@ -133,7 +140,8 @@ static int blk_phys_contig_segment(struc if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt))) return 0; - if (bio->bi_size + nxt->bi_size > q->max_segment_size) + if (bio->bi_seg_back_size + nxt->bi_seg_front_size > + q->max_segment_size) return 0; /* @@ -377,6 +385,8 @@ static int ll_merge_requests_fn(struct r { int total_phys_segments; int total_hw_segments; + unsigned int seg_size = + req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; /* * First check if the either of the requests are re-queued @@ -392,8 +402,13 @@ static int ll_merge_requests_fn(struct r return 0; total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; - if (blk_phys_contig_segment(q, req->biotail, next->bio)) + if (blk_phys_contig_segment(q, req->biotail, next->bio)) { + if (req->nr_phys_segments == 1) + req->bio->bi_seg_front_size = seg_size; + if (next->nr_phys_segments == 1) + next->biotail->bi_seg_back_size = seg_size; total_phys_segments--; + } if (total_phys_segments > q->max_phys_segments) return 0; --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -98,6 +98,13 @@ struct bio { unsigned int bi_size; /* residual I/O count */ /* + * To keep track of the max segment size, we account for the + * sizes of the first and last mergeable segments in this bio. + */ + unsigned int bi_seg_front_size; + unsigned int bi_seg_back_size; + + /* * To keep track of the max hw size, we account for the * sizes of the first and last virtually mergeable segments * in this bio -- -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/