From patchwork Fri Jul 19 09:16:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13737040 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 536F7C3DA5D for ; Fri, 19 Jul 2024 09:17:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2E4A6B008C; Fri, 19 Jul 2024 05:17:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BDFE96B0092; Fri, 19 Jul 2024 05:17:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A58076B0093; Fri, 19 Jul 2024 05:17:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 80BE16B008C for ; Fri, 19 Jul 2024 05:17:31 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id EEC1C14051B for ; Fri, 19 Jul 2024 09:17:30 +0000 (UTC) X-FDA: 82355949060.16.4E83E53 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf22.hostedemail.com (Postfix) with ESMTP id D426EC0014 for ; Fri, 19 Jul 2024 09:17:28 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=mbTEsiXY; dkim=pass header.d=suse.com header.s=susede1 header.b=mbTEsiXY; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf22.hostedemail.com: domain of wqu@suse.com designates 195.135.223.131 as permitted sender) smtp.mailfrom=wqu@suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721380615; a=rsa-sha256; cv=none; b=FvdcifgFqJZYZ3QtTtPMGQgCpB27bztP91bh0Uag6PZBkFONX0BBTz+yazQVps9q0Glvmw HkfriBO4pCpUklVxNZDUYWFK8YODiwQR0epiCX54YaBLut4mLYFEeLjH1GmmoUw9F3fA4W yQ22sk3cDk1Bwo3Ij8mM6X6jNT0iGL0= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=mbTEsiXY; dkim=pass header.d=suse.com header.s=susede1 header.b=mbTEsiXY; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf22.hostedemail.com: domain of wqu@suse.com designates 195.135.223.131 as permitted sender) smtp.mailfrom=wqu@suse.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721380615; 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=s3EcZAi4vQxqy0nGMwVlJFTnfaXGD9fvU+gggCPP2PQ=; b=koUB+58NN/AUB643AfuZFSQTIPV5WQ+eWBK3OCGBMx0zHvvk0bPLcFKuDCaH4z3gNL0uIL vm+5jjoGA1iCYp0F8EX4EbFo+b0fg7z75DeAtgtNGTUe4EhSnt6zOrWOvPoHU/iEmo+JiF rMktdbxuSv3MS8EsNB5OQay868gKHRs= Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 8649B1F79C; Fri, 19 Jul 2024 09:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1721380647; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s3EcZAi4vQxqy0nGMwVlJFTnfaXGD9fvU+gggCPP2PQ=; b=mbTEsiXYw587yHmWA8OdqpXmG3VLkE9UPFrS6k5KpYe4ttNHWwohgXza1bgqEnzFhAOXl6 n/1sWeLxL+SSsfVUohj3Z8SQmfhRhVl1X2ZecWizGOt8eGgI+Bqb3J2xMToW8scQt7xTx1 l++YfCiWYu0ZZ2Xb8zoYVmZbR+HrxNM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1721380647; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s3EcZAi4vQxqy0nGMwVlJFTnfaXGD9fvU+gggCPP2PQ=; b=mbTEsiXYw587yHmWA8OdqpXmG3VLkE9UPFrS6k5KpYe4ttNHWwohgXza1bgqEnzFhAOXl6 n/1sWeLxL+SSsfVUohj3Z8SQmfhRhVl1X2ZecWizGOt8eGgI+Bqb3J2xMToW8scQt7xTx1 l++YfCiWYu0ZZ2Xb8zoYVmZbR+HrxNM= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 313D6136F7; Fri, 19 Jul 2024 09:17:23 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id AOMcNyMvmmZPRAAAD6G6ig (envelope-from ); Fri, 19 Jul 2024 09:17:23 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, cgroups@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka Subject: [PATCH v6 2/3] btrfs: always uses root memcgroup for filemap_add_folio() Date: Fri, 19 Jul 2024 18:46:58 +0930 Message-ID: <8a425904c03623790d2ffd2ff5ea4944cc6fe876.1721380449.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D426EC0014 X-Stat-Signature: uqkgx11u5wijrma3d3nz7e68sw1s64h8 X-Rspam-User: X-HE-Tag: 1721380648-728017 X-HE-Meta: U2FsdGVkX1+spDl8zlV4mkncBaDGLnrSRETtNG0mDyXOgHpKDRvStaRYT9fp9Zt+MqzrRltnEcgBY+iHaWCxLYeQfxDMLwWGtBB70UDTpbSZmsk3ITDw3WBTBuL7YBvsNbXfQV5Cr3JNWdJTlBGpDhw22OocNn6+49jcb4AOm4w1h+r2x2fQsg8+UJVKoHdpXnSJTQNXn3rQ9oglsu0QIfqF8qr1dEb9lAgIUYuIRZWtakumSlNWMIn1SyrRtIAjsTyqhsgXkY1EF1IhQXWNRoXScg9ejK+VXySYRjqPPY2Cfq5IkB1A6XRrN9IdBiJcVV78ThFtna42dlU2kUak7gf04RcQfJStspSAJ+STyZayZ/eIM8ZDEM6Vq1KQ7aEMjNFPUeO90AInU+ElFj99xLXTrfSnXzDBHvmQ8iQPB/jaTT49jgcUdx9clguhvOam5FlNZpg3ifO98eR3wxo5V7q0ksJBTEW5m1x869jo9wHg0/tuajtCCkwy1WSUJCjHsgxnBMO4E9GKtGEkge/1oACyJFIgVjjNfFa2At8eBmVYSZju0FfotOhh0fq/D0uQZ6M1bIt6j5ezpvCMN/fYvKZ85f6+Gn4O3ftfeXu/qMQ13ouAIBFMp8WGof/y6zmtQd10QZYvU9a6R8RYkFZ/SEf7hinlNnJbYCrIjr+GnAAk9n3GOsfclLmddzHAzI+OBTpFjr4+wOSzERBbm3ecQJupkUwMFDvcfh0fRjKyeoi7uW/OVJc7bFuMXtea9V3ehXqEx/a+qftKR7sUTCG62UzBwbyNCkZ3tga0nsfTmAB9hy1ip2KwHqY3yq8Zx6dxGra//v74VXkweJRyBk/f/dbIPvaY0BzZCYp3UfYLmST1ufAppKCC9cUJnxddIjGlTngishHEJe2bpaXnkLNiyMrp2pbGk5xz0FWK6UDBPI3265YmoJiuqkpRHGGlzC+RHpAFozMxFPhaYPAKyQ+ jVA5qBbd /ryq8N+0ygHCFu1A1y+Kk5YTgS32MUh5Lg7oq5QBf6k0NklcjgyqQagVyey5dWBy+O8XD+mluHBkbgatoKAvGOwqQ+XTG0KVt8ZOvxp8k9N4DGzeq77pkqQwLj7+pNHSYM9LbLymv1ld6p6QVlgORcDzAUfzJvxbm1tC5h26gdIeE1cEiv0foKRUq3k341Oo6RYu2modX9KoHTAV7CBL3Y0QxWjBh1xUvujj2XEuHbCDcpjlzDLwvyW/aT2/faoh+uSIXA7xTEI1ul7kOmpqJj627ynMMGEi62IvZpyj+mOOuxTr2kgTBew+JmfOBW6MoT0XahYX774IUQkE0BguiL5NT3YQXjx7Oh/zlp7Np+3CIUR3hDN13CmRmCw== 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: [BACKGROUND] The function filemap_add_folio() charges the memory cgroup, as we assume all page caches are accessible by user space progresses thus needs the cgroup accounting. However btrfs is a special case, it has a very large metadata thanks to its support of data csum (by default it's 4 bytes per 4K data, and can be as large as 32 bytes per 4K data). This means btrfs has to go page cache for its metadata pages, to take advantage of both cache and reclaim ability of filemap. This has a tiny problem, that all btrfs metadata pages have to go through the memcgroup charge, even all those metadata pages are not accessible by the user space, and doing the charging can introduce some latency if there is a memory limits set. Btrfs currently uses __GFP_NOFAIL flag as a workaround for this cgroup charge situation so that metadata pages won't really be limited by memcgroup. [ENHANCEMENT] Instead of relying on __GFP_NOFAIL to avoid charge failure, use root memory cgroup to attach metadata pages. With root memory cgroup, we directly skip the charging part, and only rely on __GFP_NOFAIL for the real memory allocation part. Suggested-by: Michal Hocko Suggested-by: Vlastimil Babka (SUSE) Signed-off-by: Qu Wenruo --- fs/btrfs/extent_io.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index aa7f8148cd0d..cfeed7673009 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -2971,6 +2971,7 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, struct btrfs_fs_info *fs_info = eb->fs_info; struct address_space *mapping = fs_info->btree_inode->i_mapping; + struct mem_cgroup *old_memcg; const unsigned long index = eb->start >> PAGE_SHIFT; struct folio *existing_folio = NULL; int ret; @@ -2981,8 +2982,17 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, ASSERT(eb->folios[i]); retry: + /* + * Btree inode is a btrfs internal inode, and not exposed to any + * user. + * Furthermore we do not want any cgroup limits on this inode. + * So we always use root_mem_cgroup as our active memcg when attaching + * the folios. + */ + old_memcg = set_active_memcg(root_mem_cgroup); ret = filemap_add_folio(mapping, eb->folios[i], index + i, GFP_NOFS | __GFP_NOFAIL); + set_active_memcg(old_memcg); if (!ret) goto finish;