From patchwork Fri Jul 19 09:16:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13737041 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 5C897C3DA59 for ; Fri, 19 Jul 2024 09:17:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E04276B0093; Fri, 19 Jul 2024 05:17:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D64356B0095; Fri, 19 Jul 2024 05:17:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB9256B0096; Fri, 19 Jul 2024 05:17:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9B3EF6B0093 for ; Fri, 19 Jul 2024 05:17:34 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4E2B08054F for ; Fri, 19 Jul 2024 09:17:34 +0000 (UTC) X-FDA: 82355949228.28.A2460F9 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf16.hostedemail.com (Postfix) with ESMTP id 39E6A180005 for ; Fri, 19 Jul 2024 09:17:31 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=CsUAb8SO; dkim=pass header.d=suse.com header.s=susede1 header.b=CsUAb8SO; spf=pass (imf16.hostedemail.com: domain of wqu@suse.com designates 195.135.223.131 as permitted sender) smtp.mailfrom=wqu@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721380631; a=rsa-sha256; cv=none; b=xaZrhSZeyQ8+7JtPLuehiR840sfL4KzYw5C8RGY/UJP3yHlZ8Cm9dTDg2/yhiMa+rUtTb4 GlPxNOqQqIzfxs1sRpi0510s+OLqpxyuMocaODYw4CZuDIRWaVmLVeWQdmUS774zx5BKp4 EDCDP6KmBBIgv1gpDCWdr3jY3chOwko= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=CsUAb8SO; dkim=pass header.d=suse.com header.s=susede1 header.b=CsUAb8SO; spf=pass (imf16.hostedemail.com: domain of wqu@suse.com designates 195.135.223.131 as permitted sender) smtp.mailfrom=wqu@suse.com; dmarc=pass (policy=quarantine) header.from=suse.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721380631; 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=mYLTpU+TQ80VryXSHafsvunreYjyK5cGZvwUB+lRbyk=; b=tk3fl5Vqk6mYoLcZjk2Uw3raHcEyVkg0ocUt2b3ymroArI6K3E1BhJyTs0GdBRfpiLCQkk DwR7FxkkfmjudVvw/c1bc5s5S7cBTQpUDy7ZFE7KY4jtrNHDa1S0eCl6MXU5lgbZYHhrzW lM4hHYgRnzRcP2KRjpb3Lylo8EpnGlk= 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 D9C251F823; Fri, 19 Jul 2024 09:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1721380650; 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=mYLTpU+TQ80VryXSHafsvunreYjyK5cGZvwUB+lRbyk=; b=CsUAb8SOTf8AI5/ia+PPxaCd7MhxyOq/jr1IFrwIdIsTetlSsfJnPpf17MAAp9bBeiWGPM 1ESBEr5zu24GX6N+UpvpZw11Hz0IKQZMfddmyhtc6XieNQ/dG88YEBOx3NmuI6itE1/3bP iti2c+hCPmuXMI+dyQKCgdBQHXEiqfg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1721380650; 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=mYLTpU+TQ80VryXSHafsvunreYjyK5cGZvwUB+lRbyk=; b=CsUAb8SOTf8AI5/ia+PPxaCd7MhxyOq/jr1IFrwIdIsTetlSsfJnPpf17MAAp9bBeiWGPM 1ESBEr5zu24GX6N+UpvpZw11Hz0IKQZMfddmyhtc6XieNQ/dG88YEBOx3NmuI6itE1/3bP iti2c+hCPmuXMI+dyQKCgdBQHXEiqfg= 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 15FD8136F7; Fri, 19 Jul 2024 09:17:27 +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 OKwdMCcvmmZPRAAAD6G6ig (envelope-from ); Fri, 19 Jul 2024 09:17:27 +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 Subject: [PATCH v6 3/3] btrfs: prefer to allocate larger folio for metadata Date: Fri, 19 Jul 2024 18:46:59 +0930 Message-ID: <559b93a99b9640b5857bbb93c35b5f361c941964.1721380449.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: p7711fgrdbyjdpopf9n3589oy4cjuag6 X-Rspamd-Queue-Id: 39E6A180005 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1721380651-881247 X-HE-Meta: U2FsdGVkX18D2J4mZXTs/4VnyrBnvYKk1FapclRA5qgq1ccgOk9waYUqFMXUi68FJbHqvHlxtnwR/dnokJiiW3Voriq3ZCZhrxFnBv1JAnjLJUeKvqK/PZrRMn3hh4doZJqcFoZhn2JORF9VwC1xgblYeaTOw4G+Hxourr8UOVLKDFvwoX+9PHtktZLjBvsm4+vyXEm/zSe4wMGgHE2DustJJgHrfFF9LdQXd7j2XGMr8ChnNH9jeieWotsZFqqW2hYKZ2shZcbE6kgnCCvsBK1fztkRzes8+VHyTprNccoCROn77MW+EUnJxKSID4u3+8GmXZwMKVJFCBtN6Hhf6K8w5b4gvnZMlwQgYiut+maOSugNfhiYp2Im2s6pfFOENbfAQylxJnfc52ZdTUwth+mDgVcrrL08Hp3+bvTqxJPqAgLmDXdOCgMdqgXuymc7MRQSbH5ceX7YN8opqSEo+rrBA4cAlOigxrlZ38C4iTvpStS0vlEdSL/Bdd3hvCiyoOtIVDwRpjEat9cSR25AzU58EYhonUrX7rtjZbWT73S4GKC1eTS5dcX78fdJ7wF2YYJ270/GP2sqWEJdHqyS/oo0KT+NCD6d3h/Yh2W3E1cRg6sPX4v22D0676UPjsBGwCDWZ4Rkd0PsP0PI0AEXYVj2dFkGKjZ5rz4+InsQ7yKwukpNAG6uQ0YMDdB3AitQDJqDU+NQrnA5U4JnSyzCde7pblMS1r2vmBcLIAiZLTcRePoDrwivPENVApWM3thIncIbW60p70YJCf2ybA7pGkB3AD29LZSyhUGt6qDUv1HTYvhaJ/X8JvC4CfZicnqjtAufLvZfJCicYhCEHJ5oEaKv6HHVXAiqY5qOeZYDQ8YdHQ0vHJSqKuTD/gHB6RvoGZdgdFPXZOLk6qNuxKqFgN0sLzn6KG9sM4wkBFfqQ5pSFpNcZUppqC+Zbwhy78zDe0xVcSEwVMK6csCGqoJ 5wRYaEVd tUcYLxa3IyzfYk69+ByVFbAsjkLgP/EHSHDTP+XJlefJqCU/TsmZXfrD2ytNyWIvqmieEmrWsxUWBfwXmhop/zmO/kkqM3frfJx8/6FU7lARkuKmd07EOXyShpZCAbAOqFTY9uFi5QWoc7E5iCjnXL/OqLLVozWsNvcv+H3/NEsLbDSRvSJZgAREskFDsGIiNBLZsoPkgg+PJt4XuwMvzEdKg8CKnCdCZEQX3LsEW9nGQS5F72iT5cybLvq7B49EP81QqEhfzgJa3bozJzGYM+WH63qFJzxJ4BKcnn5Tzxgrj1f8h5noze23gIOrHGnl6hk46NPM4gI28dYAGnoPuDfOhoACIioy0C6olM2YfCqz7xJNjZxS9EIIm8Y8775dNBPJf1OLmv71IPeg= 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: For btrfs metadata, the high order folios are only utilized when all the following conditions are met: - The extent buffer start is aligned to nodesize This should be the common case for any btrfs in the last 5 years. - The nodesize is larger than page size Or there is no need to use larger folios at all. - MM layer can fulfill our folio allocation request - The larger folio must exactly cover the extent buffer No longer no smaller, must be an exact fit. This is to make extent buffer accessors much easier. They only need to check the first slot in eb->folios[], to determine their access unit (need per-page handling or a large folio covering the whole eb). There is another small blockage, filemap APIs can not guarantee the folio size. For example, by default we go 16K nodesize on x86_64, meaning a larger folio we expect would be with order 2 (size 16K). We don't accept 2 order 1 (size 8K) folios, or we fall back to 4 order 0 (page sized) folios. So here we go a different workaround, allocate a order 2 folio first, then attach them to the filemap of metadata. Thus here comes several results related to the attach attempt of eb folios: 1) We can attach the pre-allocated eb folio to filemap This is the most simple and hot path, we just continue our work setting up the extent buffer. 2) There is an existing folio in the filemap 2.0) Subpage case We would reuse the folio no matter what, subpage is doing a different way handling folio->private (a bitmap other than a pointer to an existing eb). 2.1) There is already a live extent buffer attached to the filemap folio This should be more or less hot path, we grab the existing eb and free the current one. 2.2) No live eb. 2.2.1) The filemap folio is larger than eb folio This is a better case, we can reuse the filemap folio, but we need to cleanup all the pre-allocated folios of the new eb before reusing. Later code should take the folio size change into consideration. 2.2.2) The filemap folio is the same size of eb folio We just free the current folio, and reuse the filemap one. No other special handling needed. 2.2.3) The filemap folio is smaller than eb folio This is the most tricky corner case, we can not easily replace the folio in filemap using our eb folio. Thus here we return -EAGAIN, to inform our caller to re-try with order 0 (of course with our larger folio freed). Otherwise all the needed infrastructure is already here, we only need to try allocate larger folio as our first try in alloc_eb_folio_array(). For now, the higher order allocation is only a preferable attempt for debug build, before we had enough test coverage and push it to end users. Signed-off-by: Qu Wenruo --- fs/btrfs/extent_io.c | 102 ++++++++++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 34 deletions(-) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index cfeed7673009..d7824644d593 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -719,12 +719,28 @@ int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array, * * For now, the folios populated are always in order 0 (aka, single page). */ -static int alloc_eb_folio_array(struct extent_buffer *eb, bool nofail) +static int alloc_eb_folio_array(struct extent_buffer *eb, int order, + bool nofail) { struct page *page_array[INLINE_EXTENT_BUFFER_PAGES] = { 0 }; int num_pages = num_extent_pages(eb); int ret; + if (order) { + gfp_t gfp; + + if (order > 0) + gfp = GFP_NOFS | __GFP_NORETRY | __GFP_NOWARN; + else + gfp = nofail ? (GFP_NOFS | __GFP_NOFAIL) : GFP_NOFS; + eb->folios[0] = folio_alloc(gfp, order); + if (likely(eb->folios[0])) { + eb->folio_size = folio_size(eb->folios[0]); + eb->folio_shift = folio_shift(eb->folios[0]); + return 0; + } + /* Fallback to 0 order (single page) allocation. */ + } ret = btrfs_alloc_page_array(num_pages, page_array, nofail); if (ret < 0) return ret; @@ -2707,7 +2723,7 @@ struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) */ set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); - ret = alloc_eb_folio_array(new, false); + ret = alloc_eb_folio_array(new, 0, false); if (ret) { btrfs_release_extent_buffer(new); return NULL; @@ -2740,7 +2756,7 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, if (!eb) return NULL; - ret = alloc_eb_folio_array(eb, false); + ret = alloc_eb_folio_array(eb, 0, false); if (ret) goto err; @@ -2955,6 +2971,14 @@ static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) return 0; } +static void free_all_eb_folios(struct extent_buffer *eb) +{ + for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) { + if (eb->folios[i]) + folio_put(eb->folios[i]); + eb->folios[i] = NULL; + } +} /* * Return 0 if eb->folios[i] is attached to btree inode successfully. @@ -2974,6 +2998,7 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, struct mem_cgroup *old_memcg; const unsigned long index = eb->start >> PAGE_SHIFT; struct folio *existing_folio = NULL; + const int eb_order = folio_order(eb->folios[0]); int ret; ASSERT(found_eb_ret); @@ -3003,15 +3028,6 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, goto retry; } - /* For now, we should only have single-page folios for btree inode. */ - ASSERT(folio_nr_pages(existing_folio) == 1); - - if (folio_size(existing_folio) != eb->folio_size) { - folio_unlock(existing_folio); - folio_put(existing_folio); - return -EAGAIN; - } - finish: spin_lock(&mapping->i_private_lock); if (existing_folio && fs_info->nodesize < PAGE_SIZE) { @@ -3020,6 +3036,7 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, eb->folios[i] = existing_folio; } else if (existing_folio) { struct extent_buffer *existing_eb; + int existing_order = folio_order(existing_folio); existing_eb = grab_extent_buffer(fs_info, folio_page(existing_folio, 0)); @@ -3031,9 +3048,34 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i, folio_put(existing_folio); return 1; } - /* The extent buffer no longer exists, we can reuse the folio. */ - __free_page(folio_page(eb->folios[i], 0)); - eb->folios[i] = existing_folio; + if (existing_order > eb_order) { + /* + * The existing one has higher order, we need to drop + * all eb folios before resuing it. + * And this should only happen for the first folio. + */ + ASSERT(i == 0); + free_all_eb_folios(eb); + eb->folios[i] = existing_folio; + } else if (existing_order == eb_order) { + /* + * Can safely reuse the filemap folio, just + * release the eb one. + */ + folio_put(eb->folios[i]); + eb->folios[i] = existing_folio; + } else { + /* + * The existing one has lower order. + * + * Just retry and fallback to order 0. + */ + ASSERT(i == 0); + folio_unlock(existing_folio); + folio_put(existing_folio); + spin_unlock(&mapping->i_private_lock); + return -EAGAIN; + } } eb->folio_size = folio_size(eb->folios[i]); eb->folio_shift = folio_shift(eb->folios[i]); @@ -3066,6 +3108,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 lockdep_owner = owner_root; bool page_contig = true; int uptodate = 1; + int order = 0; int ret; if (check_eb_alignment(fs_info, start)) @@ -3082,6 +3125,10 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, btrfs_warn_32bit_limit(fs_info); #endif + if (IS_ENABLED(CONFIG_BTRFS_DEBUG) && fs_info->nodesize > PAGE_SIZE && + IS_ALIGNED(start, fs_info->nodesize)) + order = ilog2(fs_info->nodesize >> PAGE_SHIFT); + eb = find_extent_buffer(fs_info, start); if (eb) return eb; @@ -3116,7 +3163,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, reallocate: /* Allocate all pages first. */ - ret = alloc_eb_folio_array(eb, true); + ret = alloc_eb_folio_array(eb, order, true); if (ret < 0) { btrfs_free_subpage(prealloc); goto out; @@ -3134,26 +3181,12 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, } /* - * TODO: Special handling for a corner case where the order of - * folios mismatch between the new eb and filemap. - * - * This happens when: - * - * - the new eb is using higher order folio - * - * - the filemap is still using 0-order folios for the range - * This can happen at the previous eb allocation, and we don't - * have higher order folio for the call. - * - * - the existing eb has already been freed - * - * In this case, we have to free the existing folios first, and - * re-allocate using the same order. - * Thankfully this is not going to happen yet, as we're still - * using 0-order folios. + * Got a corner case where the existing folio is lower order, + * fallback to 0 order and retry. */ if (unlikely(ret == -EAGAIN)) { - ASSERT(0); + order = 0; + free_all_eb_folios(eb); goto reallocate; } attached++; @@ -3164,6 +3197,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, * and free the allocated page. */ folio = eb->folios[i]; + num_folios = num_extent_folios(eb); WARN_ON(btrfs_folio_test_dirty(fs_info, folio, eb->start, eb->len)); /*