Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2714297pxv; Sun, 11 Jul 2021 23:35:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgnk8wLvp83THHp1Kx2LA/P7bbHBOMbKLrjNU18bUnrCCfoEGkmxfYxw3427YsHy52XpZp X-Received: by 2002:a05:6e02:1154:: with SMTP id o20mr19033928ill.168.1626071703714; Sun, 11 Jul 2021 23:35:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626071703; cv=none; d=google.com; s=arc-20160816; b=SoH5Xg3JKj1Hlw4TfV1XHVpYSfOGxJwi/so4fjtsWVy/HAe+1uxoCSqyPVfa9Wlom1 dsy5C9CBxC02d5z5vA28XZQa/uOJ5T52lHLcnfMoQrskgfoXlhhf89R6dL6IbF45poKy ZqfEeVY6YrgVajkDvBiBIRKCrsuANJ7ILB4jjTfBAUWpXHwb8nOELYCdBiL5WRwrPUEv 9vxbQ19RxX94sqlE4AZPjHgt2VLeZ97Pp5txOJ14byvVuXFczUfba4XPN455LO3EMjRS t2bl8TMgZCss2V+U6girwVE0XN/bXc8mPhTFPrqAdW0QpOtFt45fcHfKCJ4tqKVQ7xWq EwHA== 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=kP/pt9urnF5L33wbEfgPVIA5i0Bm1fObrhmcsrAFbfA=; b=P4+eys9T/Y2N7XgYbiWCWf94ABzM55eAv53IcKvfocZPUXx1BtZURRosBI7ysVkXf8 ZJPnsbX53bbfdyGFwdP5Ek0qmKgU7cjm1vfAP0VGx4LkSyflhzXxzCWAGm2ekSMgWozF OG8pJ2fdq8mg+HqMBeF25vAbVWQ/NzabiKLbrdqn3Lm0gtouzPpbW48SqN1ndhX5bY1n A3e+mwiYyrXWRkN4pa2SinX+MZljeKX+7QFtCilTCx0Hrcx7u3Bnk3vOZy/43ZKMzPij VIfAsaXr8Stqxvc4HGKSWxqcfbzf5M5fRxpQaHnD21PlnhCzq9EKrffwMgfWQxEtbgDM 17sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wZbLMuMa; 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 x1si12781109jan.58.2021.07.11.23.34.52; Sun, 11 Jul 2021 23:35:03 -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=wZbLMuMa; 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 S235913AbhGLGfw (ORCPT + 99 others); Mon, 12 Jul 2021 02:35:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:53100 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234590AbhGLGau (ORCPT ); Mon, 12 Jul 2021 02:30:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1136A60238; Mon, 12 Jul 2021 06:28:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626071282; bh=l3oMfcZuVVcJ/3YBW7T2ba96pIEBs5pyoAV6faSdfWY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wZbLMuMa9AQBggXY0gWHC5vH6cSFtGFxPOSPqlXH2ZV7n2FhX3lOh4MKdY3d0mhhr +3NPn5RMxrrqgOpq43rIoNot5OM+UdOUrfF8rLjNG/D7IvfQxUecFXYT5r4NpibDkq YBQwdEhzqfGSo21xfbYEJygt2alfgw7nku8BZhnE= 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.4 345/348] block: return the correct bvec when checking for gaps Date: Mon, 12 Jul 2021 08:12:09 +0200 Message-Id: <20210712060749.915442436@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060659.886176320@linuxfoundation.org> References: <20210712060659.886176320@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 @@ -40,9 +40,6 @@ #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))) @@ -246,7 +243,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) @@ -254,10 +251,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);