From patchwork Thu Dec 22 15:02:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13080074 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 341F3C4332F for ; Thu, 22 Dec 2022 15:04:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235860AbiLVPEf (ORCPT ); Thu, 22 Dec 2022 10:04:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235865AbiLVPEP (ORCPT ); Thu, 22 Dec 2022 10:04:15 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1155327DD0 for ; Thu, 22 Dec 2022 07:02:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671721357; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=75w1pfVKB1Ro0XqkbXmr8eWxoXfk22o+qXL/5b9cucw=; b=RJyES0TDThBSqRZCVOF7bAIcfhChVykin+8LFDG/nSg7QFfCJzqgJHixS4F+544kpos2VV VSfc4WoaIEqdTWwtO0Lv4vjmD39IcEjTAxUdr8i8P+dhHXDNQ3rDvxRujpUIwa6wMagsBF 7tPauQkMawlelG/lF/uWqAB0I7T0ouc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-290-HaKXfIxlMWuof_yakpjAKg-1; Thu, 22 Dec 2022 10:02:34 -0500 X-MC-Unique: HaKXfIxlMWuof_yakpjAKg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A1C872803D7E; Thu, 22 Dec 2022 15:02:32 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4BBDC2166B29; Thu, 22 Dec 2022 15:02:30 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH v5 3/3] mm: Make filemap_release_folio() better inform shrink_folio_list() From: David Howells To: Matthew Wilcox Cc: Linus Torvalds , Steve French , Shyam Prasad N , Rohith Surabattula , Dave Wysochanski , Dominique Martinet , Ilya Dryomov , linux-cachefs@redhat.com, linux-cifs@vger.kernel.org, linux-afs@lists.infradead.org, v9fs-developer@lists.sourceforge.net, ceph-devel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, dhowells@redhat.com, Linus Torvalds , Jeff Layton , 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@lists.ozlabs.org, linux-cachefs@redhat.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Thu, 22 Dec 2022 15:02:29 +0000 Message-ID: <167172134962.2334525.570622889806603086.stgit@warthog.procyon.org.uk> In-Reply-To: <167172131368.2334525.8569808925687731937.stgit@warthog.procyon.org.uk> References: <167172131368.2334525.8569808925687731937.stgit@warthog.procyon.org.uk> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org Make filemap_release_folio() return one of three values: (0) FILEMAP_CANT_RELEASE_FOLIO Couldn't release the folio's private data, so the folio can't itself be released. (1) FILEMAP_RELEASED_FOLIO The private data on the folio was released and the folio can be released. (2) FILEMAP_FOLIO_HAD_NO_PRIVATE There was no private data on the folio and the folio can be released. The first must be zero so that existing tests of !filemap_release_folio() continue to work as expected; similarly the other two must both be non-zero so that existing tests of filemap_release_folio() continue to work as expected. Using this, make shrink_folio_list() choose which of three cases to follow based on the return from filemap_release_folio() rather than testing the folio's private bit itself. 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: 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-fsdevel@vger.kernel.org cc: linux-mm@kvack.org Link: https://lore.kernel.org/r/1459152.1669208550@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/166924373637.1772793.2622483388224911574.stgit@warthog.procyon.org.uk/ # v4 --- include/linux/pagemap.h | 7 ++++++- mm/filemap.c | 20 ++++++++++++++------ mm/vmscan.c | 29 +++++++++++++++-------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a0d433e0addd..cd00fb3b524b 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1121,7 +1121,12 @@ void __filemap_remove_folio(struct folio *folio, void *shadow); void replace_page_cache_folio(struct folio *old, struct folio *new); void delete_from_page_cache_batch(struct address_space *mapping, struct folio_batch *fbatch); -bool filemap_release_folio(struct folio *folio, gfp_t gfp); +enum filemap_released_folio { + FILEMAP_CANT_RELEASE_FOLIO = 0, /* (This must be 0) Release failed */ + FILEMAP_RELEASED_FOLIO = 1, /* Folio's private data released */ + FILEMAP_FOLIO_HAD_NO_PRIVATE = 2, /* Folio had no private data */ +}; +enum filemap_released_folio filemap_release_folio(struct folio *folio, gfp_t gfp); loff_t mapping_seek_hole_data(struct address_space *, loff_t start, loff_t end, int whence); diff --git a/mm/filemap.c b/mm/filemap.c index 344146c170b0..217ca847773a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3953,20 +3953,28 @@ EXPORT_SYMBOL(generic_file_write_iter); * this page (__GFP_IO), and whether the call may block * (__GFP_RECLAIM & __GFP_FS). * - * Return: %true if the release was successful, otherwise %false. + * Return: %FILEMAP_RELEASED_FOLIO if the release was successful, + * %FILEMAP_CANT_RELEASE_FOLIO if the private data couldn't be released and + * %FILEMAP_FOLIO_HAD_NO_PRIVATE if there was no private data. */ -bool filemap_release_folio(struct folio *folio, gfp_t gfp) +enum filemap_released_folio filemap_release_folio(struct folio *folio, + gfp_t gfp) { struct address_space * const mapping = folio->mapping; + bool released; BUG_ON(!folio_test_locked(folio)); if (!folio_needs_release(folio)) - return true; + return FILEMAP_FOLIO_HAD_NO_PRIVATE; if (folio_test_writeback(folio)) - return false; + return FILEMAP_CANT_RELEASE_FOLIO; if (mapping && mapping->a_ops->release_folio) - return mapping->a_ops->release_folio(folio, gfp); - return try_to_free_buffers(folio); + released = mapping->a_ops->release_folio(folio, gfp); + else + released = try_to_free_buffers(folio); + + return released ? + FILEMAP_RELEASED_FOLIO : FILEMAP_CANT_RELEASE_FOLIO; } EXPORT_SYMBOL(filemap_release_folio); diff --git a/mm/vmscan.c b/mm/vmscan.c index bded71961143..b1e5ca348223 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1996,25 +1996,26 @@ 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_needs_release(folio)) { - if (!filemap_release_folio(folio, sc->gfp_mask)) - goto activate_locked; + switch (filemap_release_folio(folio, sc->gfp_mask)) { + case FILEMAP_CANT_RELEASE_FOLIO: + goto activate_locked; + case FILEMAP_RELEASED_FOLIO: if (!mapping && folio_ref_count(folio) == 1) { folio_unlock(folio); if (folio_put_testzero(folio)) goto free_it; - else { - /* - * rare race with speculative reference. - * the speculative reference will free - * this folio shortly, so we may - * increment nr_reclaimed here (and - * leave it off the LRU). - */ - nr_reclaimed += nr_pages; - continue; - } + /* + * rare race with speculative reference. the + * speculative reference will free this folio + * shortly, so we may increment nr_reclaimed + * here (and leave it off the LRU). + */ + nr_reclaimed += nr_pages; + continue; } + break; + case FILEMAP_FOLIO_HAD_NO_PRIVATE: + break; } if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {