Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp760159pxb; Tue, 5 Apr 2022 22:07:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeEieubBqAbtM2waqxQSttWtTapGlKivcRanLUEJHVGTi20Iw/5aRE0HROiVFRhoR/TvLV X-Received: by 2002:a17:90b:b18:b0:1ca:a816:621a with SMTP id bf24-20020a17090b0b1800b001caa816621amr8018199pjb.36.1649221656797; Tue, 05 Apr 2022 22:07:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649221656; cv=none; d=google.com; s=arc-20160816; b=HdvazjpBQtSwtYyALtWx02Fl1XOBREeyGV/1sdXpCtEGp5c0Xg2iMNfOBcWUQfHmic 3XBD09iDasS6Fh0IloLvWEuJzwxAklGSKlvhev74T5WWbK14A9zo5vZazNuYJW3PT+7/ Kg7BDpxXmZjNL2ne6WVEFLQ3pQTvJSlw8+/cg9soSUy5wZxx5snM1V1P/tkHRN7WBgFt QRt+PPX/s5z+XX93CESL815hBFmBMDGFUPW0W5FTvA1CoOLr6dIHII5NLKAxE64A+VVC uBnZd37FyYHPZq8S39/95qojIGxttwT7WpDt1e2ZsNer0SaQ1QRyfpRweKFNC+dWL088 Ur8A== 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=25wOK34d8Rje6qaKC1FtSkg2k2zo4dCu4W7vqa2rxoM=; b=yevF3lM1xaSj3Zietsm5r0AI/DwK0OBMIkD73lx8boBcI1uuoOJy/BFmsQM/wphAC8 mPJGwsbRzmjk0oyFxrWpF8u19i5qVda5NF7BquREoTRkmJ3IF2E/TpE56zXCpt0Dh48o oMPh/nRrmbpiWWaYOmI4xEsGRT9FU69wjf5lvcRRZFptWuRnOXg5HGfzMO3Vu02XFGB+ LXz6AbviYzUrW2J2EnTe9xDuukezIuFXlQDdofruGonwZ462LQF6ouwgknmw5JQoc4Jx /W0+b2uwzBE643PGBAkmAZ/pmpi1aByz9e4j43gAZqp1b/hD6BiWWDx5w2i1cRzpoZmW gCBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bpw1R1Vv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h6-20020aa796c6000000b004fa3a8e006fsi14305041pfq.294.2022.04.05.22.07.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 22:07:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bpw1R1Vv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7E2C7191411; Tue, 5 Apr 2022 20:31:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239982AbiDEL0D (ORCPT + 99 others); Tue, 5 Apr 2022 07:26:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242941AbiDEItn (ORCPT ); Tue, 5 Apr 2022 04:49:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 531D7793A4; Tue, 5 Apr 2022 01:38:00 -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 ams.source.kernel.org (Postfix) with ESMTPS id 20F55B81C82; Tue, 5 Apr 2022 08:37:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7BC8FC385A1; Tue, 5 Apr 2022 08:37:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649147863; bh=B/RiR4B/SIpEqAuuuwCZ/BSWIAePiwRb5+A2vsL9lPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bpw1R1VvlL70HHMLPpbaEk4ZvwizQy6iAp1Y6HDP5lh3JMO2AVVUOulW65T5RKBZN NpM/Qe7/xWb/13TdKdxsjLIUy52fM+g86jcla8kb5CsU3+1BouPm4pA3U3r4Elemhr UJs/l3tIa0ihNl+Q/41RTnoyw/hwFfcIMToeZMag= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tejun Heo , Josef Bacik , Jens Axboe Subject: [PATCH 5.16 0148/1017] block: dont merge across cgroup boundaries if blkcg is enabled Date: Tue, 5 Apr 2022 09:17:41 +0200 Message-Id: <20220405070358.599717605@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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: Tejun Heo commit 6b2b04590b51aa4cf395fcd185ce439cab5961dc upstream. blk-iocost and iolatency are cgroup aware rq-qos policies but they didn't disable merges across different cgroups. This obviously can lead to accounting and control errors but more importantly to priority inversions - e.g. an IO which belongs to a higher priority cgroup or IO class may end up getting throttled incorrectly because it gets merged to an IO issued from a low priority cgroup. Fix it by adding blk_cgroup_mergeable() which is called from merge paths and rejects cross-cgroup and cross-issue_as_root merges. Signed-off-by: Tejun Heo Fixes: d70675121546 ("block: introduce blk-iolatency io controller") Cc: stable@vger.kernel.org # v4.19+ Cc: Josef Bacik Link: https://lore.kernel.org/r/Yi/eE/6zFNyWJ+qd@slm.duckdns.org Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- block/blk-merge.c | 11 +++++++++++ include/linux/blk-cgroup.h | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -598,6 +599,9 @@ static inline unsigned int blk_rq_get_ma static inline int ll_new_hw_segment(struct request *req, struct bio *bio, unsigned int nr_phys_segs) { + if (!blk_cgroup_mergeable(req, bio)) + goto no_merge; + if (blk_integrity_merge_bio(req->q, req, bio) == false) goto no_merge; @@ -694,6 +698,9 @@ static int ll_merge_requests_fn(struct r if (total_phys_segments > blk_rq_get_max_segments(req)) return 0; + if (!blk_cgroup_mergeable(req, next->bio)) + return 0; + if (blk_integrity_merge_rq(q, req, next) == false) return 0; @@ -907,6 +914,10 @@ bool blk_rq_merge_ok(struct request *rq, if (rq->rq_disk != bio->bi_bdev->bd_disk) return false; + /* don't merge across cgroup boundaries */ + if (!blk_cgroup_mergeable(rq, bio)) + return false; + /* only merge integrity protected bio into ditto rq */ if (blk_integrity_merge_bio(rq->q, rq, bio) == false) return false; --- a/include/linux/blk-cgroup.h +++ b/include/linux/blk-cgroup.h @@ -24,6 +24,7 @@ #include #include #include +#include /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */ #define BLKG_STAT_CPU_BATCH (INT_MAX / 2) @@ -604,6 +605,21 @@ static inline void blkcg_clear_delay(str atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); } +/** + * blk_cgroup_mergeable - Determine whether to allow or disallow merges + * @rq: request to merge into + * @bio: bio to merge + * + * @bio and @rq should belong to the same cgroup and their issue_as_root should + * match. The latter is necessary as we don't want to throttle e.g. a metadata + * update because it happens to be next to a regular IO. + */ +static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) +{ + return rq->bio->bi_blkg == bio->bi_blkg && + bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio); +} + void blk_cgroup_bio_start(struct bio *bio); void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); @@ -659,6 +675,7 @@ static inline void blkg_put(struct blkcg static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; } static inline void blkcg_bio_issue_init(struct bio *bio) { } static inline void blk_cgroup_bio_start(struct bio *bio) { } +static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; } #define blk_queue_for_each_rl(rl, q) \ for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)