Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3513996pxj; Mon, 7 Jun 2021 12:37:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytd6SnHX9xCgywmhjSQaoeCZ6Rj2Ooxj3a8iQk3YCD4TNDHqta1RRyidYLkrHtg3WyFzAF X-Received: by 2002:a17:907:f9b:: with SMTP id kb27mr9263293ejc.44.1623094663742; Mon, 07 Jun 2021 12:37:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623094663; cv=none; d=google.com; s=arc-20160816; b=Vi1W+XV1UJgTj8H9D6tnU+cqMaubgkB0SdVeX+zqg4NoxKnYt+FZLfgmZrwhkL7SXl BibTgGZeOZq94wNaw1Wk9oKBcAIaorFr4TMWMKgUAiX4NpzJS5v2FWw9/gV60zQbFMz/ wAW2Ez+78Ne/GQ7lZiRc6p9r+S5EYHc4JrVZ/6Xome4+/Bmn+VbZmUUPEUiPv1CYCL9i 5d7ElulfwsRqmkSDgDEyuhnsdeBbnOQ0hEw5jj3dswGvnO2geEEcHB2HKmw9Fu+XaL0+ 45hyxQ/+99e5eRaAk/IUiDl9JH2OBHiBP01SM06bUNJ5G7wcLL/sg70bhTlI76O7Y52T IZlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=VpnHUDEF9AccYFVvo2kPdN3Gw49CLQLaBsY8vvbjUiE=; b=ZsGnWi3tFs24m1/yoRiUzFgW2St+X6Y/rvK14N91dNX1oW3f/99pn3rxZriRMJQt55 1UIatj/HBfBLL67pItb4FCwiQR4721c72wQNlBu0rPQx4TdUXdRVdCTpmeIDlJUGQYT7 FIHNsM0duXOw27Mr3JkZYiuo4HX3WPVzUr7RbNRI1wfCDiW3Z8/YTGD4uQkeEKZziVfg VGmJsZ9H1V/5TgQPS0NWeo2FaMmm5cvZlZxYihscvclhgf94Emf4+OZT6aoKNj3OSxq+ cAoVRa8PbPwMaHUOuxFc51Q5QAxIzTypYl+Dmi6TSSVDRy4E6ap1XKVqj1qdTdpyUs0h Hv4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxonhyperv.com header.s=default header.b=h5kY0IfD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxonhyperv.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f22si12307940ejb.54.2021.06.07.12.37.19; Mon, 07 Jun 2021 12:37:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxonhyperv.com header.s=default header.b=h5kY0IfD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxonhyperv.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230425AbhFGTgB (ORCPT + 99 others); Mon, 7 Jun 2021 15:36:01 -0400 Received: from linux.microsoft.com ([13.77.154.182]:57162 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230311AbhFGTgA (ORCPT ); Mon, 7 Jun 2021 15:36:00 -0400 Received: by linux.microsoft.com (Postfix, from userid 1004) id A309520B83D0; Mon, 7 Jun 2021 12:34:08 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A309520B83D0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxonhyperv.com; s=default; t=1623094448; bh=VpnHUDEF9AccYFVvo2kPdN3Gw49CLQLaBsY8vvbjUiE=; h=From:To:Cc:Subject:Date:From; b=h5kY0IfDu/+yWnaNaPR06Olpy3SnlNiBe8Z3xnHkElkxK+AeXv96uQkybYBy/evCq t41YBxDCKP353h1RxPB+wBWMrQo+Hdz22+nPGX1OIoNqgPM+lovPm+dbdxAR+2PPFc rUor92gL2HyqnyW4b0CTQnBCRP0F0/YiDp2d5gVM= From: longli@linuxonhyperv.com To: linux-block@vger.kernel.org Cc: Long Li , Jens Axboe , Johannes Thumshirn , Pavel Begunkov , Ming Lei , Tejun Heo , "Matthew Wilcox (Oracle)" , Jeffle Xu , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [Patch v3] block: return the correct bvec when checking for gaps Date: Mon, 7 Jun 2021 12:34:05 -0700 Message-Id: <1623094445-22332-1-git-send-email-longli@linuxonhyperv.com> X-Mailer: git-send-email 1.8.3.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Long Li After commit 07173c3ec276 ("block: enable multipage bvecs"), a bvec can have multiple pages. But bio_will_gap() still assumes one page bvec while checking for merging. If the pages in the bvec go across the seg_boundary_mask, this check for merging can potentially succeed if only the 1st page is tested, and can fail if all the pages are tested. Later, when SCSI builds the SG list the same check for merging is done in __blk_segment_map_sg_merge() with all the pages in the bvec tested. This time the check may fail if the pages in bvec go across the seg_boundary_mask (but tested okay in bio_will_gap() earlier, so those BIOs were merged). If this check fails, we end up with a broken SG list for drivers assuming the SG list not having offsets in intermediate pages. This results in incorrect pages written to the disk. Fix this by returning the multi-page bvec when testing gaps for merging. Cc: Jens Axboe Cc: Johannes Thumshirn Cc: Pavel Begunkov Cc: Ming Lei Cc: Tejun Heo Cc: "Matthew Wilcox (Oracle)" Cc: Jeffle Xu Cc: linux-kernel@vger.kernel.org Cc: stable@vger.kernel.org Fixes: 07173c3ec276 ("block: enable multipage bvecs") Signed-off-by: Long Li Reviewed-by: Ming Lei --- Changes v2: added commit details on how data corruption happens v3: reorganized the code/comments in bio_get_last_bvec include/linux/bio.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/include/linux/bio.h b/include/linux/bio.h index a0b4cfdf62a4..d2b98efb5cc5 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -44,9 +44,6 @@ static inline unsigned int bio_max_segs(unsigned int nr_segs) #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter) #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter) -#define bio_multiple_segments(bio) \ - ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len) - #define bvec_iter_sectors(iter) ((iter).bi_size >> 9) #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter))) @@ -271,7 +268,7 @@ static inline void bio_clear_flag(struct bio *bio, unsigned int bit) static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) { - *bv = bio_iovec(bio); + *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); } static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) @@ -279,10 +276,9 @@ static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) struct bvec_iter iter = bio->bi_iter; int idx; - if (unlikely(!bio_multiple_segments(bio))) { - *bv = bio_iovec(bio); - return; - } + bio_get_first_bvec(bio, bv); + if (bv->bv_len == bio->bi_iter.bi_size) + return; /* this bio only has a single bvec */ bio_advance_iter(bio, &iter, iter.bi_size); -- 2.17.1