From mboxrd@z Thu Jan 1 00:00:00 1970 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 1447AD1813C for ; Mon, 14 Oct 2024 18:38:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A18DE6B008A; Mon, 14 Oct 2024 14:38:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C8586B0092; Mon, 14 Oct 2024 14:38:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B7446B0093; Mon, 14 Oct 2024 14:38:57 -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 6DE5B6B008A for ; Mon, 14 Oct 2024 14:38:57 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DEE171A0B29 for ; Mon, 14 Oct 2024 18:38:42 +0000 (UTC) X-FDA: 82673069262.08.B4E8501 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by imf17.hostedemail.com (Postfix) with ESMTP id ED37B40008 for ; Mon, 14 Oct 2024 18:38:49 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=abRtzuYf; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf17.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728931030; a=rsa-sha256; cv=none; b=vBRdFHZNMbyxZeYspvnHPgPocscIsS92OnC6sEZYelPSws/wINLI/1eAzhduaGTAQsz4cO U7zLLA+xvmB8IdAkLP8Dm9kPM7w1cs2yI0vu6ZQTR17owsFcvPsoH6Ssz1diTGgCfNOkbL ixGUGCAEuOjDYM1Q21OesouQu7It1Cg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=abRtzuYf; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf17.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728931030; 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=IqlvY1cmnsPONjSxitMWFO4oU3mXdPExHv7VOQKaoE8=; b=Xfy3MGenDzKAKTCx/n2eL/H6It6cRPoGzq06aP6ZZiz8cdX6FmJeZoCTzBSI3sJKlw6F4l L1ba0ruOC+nx0qweg+ekiqQ9vYe3OvGlGRcpsqVsgsfpU9tERqqFKrzJdIkCPXxtLk4SPt DY2p7amBDauvckXwAcYr9O+rRB70whQ= Date: Mon, 14 Oct 2024 11:38:46 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1728931132; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=IqlvY1cmnsPONjSxitMWFO4oU3mXdPExHv7VOQKaoE8=; b=abRtzuYfqExHRbn1ZNvhyM3nl48wV2cfPCC/2XVniLXYM0PmDPqOwlWdvRy/WgImTFwscS QXy3m49XgkR3ItWwncrHp5RYxT0jtZmE7yjog8Uh9EFJTDr4Wqg8yUdd4BJcDU6aYdrV2I TWSXk49uBT4XLiKmMlwCeuEP/9Q8puI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Joanne Koong Cc: miklos@szeredi.hu, linux-fsdevel@vger.kernel.org, josef@toxicpanda.com, bernd.schubert@fastmail.fm, jefflexu@linux.alibaba.com, hannes@cmpxchg.org, linux-mm@kvack.org, kernel-team@meta.com Subject: Re: [PATCH v2 1/2] mm: skip reclaiming folios in writeback contexts that may trigger deadlock Message-ID: <265keu5uzo3gzqrvhcn2cagii4sak3e2a372ra7jlav35fnkrx@aicyzyftun3l> References: <20241014182228.1941246-1-joannelkoong@gmail.com> <20241014182228.1941246-2-joannelkoong@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241014182228.1941246-2-joannelkoong@gmail.com> X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: ED37B40008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: xttjp98h8b76bnj8mutg581d6qp9x37h X-HE-Tag: 1728931129-9284 X-HE-Meta: U2FsdGVkX18pTY1cKBN+4oshKIa9qn4yd/2ic6+LtUvqCDoleDtznMDUjuDjXlCIv+5K8vnInJGuK1laR/Fwzqu4Xm5ahAC/b/L8oiPbSvuQfTKwRWKc+vSyOJHSAMdoOe5FxNyenRwjdwaIw9vW9lvSJRIX3tOTClhkokGi2rl4mnljlrhWL3rGNHl5bFDN7yZafKT9wX69AKfwshp2x0+anErQMaeqKzmhv2mjCYbvThHiI/vc5L9DCBdUWJ22utw9dTAXkw7iUkBRq49aksxc796URkITKUBP2iX1MSOWwvYzYTTyyFr6PJSh1YpWE8r2rUzYRaj0G2MlkY8kRb7wsl4kTvDq68b3rz0t+MX+LdQ0aOon/16UB8N3jVpzeGrTxyA64nxZlKel65vIQTRlm1OjZcA9t2SsOib/6AReX5u/GuHC5OVp3xbKkluzXCL9e0HPfXqyP7O8wQWdup/C9pMCElRnX/WqxRvweKYy6otg1bVBoZ8xQrx/L/2AF8QD5sR6li/tE8AmwDgnxj3s5gDGy39c8vWK25osb7f4PpQjK2J9AZS8Y1+35EFHZ2gcpf8mG6lEUHWPI6fD/xh2Jh8fFtmaEExq0P86D6yA7ekKwXij6kbPL3T+oTQWpLKiQOf8xTBNeagxWPNPGHyzgQvKAKARiV1S+xrp7CTgRvdFcsSYtaH0fiiOLNhKMObzhJOoc0mjltQQXlm207EGyAhW2aO68wUfXGYX1l/WHUTghEWT0KD4lAX6ZdjqjLM2wDm3HcinQZ40jFanNd0DATfvPxflih/sRMLT602LwF+msjja9cGtP2zdxZ42YwG1Z1L41b4UGlQaMwnirQCpmzq3jY+4ooWCzyrfv4rc2yPZSY62Ae4sBMJyTcwst021udGJLT/QBtSSWXhAuMgxPwP9s+cxzqA45HY6kXQ9f3VPoX9iFYMIzyyUAFcIYedGb4zZkrIicWoVOZE +9zJ3elQ ZKK/+LxoxaF2BR2YVHoL4rGh4rGp/QrOZKRqUPRmmf4cgkruj22AXFpxopsNDD0vV/GITngAMcYvGJ8YUapUhC9TlOS8GpFr8c8pedZEoJ/ywltkCUc7CA3DqCnob8OPV8EkBKAfHvT9FGFAQ1AV7WvbQWzqKCdwa0djcTX+r5MqNPbw= 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: On Mon, Oct 14, 2024 at 11:22:27AM GMT, Joanne Koong wrote: > 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 */ Isn't it "Do not wait for writeback completion for folios of this mapping during reclaim"? > /* 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); We should not remove the above line as it will make anon pages added to the swap in this code path skip reclaim. Basically the mapping of anon pages which are not yet in swap cache, will be null at the newly added mapping = folio_mapping(folio) but will be swapcache mapping at this location (there is add_to_swap() in between), so if we remove this line, the kernel will skip the reclaim of that folio in this iteration. This will increase memory pressure. > if (folio_test_dirty(folio)) { > /* > * Only kswapd can writeback filesystem folios > -- > 2.43.5 >