From patchwork Mon Apr 14 22:22:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joanne Koong X-Patchwork-Id: 14051091 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 6760AC369B2 for ; Mon, 14 Apr 2025 22:22:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF63E2800A5; Mon, 14 Apr 2025 18:22:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB561280099; Mon, 14 Apr 2025 18:22:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94ACC2800A5; Mon, 14 Apr 2025 18:22:35 -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 6DAFC280099 for ; Mon, 14 Apr 2025 18:22:35 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 51167C0793 for ; Mon, 14 Apr 2025 22:22:36 +0000 (UTC) X-FDA: 83334074712.09.BEFD774 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf16.hostedemail.com (Postfix) with ESMTP id 356A0180004 for ; Mon, 14 Apr 2025 22:22:34 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MY2Y3BTz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.214.172 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=1744669354; 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=+oWkQQZCAOmznqdU/77/ScxMiLxoOGJqTV4LeWuhZ1M=; b=4sloDb+Aye4bF5N+pWZ65QgdNwt0W+2QCCn/zRL3rU93ilpYUQFCBT7Og/AfNVJyV8mGKg dN1x8eE+9HNI7Jn0Z6JjTZn09p5Ace9I906DvSe67qXtaFyeqHENzeyyyJt/60ji3KpGK1 7uAbWkA1eQCy05DVGbmuJ5M4FG39Cp8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MY2Y3BTz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=joannelkoong@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744669354; a=rsa-sha256; cv=none; b=ptkNsZMqmXnToFdRQeL9n+1Laq0ZQ7uMk5eYEabTDVxzEO4Kl1bt8odgmRaCMv+aRovZSF WzVPtUyUKksYIBDxSXPr8Bk9VcQ69E2kr1L+GRul06bkykzlHJQp2Nd3kG80pr68iHIqqA fij3XEBHl4PH4XvH1ikFW0lVZNhPaYA= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-224341bbc1dso42915795ad.3 for ; Mon, 14 Apr 2025 15:22:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744669353; x=1745274153; 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=+oWkQQZCAOmznqdU/77/ScxMiLxoOGJqTV4LeWuhZ1M=; b=MY2Y3BTzLlMk2V2j+KfZnTT18/pbxo2McEkYuxjvk6B+Rcl7K5c7Tmbkf1eNX5cgi8 ErhyrOOYOW/Ut9YEkZ3chwy3O/A7bL33DU+iUgjcS1/fT0D8fpw/0xkTzTDc07rLmKw9 zurR2Ylug19cqpZqu/tj8vbRwvDsVU1qv+AjrreuIOwn7kfspxrBgFQqfGBqLYasa/Qv JKgoiGsP2nTqWS9NI2An3OdboAlb5DydetjD97H5basDqF78tQ29CfX9LGDPGrZqWIoJ eIFpBCO5CA5e63ZELjKksQT3Amt5cIMcEDcVKnxEhyUzeVhyXzpoYdqMhWOMLceVAAY3 pQKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744669353; x=1745274153; 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=+oWkQQZCAOmznqdU/77/ScxMiLxoOGJqTV4LeWuhZ1M=; b=n0kNHMGf7n/bYXIR/BZY+xbYGtuGJoFHAJHbBvZiNgweNwCnJJNurAHmoBX2wT7I6n UuqxNP8V40v9RPoQYgi4aDLwf6IvBSUWe436JOUfP76Ysg7WLk3gVGRTwLzfl+Oyj/YC /MQLnOlBamqFzjgwUgYFaDhxBv+9ZEstQLhat32rKFzarf/OoXeWNAEmx6GavCYCN903 AxjtlriJH32WqCy9Bc20PoYDzgnJfHM0zik5snYWHKJUvyHMulcWF6qLtTOR+sxIN7Ed B+YPnsFB57ABAeYKzKalRvpNngX3YeDi3q3XEc3vq/wXwWDjVuixPt0oAWTHtPpBaoLX dy1A== X-Forwarded-Encrypted: i=1; AJvYcCXyPO3VzGJmMlA7k1k1K5+z54/QJjXNsjQdSeDyzSsucQa8pX9H0aPKhqInLTfGdSo7nE/hqwZVaQ==@kvack.org X-Gm-Message-State: AOJu0YzcGIcRYb/bsT8TUMfVHRkqgP4YjXlkTkITeWrEjYxNXT0LOA5z /o6/1Oh+lpcZSjRTnN7uAtd8SvkE56Xr0kOYto+3d1OPYXbB0XqI X-Gm-Gg: ASbGncsx7ebqZySVpcMiVQHz5StqqbU07eedRGVc7u2xEBX8unrR1zVluEjQGOVlkTy ys951Ub5rqRwePX6HPCVOJdgKfAtABbIet97MylEQI6gUNO6WPtJcipQ07tpn5nUirKFLyQr1VO 5FnyaEw5PLqPRsnbWdbfRMsuy4ws6EQbeazBo5XMxs6yVz9AkLEkabKCB+wZdm/ZZWgoKsdXV+j elNURkH2ZT7UxH0HusU+lEFzTLt3w6V9jAyIysCLG/+XtXKx4exMMZm171KMqdYRlSL+1zADVVx AQJCiRE5a3EHOcgQYmOiUKZhTCjJL4EEFoxt X-Google-Smtp-Source: AGHT+IHEGRK/+NruAidysEEd0d6+/ry3wrbvhjNA81WN8QlEjydKcUEb/SGYslzu/JsgG0cqfHY9dA== X-Received: by 2002:a17:902:fc4b:b0:223:5e56:a1ce with SMTP id d9443c01a7336-22bea4f26afmr196339345ad.32.1744669352990; Mon, 14 Apr 2025 15:22:32 -0700 (PDT) Received: from localhost ([2a03:2880:ff:72::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22ac7b8afe3sm104279825ad.75.2025.04.14.15.22.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Apr 2025 15:22:32 -0700 (PDT) From: Joanne Koong To: miklos@szeredi.hu, akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: jefflexu@linux.alibaba.com, shakeel.butt@linux.dev, david@redhat.com, bernd.schubert@fastmail.fm, ziy@nvidia.com, jlayton@kernel.org, kernel-team@meta.com, Miklos Szeredi Subject: [PATCH v8 1/2] mm: skip folio reclaim in legacy memcg contexts for deadlockable mappings Date: Mon, 14 Apr 2025 15:22:09 -0700 Message-ID: <20250414222210.3995795-2-joannelkoong@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250414222210.3995795-1-joannelkoong@gmail.com> References: <20250414222210.3995795-1-joannelkoong@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 356A0180004 X-Stat-Signature: 9db9pypa1dk14zyfezwc1jbkwer8hmi1 X-Rspam-User: X-HE-Tag: 1744669354-41147 X-HE-Meta: U2FsdGVkX1+JEPlAMxtoW2qDkFHVcNnZK/zK200HV6G0bKelHKinyGl9Ma9xK9C+Vg9ZrtD7WGjkvb3OpBOfNyrmigq17P2gyXntkwd6kxtI3LN/qWL20LQ0Rp6rLYKTh84Y5YK3FyjZog5vH5SbtBaGeHvRY/e8rro+lLb66OVipOpXFFNbUidjgsDCEPLhHcBHop1mHz8/J/vUEXm64d4uV9W81WBS9GtfIsvxmFYcAoSto53N6krRbkRcxe1aiEjTqEBpjgvXUh4Tl4K4ibXpHYLMVOZrl+GFA39/yMv59bgXc9N8Z3gLNqOdjxmAiGoeicVuqmzJLHnSoMLAGJ8gQ4HLMze4njDSe/5PYFzHX61TvSy1/jYOOyJNCIZomZ7uByZSWbzyx7YkociwgDVBpksuzB+vSOfNGMd/A8LXoRyeptdFedRa/YTzqFvXZGCaIKh1hNiCdBMF4CfI25LxHDPTSfyOZsLdXGcdv9CtuD2jYTwmI+a6v5F8BzJfhE3vAVGbGPlL6LLDG/UFuDce2adeporijj80cdw1ghTqJcTZUEqcAcmS09WHyHudlJYFhwYJmVS/OBu7MASmweHWUkcfxKuGfJ+qRMF/oI6v/no0LXt0Ao9rXMRX3Nh2PqCaA1lInhSY6tPrcljKTMsrDjkQ/WL7SBTz4MEsHBGrjxYG2ucJ47qtUSNaLCfKCgE6HNwOyoIsS8QuHyIM+P9vh/JlbhpujBS5X6eijlwXx3DjLyEGkZfV7OSB14PovD7phx0tacdveyzFXZU00loerRxLnTE4FSwwRoh4ev+tWy9dwiRIy4swTuWUuug2ICcN5yeK4M4b+MOqRCiyn1wa6676eObFtGFGzKxUS1lE7g7BASNs11oiPiAEoB5VxJDMk301FSQ87wrfVeICzEbdJn6JZZV3O9Riw5U39Axh+JeXREAf6eRcU68uB2rUF0fRNpnj28qv7FnecrQ a3lUtq6E KRIrzy4VlJSxUk3Y9zjllDeh7VMjtnxDiJxw2UBF3u9BuwFbdZSprUqlfaTL31VP7DHVHa1lIXC5GjRdGeg9mJMg7Zi5/lWWO4kEDbqTzWbcPhTTgwbKN6B92OhaBD3o3sSX1/Vr6JO5BVJXZzvm/Iy+loa9kAPwql7dz6vRUNA98y0I+IiF2p94QFT2lFrzLVLK2zQLtlP951FFXY8xaZtinbWnhVdhkViu+8lsXLmpQz+y7brf8evPAfvIbKSUuS0oHndGZRk6zxU7Pe5C7bgFBByD3m9wN6f5HcwiqZJZ5afymArbvUjPZLI58dQlGmY/FFJLP2Fg73vgs/FSoLz0HctkuGBNlWi6tX/4Jw9RffT1xkI4ciXEpKABUWkUQj8KJd+63By9FTBOQ+OfGeH8IsydQvpCg33ArCx2ab1id7T2nYIGDh6Got5r0QhhZn1g1wEFDxiKleZjFSJ9rdY4geULmKXX7Gtr0 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. If a filesystem (eg fuse) may deadlock due to reclaim waiting on writeback, then the filesystem needs to add inefficient messy workarounds to prevent this. To improve the performance of these filesystems, this commit adds two things: a) a AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM mapping flag that filesystems may set to indicate that reclaim should not wait on writeback b) if legacy memcg encounters a folio with this AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM flag set (eg case 3), the folio will be activated and skip reclaim (eg default to behavior in case 2) instead. Signed-off-by: Joanne Koong Reviewed-by: Shakeel Butt Acked-by: Miklos Szeredi Reviewed-by: Jeff Layton Acked-by: David Hildenbrand --- include/linux/pagemap.h | 11 +++++++++++ mm/vmscan.c | 12 +++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 47bfc6b1b632..14c17279333f 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_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM = 9, /* 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_writeback_may_deadlock_on_reclaim(struct address_space *mapping) +{ + set_bit(AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM, &mapping->flags); +} + +static inline bool mapping_writeback_may_deadlock_on_reclaim(struct address_space *mapping) +{ + return test_bit(AS_WRITEBACK_MAY_DEADLOCK_ON_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 c767d71c43d7..977ae68b6d5d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1165,8 +1165,10 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, * 2) Global or new memcg reclaim encounters a folio that is * not marked for immediate reclaim, or the caller does not * have __GFP_FS (or __GFP_IO if it's simply going to swap, - * not to fs). In this case mark the folio for immediate - * reclaim and continue scanning. + * not to fs), or the folio belongs to a mapping where + * waiting on writeback during reclaim may lead to a deadlock. + * In this case mark the folio for immediate reclaim and + * continue scanning. * * Require may_enter_fs() because we would wait on fs, which * may not have submitted I/O yet. And the loop driver might @@ -1191,6 +1193,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, * takes to write them to disk. */ if (folio_test_writeback(folio)) { + mapping = folio_mapping(folio); + /* Case 1 above */ if (current_is_kswapd() && folio_test_reclaim(folio) && @@ -1201,7 +1205,9 @@ 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_writeback_may_deadlock_on_reclaim(mapping))) { /* * This is slightly racy - * folio_end_writeback() might have From patchwork Mon Apr 14 22:22:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joanne Koong X-Patchwork-Id: 14051092 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 E4EFBC369B2 for ; Mon, 14 Apr 2025 22:22:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A89C9280014; Mon, 14 Apr 2025 18:22:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 94A31280099; Mon, 14 Apr 2025 18:22:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F16D2800A7; Mon, 14 Apr 2025 18:22:37 -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 3638D280099 for ; Mon, 14 Apr 2025 18:22:37 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CAC591A0788 for ; Mon, 14 Apr 2025 22:22:37 +0000 (UTC) X-FDA: 83334074754.02.BEB7B19 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf14.hostedemail.com (Postfix) with ESMTP id DFF24100008 for ; Mon, 14 Apr 2025 22:22:35 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="EJ4N+Cf/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.210.173 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=1744669356; 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=LsFqbhxM4pgtOJhfaLf4Zo+4iOEVTF9KL+GNR5mG/Ys=; b=dtYca67rwr5Gyjl6+zz9R0araRm8lkVHfsAORWHprK8Eh2SgblgCKoGE+ij8YGBiF8PfbA Y2sG9Flym2KHwIkS8XhGLmV2ck47SxwaPJQgYyEah4ztzYSFhD8hVd5IwkzCeItSLenEz4 pOoN8FlSB3OQQDGQU0O80EYBRqDP7Zw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744669356; a=rsa-sha256; cv=none; b=OOCuZ2YgECaeXuPsNNpsS2yjYfiBepSC/Up4Orb3qQ2tEexAsSVPyyuzNTJTe2iDBGuLZ4 YGHEoSNBrDt5pAJp5V/E73UWG+l3Lm5qIavptMSXIDi/hro7++8yLSvE5FtwmKZb4fgDyf 54lFxfavl8g/zb9SSC2FI6v89rP3Q/8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="EJ4N+Cf/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of joannelkoong@gmail.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=joannelkoong@gmail.com Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-72d3b48d2ffso4665653b3a.2 for ; Mon, 14 Apr 2025 15:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744669355; x=1745274155; 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=LsFqbhxM4pgtOJhfaLf4Zo+4iOEVTF9KL+GNR5mG/Ys=; b=EJ4N+Cf/FD9wbp/nGGfZHxypBQ7yzUUcPdb/IHEVmcmNywkGO6z39Nca+wdO5oMZA5 8NKwtAymIMvMR7uIEp0W6ZojkcZeC/yUGkStU0omX/qmZlodffjCY0wlsOvRrje/pi1H SiHb0Ed7CheXErkHFs7TD5NfYQZPWeBeJpALxlazTUey5Vtj1QW2EXXI71m5WdbiPkB3 veQk0av6IqVJgAbjU/CNERRUelTfAQEaUWVG1dGMNEqHgMeje5WFVl5oLGa7sVYZ44mf c9dqEHs28dW4JOiTgTB+Gx1YKOqHxApg+aF23pDpqEx50e72zFYSJ1H/E9ePJInSU/FV T0zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744669355; x=1745274155; 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=LsFqbhxM4pgtOJhfaLf4Zo+4iOEVTF9KL+GNR5mG/Ys=; b=hVKSiNscDYbicquDIem6+xarKJbPdTymiw2Fqg2LL6jWkBu4U1WfV9EfW1do21b/gD pU+rQ3/0gEcK0bEQxqXxKF+eo/KmiR6gU8YWshAJ1549foogt/9bXgQbP1Ys69xw2J5f W1pBl9a6cSopOYvevRfdZfdlUrADtLJytpVBqMD6lutaUVNpaPE0pkaglv9XptLtgWdL gEWIUJOS+2Chy7GbM4esPVBpf8zYjbF6d/ApJqqeBGYPRkuRcCECXZkvtsUqB9nL/sLb tLxKYhPG3c/6anc0zN+4EY6jQjI5Vb4iRstqJu7EVoxQHiM+CchKOX4FFM60Zzsl8ZqU Kybg== X-Forwarded-Encrypted: i=1; AJvYcCWtwUKEqOM4GJgdyW47C1tlpY2aEohr6ghCn7wXJ3znpI18rckamLDb7xNg0G1UBBXBgE3RBVvKQg==@kvack.org X-Gm-Message-State: AOJu0YwVdMaC5VSd2p4otWbtHE1EaW73Kgt3PKICzlR0VGY48NFK5u+B mtk5160bhJ8d3rqIDtMAJ9mjI++bMWk5ybBIj3LzxD35dEllXfS4ibk8+g== X-Gm-Gg: ASbGnctd8Cz1MGIPO56Kglv/BN8KiJ6nUopEranzMndN0EUI9Cmq/DQQ7vE2rq7erQB tG2xkD94ViyKFQoMTgin2qDo4flo0aZWrZJKdLt+3iREgFRTdpFW5SI6EEdhD2TDIlY1jrKh43d 1zpoYtCQB7DdP7omy7GmuY1QW1ttXTePzM8WE8QUNdHV/6TwNhZtp7cNrDKA20MOBh5xIfjXnBE 32QoKU7YJsjJVPLKVoe2RxjZmm4NV57DYvSWB91xyxus8CemRTv4t214/zFIp8rT8hXC+g8lOsu fhvL/5GimdfvLHngYlU5mAbeFy+09P/wbRo= X-Google-Smtp-Source: AGHT+IHxD6UK3e9Y9j5HETQfQkhCGGaYx7CcULXjHDJHTnlNyUviuv9ivn1UO/FZbIh1rTn5YzBmbg== X-Received: by 2002:a05:6a21:c8c:b0:1fd:f8dc:83f6 with SMTP id adf61e73a8af0-2017999735fmr24391145637.40.1744669354717; Mon, 14 Apr 2025 15:22:34 -0700 (PDT) Received: from localhost ([2a03:2880:ff:a::]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b02a2d3a4easm8186421a12.52.2025.04.14.15.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Apr 2025 15:22:34 -0700 (PDT) From: Joanne Koong To: miklos@szeredi.hu, akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: jefflexu@linux.alibaba.com, shakeel.butt@linux.dev, david@redhat.com, bernd.schubert@fastmail.fm, ziy@nvidia.com, jlayton@kernel.org, kernel-team@meta.com, Miklos Szeredi Subject: [PATCH v8 2/2] fuse: remove tmp folio for writebacks and internal rb tree Date: Mon, 14 Apr 2025 15:22:10 -0700 Message-ID: <20250414222210.3995795-3-joannelkoong@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250414222210.3995795-1-joannelkoong@gmail.com> References: <20250414222210.3995795-1-joannelkoong@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DFF24100008 X-Stat-Signature: uhroahg98fze9odqx36nwmda17973qkp X-Rspam-User: X-HE-Tag: 1744669355-212885 X-HE-Meta: U2FsdGVkX188BOJDecvE6qQse6djrgf1SJYZp6xzD4CjByGj0FXoEAfKMfbU3GUQhAK2K7oYJTDVuxrG5Hm1G2+6Wm5NvoHQbAb4132Kx2rQTaPwIw4ZSetILF3+psadmoKyeBQ5y3CbsN++vE5/P4hEf5PvJTVY0h1kPpKFbnwVjsFFBTYxmmobvylUY1VfWqS8quIJGcLPW1txRAIl6NNDWZxPK0rSd7VFYfl0KRqPHSYF4uYZ5x1H7b9zzDxw1R6bIuqLAIEgwzlgTUAvefmvscjMR6Ad+YX/QWPyySW+7KWhagWGoUp9QgR485FAvpUrpICJTQi47yDrmy+ityg4AIAykR7Ab+sdkJFtfWlCSWMKTLiIo+mRyq4omtjakAOZqm2MVrKoZB0eKeQz30Q2qTpQCRQt4C70oH6DEANV/SOSikLLucZfl2RKpQ9YnXyy4mtO19/30mk15RCRyMennXp9fYqyL/FLv77m32vAedNNcIzOZUa/+lq/fzojY7lF9bH2/QS5XINqH0CvSEosq2QHhYhJ0z/vkySE07gFXRFKEC936b9qVKK4zHR+A9QcVRi4QBFeCH/R+SCHrBjRVQXL6Qyfza9uOfmm0q0+7Kf4NPNP6S5dtkd5VU/jhdH2iSqZeu7c+FmtBULff6chxlPAv5CUHt8mHTuHkAjs1AWMDZwgYOGVKaaAA2rME1ZxnLfgtqd0XXNjfiHkTYxKWkT4s8fXFQHCZotsHSEc5C1EVdRb2gcqZBIXtVvU8X3Ox5xC0/lGCvNUMOulMwyuvYtoD48mJgoQz9BlWY1MrmKVHGUQ8hvMirlk9qEEfy/5dsU+eu4xn8haRdSxJVLkj3YVHxPHXEkF33SuZzBdUUf1LNjXOiPhhLN+t/wi1KOl2De/2eN2iIkgE0I5YpH3u79pgSMQwrTUmxQZQghS7zgppPcnirtyo2mf1+3WS7gGglhbIlrqs2011Z/ xgw3sUZx C2yud+5/GsLUqpVdlJYRgNvcDlTYpqf+fHTJNywAwV/DdoeBNC1bbgzLoPQZqjfupFyTz9e2qDQv2qqhu1Hyb7x2pJIssuW0XQcJsgNeb3cbs/qJFzEAmO+uqgksD3l4UQHmslnnqIs0/1YrZiX4qzSczvtsAzNkAWGXb0EeFLLOx9C4ti8VXTRsInMLzfasLZ7AyJokS0QNmYmiO1LLZ3H0ntEOkLc5XJsrFXdXRUyBpgE/CKmcfE5yLfzar2A2MnmLhoQ3zh73m9+wqeiWNNq6ag44m4CFmIg9x+GVk15sbNwQS4PBipUItlNs1pIROJS+uFRq8uwIBKK2nVrtSoBw4l7OhtCOCRNA456CgbWkOIvk2flyFbOh26ptunkhhI3ro463Mz4FufR/pi7BkWtHMM3uZbnFYvQcHf7wstjS+CPBxi3DOj7oT7OJoCyCZHR/Mh/iZudV0wfF5ABMO+Jhpd2jvBST3yhKLplFx/oJRyZyqWTsUR8fFZQ== 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: In the current FUSE writeback design (see commit 3be5a52b30aa ("fuse: support writable mmap")), a temp page is allocated for every dirty page to be written back, the contents of the dirty page are copied over to the temp page, and the temp page gets handed to the server to write back. This is done so that writeback may be immediately cleared on the dirty page, and this in turn is done in order to mitigate the following deadlock scenario that may arise if reclaim waits on writeback on the dirty page to complete: * 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 * the FUSE server can't write back the folio since it's stuck in direct reclaim With a recent change that added AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM and mitigates the situation described above, FUSE writeback does not need to use temp pages if it sets AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM on its inode mappings. This commit sets AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM on the inode mappings and removes the temporary pages + extra copying and the internal rb tree. fio benchmarks -- (using averages observed from 10 runs, throwing away outliers) Setup: sudo mount -t tmpfs -o size=30G tmpfs ~/tmp_mount ./libfuse/build/example/passthrough_ll -o writeback -o max_threads=4 -o source=~/tmp_mount ~/fuse_mount fio --name=writeback --ioengine=sync --rw=write --bs={1k,4k,1M} --size=2G --numjobs=2 --ramp_time=30 --group_reporting=1 --directory=/root/fuse_mount bs = 1k 4k 1M Before 351 MiB/s 1818 MiB/s 1851 MiB/s After 341 MiB/s 2246 MiB/s 2685 MiB/s % diff -3% 23% 45% Signed-off-by: Joanne Koong Reviewed-by: Jingbo Xu Acked-by: Miklos Szeredi Reviewed-by: Jeff Layton --- fs/fuse/file.c | 364 ++++------------------------------------------- fs/fuse/fuse_i.h | 3 - 2 files changed, 28 insertions(+), 339 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index d63e56fd3dd2..d31af1389c1b 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -415,89 +415,11 @@ u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) struct fuse_writepage_args { struct fuse_io_args ia; - struct rb_node writepages_entry; struct list_head queue_entry; - struct fuse_writepage_args *next; struct inode *inode; struct fuse_sync_bucket *bucket; }; -static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi, - pgoff_t idx_from, pgoff_t idx_to) -{ - struct rb_node *n; - - n = fi->writepages.rb_node; - - while (n) { - struct fuse_writepage_args *wpa; - pgoff_t curr_index; - - wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry); - WARN_ON(get_fuse_inode(wpa->inode) != fi); - curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT; - if (idx_from >= curr_index + wpa->ia.ap.num_folios) - n = n->rb_right; - else if (idx_to < curr_index) - n = n->rb_left; - else - return wpa; - } - return NULL; -} - -/* - * Check if any page in a range is under writeback - */ -static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from, - pgoff_t idx_to) -{ - struct fuse_inode *fi = get_fuse_inode(inode); - bool found; - - if (RB_EMPTY_ROOT(&fi->writepages)) - return false; - - spin_lock(&fi->lock); - found = fuse_find_writeback(fi, idx_from, idx_to); - spin_unlock(&fi->lock); - - return found; -} - -static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) -{ - return fuse_range_is_writeback(inode, index, index); -} - -/* - * Wait for page writeback to be completed. - * - * Since fuse doesn't rely on the VM writeback tracking, this has to - * use some other means. - */ -static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) -{ - struct fuse_inode *fi = get_fuse_inode(inode); - - wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); -} - -static inline bool fuse_folio_is_writeback(struct inode *inode, - struct folio *folio) -{ - pgoff_t last = folio_next_index(folio) - 1; - return fuse_range_is_writeback(inode, folio_index(folio), last); -} - -static void fuse_wait_on_folio_writeback(struct inode *inode, - struct folio *folio) -{ - struct fuse_inode *fi = get_fuse_inode(inode); - - wait_event(fi->page_waitq, !fuse_folio_is_writeback(inode, folio)); -} - /* * Wait for all pending writepages on the inode to finish. * @@ -532,10 +454,6 @@ static int fuse_flush(struct file *file, fl_owner_t id) if (err) return err; - inode_lock(inode); - fuse_sync_writes(inode); - inode_unlock(inode); - err = filemap_check_errors(file->f_mapping); if (err) return err; @@ -886,13 +804,6 @@ static int fuse_do_readfolio(struct file *file, struct folio *folio) ssize_t res; u64 attr_ver; - /* - * With the temporary pages that are used to complete writeback, we can - * have writeback that extends beyond the lifetime of the folio. So - * make sure we read a properly synced folio. - */ - fuse_wait_on_folio_writeback(inode, folio); - attr_ver = fuse_get_attr_version(fm->fc); /* Don't overflow end offset */ @@ -1005,17 +916,12 @@ static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file) static void fuse_readahead(struct readahead_control *rac) { struct inode *inode = rac->mapping->host; - struct fuse_inode *fi = get_fuse_inode(inode); struct fuse_conn *fc = get_fuse_conn(inode); unsigned int max_pages, nr_pages; - pgoff_t first = readahead_index(rac); - pgoff_t last = first + readahead_count(rac) - 1; if (fuse_is_bad(inode)) return; - wait_event(fi->page_waitq, !fuse_range_is_writeback(inode, first, last)); - max_pages = min_t(unsigned int, fc->max_pages, fc->max_read / PAGE_SIZE); @@ -1181,7 +1087,7 @@ static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, int err; for (i = 0; i < ap->num_folios; i++) - fuse_wait_on_folio_writeback(inode, ap->folios[i]); + folio_wait_writeback(ap->folios[i]); fuse_write_args_fill(ia, ff, pos, count); ia->write.in.flags = fuse_write_flags(iocb); @@ -1638,7 +1544,7 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, return res; } } - if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { + if (!cuse && filemap_range_has_writeback(mapping, pos, (pos + count - 1))) { if (!write) inode_lock(inode); fuse_sync_writes(inode); @@ -1835,38 +1741,34 @@ static ssize_t fuse_splice_write(struct pipe_inode_info *pipe, struct file *out, static void fuse_writepage_free(struct fuse_writepage_args *wpa) { struct fuse_args_pages *ap = &wpa->ia.ap; - int i; if (wpa->bucket) fuse_sync_bucket_dec(wpa->bucket); - for (i = 0; i < ap->num_folios; i++) - folio_put(ap->folios[i]); - fuse_file_put(wpa->ia.ff, false); kfree(ap->folios); kfree(wpa); } -static void fuse_writepage_finish_stat(struct inode *inode, struct folio *folio) -{ - struct backing_dev_info *bdi = inode_to_bdi(inode); - - dec_wb_stat(&bdi->wb, WB_WRITEBACK); - node_stat_sub_folio(folio, NR_WRITEBACK_TEMP); - wb_writeout_inc(&bdi->wb); -} - static void fuse_writepage_finish(struct fuse_writepage_args *wpa) { struct fuse_args_pages *ap = &wpa->ia.ap; struct inode *inode = wpa->inode; struct fuse_inode *fi = get_fuse_inode(inode); + struct backing_dev_info *bdi = inode_to_bdi(inode); int i; - for (i = 0; i < ap->num_folios; i++) - fuse_writepage_finish_stat(inode, ap->folios[i]); + for (i = 0; i < ap->num_folios; i++) { + /* + * Benchmarks showed that ending writeback within the + * scope of the fi->lock alleviates xarray lock + * contention and noticeably improves performance. + */ + folio_end_writeback(ap->folios[i]); + dec_wb_stat(&bdi->wb, WB_WRITEBACK); + wb_writeout_inc(&bdi->wb); + } wake_up(&fi->page_waitq); } @@ -1877,7 +1779,6 @@ static void fuse_send_writepage(struct fuse_mount *fm, __releases(fi->lock) __acquires(fi->lock) { - struct fuse_writepage_args *aux, *next; struct fuse_inode *fi = get_fuse_inode(wpa->inode); struct fuse_write_in *inarg = &wpa->ia.write.in; struct fuse_args *args = &wpa->ia.ap.args; @@ -1914,19 +1815,8 @@ __acquires(fi->lock) out_free: fi->writectr--; - rb_erase(&wpa->writepages_entry, &fi->writepages); fuse_writepage_finish(wpa); spin_unlock(&fi->lock); - - /* After rb_erase() aux request list is private */ - for (aux = wpa->next; aux; aux = next) { - next = aux->next; - aux->next = NULL; - fuse_writepage_finish_stat(aux->inode, - aux->ia.ap.folios[0]); - fuse_writepage_free(aux); - } - fuse_writepage_free(wpa); spin_lock(&fi->lock); } @@ -1954,43 +1844,6 @@ __acquires(fi->lock) } } -static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root, - struct fuse_writepage_args *wpa) -{ - pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT; - pgoff_t idx_to = idx_from + wpa->ia.ap.num_folios - 1; - struct rb_node **p = &root->rb_node; - struct rb_node *parent = NULL; - - WARN_ON(!wpa->ia.ap.num_folios); - while (*p) { - struct fuse_writepage_args *curr; - pgoff_t curr_index; - - parent = *p; - curr = rb_entry(parent, struct fuse_writepage_args, - writepages_entry); - WARN_ON(curr->inode != wpa->inode); - curr_index = curr->ia.write.in.offset >> PAGE_SHIFT; - - if (idx_from >= curr_index + curr->ia.ap.num_folios) - p = &(*p)->rb_right; - else if (idx_to < curr_index) - p = &(*p)->rb_left; - else - return curr; - } - - rb_link_node(&wpa->writepages_entry, parent, p); - rb_insert_color(&wpa->writepages_entry, root); - return NULL; -} - -static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa) -{ - WARN_ON(fuse_insert_writeback(root, wpa)); -} - static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args, int error) { @@ -2010,41 +1863,6 @@ static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args, if (!fc->writeback_cache) fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY); spin_lock(&fi->lock); - rb_erase(&wpa->writepages_entry, &fi->writepages); - while (wpa->next) { - struct fuse_mount *fm = get_fuse_mount(inode); - struct fuse_write_in *inarg = &wpa->ia.write.in; - struct fuse_writepage_args *next = wpa->next; - - wpa->next = next->next; - next->next = NULL; - tree_insert(&fi->writepages, next); - - /* - * Skip fuse_flush_writepages() to make it easy to crop requests - * based on primary request size. - * - * 1st case (trivial): there are no concurrent activities using - * fuse_set/release_nowrite. Then we're on safe side because - * fuse_flush_writepages() would call fuse_send_writepage() - * anyway. - * - * 2nd case: someone called fuse_set_nowrite and it is waiting - * now for completion of all in-flight requests. This happens - * rarely and no more than once per page, so this should be - * okay. - * - * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle - * of fuse_set_nowrite..fuse_release_nowrite section. The fact - * that fuse_set_nowrite returned implies that all in-flight - * requests were completed along with all of their secondary - * requests. Further primary requests are blocked by negative - * writectr. Hence there cannot be any in-flight requests and - * no invocations of fuse_writepage_end() while we're in - * fuse_set_nowrite..fuse_release_nowrite section. - */ - fuse_send_writepage(fm, next, inarg->offset + inarg->size); - } fi->writectr--; fuse_writepage_finish(wpa); spin_unlock(&fi->lock); @@ -2131,19 +1949,16 @@ static void fuse_writepage_add_to_bucket(struct fuse_conn *fc, } static void fuse_writepage_args_page_fill(struct fuse_writepage_args *wpa, struct folio *folio, - struct folio *tmp_folio, uint32_t folio_index) + uint32_t folio_index) { struct inode *inode = folio->mapping->host; struct fuse_args_pages *ap = &wpa->ia.ap; - folio_copy(tmp_folio, folio); - - ap->folios[folio_index] = tmp_folio; + ap->folios[folio_index] = folio; ap->descs[folio_index].offset = 0; ap->descs[folio_index].length = PAGE_SIZE; inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); - node_stat_add_folio(tmp_folio, NR_WRITEBACK_TEMP); } static struct fuse_writepage_args *fuse_writepage_args_setup(struct folio *folio, @@ -2178,18 +1993,12 @@ static int fuse_writepage_locked(struct folio *folio) struct fuse_inode *fi = get_fuse_inode(inode); struct fuse_writepage_args *wpa; struct fuse_args_pages *ap; - struct folio *tmp_folio; struct fuse_file *ff; - int error = -ENOMEM; - - tmp_folio = folio_alloc(GFP_NOFS | __GFP_HIGHMEM, 0); - if (!tmp_folio) - goto err; + int error = -EIO; - error = -EIO; ff = fuse_write_file_get(fi); if (!ff) - goto err_nofile; + goto err; wpa = fuse_writepage_args_setup(folio, ff); error = -ENOMEM; @@ -2200,22 +2009,17 @@ static int fuse_writepage_locked(struct folio *folio) ap->num_folios = 1; folio_start_writeback(folio); - fuse_writepage_args_page_fill(wpa, folio, tmp_folio, 0); + fuse_writepage_args_page_fill(wpa, folio, 0); spin_lock(&fi->lock); - tree_insert(&fi->writepages, wpa); list_add_tail(&wpa->queue_entry, &fi->queued_writes); fuse_flush_writepages(inode); spin_unlock(&fi->lock); - folio_end_writeback(folio); - return 0; err_writepage_args: fuse_file_put(ff, false); -err_nofile: - folio_put(tmp_folio); err: mapping_set_error(folio->mapping, error); return error; @@ -2225,7 +2029,6 @@ struct fuse_fill_wb_data { struct fuse_writepage_args *wpa; struct fuse_file *ff; struct inode *inode; - struct folio **orig_folios; unsigned int max_folios; }; @@ -2260,69 +2063,11 @@ static void fuse_writepages_send(struct fuse_fill_wb_data *data) struct fuse_writepage_args *wpa = data->wpa; struct inode *inode = data->inode; struct fuse_inode *fi = get_fuse_inode(inode); - int num_folios = wpa->ia.ap.num_folios; - int i; spin_lock(&fi->lock); list_add_tail(&wpa->queue_entry, &fi->queued_writes); fuse_flush_writepages(inode); spin_unlock(&fi->lock); - - for (i = 0; i < num_folios; i++) - folio_end_writeback(data->orig_folios[i]); -} - -/* - * Check under fi->lock if the page is under writeback, and insert it onto the - * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's - * one already added for a page at this offset. If there's none, then insert - * this new request onto the auxiliary list, otherwise reuse the existing one by - * swapping the new temp page with the old one. - */ -static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa, - struct folio *folio) -{ - struct fuse_inode *fi = get_fuse_inode(new_wpa->inode); - struct fuse_writepage_args *tmp; - struct fuse_writepage_args *old_wpa; - struct fuse_args_pages *new_ap = &new_wpa->ia.ap; - - WARN_ON(new_ap->num_folios != 0); - new_ap->num_folios = 1; - - spin_lock(&fi->lock); - old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa); - if (!old_wpa) { - spin_unlock(&fi->lock); - return true; - } - - for (tmp = old_wpa->next; tmp; tmp = tmp->next) { - pgoff_t curr_index; - - WARN_ON(tmp->inode != new_wpa->inode); - curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT; - if (curr_index == folio->index) { - WARN_ON(tmp->ia.ap.num_folios != 1); - swap(tmp->ia.ap.folios[0], new_ap->folios[0]); - break; - } - } - - if (!tmp) { - new_wpa->next = old_wpa->next; - old_wpa->next = new_wpa; - } - - spin_unlock(&fi->lock); - - if (tmp) { - fuse_writepage_finish_stat(new_wpa->inode, - folio); - fuse_writepage_free(new_wpa); - } - - return false; } static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio, @@ -2331,15 +2076,6 @@ static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio, { WARN_ON(!ap->num_folios); - /* - * Being under writeback is unlikely but possible. For example direct - * read to an mmaped fuse file will set the page dirty twice; once when - * the pages are faulted with get_user_pages(), and then after the read - * completed. - */ - if (fuse_folio_is_writeback(data->inode, folio)) - return true; - /* Reached max pages */ if (ap->num_folios == fc->max_pages) return true; @@ -2349,7 +2085,7 @@ static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio, return true; /* Discontinuity */ - if (data->orig_folios[ap->num_folios - 1]->index + 1 != folio_index(folio)) + if (ap->folios[ap->num_folios - 1]->index + 1 != folio_index(folio)) return true; /* Need to grow the pages array? If so, did the expansion fail? */ @@ -2368,7 +2104,6 @@ static int fuse_writepages_fill(struct folio *folio, struct inode *inode = data->inode; struct fuse_inode *fi = get_fuse_inode(inode); struct fuse_conn *fc = get_fuse_conn(inode); - struct folio *tmp_folio; int err; if (!data->ff) { @@ -2383,54 +2118,23 @@ static int fuse_writepages_fill(struct folio *folio, data->wpa = NULL; } - err = -ENOMEM; - tmp_folio = folio_alloc(GFP_NOFS | __GFP_HIGHMEM, 0); - if (!tmp_folio) - goto out_unlock; - - /* - * The page must not be redirtied until the writeout is completed - * (i.e. userspace has sent a reply to the write request). Otherwise - * there could be more than one temporary page instance for each real - * page. - * - * This is ensured by holding the page lock in page_mkwrite() while - * checking fuse_page_is_writeback(). We already hold the page lock - * since clear_page_dirty_for_io() and keep it held until we add the - * request to the fi->writepages list and increment ap->num_folios. - * After this fuse_page_is_writeback() will indicate that the page is - * under writeback, so we can release the page lock. - */ if (data->wpa == NULL) { err = -ENOMEM; wpa = fuse_writepage_args_setup(folio, data->ff); - if (!wpa) { - folio_put(tmp_folio); + if (!wpa) goto out_unlock; - } fuse_file_get(wpa->ia.ff); data->max_folios = 1; ap = &wpa->ia.ap; } folio_start_writeback(folio); - fuse_writepage_args_page_fill(wpa, folio, tmp_folio, ap->num_folios); - data->orig_folios[ap->num_folios] = folio; + fuse_writepage_args_page_fill(wpa, folio, ap->num_folios); err = 0; - if (data->wpa) { - /* - * Protected by fi->lock against concurrent access by - * fuse_page_is_writeback(). - */ - spin_lock(&fi->lock); - ap->num_folios++; - spin_unlock(&fi->lock); - } else if (fuse_writepage_add(wpa, folio)) { + ap->num_folios++; + if (!data->wpa) data->wpa = wpa; - } else { - folio_end_writeback(folio); - } out_unlock: folio_unlock(folio); @@ -2457,13 +2161,6 @@ static int fuse_writepages(struct address_space *mapping, data.wpa = NULL; data.ff = NULL; - err = -ENOMEM; - data.orig_folios = kcalloc(fc->max_pages, - sizeof(struct folio *), - GFP_NOFS); - if (!data.orig_folios) - goto out; - err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data); if (data.wpa) { WARN_ON(!data.wpa->ia.ap.num_folios); @@ -2472,7 +2169,6 @@ static int fuse_writepages(struct address_space *mapping, if (data.ff) fuse_file_put(data.ff, false); - kfree(data.orig_folios); out: return err; } @@ -2497,8 +2193,6 @@ static int fuse_write_begin(struct file *file, struct address_space *mapping, if (IS_ERR(folio)) goto error; - fuse_wait_on_page_writeback(mapping->host, folio->index); - if (folio_test_uptodate(folio) || len >= folio_size(folio)) goto success; /* @@ -2561,13 +2255,9 @@ static int fuse_launder_folio(struct folio *folio) { int err = 0; if (folio_clear_dirty_for_io(folio)) { - struct inode *inode = folio->mapping->host; - - /* Serialize with pending writeback for the same page */ - fuse_wait_on_page_writeback(inode, folio->index); err = fuse_writepage_locked(folio); if (!err) - fuse_wait_on_page_writeback(inode, folio->index); + folio_wait_writeback(folio); } return err; } @@ -2611,7 +2301,7 @@ static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) return VM_FAULT_NOPAGE; } - fuse_wait_on_folio_writeback(inode, folio); + folio_wait_writeback(folio); return VM_FAULT_LOCKED; } @@ -3429,9 +3119,12 @@ static const struct address_space_operations fuse_file_aops = { void fuse_init_file_inode(struct inode *inode, unsigned int flags) { struct fuse_inode *fi = get_fuse_inode(inode); + struct fuse_conn *fc = get_fuse_conn(inode); inode->i_fop = &fuse_file_operations; inode->i_data.a_ops = &fuse_file_aops; + if (fc->writeback_cache) + mapping_set_writeback_may_deadlock_on_reclaim(&inode->i_data); INIT_LIST_HEAD(&fi->write_files); INIT_LIST_HEAD(&fi->queued_writes); @@ -3439,7 +3132,6 @@ void fuse_init_file_inode(struct inode *inode, unsigned int flags) fi->iocachectr = 0; init_waitqueue_head(&fi->page_waitq); init_waitqueue_head(&fi->direct_io_waitq); - fi->writepages = RB_ROOT; if (IS_ENABLED(CONFIG_FUSE_DAX)) fuse_dax_inode_init(inode, flags); diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index d56d4fd956db..f4d2d73b33c4 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -161,9 +161,6 @@ struct fuse_inode { /* waitq for direct-io completion */ wait_queue_head_t direct_io_waitq; - - /* List of writepage requestst (pending or sent) */ - struct rb_root writepages; }; /* readdir cache (directory only) */