From nobody Sat Apr 11 06:58:09 2026 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 77A93C6FA8E for ; Thu, 2 Mar 2023 23:28:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229977AbjCBX2R (ORCPT ); Thu, 2 Mar 2023 18:28:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229728AbjCBX2L (ORCPT ); Thu, 2 Mar 2023 18:28:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 458872693 for ; Thu, 2 Mar 2023 15:28:10 -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=X3niQqWSDaRdijh0GPlKHY1yoysmmoQVBPzADYFTAAk=; b=A1T9i9l/KhB0qQZfQ43uw6v8kl DTCTRWDp8ClUN+kK4tmCnmWxFQZNAu7/KlswhmxIBSNnubcoAF/0v32hcIh7vsx/2fYebdrGuOFfD cAnCJAhSbnS1joVOILJsNws5NnKu+5mYSq1Eg2EeWdf/r2/gWG/2PiHtkicrXr4nv4g8KNvqyd+h5 zSb4BZm/8ekQ3nBFaKCQSpzvzbNAJor5LMWh0xvyjEChMKRIwSW6QtvSbE5HH6mk+eiwxMNN1wRYL NN7yYd8Xgu8+6Yqm6vtCggq8UsMK7X8LuLapwpCc3bG7p2c7yZG4p+iTMZohqzaupylW/idKcbw0L a/8QUp7w==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXsL9-003j3I-3W; Thu, 02 Mar 2023 23:27:59 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 1/6] shmem: remove check for folio lock on writepage() Date: Thu, 2 Mar 2023 15:27:53 -0800 Message-Id: <20230302232758.888157-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230302232758.888157-1-mcgrof@kernel.org> References: <20230302232758.888157-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 Acked-by: David Hildenbrand Reviewed-by: Christian Brauner --- mm/shmem.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index 1af85259b6fc..7fff1a3af092 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1354,7 +1354,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 Sat Apr 11 06:58:09 2026 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 04F7FC6FA8E for ; Thu, 2 Mar 2023 23:28:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230009AbjCBX2Y (ORCPT ); Thu, 2 Mar 2023 18:28:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjCBX2L (ORCPT ); Thu, 2 Mar 2023 18:28:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26C741994 for ; Thu, 2 Mar 2023 15:28:09 -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=Fz4rjXU4pv+JoX8GiRxuwd31ga6ygJiOfjuBNAA23TI=; b=TFaKHOzs5zZrjEDnSHRp+DurxS EZpK7uL5ROEanWcdSjADz8IiT9IvqFH7WDeg96aXeb7m3m2MRMicspkiYt/i4MJb32izDxDrcXfkR Ds12Y4SSHzRZi2EavEPEHkitO37g6qnc2bqD93P2Sq+ov2YZdiNMiwq8LHIGSBEKfaxEzkuVTb/Li eyQolkb0Tm/6Zpnk/sRhTSjX8E0AO5dabHME4Z4quDrlZlNbzf2m9AK6EB8FSAvjOQwlwch64nCYm vap3IRDQrZhqDi0Vi31lW7yLoGiYQQ7Tck2NBz7gPOMSSu22zuLA/5dPm8V7ADT8iujNfWaqSicNC 2GU4CIiQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXsL9-003j3K-61; Thu, 02 Mar 2023 23:27:59 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 2/6] shmem: set shmem_writepage() variables early Date: Thu, 2 Mar 2023 15:27:54 -0800 Message-Id: <20230302232758.888157-3-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230302232758.888157-1-mcgrof@kernel.org> References: <20230302232758.888157-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 Acked-by: David Hildenbrand Reviewed-by: Christian Brauner --- mm/shmem.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 7fff1a3af092..2b9ff585a553 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1334,9 +1334,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 @@ -1354,10 +1354,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 Sat Apr 11 06:58:09 2026 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 19E2FC678D4 for ; Thu, 2 Mar 2023 23:28:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230053AbjCBX21 (ORCPT ); Thu, 2 Mar 2023 18:28:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229936AbjCBX2L (ORCPT ); Thu, 2 Mar 2023 18:28:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A55C125BF for ; Thu, 2 Mar 2023 15:28:09 -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=Hpr/mKVZzGGQqVrubmxhAsrFv4PlfL2VXUxF3A6d69A=; b=hKi5B7rksyIKLYpIpojH3COwse 6yVboytacnRm0DNMUh5BKMfZwbdWYuNUklJLKFbD4wjEle33GCTpvZJdXbQPCk3hIf0DFStd9tL+t jq4IAgiksjYqh8v8mIireTRU6LNVj2zfYQ9mtUda+GZCTq6XHxJgjkKPxVVTVA6t/x9znl5F31erd JzgAsoy7GHNSk6QSSzDOJz7LMMAQ1DbbEpT234nu/jFOtmuCwstY5Gm50cCZVHGdz0FpM/qmB87KL Z3RJNpaMrZyQyQJFwvIzGInyQpaIjBIYAAKuhUc2yvfiaKNxUBSuS7LOizfLNgOD6cguhCKiDt7p1 C3zX/D7w==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXsL9-003j3M-8Z; Thu, 02 Mar 2023 23:27:59 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 3/6] shmem: move reclaim check early on writepages() Date: Thu, 2 Mar 2023 15:27:55 -0800 Message-Id: <20230302232758.888157-4-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230302232758.888157-1-mcgrof@kernel.org> References: <20230302232758.888157-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 Acked-by: David Hildenbrand Reviewed-by: Christian Brauner Reviewed-by: Yosry Ahmed --- mm/shmem.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 2b9ff585a553..a5a6da51087e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1340,6 +1340,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, @@ -1360,18 +1372,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 Sat Apr 11 06:58:09 2026 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 66D3BC6FA8E for ; Thu, 2 Mar 2023 23:28:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229962AbjCBX2N (ORCPT ); Thu, 2 Mar 2023 18:28:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229458AbjCBX2L (ORCPT ); Thu, 2 Mar 2023 18:28:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAC5212598 for ; Thu, 2 Mar 2023 15:28:08 -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=qG5U0uWPFmEbIzb8qGwDd3VP74J/BRogOkPK92T1eiw=; b=4Mn4cSCAgRAwfc+ofEk7syFvU4 lgI+PixYXC7I6rMVPFt85Y4M8oB/dWwp5q6YY7j/3hgM2uh2P7ZtiZ22ho1zzeWefT3wd+huLJpxd mvGW2hfn1RtW61xAcyyeVfH9HN8JlRi29H3roFcGHcEC+b/G8QXX5LxaV7GoFHadburRxTnA/MMf9 +OPgxbeXJdcEcdQlRLQ0xbxT//SyOp14A7a7SO3dbpZWiKOO2+0j1sd9qtehWMvQZKVPoRxZhSOC9 XOO9h5XjMa5XF91JikH0HlR1tccdHYPW2mUwbxqK+PvPIFK+WTgqQha6vzzE6HKxjq0hmzN8pLejD /ppNx6LQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXsL9-003j3O-BB; Thu, 02 Mar 2023 23:27:59 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 4/6] shmem: skip page split if we're not reclaiming Date: Thu, 2 Mar 2023 15:27:56 -0800 Message-Id: <20230302232758.888157-5-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230302232758.888157-1-mcgrof@kernel.org> References: <20230302232758.888157-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 Acked-by: David Hildenbrand Reviewed-by: Christian Brauner Reviewed-by: Yosry Ahmed --- mm/shmem.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index a5a6da51087e..6006dbb7dbcb 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1352,6 +1352,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, @@ -1367,10 +1373,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 Sat Apr 11 06:58:09 2026 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 021B5C7EE37 for ; Thu, 2 Mar 2023 23:28:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229960AbjCBX2O (ORCPT ); Thu, 2 Mar 2023 18:28:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbjCBX2L (ORCPT ); Thu, 2 Mar 2023 18:28:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA7A111679 for ; Thu, 2 Mar 2023 15:28:08 -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=skC96Wt8xxhIMwjo9L+gt44Hnic6JkSNTvyHUyToP4E=; b=K/eQSla3MTHn7M0Rb9rcMYDVlA e37q4SAkKhrm7U/RKwrVlRksYjpLSQ7cyFhGajl9HtpsHsUn6N0cRNl0Qv09F6TFF8NJALHGbqV9Y MKle2ZzppMMuer2n5cLdK6TEumXDYu//CEasZglQ4GBDSIpbcgf9zrLrspyNLR57fU9TjiN84RzjX DJKtgrXnUJH4f9ErONxxluGJkBZMTUgr6znqZikMJYzDqfZ43mgtSToV2ADG2RhlMqjD5DNDGVY4q QwjsemBTEGNoJzgAOOd9LwEPm7jsyyl3cLget4EvACCEKtoo27Ivb7ie0zI+m3cji7ZAqVl11QKp8 lTov+nwA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXsL9-003j3Q-Dq; Thu, 02 Mar 2023 23:27:59 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 5/6] shmem: update documentation Date: Thu, 2 Mar 2023 15:27:57 -0800 Message-Id: <20230302232758.888157-6-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230302232758.888157-1-mcgrof@kernel.org> References: <20230302232758.888157-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" Update the docs to reflect a bit better why some folks prefer tmpfs over ramfs and clarify a bit more about the difference between brd ramdisks. Signed-off-by: Luis Chamberlain Reviewed-by: Christian Brauner Reviewed-by: David Hildenbrand --- Documentation/filesystems/tmpfs.rst | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystem= s/tmpfs.rst index 0408c245785e..e77ebdacadd0 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -13,14 +13,25 @@ everything stored therein is lost. =20 tmpfs puts everything into the kernel internal caches and grows and shrinks to accommodate the files it contains and is able to swap -unneeded pages out to swap space. It has maximum size limits which can -be adjusted on the fly via 'mount -o remount ...' - -If you compare it to ramfs (which was the template to create tmpfs) -you gain swapping and limit checking. Another similar thing is the RAM -disk (/dev/ram*), which simulates a fixed size hard disk in physical -RAM, where you have to create an ordinary filesystem on top. Ramdisks -cannot swap and you do not have the possibility to resize them. +unneeded pages out to swap space. + +tmpfs extends ramfs with a few userspace configurable options listed and +explained further below, some of which can be reconfigured dynamically on = the +fly using a remount ('mount -o remount ...') of the filesystem. A tmpfs +filesystem can be resized but it cannot be resized to a size below its cur= rent +usage. tmpfs also supports POSIX ACLs, and extended attributes for the +trusted.* and security.* namespaces. ramfs does not use swap and you cannot +modify any parameter for a ramfs filesystem. The size limit of a ramfs +filesystem is how much memory you have available, and so care must be take= n if +used so to not run out of memory. + +An alternative to tmpfs and ramfs is to use brd to create RAM disks +(/dev/ram*), which allows you to simulate a block device disk in physical = RAM. +To write data you would just then need to create an regular filesystem on = top +this ramdisk. As with ramfs, brd ramdisks cannot swap. brd ramdisks are al= so +configured in size at initialization and you cannot dynamically resize the= m. +Contrary to brd ramdisks, tmpfs has its own filesystem, it does not rely o= n the +block layer at all. =20 Since tmpfs lives completely in the page cache and on swap, all tmpfs pages will be shown as "Shmem" in /proc/meminfo and "Shared" in --=20 2.39.1 From nobody Sat Apr 11 06:58:09 2026 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 4087BC678D4 for ; Thu, 2 Mar 2023 23:28:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230002AbjCBX2V (ORCPT ); Thu, 2 Mar 2023 18:28:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229933AbjCBX2L (ORCPT ); Thu, 2 Mar 2023 18:28:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA90011E94 for ; Thu, 2 Mar 2023 15:28:08 -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=aVGWGg28pehMWy6o19l/TsZGWBXxeVmCZNXdGLwnZq4=; b=TlDiW9om0AtjuznlGSshw1b4sC ghR2WXi7V+f5PZ1jG/o45vCIdVVPAervPRo0lvMQJdxtcovf9IN60XRQyXJ1TOSoZDYvbKBXbI/Wi kuqHDamMQIx8Oh2hC1RmvcgDdngdxl8vCJelPxKnPdI6sZqCLJP7oKLTX7HCNfOVjJ+oJWhrHo56l jIjCnoGvXoEsBAierza5C+Z0oTy0fXnLWb9sfdDC4drisylwEXR0+Vwcjgn7AYvnMbzbxgtrQjiQg eoDfsRFufkoQ6AunUMT7LpC6d+FMUW/ESv6NhPIS/cqtmkwozQFqphCo+anHmfRTZfKqu4eV620Lr q5ku9HZg==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXsL9-003j3S-Gf; Thu, 02 Mar 2023 23:27:59 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org Cc: linux-mm@kvack.org, p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 6/6] shmem: add support to ignore swap Date: Thu, 2 Mar 2023 15:27:58 -0800 Message-Id: <20230302232758.888157-7-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230302232758.888157-1-mcgrof@kernel.org> References: <20230302232758.888157-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. Acked-by: Christian Brauner Signed-off-by: Luis Chamberlain Reviewed-by: Christian Brauner --- Documentation/filesystems/tmpfs.rst | 9 ++++++--- Documentation/mm/unevictable-lru.rst | 2 ++ include/linux/shmem_fs.h | 1 + mm/shmem.c | 28 +++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystem= s/tmpfs.rst index e77ebdacadd0..551b621f34d9 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -13,7 +13,8 @@ everything stored therein is lost. =20 tmpfs puts everything into the kernel internal caches and grows and shrinks to accommodate the files it contains and is able to swap -unneeded pages out to swap space. +unneeded pages out to swap space, if swap was enabled for the tmpfs +filesystem. =20 tmpfs extends ramfs with a few userspace configurable options listed and explained further below, some of which can be reconfigured dynamically on = the @@ -33,8 +34,8 @@ configured in size at initialization and you cannot dynam= ically resize them. Contrary to brd ramdisks, tmpfs has its own filesystem, it does not rely o= n the block layer at all. =20 -Since tmpfs lives completely in the page cache and on swap, all tmpfs -pages will be shown as "Shmem" in /proc/meminfo and "Shared" in +Since tmpfs lives completely in the page cache and on optionally on swap, +all tmpfs pages will be shown as "Shmem" in /proc/meminfo and "Shared" in free(1). Notice that these counters also include shared memory (shmem, see ipcs(1)). The most reliable way to get the count is using df(1) and du(1). @@ -83,6 +84,8 @@ nr_inodes The maximum number of inodes for this instance= . The default is half of the number of your physical RAM pages, or (on a machine with highmem) the number of lowmem RAM pages, whichever is the lower. +noswap Disables swap. Remounts must respect the original settings. + By default swap is enabled. =3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 These parameters accept a suffix k, m or g for kilo, mega and giga and diff --git a/Documentation/mm/unevictable-lru.rst b/Documentation/mm/unevic= table-lru.rst index 92ac5dca420c..3cdcbb6e00a0 100644 --- a/Documentation/mm/unevictable-lru.rst +++ b/Documentation/mm/unevictable-lru.rst @@ -42,6 +42,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 103d1000a5a2..21989d4f8cbe 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 6006dbb7dbcb..cd36cb3d974c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -119,10 +119,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 @@ -1337,6 +1339,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 @@ -1352,7 +1355,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) @@ -2489,6 +2492,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); @@ -3576,6 +3581,7 @@ enum shmem_param { Opt_uid, Opt_inode32, Opt_inode64, + Opt_noswap, }; =20 static const struct constant_table shmem_param_enums_huge[] =3D { @@ -3597,6 +3603,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 @@ -3680,6 +3687,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 @@ -3778,6 +3789,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; @@ -3798,6 +3817,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; @@ -3852,6 +3875,8 @@ static int shmem_show_options(struct seq_file *seq, s= truct dentry *root) seq_printf(seq, ",huge=3D%s", shmem_format_huge(sbinfo->huge)); #endif shmem_show_mpol(seq, sbinfo->mpol); + if (sbinfo->noswap) + seq_printf(seq, ",noswap"); return 0; } =20 @@ -3895,6 +3920,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