Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4209269imm; Fri, 18 May 2018 00:55:07 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrk2unS4k5DQgwifwsbJ5neI+ixiPTrGkyMBu1B/4uDMlirqzGH5spEilD1TZF+9NX3R2Xv X-Received: by 2002:a17:902:988b:: with SMTP id s11-v6mr8478069plp.304.1526630107312; Fri, 18 May 2018 00:55:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526630107; cv=none; d=google.com; s=arc-20160816; b=TBaDdka4sagh76srtiRpbb84Hvz1OTwmdgw/fV4ebAlQmJb62mj6ZZNzzqYt6+8syf u3orTm1xHxhl2PmotfeAO5nfRMpDiuY4P4oEpHlCe9Zib0pJ1HdE0bJavFTvtqAIa22+ 4FB0Zd8coj9PUTDc67AvAwsGDmm72TrlMHPYfdIPEtzT02nVQVSwzP2bldipyDPCQG91 oVgcVhh6458Fh63Ma90XIK8pE+eCjmwtmeyPnVzDCIg9Gb13lJZpQppSUx4hommseoYn HZcsKVEzS/ngX62pPPgFtKYAEbVhjr8PUOkg5iozE6v2hrgDRMo+R3HV3YZbr7EtVb/P A86A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=sOaXvFMRxJ8wbH6KTtdYddcySDG7QIVvtK9vhH4NLIM=; b=hT2Vj31Z7IZnfSv2nHYRs8ufyrheSM7xNM4Tjc+HuOF9xtmZHKbJV3FSLthEUtSROL ZKmQSl2yWBqSgmipiFMyj4gGXfzrcwswS3EB8EZyYv+FkIrGwIm5N1TQMNW6sVpCh6XY ErwpgIQEemgKJFugc2NrbwkiPi0ah3j7zMdT/XLuIGci052Ab1icN30FGxX3GF89NjIN MOPHfffnjKpLZ2/u7McBipP/uCDBaNaLHmYOBIukjE14TRKalwyWWpD0WMhjOlY6owRT tjxFYgTSJ8ttsanvFNRS4XoEOzU4U79cj0TRlJDixgwnc95Hz6wTFCHcdp8/ruMpKIPZ RT5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kFZi5+pA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q9-v6si5544905pgt.5.2018.05.18.00.54.53; Fri, 18 May 2018 00:55:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kFZi5+pA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752373AbeERHuN (ORCPT + 99 others); Fri, 18 May 2018 03:50:13 -0400 Received: from mail-qt0-f196.google.com ([209.85.216.196]:37487 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752334AbeERHuJ (ORCPT ); Fri, 18 May 2018 03:50:09 -0400 Received: by mail-qt0-f196.google.com with SMTP id q13-v6so9166019qtp.4; Fri, 18 May 2018 00:50:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=sOaXvFMRxJ8wbH6KTtdYddcySDG7QIVvtK9vhH4NLIM=; b=kFZi5+pApYHvXJhhnNp86rkQ4skXY7d2ReMbbzTXMLjR+jrrUjOak+3YoR/MHxze9g GnJ+RvQmSscwQTMvnwDklJgNjg8eRRsJxwskxNAIslaj1VukOVA721S6O6YXGf/t3dKX zLl3XCYJDh95hkAgx3ozB6M4nvrzmp/OQeXhBKJFZ8+2XpcJGVn94YHZ1cc4GdPd/nmr btisvQFro1W5yziT7YzOTE4bYJdD/Xzb4Bjg5ONlzXJiGxeC1jhYMByRhv+xw0rpSJ8m FGsapbW6t+xPm2arleqaVtrTK14Rqs0w6I3EeEdEOVuHyHZdaVp8SgpN/eRK4UA/FnAA nN6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=sOaXvFMRxJ8wbH6KTtdYddcySDG7QIVvtK9vhH4NLIM=; b=ql5hetBZ5sN4XXrzXV5FdC9Lo29+3DRi/OQnDu476vEKiAfQbITeOW/fn2oDa8pOok pv+z71fGxI8qQTSq05JQgOAoEveVD2a2RtG2FAA2DdiFsygTwzTnhy/PmCx43PZ4Qhic itFjVtIAHM4h0aLYE0tv/ZZoSA8YghmxQuqsiie6Mt4sfoI2HUnbetz5LOcK6KqbR9Rm J5J/Qtw8jiv8bXWFQWreDmLungw5n0IuJcTx+Dm7iEhLlaxy/W1xkpuvTGNtUcwCuYsL EoM6RuumtocSGvFm823xd04vX6wR20FvJiq5f9ZdyFitZ64s3ONJYGaP46/YUO9AXy8L b5Sw== X-Gm-Message-State: ALKqPwfqcdbRC1n8Bo7VHPU8Qbn+28Hd2rCd6IVpiVbRGSxfPLBascyQ PqtbOKba6RCgWfSyMvgnRgiBFV7MVA== X-Received: by 2002:ac8:253b:: with SMTP id 56-v6mr8264180qtm.349.1526629808421; Fri, 18 May 2018 00:50:08 -0700 (PDT) Received: from localhost.localdomain (c-71-234-172-214.hsd1.vt.comcast.net. [71.234.172.214]) by smtp.gmail.com with ESMTPSA id s64-v6sm5443004qkl.85.2018.05.18.00.50.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 May 2018 00:50:07 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-mm@kvack.org, Jens Axboe , Ingo Molnar Cc: Kent Overstreet Subject: [PATCH 06/10] block: Split out bio_list_copy_data() Date: Fri, 18 May 2018 03:49:09 -0400 Message-Id: <20180518074918.13816-12-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180518074918.13816-1-kent.overstreet@gmail.com> References: <20180518074918.13816-1-kent.overstreet@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Found a bug (with ASAN) where we were passing a bio to bio_copy_data() with bi_next not NULL, when it should have been - a driver had left bi_next set to something after calling bio_endio(). Since the normal case is only copying single bios, split out bio_list_copy_data() to avoid more bugs like this in the future. Signed-off-by: Kent Overstreet --- block/bio.c | 83 +++++++++++++++++++++++++---------------- drivers/block/pktcdvd.c | 2 +- include/linux/bio.h | 5 ++- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/block/bio.c b/block/bio.c index d7bd765e9e..c58544d4bc 100644 --- a/block/bio.c +++ b/block/bio.c @@ -971,32 +971,16 @@ void bio_advance(struct bio *bio, unsigned bytes) } EXPORT_SYMBOL(bio_advance); -void bio_copy_data_iter(struct bio *dst, struct bvec_iter dst_iter, - struct bio *src, struct bvec_iter src_iter) +void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, + struct bio *src, struct bvec_iter *src_iter) { struct bio_vec src_bv, dst_bv; void *src_p, *dst_p; unsigned bytes; - while (1) { - if (!src_iter.bi_size) { - src = src->bi_next; - if (!src) - break; - - src_iter = src->bi_iter; - } - - if (!dst_iter.bi_size) { - dst = dst->bi_next; - if (!dst) - break; - - dst_iter = dst->bi_iter; - } - - src_bv = bio_iter_iovec(src, src_iter); - dst_bv = bio_iter_iovec(dst, dst_iter); + while (src_iter->bi_size && dst_iter->bi_size) { + src_bv = bio_iter_iovec(src, *src_iter); + dst_bv = bio_iter_iovec(dst, *dst_iter); bytes = min(src_bv.bv_len, dst_bv.bv_len); @@ -1010,31 +994,66 @@ void bio_copy_data_iter(struct bio *dst, struct bvec_iter dst_iter, kunmap_atomic(dst_p); kunmap_atomic(src_p); - bio_advance_iter(src, &src_iter, bytes); - bio_advance_iter(dst, &dst_iter, bytes); + bio_advance_iter(src, src_iter, bytes); + bio_advance_iter(dst, dst_iter, bytes); } } EXPORT_SYMBOL(bio_copy_data_iter); /** - * bio_copy_data - copy contents of data buffers from one chain of bios to - * another - * @src: source bio list - * @dst: destination bio list - * - * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats - * @src and @dst as linked lists of bios. + * bio_copy_data - copy contents of data buffers from one bio to another + * @src: source bio + * @dst: destination bio * * Stops when it reaches the end of either @src or @dst - that is, copies * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios). */ void bio_copy_data(struct bio *dst, struct bio *src) { - bio_copy_data_iter(dst, dst->bi_iter, - src, src->bi_iter); + struct bvec_iter src_iter = src->bi_iter; + struct bvec_iter dst_iter = dst->bi_iter; + + bio_copy_data_iter(dst, &dst_iter, src, &src_iter); } EXPORT_SYMBOL(bio_copy_data); +/** + * bio_list_copy_data - copy contents of data buffers from one chain of bios to + * another + * @src: source bio list + * @dst: destination bio list + * + * Stops when it reaches the end of either the @src list or @dst list - that is, + * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of + * bios). + */ +void bio_list_copy_data(struct bio *dst, struct bio *src) +{ + struct bvec_iter src_iter = src->bi_iter; + struct bvec_iter dst_iter = dst->bi_iter; + + while (1) { + if (!src_iter.bi_size) { + src = src->bi_next; + if (!src) + break; + + src_iter = src->bi_iter; + } + + if (!dst_iter.bi_size) { + dst = dst->bi_next; + if (!dst) + break; + + dst_iter = dst->bi_iter; + } + + bio_copy_data_iter(dst, &dst_iter, src, &src_iter); + } +} +EXPORT_SYMBOL(bio_list_copy_data); + struct bio_map_data { int is_our_pages; struct iov_iter iter; diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c index c61d20c9f3..00ea788b17 100644 --- a/drivers/block/pktcdvd.c +++ b/drivers/block/pktcdvd.c @@ -1285,7 +1285,7 @@ static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt) * Fill-in bvec with data from orig_bios. */ spin_lock(&pkt->lock); - bio_copy_data(pkt->w_bio, pkt->orig_bios.head); + bio_list_copy_data(pkt->w_bio, pkt->orig_bios.head); pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE); spin_unlock(&pkt->lock); diff --git a/include/linux/bio.h b/include/linux/bio.h index 5a6ee955a8..98b175cc00 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -505,9 +505,10 @@ static inline void bio_flush_dcache_pages(struct bio *bi) } #endif -extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter dst_iter, - struct bio *src, struct bvec_iter src_iter); +extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, + struct bio *src, struct bvec_iter *src_iter); extern void bio_copy_data(struct bio *dst, struct bio *src); +extern void bio_list_copy_data(struct bio *dst, struct bio *src); extern void bio_free_pages(struct bio *bio); extern struct bio *bio_copy_user_iov(struct request_queue *, -- 2.17.0