From nobody Wed Sep 10 23:58:55 2025 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 B3A78C64EC7 for ; Thu, 23 Feb 2023 02:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232929AbjBWCo3 (ORCPT ); Wed, 22 Feb 2023 21:44:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231567AbjBWCo1 (ORCPT ); Wed, 22 Feb 2023 21:44:27 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94DCB279B5 for ; Wed, 22 Feb 2023 18:44:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=krGz8JNPEfUKWYOBIr5M4Mxvx6mftCSbjb3cTtYDfCE=; b=WPU7CMVf2DgW2/TSJHS9J0wBbm 7uuRWz2+zFq+EasHiqr1iOkkS9NL5LWxlcCD3ssYHfVxIZqQ+gtxv8xIAdydY8zeX2XXxPd32DMrA xH98Va7Idt0dDo+kqQgqTYTrqEJ2RAZYXxd257F82UMExgv3lmhVcPSPPsWAeKfauFFTA3TPeq+Di Uq9O1gx1dLSp7+uLiQvB2zJWZIz0xjOpuHO5MScdavofEmCokIJtKYMbj90wP8/izrEAVUewGnRZo UUKkZ2ofyPO4lPHlNe3XteK6m8dKpiwygeke619PsuduCvAAoLvnnP9ZRzHoaSHqgmML7d+uGrreJ IZ2d8kFg==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pV1ah-00EmN8-6C; Thu, 23 Feb 2023 02:44:15 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, dave@stgolabs.net, a.manzanares@samsung.com, yosryahmed@google.com, mcgrof@kernel.org, linux-kernel@vger.kernel.org Subject: [RFC v2 1/5] shmem: remove check for folio lock on writepage() Date: Wed, 22 Feb 2023 18:44:08 -0800 Message-Id: <20230223024412.3522465-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230223024412.3522465-1-mcgrof@kernel.org> References: <20230223024412.3522465-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Luis Chamberlain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Matthew notes we should not need to check the folio lock on the writepage() callback so remove it. This sanity check has been lingering since linux-history days. We remove this as we tidy up the writepage() callback to make things a bit clearer. Suggested-by: Matthew Wilcox Signed-off-by: Luis Chamberlain --- mm/shmem.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index 28f3c699c8ce..b3ad619328bf 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1351,7 +1351,6 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) folio_clear_dirty(folio); } =20 - BUG_ON(!folio_test_locked(folio)); mapping =3D folio->mapping; index =3D folio->index; inode =3D mapping->host; --=20 2.39.1 From nobody Wed Sep 10 23:58:55 2025 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 48B65C636D6 for ; Thu, 23 Feb 2023 02:44:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233068AbjBWCok (ORCPT ); Wed, 22 Feb 2023 21:44:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233082AbjBWCo2 (ORCPT ); Wed, 22 Feb 2023 21:44:28 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E98226CEC for ; Wed, 22 Feb 2023 18:44:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=Zf/VP5ikCHj8OLL6rer4hxqpVXK1ALaeitWH3YJJWGw=; b=K56am7J4RdGNuU6o8W0jdqS0ki klpCVr4uB8PIfFhNoUaw6mxyic+kMZCAItqPxG3BiXoGQRqZaOocgPrH8mWw6CjcKST0X8JSSiCeA QpOTOeoXX9qjWWrb0lLLZffp5U5PkWf0kMvODDM27SmUhugLUSV5VWCWJf97zZ+rLq0Ow8isJpKAD TMwO2rPqOxnEnU+FpRlGvZZwYXSjTdYixOT0+ynh1x0slK5e1rlLbrccAEbpoTtEnfYzvg4bZcNGx D9TolFbzyTqguOlMSvw5Qzrs2hGcv8k/0Y2R3MBKDoLHYT3zuIuEb9r6VtYKNR7qareuSz5A8lT/9 XlQqeXew==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pV1ah-00EmNA-7d; Thu, 23 Feb 2023 02:44:15 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, dave@stgolabs.net, a.manzanares@samsung.com, yosryahmed@google.com, mcgrof@kernel.org, linux-kernel@vger.kernel.org Subject: [RFC v2 2/5] shmem: set shmem_writepage() variables early Date: Wed, 22 Feb 2023 18:44:09 -0800 Message-Id: <20230223024412.3522465-3-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230223024412.3522465-1-mcgrof@kernel.org> References: <20230223024412.3522465-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Luis Chamberlain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" shmem_writepage() sets up variables typically used *after* a possible huge page split. However even if that does happen the address space mapping should not change, and the inode does not change either. So it should be safe to set that from the very beginning. This commit makes no functional changes. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b3ad619328bf..1269482d0a5c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1331,9 +1331,9 @@ int shmem_unuse(unsigned int type) static int shmem_writepage(struct page *page, struct writeback_control *wb= c) { struct folio *folio =3D page_folio(page); - struct shmem_inode_info *info; - struct address_space *mapping; - struct inode *inode; + struct address_space *mapping =3D folio->mapping; + struct inode *inode =3D mapping->host; + struct shmem_inode_info *info =3D SHMEM_I(inode); swp_entry_t swap; pgoff_t index; =20 @@ -1351,10 +1351,7 @@ static int shmem_writepage(struct page *page, struct= writeback_control *wbc) folio_clear_dirty(folio); } =20 - mapping =3D folio->mapping; index =3D folio->index; - inode =3D mapping->host; - info =3D SHMEM_I(inode); if (info->flags & VM_LOCKED) goto redirty; if (!total_swap_pages) --=20 2.39.1 From nobody Wed Sep 10 23:58:55 2025 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 9C407C61DA4 for ; Thu, 23 Feb 2023 02:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231567AbjBWCoc (ORCPT ); Wed, 22 Feb 2023 21:44:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232901AbjBWCo1 (ORCPT ); Wed, 22 Feb 2023 21:44:27 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9540E279BF for ; Wed, 22 Feb 2023 18:44:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=vQjHgoe5Jm+n0b1ADMVciA5nvYNCQKcIEIUc76XY5yA=; b=nobDFyPW1FDWru83lnPC9EwGl6 R/FHdKwibKNjX3q9wvjpliuLql+NoQ9c0rhOKA0yrjL0c2fVAZ8p5RDkCW6T/uLryGWU6LLvIkYK3 H8ZiDSzxHfM1HiVyVJJIIX+A6Fs2dWqSFF4Mm8kGPxhMBXeV+81ZHgNBeGyY6UUKuDDPhMnjqGLO5 TABWOhP+CZw4oNP8VfJdPVbrNk70Mb5neyvgQ6W/IecffA3e/SrfwO1okz2f1jrhReSmm+2oITWqW LBsLcoG8bQlb1BH852kJkU4dTTBSJiFZIiW9Bx4gy3pLT+qeSzmlOw0EHq2ID8LlnX29yOscwqVJd SxirXIAw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pV1ah-00EmNC-8r; Thu, 23 Feb 2023 02:44:15 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, dave@stgolabs.net, a.manzanares@samsung.com, yosryahmed@google.com, mcgrof@kernel.org, linux-kernel@vger.kernel.org Subject: [RFC v2 3/5] shmem: move reclaim check early on writepages() Date: Wed, 22 Feb 2023 18:44:10 -0800 Message-Id: <20230223024412.3522465-4-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230223024412.3522465-1-mcgrof@kernel.org> References: <20230223024412.3522465-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Luis Chamberlain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" i915_gem requires huge folios to be split when swapping. However we have check for usage of writepages() to ensure it used only for swap purposes later. Avoid the splits if we're not being called for reclaim, even if they should in theory not happen. This makes the conditions easier to follow on shem_writepage(). Signed-off-by: Luis Chamberlain --- mm/shmem.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 1269482d0a5c..626eb1a0856c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1337,6 +1337,18 @@ static int shmem_writepage(struct page *page, struct= writeback_control *wbc) swp_entry_t swap; pgoff_t index; =20 + /* + * Our capabilities prevent regular writeback or sync from ever calling + * shmem_writepage; but a stacking filesystem might use ->writepage of + * its underlying filesystem, in which case tmpfs should write out to + * swap only in response to memory pressure, and not for the writeback + * threads or sync. + */ + if (!wbc->for_reclaim) { + WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ + goto redirty; + } + /* * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, @@ -1357,18 +1369,6 @@ static int shmem_writepage(struct page *page, struct= writeback_control *wbc) if (!total_swap_pages) goto redirty; =20 - /* - * Our capabilities prevent regular writeback or sync from ever calling - * shmem_writepage; but a stacking filesystem might use ->writepage of - * its underlying filesystem, in which case tmpfs should write out to - * swap only in response to memory pressure, and not for the writeback - * threads or sync. - */ - if (!wbc->for_reclaim) { - WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ - goto redirty; - } - /* * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC * value into swapfile.c, the only way we can correctly account for a --=20 2.39.1 From nobody Wed Sep 10 23:58:55 2025 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 DF931C61DA4 for ; Thu, 23 Feb 2023 02:44:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229468AbjBWCoo (ORCPT ); Wed, 22 Feb 2023 21:44:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233092AbjBWCo2 (ORCPT ); Wed, 22 Feb 2023 21:44:28 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 521C726586 for ; Wed, 22 Feb 2023 18:44:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=kiZfMZIUImYTT26kimjDxbkl+uiLHr4foE9jVeUonNw=; b=sn2iEaZxSxcZjr+lLEI+B9ZNfD ag2rd1ZA2IdkPMeYOuIbwUEge8ajYxG0X3vo+WuNHeE+62APWJEO/TQ+BEwvXPPTKO08otnIXs5Yn H5l53U6iDGmiMIOfWkieSJ1WN0Q/joKc8yr4grfgcI+SPDH7VlzINQV7egc10DDUTWILqPocCSj1o XDf9ihIRyG8DyPLMC9rit2JBlfpauaPYiOzpZfEAHTt+yslONZY1xDy5BC4wiV6lsokBvqZVjPYU9 M/s2WFxV74jDhTSMO3eHfu513os/QCbn/WTrt9tXhV/ao06RvY6cmntgbABP0/E6sLUKQ8wKo25Fz rCFNadYA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pV1ah-00EmNE-A6; Thu, 23 Feb 2023 02:44:15 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, dave@stgolabs.net, a.manzanares@samsung.com, yosryahmed@google.com, mcgrof@kernel.org, linux-kernel@vger.kernel.org Subject: [RFC v2 4/5] shmem: skip page split if we're not reclaiming Date: Wed, 22 Feb 2023 18:44:11 -0800 Message-Id: <20230223024412.3522465-5-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230223024412.3522465-1-mcgrof@kernel.org> References: <20230223024412.3522465-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Luis Chamberlain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In theory when info->flags & VM_LOCKED we should not be getting shem_writepage() called so we should be verifying this with a WARN_ON_ONCE(). Since we should not be swapping then best to ensure we also don't do the folio split earlier too. So just move the check early to avoid folio splits in case its a dubious call. We also have a similar early bail when !total_swap_pages so just move that earlier to avoid the possible folio split in the same situation. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 626eb1a0856c..a49b31d38627 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1349,6 +1349,12 @@ static int shmem_writepage(struct page *page, struct= writeback_control *wbc) goto redirty; } =20 + if (WARN_ON_ONCE(info->flags & VM_LOCKED)) + goto redirty; + + if (!total_swap_pages) + goto redirty; + /* * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, @@ -1364,10 +1370,6 @@ static int shmem_writepage(struct page *page, struct= writeback_control *wbc) } =20 index =3D folio->index; - if (info->flags & VM_LOCKED) - goto redirty; - if (!total_swap_pages) - goto redirty; =20 /* * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC --=20 2.39.1 From nobody Wed Sep 10 23:58:55 2025 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 BDB4AC64ED8 for ; Thu, 23 Feb 2023 02:44:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232476AbjBWCoe (ORCPT ); Wed, 22 Feb 2023 21:44:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjBWCo1 (ORCPT ); Wed, 22 Feb 2023 21:44:27 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC79E27D42 for ; Wed, 22 Feb 2023 18:44:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=wt4p/r9ymCcnGEqY87sHknk0U2eY7wcxJejPwvb0Ylo=; b=TrDfJdxVZcws00Hj9FGWVMTTCL LfcxodfOiVzFwmzhWMsfva7J5bIJGP2E7QgIGmqqfo0DOX04NoMNtbtNomLSuHaXzr0rQJ8uBJCPI llX7IWzs7lCHCrQRtkGRVdkzf6Wj9qUR7dRDXpKE8czsLpuellL8pHfWdJLVatBbwk7uRCU3k8RLP tH5N9MsQXVK3OPjAO672R0qfraGFGCGc5hq8V5GARE9bDCNt5yr0aFIUrtNVR8Txu6e8QOcsB1HBw OOyzrDPA419O0WyksHMacOMnEMt9/aWoLIr4DUUWS29M99FQ9baEFP4R0ZkbDchM4BVFJH0OqqpIR 7yC1G3EA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pV1ah-00EmNH-BN; Thu, 23 Feb 2023 02:44:15 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, dave@stgolabs.net, a.manzanares@samsung.com, yosryahmed@google.com, mcgrof@kernel.org, linux-kernel@vger.kernel.org Subject: [RFC v2 5/5] shmem: add support to ignore swap Date: Wed, 22 Feb 2023 18:44:12 -0800 Message-Id: <20230223024412.3522465-6-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230223024412.3522465-1-mcgrof@kernel.org> References: <20230223024412.3522465-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Luis Chamberlain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In doing experimentations with shmem having the option to avoid swap becomes a useful mechanism. One of the *raves* about brd over shmem is you can avoid swap, but that's not really a good reason to use brd if we can instead use shmem. Using brd has its own good reasons to exist, but just because "tmpfs" doesn't let you do that is not a great reason to avoid it if we can easily add support for it. I don't add support for reconfiguring incompatible options, but if we really wanted to we can add support for that. To avoid swap we use mapping_set_unevictable() upon inode creation, and put a WARN_ON_ONCE() stop-gap on writepages() for reclaim. Signed-off-by: Luis Chamberlain Acked-by: Christian Brauner --- Documentation/mm/unevictable-lru.rst | 2 ++ include/linux/shmem_fs.h | 1 + mm/shmem.c | 26 +++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Documentation/mm/unevictable-lru.rst b/Documentation/mm/unevic= table-lru.rst index 53e59433497a..d7e11f492289 100644 --- a/Documentation/mm/unevictable-lru.rst +++ b/Documentation/mm/unevictable-lru.rst @@ -44,6 +44,8 @@ The unevictable list addresses the following classes of u= nevictable pages: =20 * Those owned by ramfs. =20 + * Those owned by tmpfs with the noswap option. + * Those mapped into SHM_LOCK'd shared memory regions. =20 * Those mapped into VM_LOCKED [mlock()ed] VMAs. diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index d09d54be4ffd..98a7d53f6cc5 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -45,6 +45,7 @@ struct shmem_sb_info { kuid_t uid; /* Mount uid for root directory */ kgid_t gid; /* Mount gid for root directory */ bool full_inums; /* If i_ino should be uint or ino_t */ + bool noswap; /* ingores VM relcaim / swap requests */ ino_t next_ino; /* The next per-sb inode number to use */ ino_t __percpu *ino_batch; /* The next per-cpu inode number to use */ struct mempolicy *mpol; /* default memory policy for mappings */ diff --git a/mm/shmem.c b/mm/shmem.c index a49b31d38627..d2f34147fc66 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -116,10 +116,12 @@ struct shmem_options { bool full_inums; int huge; int seen; + bool noswap; #define SHMEM_SEEN_BLOCKS 1 #define SHMEM_SEEN_INODES 2 #define SHMEM_SEEN_HUGE 4 #define SHMEM_SEEN_INUMS 8 +#define SHMEM_SEEN_NOSWAP 16 }; =20 #ifdef CONFIG_TMPFS @@ -1334,6 +1336,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) struct address_space *mapping =3D folio->mapping; struct inode *inode =3D mapping->host; struct shmem_inode_info *info =3D SHMEM_I(inode); + struct shmem_sb_info *sbinfo =3D SHMEM_SB(inode->i_sb); swp_entry_t swap; pgoff_t index; =20 @@ -1349,7 +1352,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) goto redirty; } =20 - if (WARN_ON_ONCE(info->flags & VM_LOCKED)) + if (WARN_ON_ONCE((info->flags & VM_LOCKED) || sbinfo->noswap)) goto redirty; =20 if (!total_swap_pages) @@ -2374,6 +2377,8 @@ static struct inode *shmem_get_inode(struct mnt_idmap= *idmap, struct super_block shmem_set_inode_flags(inode, info->fsflags); INIT_LIST_HEAD(&info->shrinklist); INIT_LIST_HEAD(&info->swaplist); + if (sbinfo->noswap) + mapping_set_unevictable(inode->i_mapping); simple_xattrs_init(&info->xattrs); cache_no_acl(inode); mapping_set_large_folios(inode->i_mapping); @@ -3461,6 +3466,7 @@ enum shmem_param { Opt_uid, Opt_inode32, Opt_inode64, + Opt_noswap, }; =20 static const struct constant_table shmem_param_enums_huge[] =3D { @@ -3482,6 +3488,7 @@ const struct fs_parameter_spec shmem_fs_parameters[] = =3D { fsparam_u32 ("uid", Opt_uid), fsparam_flag ("inode32", Opt_inode32), fsparam_flag ("inode64", Opt_inode64), + fsparam_flag ("noswap", Opt_noswap), {} }; =20 @@ -3565,6 +3572,10 @@ static int shmem_parse_one(struct fs_context *fc, st= ruct fs_parameter *param) ctx->full_inums =3D true; ctx->seen |=3D SHMEM_SEEN_INUMS; break; + case Opt_noswap: + ctx->noswap =3D true; + ctx->seen |=3D SHMEM_SEEN_NOSWAP; + break; } return 0; =20 @@ -3663,6 +3674,14 @@ static int shmem_reconfigure(struct fs_context *fc) err =3D "Current inum too high to switch to 32-bit inums"; goto out; } + if ((ctx->seen & SHMEM_SEEN_NOSWAP) && ctx->noswap && !sbinfo->noswap) { + err =3D "Cannot disable swap on remount"; + goto out; + } + if (!(ctx->seen & SHMEM_SEEN_NOSWAP) && !ctx->noswap && sbinfo->noswap) { + err =3D "Cannot enable swap on remount if it was disabled on first mount= "; + goto out; + } =20 if (ctx->seen & SHMEM_SEEN_HUGE) sbinfo->huge =3D ctx->huge; @@ -3683,6 +3702,10 @@ static int shmem_reconfigure(struct fs_context *fc) sbinfo->mpol =3D ctx->mpol; /* transfers initial ref */ ctx->mpol =3D NULL; } + + if (ctx->noswap) + sbinfo->noswap =3D true; + raw_spin_unlock(&sbinfo->stat_lock); mpol_put(mpol); return 0; @@ -3780,6 +3803,7 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) ctx->inodes =3D shmem_default_max_inodes(); if (!(ctx->seen & SHMEM_SEEN_INUMS)) ctx->full_inums =3D IS_ENABLED(CONFIG_TMPFS_INODE64); + sbinfo->noswap =3D ctx->noswap; } else { sb->s_flags |=3D SB_NOUSER; } --=20 2.39.1