Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3256245rwb; Mon, 15 Aug 2022 22:28:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR5lU+7dCD1Y85MAuHP+OSF8V0izanU+CqCefLmcUrVnRLZyewx+LCl0ogZh9q0OG46unWL1 X-Received: by 2002:a17:902:bd49:b0:170:953d:c489 with SMTP id b9-20020a170902bd4900b00170953dc489mr20528411plx.96.1660627693702; Mon, 15 Aug 2022 22:28:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660627693; cv=none; d=google.com; s=arc-20160816; b=sK8/LBDAN5aQmmiyXSiH8uQudBMLH21SdgirMa0ZmbZ+Ou8c+fDgtDgDG/59JugKEM /8VA7E+biG3/2Vc7g/o/CQXi5H1HHmdFKGVXn0lnuZ/vd0NVRCQSa5sH/8OhECVqyU5q tQocuWoa0msNK0uO6fhryMnVe+ZcMuVfLUJW0iGxsTiBpIveAogae/RaEuOKZFnCxstP f5qctVfkWj/tkQZ1QQs+tmnFRSba8cpZZwND7kwopLK1gMCTAJ6qldqqEQBFKQfTs5J8 LeyxBDt4Pu35WdF/VnWauukpAHh9CI3U+eXfJ/T1jyXUXgc3ecA4fmTR2pOQjOZ2eOcn 46Zg== 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=ghZ8sYqjv4iYXezCeknPWcXvULvl7cD6AdnRrDo/wxk=; b=yLY+LOEnwOLfdc2ZvSJY32hxQz6PVHYunKc+bktGl+7mBHtE+mVSJ6Q1EwWsaELbQa YR35iPazXzShi3br/eDmrfNCWHIBexp7lDi+EUjqxnbTKzfumLH2KZBBeQPy1iOjyvB0 OZYQEw/7DYStxYr/D4ijAWtvI+3jWmz9wW7yBoPV/ShRrBHLMFsc00Z5DHIKFVSuvAAb XlNB7w1wYhLz6nYyb/DcFIsBfb2MBgnH+EJEJH629qzmna1ExEy2VWjzjP6uAkIK4cVp 4XsmRt6Ihw3k+7WcYAXn61MUKYS13J8z9sqqcgjyk/G5YwhhlGGM1vfeJo5Er1d26t/l pO3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=G9ev6Cbu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u19-20020a63df13000000b0041f12e5fc3asi12784178pgg.438.2022.08.15.22.28.02; Mon, 15 Aug 2022 22:28:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=G9ev6Cbu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233439AbiHPEyI (ORCPT + 99 others); Tue, 16 Aug 2022 00:54:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233658AbiHPEvb (ORCPT ); Tue, 16 Aug 2022 00:51:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39313DB064; Mon, 15 Aug 2022 13:49:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C210260FC4; Mon, 15 Aug 2022 20:49:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F9B3C433D6; Mon, 15 Aug 2022 20:49:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660596541; bh=WxKy7OIihBZFfS6tzIpJMPQGFP/yMFa8G1Q4lBvrQmM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G9ev6CbuvQ1NPmuVW5+3qra3RXAQKblRd7qS1eXU9t+wqAmf2dD76f2pdIGpGcTeB LM5/LFpRXJkfkSjSJOA6eP9V1hZcBHg0Jr9EN5BiRQDve41v32o8Gyn7nJ7IiQI1KN AbiMifDdR/5J2mPG4/SLkw9+BW1DpzYvCEYCaZOg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Snitzer , Sasha Levin Subject: [PATCH 5.19 1111/1157] dm: fix dm-raid crash if md_handle_request() splits bio Date: Mon, 15 Aug 2022 20:07:47 +0200 Message-Id: <20220815180524.748652319@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Snitzer [ Upstream commit 9dd1cd3220eca534f2d47afad7ce85f4c40118d8 ] Commit ca522482e3eaf ("dm: pass NULL bdev to bio_alloc_clone") introduced the optimization to _not_ perform bio_associate_blkg()'s relatively costly work when DM core clones its bio. But in doing so it exposed the possibility for DM's cloned bio to alter DM target behavior (e.g. crash) if a target were to issue IO without first calling bio_set_dev(). The DM raid target can trigger an MD crash due to its need to split the DM bio that is passed to md_handle_request(). The split will recurse to submit_bio_noacct() using a bio with an uninitialized ->bi_blkg. This NULL bio->bi_blkg causes blk_throtl_bio() to dereference a NULL blkg_to_tg(bio->bi_blkg). Fix this in DM core by adding a new 'needs_bio_set_dev' target flag that will make alloc_tio() call bio_set_dev() on behalf of the target. dm-raid is the only target that requires this flag. bio_set_dev() initializes the DM cloned bio's ->bi_blkg, using bio_associate_blkg, before passing the bio to md_handle_request(). Long-term fix would be to audit and refactor MD code to rely on DM to split its bio, using dm_accept_partial_bio(), but there are MD raid personalities (e.g. raid1 and raid10) whose implementation are tightly coupled to handling the bio splitting inline. Fixes: ca522482e3eaf ("dm: pass NULL bdev to bio_alloc_clone") Cc: stable@vger.kernel.org Signed-off-by: Mike Snitzer Signed-off-by: Sasha Levin --- drivers/md/dm-raid.c | 1 + drivers/md/dm.c | 13 ++++++------- include/linux/device-mapper.h | 6 ++++++ include/uapi/linux/dm-ioctl.h | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/md/dm-raid.c b/drivers/md/dm-raid.c index a55fc28d2a29..ba3638d1d046 100644 --- a/drivers/md/dm-raid.c +++ b/drivers/md/dm-raid.c @@ -3097,6 +3097,7 @@ static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv) INIT_WORK(&rs->md.event_work, do_table_event); ti->private = rs; ti->num_flush_bios = 1; + ti->needs_bio_set_dev = true; /* Restore any requested new layout for conversion decision */ rs_config_restore(rs, &rs_layout); diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 2b75f1ef7386..36c704b50ac3 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -578,9 +578,6 @@ static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) struct bio *clone; clone = bio_alloc_clone(NULL, bio, GFP_NOIO, &md->mempools->io_bs); - /* Set default bdev, but target must bio_set_dev() before issuing IO */ - clone->bi_bdev = md->disk->part0; - tio = clone_to_tio(clone); tio->flags = 0; dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO); @@ -614,6 +611,7 @@ static void free_io(struct dm_io *io) static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask) { + struct mapped_device *md = ci->io->md; struct dm_target_io *tio; struct bio *clone; @@ -623,14 +621,10 @@ static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, /* alloc_io() already initialized embedded clone */ clone = &tio->clone; } else { - struct mapped_device *md = ci->io->md; - clone = bio_alloc_clone(NULL, ci->bio, gfp_mask, &md->mempools->bs); if (!clone) return NULL; - /* Set default bdev, but target must bio_set_dev() before issuing IO */ - clone->bi_bdev = md->disk->part0; /* REQ_DM_POLL_LIST shouldn't be inherited */ clone->bi_opf &= ~REQ_DM_POLL_LIST; @@ -646,6 +640,11 @@ static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, tio->len_ptr = len; tio->old_sector = 0; + /* Set default bdev, but target must bio_set_dev() before issuing IO */ + clone->bi_bdev = md->disk->part0; + if (unlikely(ti->needs_bio_set_dev)) + bio_set_dev(clone, md->disk->part0); + if (len) { clone->bi_iter.bi_size = to_bytes(*len); if (bio_integrity(clone)) diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h index 47a01c7cffdf..e9c043f12e53 100644 --- a/include/linux/device-mapper.h +++ b/include/linux/device-mapper.h @@ -373,6 +373,12 @@ struct dm_target { * after returning DM_MAPIO_SUBMITTED from its map function. */ bool accounts_remapped_io:1; + + /* + * Set if the target will submit the DM bio without first calling + * bio_set_dev(). NOTE: ideally a target should _not_ need this. + */ + bool needs_bio_set_dev:1; }; void *dm_per_bio_data(struct bio *bio, size_t data_size); diff --git a/include/uapi/linux/dm-ioctl.h b/include/uapi/linux/dm-ioctl.h index 2e9550fef90f..27ad9671f2df 100644 --- a/include/uapi/linux/dm-ioctl.h +++ b/include/uapi/linux/dm-ioctl.h @@ -286,9 +286,9 @@ enum { #define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl) #define DM_VERSION_MAJOR 4 -#define DM_VERSION_MINOR 46 +#define DM_VERSION_MINOR 47 #define DM_VERSION_PATCHLEVEL 0 -#define DM_VERSION_EXTRA "-ioctl (2022-02-22)" +#define DM_VERSION_EXTRA "-ioctl (2022-07-28)" /* Status bits */ #define DM_READONLY_FLAG (1 << 0) /* In/Out */ -- 2.35.1