From nobody Wed Dec 17 04:37:12 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 5BD37C77B76 for ; Fri, 21 Apr 2023 21:44:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233618AbjDUVoW (ORCPT ); Fri, 21 Apr 2023 17:44:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229748AbjDUVoS (ORCPT ); Fri, 21 Apr 2023 17:44:18 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E26E61FC7; Fri, 21 Apr 2023 14:44:14 -0700 (PDT) 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=HAZkfNRruAgARTe618ACsQ6wCYRRSd9EZooPKA+qgbw=; b=DnGs3ejEo/+K910m27ugy2zmJh qb5mcLlEfR2wdQNE/p1//CuFDbtuczZKd7j27Gu+GCzFIVBmDmCr5B7yxLhtPcDngNB4k80THHDuR w2DMyrzuDU9msgM7Zy+oghoVKusNkUcBRoJC/pAS7Cc8RxdQEaVjJ+Bd2tc6Z6+C7gpfmYibKo708 Vi5Tb7IXkNa4kvn4N3d3BddjBa5cCKcG9E33SvWZ4GkM7ZwxcmNuAPg77qD1HGAf/ilG221q1rmJ5 9ejkkgHgonznxoi20BOMaCnyIcdksGRblL0lJg1XSWnZUtylSqq5ykxsRsCRkUyoDucbBY5Vd4B0h p73Bmwcw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btoi-1l; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 1/8] shmem: replace BLOCKS_PER_PAGE with PAGE_SECTORS Date: Fri, 21 Apr 2023 14:43:53 -0700 Message-Id: <20230421214400.2836131-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" Instead of having our own macro use the generic PAGE_SECTORS. It also makes it clearer what we are trying to compute here on the inode->i_blocks. We get the inode size by as define din __inode_get_bytes() by: (inode->i_blocks << SECTOR_SHIFT) + inode->i_bytes This produces no functional changes. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b5d102a2a766..5bf92d571092 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -86,7 +86,6 @@ static struct vfsmount *shm_mnt; =20 #include "internal.h" =20 -#define BLOCKS_PER_PAGE (PAGE_SIZE/512) #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) =20 /* Pretend that each entry is of this size in directory's i_size */ @@ -363,7 +362,7 @@ static void shmem_recalc_inode(struct inode *inode) freed =3D info->alloced - info->swapped - inode->i_mapping->nrpages; if (freed > 0) { info->alloced -=3D freed; - inode->i_blocks -=3D freed * BLOCKS_PER_PAGE; + inode->i_blocks -=3D freed * PAGE_SECTORS; shmem_inode_unacct_blocks(inode, freed); } } @@ -381,7 +380,7 @@ bool shmem_charge(struct inode *inode, long pages) =20 spin_lock_irqsave(&info->lock, flags); info->alloced +=3D pages; - inode->i_blocks +=3D pages * BLOCKS_PER_PAGE; + inode->i_blocks +=3D pages * PAGE_SECTORS; shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); =20 @@ -397,7 +396,7 @@ void shmem_uncharge(struct inode *inode, long pages) =20 spin_lock_irqsave(&info->lock, flags); info->alloced -=3D pages; - inode->i_blocks -=3D pages * BLOCKS_PER_PAGE; + inode->i_blocks -=3D pages * PAGE_SECTORS; shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); =20 @@ -2002,7 +2001,7 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, =20 spin_lock_irq(&info->lock); info->alloced +=3D folio_nr_pages(folio); - inode->i_blocks +=3D (blkcnt_t)BLOCKS_PER_PAGE << folio_order(folio); + inode->i_blocks +=3D (blkcnt_t) PAGE_SECTORS << folio_order(folio); shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); alloced =3D true; @@ -2659,7 +2658,7 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, =20 spin_lock_irq(&info->lock); info->alloced++; - inode->i_blocks +=3D BLOCKS_PER_PAGE; + inode->i_blocks +=3D PAGE_SECTORS; shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); =20 --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 12BC1C7EE24 for ; Fri, 21 Apr 2023 21:44:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232808AbjDUVo3 (ORCPT ); Fri, 21 Apr 2023 17:44:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233393AbjDUVoS (ORCPT ); Fri, 21 Apr 2023 17:44:18 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E25F21FC6; Fri, 21 Apr 2023 14:44:14 -0700 (PDT) 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=Pwtny366FbTbrb7vPgMXDOxfQYz6bsR4vwvBIbUUYzg=; b=l1UKRS+6uYC4EXAjJgBrQTAp5j xsYvwCnnNonL+zWZMEy71A3Ta6km8F6dSzKQvEcpRo5TV7XBXl9OERI0fsz5C845ifOI1VFCEHRc1 Zre0qUD7V89WFxot/x0Dllx9YnFxYQ7Yz7zDqe598RN0r0tdnLI/4pmF0Tvd/CygY81NUfyF7TcfO R1q/JTspIPOKlYq6NoZibJ4hLTLlDqaLHYjGQRhRwv9u7o7cGbziZ6ambxSZLU25fgMv7Z5We7Z9g 4m2TriUxFygO3OJdPvzDe0pBPp5xw1iklrlut7//xUpzOnTPbemmyDvmuxTGhDtulG3XxP+CyxfK9 9RMFdwJQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btok-1t; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 2/8] shmem: convert to use folio_test_hwpoison() Date: Fri, 21 Apr 2023 14:43:54 -0700 Message-Id: <20230421214400.2836131-3-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" The PageHWPoison() call can be converted over to the respective folio call folio_test_hwpoison(). This introduces no functional changes. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 5bf92d571092..6f117c3cbe89 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3483,7 +3483,7 @@ static const char *shmem_get_link(struct dentry *dent= ry, folio =3D filemap_get_folio(inode->i_mapping, 0); if (IS_ERR(folio)) return ERR_PTR(-ECHILD); - if (PageHWPoison(folio_page(folio, 0)) || + if (folio_test_hwpoison(folio) || !folio_test_uptodate(folio)) { folio_put(folio); return ERR_PTR(-ECHILD); @@ -3494,7 +3494,7 @@ static const char *shmem_get_link(struct dentry *dent= ry, return ERR_PTR(error); if (!folio) return ERR_PTR(-ECHILD); - if (PageHWPoison(folio_page(folio, 0))) { + if (folio_test_hwpoison(folio)) { folio_unlock(folio); folio_put(folio); return ERR_PTR(-ECHILD); @@ -4672,7 +4672,7 @@ struct page *shmem_read_mapping_page_gfp(struct addre= ss_space *mapping, return &folio->page; =20 page =3D folio_file_page(folio, index); - if (PageHWPoison(page)) { + if (folio_test_hwpoison(folio)) { folio_put(folio); return ERR_PTR(-EIO); } --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 0C4FDC7618E for ; Fri, 21 Apr 2023 21:44:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233739AbjDUVow (ORCPT ); Fri, 21 Apr 2023 17:44:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233609AbjDUVoT (ORCPT ); Fri, 21 Apr 2023 17:44:19 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96E602700; Fri, 21 Apr 2023 14:44:15 -0700 (PDT) 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=TaEL6wJEWmG7v/Kj+xMCxG7l/Tv1LU1WrJwnB3EGy3E=; b=N942EdhFvz8Gq8X0vru14SS2Tz NSMMc3ogGzPG2/igUrApVOD7nvcB5K/4UI28nLVYOwAB52r3+fcJXP9APHpwXmOM3q6qrw2BgTt/j caFXw1rBuZp75667SgZQedCUy2WjgaS5ME72NMg2F8DmvIV9HV4lZYCengkZYqa2jjMLpEBGuGQm3 cAzPOhEk2Y75zB3VUvyjKzHud3xR7zENlmf188YTgv99Mkb7ucqaY9iy2HJd2LE35twbd41deBG5N HX2mbF5Ndbygf6Towllz/Gv1UT754OGwbCZbye8ao31rI5EjMABW1FJPa80ME9BC2gfYF+I0GujRk iyqZUPTw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btom-22; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 3/8] shmem: account for high order folios Date: Fri, 21 Apr 2023 14:43:55 -0700 Message-Id: <20230421214400.2836131-4-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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 uses the shem_info_inode alloced, swapped to account for allocated pages and swapped pages. In preparation for high order folios adjust the accounting to use folio_nr_pages(). This should produce no functional changes yet as higher order folios are not yet used or supported in shmem. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 6f117c3cbe89..d76e86ff356e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -806,15 +806,15 @@ unsigned long shmem_partial_swap_usage(struct address= _space *mapping, pgoff_t start, pgoff_t end) { XA_STATE(xas, &mapping->i_pages, start); - struct page *page; + struct folio *folio; unsigned long swapped =3D 0; =20 rcu_read_lock(); - xas_for_each(&xas, page, end - 1) { - if (xas_retry(&xas, page)) + xas_for_each(&xas, folio, end - 1) { + if (xas_retry(&xas, folio)) continue; - if (xa_is_value(page)) - swapped++; + if (xa_is_value(folio)) + swapped+=3D(folio_nr_pages(folio)); =20 if (need_resched()) { xas_pause(&xas); @@ -941,10 +941,15 @@ static void shmem_undo_range(struct inode *inode, lof= f_t lstart, loff_t lend, folio =3D fbatch.folios[i]; =20 if (xa_is_value(folio)) { + long swaps_freed =3D 0; if (unfalloc) continue; - nr_swaps_freed +=3D !shmem_free_swap(mapping, - indices[i], folio); + swaps_freed =3D folio_nr_pages(folio); + if (!shmem_free_swap(mapping, indices[i], folio)) { + if (swaps_freed > 1) + pr_warn("swaps freed > 1 -- %lu\n", swaps_freed); + nr_swaps_freed +=3D swaps_freed; + } continue; } =20 @@ -1010,14 +1015,18 @@ static void shmem_undo_range(struct inode *inode, l= off_t lstart, loff_t lend, folio =3D fbatch.folios[i]; =20 if (xa_is_value(folio)) { + long swaps_freed =3D 0; if (unfalloc) continue; + swaps_freed =3D folio_nr_pages(folio); if (shmem_free_swap(mapping, indices[i], folio)) { /* Swap was replaced by page: retry */ index =3D indices[i]; break; } - nr_swaps_freed++; + if (swaps_freed > 1) + pr_warn("swaps freed > 1 -- %lu\n", swaps_freed); + nr_swaps_freed+=3Dswaps_freed; continue; } =20 @@ -1448,7 +1457,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) NULL) =3D=3D 0) { spin_lock_irq(&info->lock); shmem_recalc_inode(inode); - info->swapped++; + info->swapped+=3Dfolio_nr_pages(folio); spin_unlock_irq(&info->lock); =20 swap_shmem_alloc(swap); @@ -1723,6 +1732,7 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, struct shmem_inode_info *info =3D SHMEM_I(inode); swp_entry_t swapin_error; void *old; + long num_swap_pages; =20 swapin_error =3D make_swapin_error_entry(); old =3D xa_cmpxchg_irq(&mapping->i_pages, index, @@ -1732,6 +1742,7 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, return; =20 folio_wait_writeback(folio); + num_swap_pages =3D folio_nr_pages(folio); delete_from_swap_cache(folio); spin_lock_irq(&info->lock); /* @@ -1739,8 +1750,8 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, * be 0 when inode is released and thus trigger WARN_ON(inode->i_blocks) = in * shmem_evict_inode. */ - info->alloced--; - info->swapped--; + info->alloced-=3Dnum_swap_pages; + info->swapped-=3Dnum_swap_pages; shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); swap_free(swap); @@ -1830,7 +1841,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, goto failed; =20 spin_lock_irq(&info->lock); - info->swapped--; + info->swapped-=3D folio_nr_pages(folio); shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); =20 @@ -2657,8 +2668,8 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, goto out_delete_from_cache; =20 spin_lock_irq(&info->lock); - info->alloced++; - inode->i_blocks +=3D PAGE_SECTORS; + info->alloced +=3D folio_nr_pages(folio); + inode->i_blocks +=3D PAGE_SECTORS << folio_order(folio); shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); =20 --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 31609C77B76 for ; Fri, 21 Apr 2023 21:44:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233725AbjDUVok (ORCPT ); Fri, 21 Apr 2023 17:44:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233527AbjDUVoT (ORCPT ); Fri, 21 Apr 2023 17:44:19 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E19BC1BC3; Fri, 21 Apr 2023 14:44:14 -0700 (PDT) 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=nny3k9GHvISmRebv/wiZT4olnvrgRU1LTkzNA8FqTDM=; b=4Ut9M+mnDbLVzS5mn357NtXNVg iW973AulcqLzyWup7ygr0muRVGEjzVtqC6kqytGxva5A/sIYSQFeBlshjjpIhF6OYCvi05IojwxWE tsKCO6qUCenqfN9MsAZAdEWGPc3v0HjGDax4M6XvZmaBXtzA5v2C3KaFqloimB+8x6u/npTruKLQs 036sfUHB5iNErVxwNzS974BCSyU1Ar0fEOvtLbwggh51AghvUuMZYxAGZfc5IZ52Xmohp9mLGlsCy TaULQnEPNlB7MAFVSLGTPIcEwbgWNxO2AlGc26ENLqMM/u81jGPh5M591B/wApNppCBuEnfc313kb pACYjTwA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btoo-2A; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 4/8] shmem: add helpers to get block size Date: Fri, 21 Apr 2023 14:43:56 -0700 Message-Id: <20230421214400.2836131-5-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" Stuff the block size as a struct shmem_sb_info member when CONFIG_TMPFS is enabled, but keep the current static value for now, and use helpers to get the blocksize. This will make the subsequent change easier to read. The static value for block size of PAGE_SIZE is used currently. The struct super_block s_blocksize_bits represents the blocksize in power of two, since the block size is always PAGE_SIZE this is PAGE_SHIFT today, but to help make this a bit more apt to scale we can use __ffs() for it instead. This commit introduces no functional changes other than __ffs() for the s_blocksize_bits and extending the struct shmem_sb_info with the blocksize. Signed-off-by: Luis Chamberlain --- include/linux/shmem_fs.h | 3 +++ mm/shmem.c | 24 +++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 9029abd29b1c..89e471fcde1d 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -36,6 +36,9 @@ struct shmem_inode_info { #define SHMEM_FL_INHERITED (FS_NODUMP_FL | FS_NOATIME_FL) =20 struct shmem_sb_info { +#ifdef CONFIG_TMPFS + u64 blocksize; +#endif unsigned long max_blocks; /* How many blocks are allowed */ struct percpu_counter used_blocks; /* How many are allocated */ unsigned long max_inodes; /* How many inodes are allowed */ diff --git a/mm/shmem.c b/mm/shmem.c index d76e86ff356e..162384b58a5c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -125,7 +125,17 @@ struct shmem_options { #define SHMEM_SEEN_NOSWAP 16 }; =20 +static u64 shmem_default_bsize(void) +{ + return PAGE_SIZE; +} + #ifdef CONFIG_TMPFS +static u64 shmem_sb_blocksize(struct shmem_sb_info *sbinfo) +{ + return sbinfo->blocksize; +} + static unsigned long shmem_default_max_blocks(void) { return totalram_pages() / 2; @@ -137,6 +147,12 @@ static unsigned long shmem_default_max_inodes(void) =20 return min(nr_pages - totalhigh_pages(), nr_pages / 2); } +#else +static u64 shmem_sb_blocksize(struct shmem_sb_info *sbinfo) +{ + return shmem_default_bsize(); +} + #endif =20 static int shmem_swapin_folio(struct inode *inode, pgoff_t index, @@ -3190,7 +3206,7 @@ static int shmem_statfs(struct dentry *dentry, struct= kstatfs *buf) struct shmem_sb_info *sbinfo =3D SHMEM_SB(dentry->d_sb); =20 buf->f_type =3D TMPFS_MAGIC; - buf->f_bsize =3D PAGE_SIZE; + buf->f_bsize =3D shmem_sb_blocksize(sbinfo); buf->f_namelen =3D NAME_MAX; if (sbinfo->max_blocks) { buf->f_blocks =3D sbinfo->max_blocks; @@ -4100,6 +4116,7 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) } sb->s_export_op =3D &shmem_export_ops; sb->s_flags |=3D SB_NOSEC | SB_I_VERSION; + sbinfo->blocksize =3D shmem_default_bsize(); #else sb->s_flags |=3D SB_NOUSER; #endif @@ -4125,8 +4142,9 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) INIT_LIST_HEAD(&sbinfo->shrinklist); =20 sb->s_maxbytes =3D MAX_LFS_FILESIZE; - sb->s_blocksize =3D PAGE_SIZE; - sb->s_blocksize_bits =3D PAGE_SHIFT; + sb->s_blocksize =3D shmem_sb_blocksize(sbinfo); + sb->s_blocksize_bits =3D __ffs(sb->s_blocksize); + WARN_ON_ONCE(sb->s_blocksize_bits !=3D PAGE_SHIFT); sb->s_magic =3D TMPFS_MAGIC; sb->s_op =3D &shmem_ops; sb->s_time_gran =3D 1; --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 F21F6C77B78 for ; Fri, 21 Apr 2023 21:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233716AbjDUVod (ORCPT ); Fri, 21 Apr 2023 17:44:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233520AbjDUVoS (ORCPT ); Fri, 21 Apr 2023 17:44:18 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07EFB1FD6; Fri, 21 Apr 2023 14:44:14 -0700 (PDT) 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=qZHhB8oJRc4fNtBxtSdci4h920oOcANZrFW/y3BBdxc=; b=qwUcweMcO4pJRMTax4NLy0VSpN KXR/HutcTxUAf1AkUOpohVQt9D1HYkGbVjqLunH8jowN6HHKGq80LixliXi2Hao7TSPjFNtf1/IzO 7hDxeIEAYf+PFCY6rZWFQl4IGZWfP0KyQ5vipRU6TqfGLflWy21DT0TJE7R0QUnYRa/WxByUTqiII BuHGKTgRoWB8MbgfuNMvCOuGnM+sX/c7GBDhVEJw9LJtgPKEyst4dKWFE7LdUCITswqVJpNqKk8W9 slkIqwAA4DulGimTxOQmf5WZEzZP8nrP9FTqQ4fOvBM8gc8Qmz0rtULu5SAg6nZRtVW89WyRdSDAo le8pzh1w==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btoq-2J; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 5/8] shmem: account for larger blocks sizes for shmem_default_max_blocks() Date: Fri, 21 Apr 2023 14:43:57 -0700 Message-Id: <20230421214400.2836131-6-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" If we end up supporting a larger block size than PAGE_SIZE the calculations= in shmem_default_max_blocks() need to be modified to take into account the fact that multiple pages would be required for a single block. Today the max number of blocks is computed based on the fact that we will by default use half of the available memory and each block is of PAGE_SIZE. And so we end up with: totalram_pages() / 2 That's becauase blocksize =3D=3D PAGE_SIZE. When blocksize > PAGE_SIZE we need to consider how how many blocks fit into totalram_pages() first, then just divide by 2. This ends up being: totalram_pages * PAGE_SIZE / blocksize / 2 totalram_pages * 2^PAGE_SHIFT / 2^bbits / 2 totalram_pages * 2^(PAGE_SHIFT - bbits - 1) We know bbits > PAGE_SHIFT so we'll end up with a negative power of 2. 2^(-some_val). We can factor the -1 out by changing this to a division of power of 2 and flipping the values for the signs: -1 * (PAGE_SHIFT - bbits -1) =3D (-PAGE_SHIFT + bbits + 1) =3D (bbits - PAGE_SHIFT + 1) And so we end up with: totalram_pages / 2^(bbits - PAGE_SHIFT + 1) We use __ffs(blocksize) as this computation is needed early on before any inode is established. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 162384b58a5c..b83596467706 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -136,9 +136,11 @@ static u64 shmem_sb_blocksize(struct shmem_sb_info *sb= info) return sbinfo->blocksize; } =20 -static unsigned long shmem_default_max_blocks(void) +static unsigned long shmem_default_max_blocks(u64 blocksize) { - return totalram_pages() / 2; + if (blocksize =3D=3D shmem_default_bsize()) + return totalram_pages() / 2; + return totalram_pages() >> (__ffs(blocksize) - PAGE_SHIFT + 1); } =20 static unsigned long shmem_default_max_inodes(void) @@ -3816,7 +3818,7 @@ static int shmem_parse_one(struct fs_context *fc, str= uct fs_parameter *param) } if (*rest) goto bad_value; - ctx->blocks =3D DIV_ROUND_UP(size, PAGE_SIZE); + ctx->blocks =3D DIV_ROUND_UP(size, shmem_default_bsize()); ctx->seen |=3D SHMEM_SEEN_BLOCKS; break; case Opt_nr_blocks: @@ -4023,7 +4025,7 @@ static int shmem_show_options(struct seq_file *seq, s= truct dentry *root) { struct shmem_sb_info *sbinfo =3D SHMEM_SB(root->d_sb); =20 - if (sbinfo->max_blocks !=3D shmem_default_max_blocks()) + if (sbinfo->max_blocks !=3D shmem_default_max_blocks(shmem_default_bsize(= ))) seq_printf(seq, ",size=3D%luk", sbinfo->max_blocks << (PAGE_SHIFT - 10)); if (sbinfo->max_inodes !=3D shmem_default_max_inodes()) @@ -4105,7 +4107,7 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) */ if (!(sb->s_flags & SB_KERNMOUNT)) { if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) - ctx->blocks =3D shmem_default_max_blocks(); + ctx->blocks =3D shmem_default_max_blocks(shmem_default_bsize()); if (!(ctx->seen & SHMEM_SEEN_INODES)) ctx->inodes =3D shmem_default_max_inodes(); if (!(ctx->seen & SHMEM_SEEN_INUMS)) --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 A5B76C7618E for ; Fri, 21 Apr 2023 21:44:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233729AbjDUVon (ORCPT ); Fri, 21 Apr 2023 17:44:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233575AbjDUVoT (ORCPT ); Fri, 21 Apr 2023 17:44:19 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92CF126B2; Fri, 21 Apr 2023 14:44:15 -0700 (PDT) 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=ZUAOcNqD6LayzeKVCuQ/BLskBnjlrIyvlaP+338TFj0=; b=4faVVxfybgSKlFUw4CjuG22i97 71PGGt9QIrJ/HwbknwCDE//N0MK6Lp3ra+wQrXZsk0eWG60sRW0X5RUf4Kenfek+eG2paFJP/MaZa 0pWwiy4StWWh+SSoxTEvENx27Ue1WY0nZEQ5jvksW4DR5M1zwWo9akRwduyxRxOL7u3B+Vg/sEuOs 8Ct23zxr9jtxEu1k9C79r6F1cOvTy0vfzN2gpw8diBX/Hb0y0aSu/dsvGfqVJ/FI8leP+YAgddYgn hK5IlPwLjcORNIqVDFOLTCGjGCfsFF6VGnKA9PA/rSAOR6sQOrz+AG5XTaTPZkVnxF4ht9+1OwIlY CcAZD47g==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btos-2R; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 6/8] shmem: consider block size in shmem_default_max_inodes() Date: Fri, 21 Apr 2023 14:43:58 -0700 Message-Id: <20230421214400.2836131-7-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" Today we allow for a max number of inodes in consideration for the smallest possible inodes with just one block of size PAGE_SIZE. The max number of inodes depend on the size of the block size then, and if we want to support higher block sizes we end up with less number of inodes. Account for this in the computation for the max number of inodes. If the blocksize is greater than the PAGE_SIZE, we simply divide the number of pages usable, multiply by the page size and divide by the blocksize. This produces no functional changes right now as we don't support larger block sizes yet. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b83596467706..5a64efd1f3c2 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -143,11 +143,14 @@ static unsigned long shmem_default_max_blocks(u64 blo= cksize) return totalram_pages() >> (__ffs(blocksize) - PAGE_SHIFT + 1); } =20 -static unsigned long shmem_default_max_inodes(void) +static unsigned long shmem_default_max_inodes(u64 blocksize) { unsigned long nr_pages =3D totalram_pages(); + unsigned long pages_for_inodes =3D min(nr_pages - totalhigh_pages(), nr_p= ages / 2); =20 - return min(nr_pages - totalhigh_pages(), nr_pages / 2); + if (blocksize =3D=3D shmem_default_bsize()) + return pages_for_inodes; + return pages_for_inodes >> (__ffs(blocksize) - PAGE_SHIFT); } #else static u64 shmem_sb_blocksize(struct shmem_sb_info *sbinfo) @@ -4028,7 +4031,7 @@ static int shmem_show_options(struct seq_file *seq, s= truct dentry *root) if (sbinfo->max_blocks !=3D shmem_default_max_blocks(shmem_default_bsize(= ))) seq_printf(seq, ",size=3D%luk", sbinfo->max_blocks << (PAGE_SHIFT - 10)); - if (sbinfo->max_inodes !=3D shmem_default_max_inodes()) + if (sbinfo->max_inodes !=3D shmem_default_max_inodes(shmem_default_bsize(= ))) seq_printf(seq, ",nr_inodes=3D%lu", sbinfo->max_inodes); if (sbinfo->mode !=3D (0777 | S_ISVTX)) seq_printf(seq, ",mode=3D%03ho", sbinfo->mode); @@ -4109,7 +4112,7 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) ctx->blocks =3D shmem_default_max_blocks(shmem_default_bsize()); if (!(ctx->seen & SHMEM_SEEN_INODES)) - ctx->inodes =3D shmem_default_max_inodes(); + ctx->inodes =3D shmem_default_max_inodes(shmem_default_bsize()); if (!(ctx->seen & SHMEM_SEEN_INUMS)) ctx->full_inums =3D IS_ENABLED(CONFIG_TMPFS_INODE64); sbinfo->noswap =3D ctx->noswap; --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 C830BC7618E for ; Fri, 21 Apr 2023 21:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233679AbjDUVoh (ORCPT ); Fri, 21 Apr 2023 17:44:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233563AbjDUVoT (ORCPT ); Fri, 21 Apr 2023 17:44:19 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68A392116; Fri, 21 Apr 2023 14:44:15 -0700 (PDT) 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=LRGm8v+C2VnFh3pJYXzbKwj2NvliqmXVbLHOoz9D9rs=; b=Gv4xXUfwg6Tqrov7HtOq12Iot5 YO5wR1wBRRe+FoidC8runo8IO16BDwaMfjnowR194HePAotck078Dc52Y5LhgHsJWaSdFz/ExDOoW 7UvJg+gaENVFRZFuUklS2jbQPIu6sP031vvUKDVO8op0VWHJ/pcNAbKtxQ6EJtE3JOGWA8i9aCI4G c82Y5dwvzAyUa4CC4Jt/QPxPn6XSF0jejQ+JpFEAjr1mjFYM6xQTv/oz9ZsmadJRNPxXXDoGL2kGk x5ARZON7gmkNiGzDhbMd8jQFJ5x3RYgTnUCgMJhDF50jxO3kH/6kEmfu6+iJAJL3r0nsysDSme+ZO aDY1SY0g==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btou-2Z; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 7/8] shmem: add high order page support Date: Fri, 21 Apr 2023 14:43:59 -0700 Message-Id: <20230421214400.2836131-8-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" To support high order block sizes we want to support a high order folios so to treat the larger block atomically. Add support for this for tmpfs mounts. Right now this produces no functional changes since we only allow one single block size, matching the PAGE_SIZE and so the order is always 0. Signed-off-by: Luis Chamberlain --- mm/shmem.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index 5a64efd1f3c2..740b4448f936 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1621,9 +1621,15 @@ static struct folio *shmem_alloc_folio(gfp_t gfp, { struct vm_area_struct pvma; struct folio *folio; + struct inode *inode =3D &info->vfs_inode; + struct super_block *i_sb =3D inode->i_sb; + int order =3D 0; + + if (!(i_sb->s_flags & SB_KERNMOUNT)) + order =3D i_sb->s_blocksize_bits - PAGE_SHIFT; =20 shmem_pseudo_vma_init(&pvma, info, index); - folio =3D vma_alloc_folio(gfp, 0, &pvma, 0, false); + folio =3D vma_alloc_folio(gfp, order, &pvma, 0, false); shmem_pseudo_vma_destroy(&pvma); =20 return folio; --=20 2.39.2 From nobody Wed Dec 17 04:37:12 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 12E44C7618E for ; Fri, 21 Apr 2023 21:44:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233733AbjDUVos (ORCPT ); Fri, 21 Apr 2023 17:44:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233608AbjDUVoT (ORCPT ); Fri, 21 Apr 2023 17:44:19 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 407EF1FF0; Fri, 21 Apr 2023 14:44:15 -0700 (PDT) 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=QZSPgijKCu3Y3/12/R8LbE206e1fX9dQfHKdMNON/W8=; b=oFywjwINQnJi1wLFD/p2jy2j7L YzaKpUjXYeUccWb/Nv5aW9K9NT0ZiPfQ5qLf6tyvltqi0NzPK5Dqiyjep/DwF9A0si0uEBf/bxZ1a gC3ggPEPcz4//L5fnHODuyCTOEd+G0oIS3iCLNKOjC8gFEP6lxkUml1fa0r33CKeLmcMdw+EKZ/rz EyUHrfWth/uiSWRR9n5AjEBXdeZUg2klO11cMNtkhUKw0bjosgZvqzfu1mlsfgxOhbCOoRLqLq3BB w3QKnGmL5aC2RSOu972POaXT0PC9eMLB7F2HwvVQ38UMYkfPO3/V1FNdAdEMLhrhATJUDT86vHTiQ 0AVSMYmQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1ppyY1-00Btow-2i; Fri, 21 Apr 2023 21:44:05 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC 8/8] shmem: add support to customize block size on multiple PAGE_SIZE Date: Fri, 21 Apr 2023 14:44:00 -0700 Message-Id: <20230421214400.2836131-9-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230421214400.2836131-1-mcgrof@kernel.org> References: <20230421214400.2836131-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" This allows tmpfs mounts to use a custom block size. We only allow block sizes greater than PAGE_SIZE, and these must also be a multiple of the PAGE_SIZE too. Only simple tests have been run so far: time for i in $(seq 1 1000000); do echo $i >> /root/ordered.txt; done real 0m21.392s user 0m8.077s sys 0m13.098s du -h /root/ordered.txt 6.6M /root/ordered.txt sha1sum /root/ordered.txt 2dcc06b7ca3b7dd8b5626af83c1be3cb08ddc76c /root/ordered.txt stat /root/ordered.txt File: /root/ordered.txt Size: 6888896 Blocks: 13456 IO Block: 4096 regular file Device: 254,1 Inode: 655717 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2023-04-21 19:34:20.709869093 +0000 Modify: 2023-04-21 19:34:43.833900042 +0000 Change: 2023-04-21 19:34:43.833900042 +0000 Birth: 2023-04-21 19:34:20.709869093 +0000 8 KiB block size: sha1sum /root/ordered.txt mount -t tmpfs -o size=3D10M,bsize=3D$((4096*2)) -o noswap tmpfs= /data-tmpfs/ cp /root/ordered.txt sha1sum /data-tmpfs/ordered.txt stat /data-tmpfs/ordered.txt 2dcc06b7ca3b7dd8b5626af83c1be3cb08ddc76c /root/ordered.txt 2dcc06b7ca3b7dd8b5626af83c1be3cb08ddc76c /data-tmpfs/ordered.txt File: /data-tmpfs/ordered.txt Size: 6888896 Blocks: 13456 IO Block: 8192 regular file Device: 0,42 Inode: 2 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2023-04-21 19:31:16.078390405 +0000 Modify: 2023-04-21 19:31:16.070391363 +0000 Change: 2023-04-21 19:31:16.070391363 +0000 Birth: 2023-04-21 19:31:16.034395676 +0000 64 KiB block size: sha1sum /root/ordered.txt mount -t tmpfs -o size=3D10M,bsize=3D$((4096*16)) -o noswap tmpf= s /data-tmpfs/ cp /root/ordered.txt /data-tmpfs/; sha1sum /data-tmpfs/ordered.txt stat /data-tmpfs/ordered.txt 2dcc06b7ca3b7dd8b5626af83c1be3cb08ddc76c /root/ordered.txt 2dcc06b7ca3b7dd8b5626af83c1be3cb08ddc76c /data-tmpfs/ordered.txt File: /data-tmpfs/ordered.txt Size: 6888896 Blocks: 13568 IO Block: 65536 regular file Device: 0,42 Inode: 2 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2023-04-21 19:32:14.669796970 +0000 Modify: 2023-04-21 19:32:14.661796959 +0000 Change: 2023-04-21 19:32:14.661796959 +0000 Birth: 2023-04-21 19:32:14.649796944 +0000 Signed-off-by: Luis Chamberlain --- mm/shmem.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 740b4448f936..64108c28eebd 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -118,11 +118,13 @@ struct shmem_options { int huge; int seen; bool noswap; + u64 blocksize; #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 +#define SHMEM_SEEN_BLOCKSIZE 32 }; =20 static u64 shmem_default_bsize(void) @@ -3779,6 +3781,7 @@ enum shmem_param { Opt_inode32, Opt_inode64, Opt_noswap, + Opt_bsize, }; =20 static const struct constant_table shmem_param_enums_huge[] =3D { @@ -3801,6 +3804,7 @@ const struct fs_parameter_spec shmem_fs_parameters[] = =3D { fsparam_flag ("inode32", Opt_inode32), fsparam_flag ("inode64", Opt_inode64), fsparam_flag ("noswap", Opt_noswap), + fsparam_u32 ("bsize", Opt_bsize), {} }; =20 @@ -3827,7 +3831,14 @@ static int shmem_parse_one(struct fs_context *fc, st= ruct fs_parameter *param) } if (*rest) goto bad_value; - ctx->blocks =3D DIV_ROUND_UP(size, shmem_default_bsize()); + if (!(ctx->seen & SHMEM_SEEN_BLOCKSIZE) || + ctx->blocksize =3D=3D shmem_default_bsize()) + ctx->blocks =3D DIV_ROUND_UP(size, shmem_default_bsize()); + else { + if (size < ctx->blocksize || size % ctx->blocksize !=3D 0) + goto bad_value; + ctx->blocks =3D DIV_ROUND_UP(size, ctx->blocksize); + } ctx->seen |=3D SHMEM_SEEN_BLOCKS; break; case Opt_nr_blocks: @@ -3892,6 +3903,23 @@ static int shmem_parse_one(struct fs_context *fc, st= ruct fs_parameter *param) ctx->noswap =3D true; ctx->seen |=3D SHMEM_SEEN_NOSWAP; break; + case Opt_bsize: + ctx->blocksize =3D result.uint_32; + ctx->seen |=3D SHMEM_SEEN_BLOCKSIZE; + /* Must be >=3D PAGE_SIZE */ + if (ctx->blocksize < PAGE_SIZE) + goto bad_value; + /* + * We cap this to allow a block to be at least allowed to + * be allocated using the buddy allocator. That's MAX_ORDER + * pages. So 4 MiB on x86_64. + */ + if (ctx->blocksize > (1 << (MAX_ORDER + PAGE_SHIFT))) + goto bad_value; + /* The blocksize must be a multiple of the page size so must be aligned = */ + if (!PAGE_ALIGNED(ctx->blocksize)) + goto bad_value; + break; } return 0; =20 @@ -3963,6 +3991,12 @@ static int shmem_reconfigure(struct fs_context *fc) raw_spin_lock(&sbinfo->stat_lock); inodes =3D sbinfo->max_inodes - sbinfo->free_inodes; =20 + if (ctx->seen & SHMEM_SEEN_BLOCKSIZE) { + if (ctx->blocksize !=3D shmem_sb_blocksize(sbinfo)) { + err =3D "Cannot modify block size on remount"; + goto out; + } + } if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { if (!sbinfo->max_blocks) { err =3D "Cannot retroactively limit size"; @@ -4078,6 +4112,8 @@ static int shmem_show_options(struct seq_file *seq, s= truct dentry *root) shmem_show_mpol(seq, sbinfo->mpol); if (sbinfo->noswap) seq_printf(seq, ",noswap"); + if (shmem_sb_blocksize(sbinfo) !=3D shmem_default_bsize()) + seq_printf(seq, ",bsize=3D%llu", shmem_sb_blocksize(sbinfo)); return 0; } =20 @@ -4115,10 +4151,12 @@ static int shmem_fill_super(struct super_block *sb,= struct fs_context *fc) * but the internal instance is left unlimited. */ if (!(sb->s_flags & SB_KERNMOUNT)) { + if (!(ctx->seen & SHMEM_SEEN_BLOCKSIZE)) + ctx->blocksize =3D shmem_default_bsize(); if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) - ctx->blocks =3D shmem_default_max_blocks(shmem_default_bsize()); + ctx->blocks =3D shmem_default_max_blocks(ctx->blocksize); if (!(ctx->seen & SHMEM_SEEN_INODES)) - ctx->inodes =3D shmem_default_max_inodes(shmem_default_bsize()); + ctx->inodes =3D shmem_default_max_inodes(ctx->blocksize); if (!(ctx->seen & SHMEM_SEEN_INUMS)) ctx->full_inums =3D IS_ENABLED(CONFIG_TMPFS_INODE64); sbinfo->noswap =3D ctx->noswap; @@ -4127,7 +4165,7 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) } sb->s_export_op =3D &shmem_export_ops; sb->s_flags |=3D SB_NOSEC | SB_I_VERSION; - sbinfo->blocksize =3D shmem_default_bsize(); + sbinfo->blocksize =3D ctx->blocksize; #else sb->s_flags |=3D SB_NOUSER; #endif @@ -4155,7 +4193,6 @@ static int shmem_fill_super(struct super_block *sb, s= truct fs_context *fc) sb->s_maxbytes =3D MAX_LFS_FILESIZE; sb->s_blocksize =3D shmem_sb_blocksize(sbinfo); sb->s_blocksize_bits =3D __ffs(sb->s_blocksize); - WARN_ON_ONCE(sb->s_blocksize_bits !=3D PAGE_SHIFT); sb->s_magic =3D TMPFS_MAGIC; sb->s_op =3D &shmem_ops; sb->s_time_gran =3D 1; --=20 2.39.2