From patchwork Mon Apr 3 22:03:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13198872 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C50EC76196 for ; Mon, 3 Apr 2023 22:03:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70CAC6B0074; Mon, 3 Apr 2023 18:03:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BE20900002; Mon, 3 Apr 2023 18:03:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 50F346B0078; Mon, 3 Apr 2023 18:03:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4240D6B0074 for ; Mon, 3 Apr 2023 18:03:44 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 208521206A4 for ; Mon, 3 Apr 2023 22:03:44 +0000 (UTC) X-FDA: 80641457568.05.9B2E335 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf09.hostedemail.com (Postfix) with ESMTP id 499BF14001F for ; Mon, 3 Apr 2023 22:03:42 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="W/ze/sAl"; spf=pass (imf09.hostedemail.com: domain of 3PU0rZAoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3PU0rZAoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680559422; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kId3QKjERA5KglVeO1VKvGvv5azlGeISt3c2gJrYgyo=; b=Nhv08jWbX/zdLd/u2k4h4vE+UcdnQsusG/aP6GWwYthXWBdfeIbkDHwuMZlXCW89ktPL2W C1HXPa5AwBpArCe3jbhLzjSx2kTaYMKH1pLEerNu/Mmor0HPdwnDpyTvif2OANI0jKcvaU pPszy10xmz9cPpvpah6qgrFAKlsdBJ0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="W/ze/sAl"; spf=pass (imf09.hostedemail.com: domain of 3PU0rZAoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3PU0rZAoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680559422; a=rsa-sha256; cv=none; b=jLE3UrSnJpZfO+uMmGEmkL/Nz3RjMGtUnICpDLHdcDrfEzOSV5FlBg7LbWSG/s4HqEBh// uBci3zO0qf7HWsvE3cqY9SZk67YiPCCWGExez42LqkznwAufuZwLm6Twi+DBlOfyfXbpMm H9w8CKE7GDPfxWvA/1GlroaaSl0T3Lo= Received: by mail-pl1-f202.google.com with SMTP id l1-20020a170903244100b001a0468b4afcso18285116pls.12 for ; Mon, 03 Apr 2023 15:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680559421; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kId3QKjERA5KglVeO1VKvGvv5azlGeISt3c2gJrYgyo=; b=W/ze/sAlU7O+7GhoVMA+gO+b3zK1GLSBb2wxG7cqOl+9vjmm6QdV40GnYhvFBAr6we 6QpU1r1iDXHFCTCDI3KvZCA5Fdx66xMvZLtqAf+RggvSzScbqU6Qb+D3U4BXwhb/OMA1 mj7EUoKa/2hKudyISX9IDUT5QtGjglqN6doUhxmfcL1+w5LR9gcm9RCdAs4iQ3yY+fKw 8EP2M2Ss5aeWYRlPCW8BW8qsios85nr5jm7RooCKJYkRU/xUY3Tcdg08l4PT4At3QjPp 8RJrh+/qwzB7v9VlZ60Az/txlakHCjAKDKadt2/B61f8k1qeYBs4X+ujruMC4SFwxMjN IvmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680559421; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kId3QKjERA5KglVeO1VKvGvv5azlGeISt3c2gJrYgyo=; b=fifin4Oj11t/em2xwE6/Fwjg9VzXwK7DA5Q89CCw86p52XSFL/OI3Y8/DHWYdtpN/c Fi9kjpuy1/rmIlFnVh+XeiNnA9llDgwVDEMzFG6nBvSudHTZyw/tYMq/uRKqeCDbAT1j KmiBJOEpPlp1TYNnCqHfnXTVBfu1uRm5EepJtlYc5reXFQfh8l52jxSD9pKE+j99OvNl UZXTkC9rqLXbjb+ORorXd9BX1G8lOZyOzml3mq99gqKj2tHq2HnmCT8gtgVfsvAayDCm hfx+SmUN/cf55C9ZB3xoX24OGRrCqFHKw0aQtO8zDtfTD9bxCUktwQXrlJUuszlWakNz u/Qg== X-Gm-Message-State: AAQBX9cNb7N14jt1ncPsWeb35RNqAuVN5NfUhxIhvRmyOM4mU5vdlIHx 3TwPOVtptoiizYWYAG1PedV2Mf4WHzpI1zO5 X-Google-Smtp-Source: AKy350bROYdy/7K8uGxBM0X/ZxH2AG6/5IpCycN9GV2HdX4MbVmRhF2WLCfVj205eUvkzCs7+3h4yAAbVuROFoGd X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a63:f307:0:b0:50c:bd0:eb8c with SMTP id l7-20020a63f307000000b0050c0bd0eb8cmr39435pgh.6.1680559421112; Mon, 03 Apr 2023 15:03:41 -0700 (PDT) Date: Mon, 3 Apr 2023 22:03:33 +0000 In-Reply-To: <20230403220337.443510-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230403220337.443510-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230403220337.443510-2-yosryahmed@google.com> Subject: [PATCH mm-unstable RFC 1/5] writeback: move wb_over_bg_thresh() call outside lock section From: Yosry Ahmed To: Alexander Viro , Christian Brauner , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed X-Rspamd-Queue-Id: 499BF14001F X-Stat-Signature: cxjmhjxbjrjsau86zhe6uzmjsi91sg4o X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1680559422-178362 X-HE-Meta: U2FsdGVkX1/S42yh7NrlZHPXAsiDAQb5mpBOsAFX8GVL8Zc5dxCFUg/M35g7Qq7Tw6CD2cQku7kfP4eY9nccxydIYhZPhsVql03KxW1MS910L26jIZhAvFpC96SrP7OhAVjZqAyaMcGfCZEYQRPDijbDlLSTpuDZbbIWJY0ZYbyZOBa5t9hdZ2BcGmw8nZ/aQ3D6eCJWPLXs/b4+nXpg0hhATApIihmrPzIThl3k+CUjhHb2fPgjwDJBPCB9qW/d5h1QsHui6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ay8w/rGF5BauxejyK82dJ8ls5lxu3qpS32z+8Einl9qXOGPx4JZ4RsQr134+DTpV+3rp5A+sgSWdmI7iS0Va9o69w6nCn+gOUlPCF7b9/GZmTOMDFugtjDWXUEdwI2pbCpZ6iDjzPJ6TnmGblF1MqtlRTdfpmy53woNeafWk/e/up8zglYfVvhIAWth3KmJaHS4WzSYcrw7VFDpixwwi9HkhZkdfC3Qegp/WCVMJo8qTvsZ1OfOkRF5PYENgUCcJh9lfHqKgUSNy7hI4egUqpFBB+W6x26snfzbxTzJEtDFd2OUv8VqRQOxpaAXRin6F0+aN6hz8vszVD01fiuJWZneAm4QdFZAh7AGzQ4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: wb_over_bg_thresh() calls mem_cgroup_wb_stats() which invokes an rstat flush, which can be expensive on large systems. Currently, wb_writeback() calls wb_over_bg_thresh() within a lock section, so we have to make the rstat flush atomically. On systems with a lot of cpus/cgroups, this can cause us to disable irqs for a long time, potentially causing problems. Move the call to wb_over_bg_thresh() outside the lock section in preparation to make the rstat flush in mem_cgroup_wb_stats() non-atomic. The list_empty(&wb->work_list) should be okay outside the lock section of wb->list_lock as it is protected by a separate lock (wb->work_lock), and wb_over_bg_thresh() doesn't seem like it is modifying any of the b_* lists the wb->list_lock is protecting. Also, the loop seems to be already releasing and reacquring the lock, so this refactoring looks safe. Signed-off-by: Yosry Ahmed Reviewed-by: Michal Koutný Reviewed-by: Jan Kara --- fs/fs-writeback.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index 195dc23e0d831..012357bc8daa3 100644 --- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -2021,7 +2021,6 @@ static long wb_writeback(struct bdi_writeback *wb, struct blk_plug plug; blk_start_plug(&plug); - spin_lock(&wb->list_lock); for (;;) { /* * Stop writeback when nr_pages has been consumed @@ -2046,6 +2045,9 @@ static long wb_writeback(struct bdi_writeback *wb, if (work->for_background && !wb_over_bg_thresh(wb)) break; + + spin_lock(&wb->list_lock); + /* * Kupdate and background works are special and we want to * include all inodes that need writing. Livelock avoidance is @@ -2075,13 +2077,19 @@ static long wb_writeback(struct bdi_writeback *wb, * mean the overall work is done. So we keep looping as long * as made some progress on cleaning pages or inodes. */ - if (progress) + if (progress) { + spin_unlock(&wb->list_lock); continue; + } + /* * No more inodes for IO, bail */ - if (list_empty(&wb->b_more_io)) + if (list_empty(&wb->b_more_io)) { + spin_unlock(&wb->list_lock); break; + } + /* * Nothing written. Wait for some inode to * become available for writeback. Otherwise @@ -2093,9 +2101,7 @@ static long wb_writeback(struct bdi_writeback *wb, spin_unlock(&wb->list_lock); /* This function drops i_lock... */ inode_sleep_on_writeback(inode); - spin_lock(&wb->list_lock); } - spin_unlock(&wb->list_lock); blk_finish_plug(&plug); return nr_pages - work->nr_pages;