Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19010753rwd; Wed, 28 Jun 2023 03:59:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ45sx6at8uLQyoMYMePAJHCk12QwlOa2PruLIq6FSvVF0Iu7VnmuATzxvcYYosHCOpak5qM X-Received: by 2002:a05:6808:2017:b0:3a1:f256:e884 with SMTP id q23-20020a056808201700b003a1f256e884mr5796518oiw.37.1687949952033; Wed, 28 Jun 2023 03:59:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687949951; cv=none; d=google.com; s=arc-20160816; b=sjc0aESO0xyM4Qkf0FECcWTsimPqTp4b8vjPt0hSZ16MpxbsfTf5U+BXnK3izFn5n3 hZKWdJy7xCvC4ocyl5ud9LGWZbUwl1E9NuaNuZgZsPNlRWgOlKP1exJ0XEvObJnjNJ2a uCyQ8dch8u6pXvJrUosVaKJCkhBS/mzwBa93nEJtBdVuVlXnTS5A63j8rHa25/UuY00l Cf4G9oAeuisbsfsQSv2IxSVIj2IURKEJmho4cYTURjLJvIf3VVg9XRz9fZiScWIFxr9U pC5F1YyuHasjJ+N7TJP/ne9tAkWFdO9NF2grVAIoFZ2ImaKioRqDbi3T3spr8NM/SqCQ 9D5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=TOeQPebD91AEi5r7Bvbf8K+TmmzdkyidP0hZg85tDnQ=; fh=PHy7KIjR1gaVBBR0tfkd6++EvP+2GDdg8uMDrxbHsAY=; b=uTc6ChpG/Q4vB//XOUcyRhqvqAGZVH7yK9aoaOO7yO8vWJfW5ix1Jx/df+AVDy1Jej v3te3s0MUt7uPFkQh0UzPDF7KUHVktH/gmVHwJI0PwoTdlI+9wIJKYDz3zzapAXDHTXY p9nzIakjp//9+C53RQ1T4Yy3E5UxMYrPayxIiIjcAmFUdMCHvmca1SiNIbcKrtIBpcla NpLCPq55oqS4NZFMU5wO7vYg9KaI950gC7q9V3ESKa8xko/nnY2gYxHv6ijXeYSp20EM SmIqvwjOO/+OE6kAaS8WyQJM7C3KQLZbq8dwlW4uk5jvSGYWp+m4jn9bEZ3U6Lkm0FCv dV9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cc8z2A57; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q3-20020a656843000000b0054ff67d6666si8563710pgt.512.2023.06.28.03.58.53; Wed, 28 Jun 2023 03:59:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cc8z2A57; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230487AbjF1Kt4 (ORCPT + 99 others); Wed, 28 Jun 2023 06:49:56 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:39206 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230119AbjF1Kty (ORCPT ); Wed, 28 Jun 2023 06:49:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687949346; h=from:from:reply-to:subject:subject: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=TOeQPebD91AEi5r7Bvbf8K+TmmzdkyidP0hZg85tDnQ=; b=cc8z2A57juF3ao1E5QB0/sHI19T2zIiziGvB95KI3VDLMHfLcJVv5pOv9hI46AX7/bCTUM VkvLXSF0nmJ2CetbjEYQpLV8CnCiI/+ZfASDNU4RLeQnMvpU+K02fnxd4HOOFPLp8Idij2 udG7st50RPF4k95s876qZ4Iz8CYuoD8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-618-L4wgRJtDMGublEOWLuvnxw-1; Wed, 28 Jun 2023 06:49:01 -0400 X-MC-Unique: L4wgRJtDMGublEOWLuvnxw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3363E85A58A; Wed, 28 Jun 2023 10:49:00 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6153F40C2063; Wed, 28 Jun 2023 10:48:57 +0000 (UTC) From: David Howells To: Andrew Morton Cc: David Howells , Matthew Wilcox , Linus Torvalds , Jeff Layton , Christoph Hellwig , linux-afs@lists.infradead.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs-developer@lists.sourceforge.net, linux-erofs@lists.ozlabs.org, linux-ext4@vger.kernel.org, linux-cachefs@redhat.com, linux-fsdevel@vger.kernel.org, Rohith Surabattula , Steve French , Shyam Prasad N , Dave Wysochanski , Dominique Martinet , Ilya Dryomov , "Theodore Ts'o" , Andreas Dilger , linux-mm@kvack.org Subject: [PATCH v7 1/2] mm: Merge folio_has_private()/filemap_release_folio() call pairs Date: Wed, 28 Jun 2023 11:48:51 +0100 Message-ID: <20230628104852.3391651-2-dhowells@redhat.com> In-Reply-To: <20230628104852.3391651-1-dhowells@redhat.com> References: <20230628104852.3391651-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Make filemap_release_folio() check folio_has_private(). Then, in most cases, where a call to folio_has_private() is immediately followed by a call to filemap_release_folio(), we can get rid of the test in the pair. There are a couple of sites in mm/vscan.c that this can't so easily be done. In shrink_folio_list(), there are actually three cases (something different is done for incompletely invalidated buffers), but filemap_release_folio() elides two of them. In shrink_active_list(), we don't have have the folio lock yet, so the check allows us to avoid locking the page unnecessarily. A wrapper function to check if a folio needs release is provided for those places that still need to do it in the mm/ directory. This will acquire additional parts to the condition in a future patch. After this, the only remaining caller of folio_has_private() outside of mm/ is a check in fuse. Reported-by: Rohith Surabattula Suggested-by: Matthew Wilcox Signed-off-by: David Howells cc: Matthew Wilcox cc: Linus Torvalds cc: Steve French cc: Shyam Prasad N cc: Rohith Surabattula cc: Dave Wysochanski cc: Dominique Martinet cc: Ilya Dryomov cc: "Theodore Ts'o" cc: Andreas Dilger cc: linux-cachefs@redhat.com cc: linux-cifs@vger.kernel.org cc: linux-afs@lists.infradead.org cc: v9fs-developer@lists.sourceforge.net cc: ceph-devel@vger.kernel.org cc: linux-nfs@vger.kernel.org cc: linux-ext4@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org --- Notes: ver #5) - Rebased on linus/master. try_to_release_page() has now been entirely replaced by filemap_release_folio(), barring one comment. - Cleaned up some pairs in ext4. ver #4) - Split from fscache fix. - Moved folio_needs_release() to mm/internal.h and removed open-coded version from filemap_release_folio(). ver #3) - Fixed mapping_clear_release_always() to use clear_bit() not set_bit(). - Moved a '&&' to the correct line. ver #2) - Rewrote entirely according to Willy's suggestion[1]. fs/ext4/move_extent.c | 12 ++++-------- fs/splice.c | 3 +-- mm/filemap.c | 2 ++ mm/huge_memory.c | 3 +-- mm/internal.h | 8 ++++++++ mm/khugepaged.c | 3 +-- mm/memory-failure.c | 8 +++----- mm/migrate.c | 3 +-- mm/truncate.c | 6 ++---- mm/vmscan.c | 8 ++++---- 10 files changed, 27 insertions(+), 29 deletions(-) diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c index b5af2fc03b2f..251584a23d05 100644 --- a/fs/ext4/move_extent.c +++ b/fs/ext4/move_extent.c @@ -340,10 +340,8 @@ move_extent_per_page(struct file *o_filp, struct inode *donor_inode, ext4_double_up_write_data_sem(orig_inode, donor_inode); goto data_copy; } - if ((folio_has_private(folio[0]) && - !filemap_release_folio(folio[0], 0)) || - (folio_has_private(folio[1]) && - !filemap_release_folio(folio[1], 0))) { + if (!filemap_release_folio(folio[0], 0) || + !filemap_release_folio(folio[1], 0)) { *err = -EBUSY; goto drop_data_sem; } @@ -362,10 +360,8 @@ move_extent_per_page(struct file *o_filp, struct inode *donor_inode, /* At this point all buffers in range are uptodate, old mapping layout * is no longer required, try to drop it now. */ - if ((folio_has_private(folio[0]) && - !filemap_release_folio(folio[0], 0)) || - (folio_has_private(folio[1]) && - !filemap_release_folio(folio[1], 0))) { + if (!filemap_release_folio(folio[0], 0) || + !filemap_release_folio(folio[1], 0)) { *err = -EBUSY; goto unlock_folios; } diff --git a/fs/splice.c b/fs/splice.c index 7a9565d8ec4f..6412848891df 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -82,8 +82,7 @@ static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe, */ folio_wait_writeback(folio); - if (folio_has_private(folio) && - !filemap_release_folio(folio, GFP_KERNEL)) + if (!filemap_release_folio(folio, GFP_KERNEL)) goto out_unlock; /* diff --git a/mm/filemap.c b/mm/filemap.c index 00f01d8ead47..31d07c2f8d32 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4134,6 +4134,8 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp) struct address_space * const mapping = folio->mapping; BUG_ON(!folio_test_locked(folio)); + if (!folio_needs_release(folio)) + return true; if (folio_test_writeback(folio)) return false; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 624671aaa60d..a14b3d1af9d7 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2688,8 +2688,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) gfp = current_gfp_context(mapping_gfp_mask(mapping) & GFP_RECLAIM_MASK); - if (folio_test_private(folio) && - !filemap_release_folio(folio, gfp)) { + if (!filemap_release_folio(folio, gfp)) { ret = -EBUSY; goto out; } diff --git a/mm/internal.h b/mm/internal.h index e6029d94bdb2..a76314764d8c 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -170,6 +170,14 @@ static inline void set_page_refcounted(struct page *page) set_page_count(page, 1); } +/* + * Return true if a folio needs ->release_folio() calling upon it. + */ +static inline bool folio_needs_release(struct folio *folio) +{ + return folio_has_private(folio); +} + extern unsigned long highest_memmap_pfn; /* diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 2d0d58fb4e7f..1e6e6a25cd52 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2058,8 +2058,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, goto out_unlock; } - if (folio_has_private(folio) && - !filemap_release_folio(folio, GFP_KERNEL)) { + if (!filemap_release_folio(folio, GFP_KERNEL)) { result = SCAN_PAGE_HAS_PRIVATE; folio_putback_lru(folio); goto out_unlock; diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 5b663eca1f29..38321eb85fb2 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -944,14 +944,12 @@ static int truncate_error_page(struct page *p, unsigned long pfn, struct folio *folio = page_folio(p); int err = mapping->a_ops->error_remove_page(mapping, p); - if (err != 0) { + if (err != 0) pr_info("%#lx: Failed to punch page: %d\n", pfn, err); - } else if (folio_has_private(folio) && - !filemap_release_folio(folio, GFP_NOIO)) { + else if (!filemap_release_folio(folio, GFP_NOIO)) pr_info("%#lx: failed to release buffers\n", pfn); - } else { + else ret = MF_RECOVERED; - } } else { /* * If the file system doesn't support it just invalidate diff --git a/mm/migrate.c b/mm/migrate.c index 01cac26a3127..5fc27d1a7eaf 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -929,8 +929,7 @@ static int fallback_migrate_folio(struct address_space *mapping, * Buffers may be managed in a filesystem specific way. * We must have no buffers or drop them. */ - if (folio_test_private(src) && - !filemap_release_folio(src, GFP_KERNEL)) + if (!filemap_release_folio(src, GFP_KERNEL)) return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; return migrate_folio(mapping, dst, src, mode); diff --git a/mm/truncate.c b/mm/truncate.c index 86de31ed4d32..6fb830369829 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -19,7 +19,6 @@ #include #include #include -#include /* grr. try_to_release_page */ #include #include #include "internal.h" @@ -276,7 +275,7 @@ static long mapping_evict_folio(struct address_space *mapping, if (folio_ref_count(folio) > folio_nr_pages(folio) + folio_has_private(folio) + 1) return 0; - if (folio_has_private(folio) && !filemap_release_folio(folio, 0)) + if (!filemap_release_folio(folio, 0)) return 0; return remove_mapping(mapping, folio); @@ -574,8 +573,7 @@ static int invalidate_complete_folio2(struct address_space *mapping, if (folio->mapping != mapping) return 0; - if (folio_has_private(folio) && - !filemap_release_folio(folio, GFP_KERNEL)) + if (!filemap_release_folio(folio, GFP_KERNEL)) return 0; spin_lock(&mapping->host->i_lock); diff --git a/mm/vmscan.c b/mm/vmscan.c index 5bf98d0a22c9..49bc412b31a2 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2058,7 +2058,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, * (refcount == 1) it can be freed. Otherwise, leave * the folio on the LRU so it is swappable. */ - if (folio_has_private(folio)) { + if (folio_needs_release(folio)) { if (!filemap_release_folio(folio, sc->gfp_mask)) goto activate_locked; if (!mapping && folio_ref_count(folio) == 1) { @@ -2703,9 +2703,9 @@ static void shrink_active_list(unsigned long nr_to_scan, } if (unlikely(buffer_heads_over_limit)) { - if (folio_test_private(folio) && folio_trylock(folio)) { - if (folio_test_private(folio)) - filemap_release_folio(folio, 0); + if (folio_needs_release(folio) && + folio_trylock(folio)) { + filemap_release_folio(folio, 0); folio_unlock(folio); } }