Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp621554ybz; Wed, 15 Apr 2020 15:18:41 -0700 (PDT) X-Google-Smtp-Source: APiQypLPJY7y+NUBSwZ1vkmfGw5fl4QqyuXyh91pCDk7VYBdm8O497IUh6TUOvrclYk8UM5J/24Y X-Received: by 2002:aa7:d794:: with SMTP id s20mr14972761edq.141.1586989120913; Wed, 15 Apr 2020 15:18:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586989120; cv=none; d=google.com; s=arc-20160816; b=UaKubjbsP6NlrCd6SWmTZxXHGJ2UvHP9j4Zdt+INVqUNSnk9hc/YAFEorvIZIxc2AR 93BFhSnwB4OUcxYywe/hYCCaUL7MZEPWH2cDNLqRsp3Eg27CuDUT2DZROFT4fSAAn7uF eAfjSNInOPsAySNHqSOi9i8V8UvomX7H70e4ql9de9j4jiB3lIcpjSanUuHofzB0ot/X NS+7JR0+yPh4YOpDCcosnxotOJ688TER+/yDvIi0Zddj805QFE2Sb89oLoCjPxi4vJ0A X94FgN7f6mUo74jv0kF85qenPYl0bAPqBCa6Jdiky3Is2/vayd7s6R+oZs2T8eV7voc7 NR/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=zpVApyPSKNUh671Lce6Q4vlcf5khOoA/VRo2OWbqlac=; b=YKtVAe8rJ30NYM9JXo5OCt7/cjnP8Q6IO17UQi0GcL0A4LJEMpak8BeHCkQzc2aKla YgbJH4wezn0FVPG/EfMU3OL1ZGrYv81JjyOe355mWM8jzFdRRkzWFN8VtCtlzzO4rldU AZg697utuwKhGc1GX6JnXWNp2IgrNXqAX/7H5ARxl96aJf8s9mHKI194eS0CmELATnW6 sNOzs0KFUiehIzUW7Ev1iQ4ReiwR9yRwZ910YSXmw2zSH45y/za9uU+EKZ2yfryvCAxG sU/TMmcJEzh7Vd7jDXBaEV8Suwo15CpzARm+6OAGAh6OUCkcoybboVgUcxa10LzENsco xALQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Qpjxm2R2; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y8si12415453edp.376.2020.04.15.15.18.17; Wed, 15 Apr 2020 15:18:40 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Qpjxm2R2; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390590AbgDOH16 (ORCPT + 99 others); Wed, 15 Apr 2020 03:27:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403937AbgDOH1n (ORCPT ); Wed, 15 Apr 2020 03:27:43 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29E2AC061A0C; Wed, 15 Apr 2020 00:27:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=zpVApyPSKNUh671Lce6Q4vlcf5khOoA/VRo2OWbqlac=; b=Qpjxm2R2rmmgUR7/xJzRAqA0Jy eSIYEXsZzoVe02qupqqavVrgYyFbF/pIykDnW8TLwp0THBDAkcBMhVrZO12b/qihH2Plo2tr2yxu2 fE6gjGUT8b4DV6V2uuLnfvSQfCBMnYDSyfxXlw5VU+J7DsnH3/sF8MtgEU1/kMCpVprtgqlRn7BF5 37fxxY7FbmpIatlpRhxO6zjpJcyK/ovO55a9qZCN5BMxLl0UEB37+Mk62YTecpH55MVg7nve71cpD UgLpPTAZhKh3UBwKKpmq03QlTQb8u1NoTSkkP0juofeITx/1Ca5aZ2mpo9dQcQEbjj3hhKkcS396i r+L6AhPQ==; Received: from hch by bombadil.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1jOcS0-0005Lr-QZ; Wed, 15 Apr 2020 07:27:12 +0000 Date: Wed, 15 Apr 2020 00:27:12 -0700 From: Christoph Hellwig To: Luis Chamberlain Cc: Christoph Hellwig , Alan Jenkins , axboe@kernel.dk, viro@zeniv.linux.org.uk, bvanassche@acm.org, gregkh@linuxfoundation.org, rostedt@goodmis.org, mingo@redhat.com, jack@suse.cz, ming.lei@redhat.com, nstange@suse.de, akpm@linux-foundation.org, mhocko@suse.com, yukuai3@huawei.com, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Omar Sandoval , Hannes Reinecke , Michal Hocko Subject: Re: [PATCH 4/5] mm/swapfile: refcount block and queue before using blkcg_schedule_throttle() Message-ID: <20200415072712.GB21099@infradead.org> References: <20200414041902.16769-1-mcgrof@kernel.org> <20200414041902.16769-5-mcgrof@kernel.org> <20200414154447.GC25765@infradead.org> <20200415054234.GQ11244@42.do-not-panic.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200415054234.GQ11244@42.do-not-panic.com> X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 15, 2020 at 05:42:34AM +0000, Luis Chamberlain wrote: > > I don't understand the atomic part of the comment. How does > > bdgrab/bdput help us there? > > The commit log above did a better job at explaining this in terms of our > goal to use the request_queue and how this use would prevent the risk of > releasing the request_queue, which could sleep. So bdput eventually does and iput, but what leads to an out of context offload? But anyway, isn't the original problem better solved by simply not releasing the queue from atomic context to start with? There isn't really any good reason we keep holding the spinlock once we have a reference on the queue, so something like this (not even compile tested) should do the work: diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index c5dc833212e1..45faa851f789 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1673,6 +1673,17 @@ void blkcg_maybe_throttle_current(void) blk_put_queue(q); } +/* consumes a reference on q */ +void __blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) +{ + if (current->throttle_queue) + blk_put_queue(current->throttle_queue); + current->throttle_queue = q; + if (use_memdelay) + current->use_memdelay = use_memdelay; + set_notify_resume(current); +} + /** * blkcg_schedule_throttle - this task needs to check for throttling * @q: the request queue IO was submitted on @@ -1694,16 +1705,8 @@ void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { if (unlikely(current->flags & PF_KTHREAD)) return; - - if (!blk_get_queue(q)) - return; - - if (current->throttle_queue) - blk_put_queue(current->throttle_queue); - current->throttle_queue = q; - if (use_memdelay) - current->use_memdelay = use_memdelay; - set_notify_resume(current); + if (blk_get_queue(q)) + __blkcg_schedule_throttle(q, use_memdelay); } /** diff --git a/include/linux/blk-cgroup.h b/include/linux/blk-cgroup.h index 35f8ffe92b70..68440cb3ea9e 100644 --- a/include/linux/blk-cgroup.h +++ b/include/linux/blk-cgroup.h @@ -679,6 +679,7 @@ static inline void blkcg_clear_delay(struct blkcg_gq *blkg) void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); +void __blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); void blkcg_maybe_throttle_current(void); #else /* CONFIG_BLK_CGROUP */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 5871a2aa86a5..4c6aa59ee593 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3749,9 +3749,10 @@ void mem_cgroup_throttle_swaprate(struct mem_cgroup *memcg, int node, gfp_t gfp_mask) { struct swap_info_struct *si, *next; + struct request_queue *q = NULL; + if (!(gfp_mask & __GFP_IO) || !memcg) return; - if (!blk_cgroup_congested()) return; @@ -3761,17 +3762,21 @@ void mem_cgroup_throttle_swaprate(struct mem_cgroup *memcg, int node, */ if (current->throttle_queue) return; + if (unlikely(current->flags & PF_KTHREAD)) + return; spin_lock(&swap_avail_lock); plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { if (si->bdev) { - blkcg_schedule_throttle(bdev_get_queue(si->bdev), - true); + if (blk_get_queue(dev_get_queue(si->bdev))) + q = dev_get_queue(si->bdev); break; } } spin_unlock(&swap_avail_lock); + if (q) + __blkcg_schedule_throttle(q, true); } #endif