From patchwork Mon Oct 14 18:22:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joanne Koong X-Patchwork-Id: 13835385 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 9609BD1813A for ; Mon, 14 Oct 2024 18:23:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E42F6B008C; Mon, 14 Oct 2024 14:23:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 094866B0092; Mon, 14 Oct 2024 14:23:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9E0F6B0093; Mon, 14 Oct 2024 14:23:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id CCA496B008C for ; Mon, 14 Oct 2024 14:23:52 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B01191611D6 for ; Mon, 14 Oct 2024 18:23:43 +0000 (UTC) X-FDA: 82673031336.12.7D0EEE9 Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) by imf02.hostedemail.com (Postfix) with ESMTP id 5ADD980006 for ; Mon, 14 Oct 2024 18:23:37 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l7l4HG+v; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.219.179 as permitted sender) smtp.mailfrom=joannelkoong@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728930183; a=rsa-sha256; cv=none; b=ldg5VAEGcU/bJxMpj3zYFQlZtLZ6ZDdVHhg/uYLUnku+IRxbsFN5COF9NBqFQgH7ovDEWR K9vvGPd74zMFfSmAXLOw2RCqHeu4koHR8Hr5gnlhBdSIXiI6IqwtYh96IIftlOf1SjyOWa M3kZLQYKMDZtcf9M1BJ2eSkaQPp72NQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l7l4HG+v; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.219.179 as permitted sender) smtp.mailfrom=joannelkoong@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728930183; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fdBz15jaWsg8cxk/btL9PXuORpPlgL7NIU4wQ/z1ydQ=; b=lhSELI6xuagMYmtiij8ULb9Yh1fDTcNFmn/0o6kJq4yJiaMnNOJ4CTxoTBlDc1Y+EFp5ng JT0wbTANEWx0tZmtl0EDCOYMdWLQjc9wobrceFoQnNu3G4NUBC76qG7r9JgGwqAIO7zDYS AvRXpQptRf5zCbu6C6nMfmSnqRo+HR8= Received: by mail-yb1-f179.google.com with SMTP id 3f1490d57ef6-e290222fdd0so3871693276.2 for ; Mon, 14 Oct 2024 11:23:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728930228; x=1729535028; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fdBz15jaWsg8cxk/btL9PXuORpPlgL7NIU4wQ/z1ydQ=; b=l7l4HG+v4iAR11Cbw+I7GReuAN6pIDL/d+IrJ+Uw97ZKe2hnelOG7+5P6b30AVRW6n 73LYnl3xSpBiaUbpLvzv74PF6GPWvg5u2xUxz8HJLUVlDS3cA7PllbAkBuNd0BMJEOwV WA1H7sgDbq9rBrxO8V2INffOyzl5gc84HumQxdEvStMEKGVnOqxMPXZAH27QKe4awM2d VHE0ep/ky/TEqq7/HX8/unyGSb3JpZl+XV05D/7yON1E7tIPlUwuKL4nqcY+7auJRTgw stCsy1JLSo8uvfVsYDfNir8rYSby77JPpwFy9wCAcfQajSf58HMeDs3ZgLzrJSUkvFhj r+2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728930228; x=1729535028; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fdBz15jaWsg8cxk/btL9PXuORpPlgL7NIU4wQ/z1ydQ=; b=JcBQQQ3AGGSYZTvw2fmANo8MvqEkDZ2evHJ873bo1i2A9P2MaOT2EsE+KLj8GOfIij BW8OuRtQIH3WX5qa4VEwmvSDG9b6gpHQgfHdJ99zPiu5BIkRPqSXA+mneHM47NpMRuW7 G17Do0jTYDFUL1cASubbGoi/TpFqEmtPnP1Bycrt32hYllLHAOyAnrBEDkIVY4IpE0ds 29UbNagIBgT4dofUc2sHiJQFvC/d96IDaHKE0m0M9CpoA90j/Gk/LoYBoL4+0gdJiOZ9 0SeWY4WaadgZI6SSgRDc3WCpJZmOoDXGfHCj/OLNdLBHCHP/IoZQjnffQt5B/dnE3YcE ZlUw== X-Forwarded-Encrypted: i=1; AJvYcCXk1eL4lP3awsae7qltttZn2v1OyHL4j9n3sK1gVCsk4HXrbMQD6BbotlKpunKGZRS/VrqURgsa3w==@kvack.org X-Gm-Message-State: AOJu0Yxz2jjn0CwJw5rPUORto4DMfDoE9i03PI7w9pXF4XrpECbn4GWN jBIyl1puCvH00gfNuikA2cfcKpXBkqhaM2xazefgAm8qv75pELwc X-Google-Smtp-Source: AGHT+IH0HBiv9xojtsirLqVwsKrhBEcPpvem9jXWisw4CBG6ZuEb3+X3anL3lNcMpVwQK3GNkEA0FQ== X-Received: by 2002:a05:690c:660a:b0:6e3:430c:b120 with SMTP id 00721157ae682-6e3477bad81mr88698067b3.5.1728930228550; Mon, 14 Oct 2024 11:23:48 -0700 (PDT) Received: from localhost (fwdproxy-nha-114.fbsv.net. [2a03:2880:25ff:72::face:b00c]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6e332cac1c6sm16249157b3.132.2024.10.14.11.23.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 11:23:48 -0700 (PDT) From: Joanne Koong To: miklos@szeredi.hu, linux-fsdevel@vger.kernel.org Cc: josef@toxicpanda.com, bernd.schubert@fastmail.fm, jefflexu@linux.alibaba.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, linux-mm@kvack.org, kernel-team@meta.com Subject: [PATCH v2 1/2] mm: skip reclaiming folios in writeback contexts that may trigger deadlock Date: Mon, 14 Oct 2024 11:22:27 -0700 Message-ID: <20241014182228.1941246-2-joannelkoong@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241014182228.1941246-1-joannelkoong@gmail.com> References: <20241014182228.1941246-1-joannelkoong@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 5ADD980006 X-Rspamd-Server: rspam01 X-Stat-Signature: g9tppbjrs9w1n4yb5dnqoybgq67c9ytc X-HE-Tag: 1728930217-557929 X-HE-Meta: U2FsdGVkX19p/2xaSXHNityMwNl1zwI+mmYfu0ipkS6SZk0CfOjef+YSyXfYODZ2fFvVq5/beugTVwwZQbI2oVrzRrImowQUP7njTASR+ekbyujvWfAuwpFMdHMIrCiSSt3osq5bv4WarxlnJpiiGIVp4ZtnNvso2LFlD3awiNLigpOQqoKKA/4x0sFW64zfP2DslMslOrbjwcPTfgRyqFvzmQwQIDCXhULryH1L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PqDGY6gd5EK8jUshyI2C4WgQ1I= 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: List-Subscribe: List-Unsubscribe: Currently in shrink_folio_list(), reclaim for folios under writeback falls into 3 different cases: 1) Reclaim is encountering an excessive number of folios under writeback and this folio has both the writeback and reclaim flags set 2) Dirty throttling is enabled (this happens if reclaim through cgroup is not enabled, if reclaim through cgroupv2 memcg is enabled, or if reclaim is on the root cgroup), or if the folio is not marked for immediate reclaim, or if the caller does not have __GFP_FS (or __GFP_IO if it's going to swap) set 3) Legacy cgroupv1 encounters a folio that already has the reclaim flag set and the caller did not have __GFP_FS (or __GFP_IO if swap) set In cases 1) and 2), we activate the folio and skip reclaiming it while in case 3), we wait for writeback to finish on the folio and then try to reclaim the folio again. In case 3, we wait on writeback because cgroupv1 does not have dirty folio throttling, as such this is a mitigation against the case where there are too many folios in writeback with nothing else to reclaim. The issue is that for filesystems where writeback may block, sub-optimal workarounds need to be put in place to avoid potential deadlocks that may arise from the case where reclaim waits on writeback. (Even though case 3 above is rare given that legacy cgroupv1 is on its way to being deprecated, this case still needs to be accounted for) For example, for FUSE filesystems, when a writeback is triggered on a folio, a temporary folio is allocated and the pages are copied over to this temporary folio so that writeback can be immediately cleared on the original folio. This additionally requires an internal rb tree to keep track of writeback state on the temporary folios. Benchmarks show roughly a ~20% decrease in throughput from the overhead incurred with 4k block size writes. The temporary folio is needed here in order to avoid the following deadlock if reclaim waits on writeback: * single-threaded FUSE server is in the middle of handling a request that needs a memory allocation * memory allocation triggers direct reclaim * direct reclaim waits on a folio under writeback (eg falls into case 3 above) that needs to be written back to the fuse server * the FUSE server can't write back the folio since it's stuck in direct reclaim This commit adds a new flag, AS_NO_WRITEBACK_RECLAIM, to "enum mapping_flags" which filesystems can set to signify that reclaim should not happen when the folio is already in writeback. This only has effects on the case where cgroupv1 memcg encounters a folio under writeback that already has the reclaim flag set (eg case 3 above), and allows for the suboptimal workarounds added to address the "reclaim wait on writeback" deadlock scenario to be removed. Signed-off-by: Joanne Koong --- include/linux/pagemap.h | 11 +++++++++++ mm/vmscan.c | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 68a5f1ff3301..513a72b8451b 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -210,6 +210,7 @@ enum mapping_flags { AS_STABLE_WRITES = 7, /* must wait for writeback before modifying folio contents */ AS_INACCESSIBLE = 8, /* Do not attempt direct R/W access to the mapping */ + AS_NO_WRITEBACK_RECLAIM = 9, /* Do not reclaim folios under writeback */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS = 5, AS_FOLIO_ORDER_MIN = 16, @@ -335,6 +336,16 @@ static inline bool mapping_inaccessible(struct address_space *mapping) return test_bit(AS_INACCESSIBLE, &mapping->flags); } +static inline void mapping_set_no_writeback_reclaim(struct address_space *mapping) +{ + set_bit(AS_NO_WRITEBACK_RECLAIM, &mapping->flags); +} + +static inline int mapping_no_writeback_reclaim(struct address_space *mapping) +{ + return test_bit(AS_NO_WRITEBACK_RECLAIM, &mapping->flags); +} + static inline gfp_t mapping_gfp_mask(struct address_space * mapping) { return mapping->gfp_mask; diff --git a/mm/vmscan.c b/mm/vmscan.c index 749cdc110c74..885d496ae652 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1110,6 +1110,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (writeback && folio_test_reclaim(folio)) stat->nr_congested += nr_pages; + mapping = folio_mapping(folio); + /* * If a folio at the tail of the LRU is under writeback, there * are three cases to consider. @@ -1165,7 +1167,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, /* Case 2 above */ } else if (writeback_throttling_sane(sc) || !folio_test_reclaim(folio) || - !may_enter_fs(folio, sc->gfp_mask)) { + !may_enter_fs(folio, sc->gfp_mask) || + (mapping && mapping_no_writeback_reclaim(mapping))) { /* * This is slightly racy - * folio_end_writeback() might have @@ -1320,7 +1323,6 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (folio_maybe_dma_pinned(folio)) goto activate_locked; - mapping = folio_mapping(folio); if (folio_test_dirty(folio)) { /* * Only kswapd can writeback filesystem folios