Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2861438pxv; Mon, 12 Jul 2021 03:50:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyO3+lJdqsbBKJjCCtFbIMFppulFCx/aUfuDKx/VWhGe+m1hjpHdLxJ06UJtM7B/3keo8q X-Received: by 2002:a05:6602:1348:: with SMTP id i8mr39495708iov.208.1626087027135; Mon, 12 Jul 2021 03:50:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626087027; cv=none; d=google.com; s=arc-20160816; b=ei6pAH9bBxcgXexnXYmEU3mjikKfc1l1OBrk2G/clA1E2QW1NuN3jqZVaGNfq+qGtl G4wsJ2ITaqLlQf0G6gfmp9Xk7+RU5xBT0IwZrIa4/d8N9zCRai+IVr/5z5f3gYakF2df 1bnsucv9zUCQG9cG4f/+tJUD55bwoL9ymyuerDDTDsYqUGXfvlKXNea8QVszOsfBy7UP yTkGCak24IZKbUJuUdKmsASnHayMrjJZJ1hO3u5k8bRS/sYnLz0RC1/PiNzxQ64xEBUW veAftMHyJBHdUjx9js8EGUgEyCHFldZ47hvULmkUoc2qzKLET0r+Nn2NtbDhMCSzqm7q hm3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3tPSRqgUhHY80wBJ0wVSQyGiZGJmvkpzgdWBsOofBFk=; b=ByyxT8olJg+aY4G3v8MF3CDIbUnQkf2JFxRDOQZAo0etgczvMWAes4JuQdOeORllnO VAGQq4j5MR3DAqLOcbYVL4cZkCU4ezAGY/hTco3xsXLqUsiekn8qlB6H/fOmL3K6ZFbn hey80OqpfpIsCUQlWDUAk8+gGofzlaYf7KOTlxVwBcrWdBC4pQqNS1DojxK4Fq3A+xDa wrZeWT8yNyx7QAydf6pAldA+SZfUq0ipSloo5TASYRubeu8bzhFidjE/nYgxL/kKZY2K FbgwwnDb9JSFFYRu/17T3siWPWfyo0UXdhiqKXLHxkoasUZPrSIazAbMUDoJ/veqLFg7 QwOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vSqz9e8f; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si18757332ilc.85.2021.07.12.03.50.15; Mon, 12 Jul 2021 03:50:27 -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=@linuxfoundation.org header.s=korg header.b=vSqz9e8f; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350733AbhGLIMq (ORCPT + 99 others); Mon, 12 Jul 2021 04:12:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:43856 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345239AbhGLH3m (ORCPT ); Mon, 12 Jul 2021 03:29:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 29CE161874; Mon, 12 Jul 2021 07:26:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626074775; bh=edKrVGmtiLVtj8iw48kFwlqYYfAz+bZy2sTnQGqvPUo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vSqz9e8fo7WA5vR8ec+hACBMtZU5DEBtq2V2CUwYa3lk7xAFcK0EAGvCwGfqKs/px wz5lp3WGVEsWfXGRxsuenybp5UGPukYLPjoYmQlgVsvdaN977jnA8P3Qz/krosHb6O qdhbzsD9h2D89lZBO+xHzwK8zR7dqrRZxbHqfGWU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jens Axboe , Johannes Thumshirn , Pavel Begunkov , Ming Lei , Tejun Heo , "Matthew Wilcox (Oracle)" , Jeffle Xu , Long Li , Christoph Hellwig Subject: [PATCH 5.12 693/700] block: return the correct bvec when checking for gaps Date: Mon, 12 Jul 2021 08:12:56 +0200 Message-Id: <20210712061049.664586571@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060924.797321836@linuxfoundation.org> References: <20210712060924.797321836@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Long Li commit c9c9762d4d44dcb1b2ba90cfb4122dc11ceebf31 upstream. 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 Reviewed-by: Christoph Hellwig Link: https://lore.kernel.org/r/1623094445-22332-1-git-send-email-longli@linuxonhyperv.com Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- include/linux/bio.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -44,9 +44,6 @@ static inline unsigned int bio_max_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 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(str 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);