From patchwork Thu Jan 13 22:08:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthew Wilcox (Oracle)" X-Patchwork-Id: 12713155 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 36D2FC433F5 for ; Thu, 13 Jan 2022 22:08:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C49276B0074; Thu, 13 Jan 2022 17:08:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD20B6B0075; Thu, 13 Jan 2022 17:08:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4BD76B0078; Thu, 13 Jan 2022 17:08:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0241.hostedemail.com [216.40.44.241]) by kanga.kvack.org (Postfix) with ESMTP id 967D06B0074 for ; Thu, 13 Jan 2022 17:08:29 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 51F2C8E3C4 for ; Thu, 13 Jan 2022 22:08:29 +0000 (UTC) X-FDA: 79026653538.03.0FD77D3 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf10.hostedemail.com (Postfix) with ESMTP id 0C81EC0004 for ; Thu, 13 Jan 2022 22:08:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=IDWd/IwHC04isId/iqsr+2lvOr59lemWZtcJ7xJcHQ8=; b=S6PRB2iNVWjdYGSM5xIlrNR7DL H3ZXNb0AaFUeQDx9woQRC3mynAnlA0zO42J4QTvToV1e4TNqV8JLEG8SSBByob0QXShOpu/f7+rfh jp/mlnGeNk1sHZtPGVBD6uPhx8Fjk1fFXvvgruGRPz47DtBNVttFY4jhqgb4/h7p43DC6PJ+Qmeo2 T155/22ATgwj7NFEIOV0QFjVxko9cSzqG/gDRqcvbbRnSIcrL7dlAQKP50G6StK2CrIK8cj6fJTdZ yG2BzbbVfP87B8f3OGkeTCp4xOKddJBqe/UIWLwH2Wizlx5WRJVpIsKjEDWyCe/A/biA75/XZkOWF zLQ7+fhw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1n88Gc-005HEW-D7; Thu, 13 Jan 2022 22:08:22 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" , Christoph Hellwig , John Hubbard , Jason Gunthorpe , William Kucharski Subject: [PATCH 1/2] mm: Add folio_put_refs() Date: Thu, 13 Jan 2022 22:08:15 +0000 Message-Id: <20220113220816.1257657-2-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220113220816.1257657-1-willy@infradead.org> References: <20220113220816.1257657-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0C81EC0004 X-Stat-Signature: 5bz4m4aedgic19j8twzuyiz6ap59rjka Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=S6PRB2iN; dmarc=none; spf=none (imf10.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspamd-Server: rspam06 X-HE-Tag: 1642111708-365668 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: This is like folio_put(), but puts N references at once instead of just one. It's like put_page_refs(), but does one atomic operation instead of two, and is available to more than just gup.c. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Christoph Hellwig Reviewed-by: John Hubbard Reviewed-by: Jason Gunthorpe Reviewed-by: William Kucharski --- include/linux/mm.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index c768a7c81b0b..cb98f75b245e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1244,6 +1244,26 @@ static inline void folio_put(struct folio *folio) __put_page(&folio->page); } +/** + * folio_put_refs - Reduce the reference count on a folio. + * @folio: The folio. + * @refs: The amount to subtract from the folio's reference count. + * + * If the folio's reference count reaches zero, the memory will be + * released back to the page allocator and may be used by another + * allocation immediately. Do not access the memory or the struct folio + * after calling folio_put_refs() unless you can be sure that these weren't + * the last references. + * + * Context: May be called in process or interrupt context, but not in NMI + * context. May be called while holding a spinlock. + */ +static inline void folio_put_refs(struct folio *folio, int refs) +{ + if (folio_ref_sub_and_test(folio, refs)) + __put_page(&folio->page); +} + static inline void put_page(struct page *page) { struct folio *folio = page_folio(page); From patchwork Thu Jan 13 22:08:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthew Wilcox (Oracle)" X-Patchwork-Id: 12713154 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 6600AC433EF for ; Thu, 13 Jan 2022 22:08:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D81B76B0073; Thu, 13 Jan 2022 17:08:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D0CEE6B0074; Thu, 13 Jan 2022 17:08:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAC266B0075; Thu, 13 Jan 2022 17:08:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0021.hostedemail.com [216.40.44.21]) by kanga.kvack.org (Postfix) with ESMTP id AC7926B0073 for ; Thu, 13 Jan 2022 17:08:27 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 675D31822B845 for ; Thu, 13 Jan 2022 22:08:27 +0000 (UTC) X-FDA: 79026653454.29.96CFD74 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf08.hostedemail.com (Postfix) with ESMTP id 1BE2F160002 for ; Thu, 13 Jan 2022 22:08:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=q9nFMAXidlMSlpH4UFL2WxiRClvDd+0fOTOBK1//bNg=; b=LclWvncTGXFpwXn41udv2Z9LpA Sifs0222WJ1sP6F8XvF70+De+xZ/JfE2ktsvrnR6XanUpZXi/Msvr5hrOaGfDjFdAA24yvNRRrdWw oMnTEVutC0iSvJ7/ER5DuO/HzTuwFKRBiW+2QBe+TWmYin5KEsArjAiKsKtpWQwNyPrtjPwqOAt7V ga5oa4bkwRtmxMvUzVP8pSH/ZLWUaL+L5bzMdOWb9RhQCKrqpL3s+mk/ERKiCUHscGh3Eo88YYddS 0YqLIOcTeLhEyGesHGO2l2qwzTcWILDpsrjWOERd9tOZIpnj6wYiIXU0ovm6qtj1yqP3nyyFtfnOR w0Mo5p8A==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1n88Gc-005HEY-Fq; Thu, 13 Jan 2022 22:08:22 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: "Matthew Wilcox (Oracle)" Subject: [PATCH 2/2] filemap: Use folio_put_refs() in filemap_free_folio() Date: Thu, 13 Jan 2022 22:08:16 +0000 Message-Id: <20220113220816.1257657-3-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220113220816.1257657-1-willy@infradead.org> References: <20220113220816.1257657-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1BE2F160002 X-Stat-Signature: i15x1qu8fn3ormnphb68f1859r3dgjfz Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=LclWvncT; dmarc=none; spf=none (imf08.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspamd-Server: rspam08 X-HE-Tag: 1642111706-343271 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: This shrinks filemap_free_folio() by 55 bytes in my .config; 24 bytes from removing the VM_BUG_ON_FOLIO() and 31 bytes from unifying the small/large folio paths. We could just use folio_ref_sub() here since the caller should hold a reference (as the VM_BUG_ON_FOLIO() was asserting), but that's fragile. Signed-off-by: Matthew Wilcox (Oracle) --- mm/filemap.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 2fd9b2f24025..afc8f5ca85ac 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -231,17 +231,15 @@ void __filemap_remove_folio(struct folio *folio, void *shadow) void filemap_free_folio(struct address_space *mapping, struct folio *folio) { void (*freepage)(struct page *); + int refs = 1; freepage = mapping->a_ops->freepage; if (freepage) freepage(&folio->page); - if (folio_test_large(folio) && !folio_test_hugetlb(folio)) { - folio_ref_sub(folio, folio_nr_pages(folio)); - VM_BUG_ON_FOLIO(folio_ref_count(folio) <= 0, folio); - } else { - folio_put(folio); - } + if (folio_test_large(folio) && !folio_test_hugetlb(folio)) + refs = folio_nr_pages(folio); + folio_put_refs(folio, refs); } /**