From patchwork Tue Jul 25 14:45:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326566 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E64ACC001DF for ; Tue, 25 Jul 2023 14:45:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BBA86B0074; Tue, 25 Jul 2023 10:45:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 661C88D0001; Tue, 25 Jul 2023 10:45:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 550366B0078; Tue, 25 Jul 2023 10:45:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 482AA6B0074 for ; Tue, 25 Jul 2023 10:45:25 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F23B01C9E9E for ; Tue, 25 Jul 2023 14:45:24 +0000 (UTC) X-FDA: 81050407368.21.C645D32 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 45A8C160017 for ; Tue, 25 Jul 2023 14:45:20 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=G3YDe+oV; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296321; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XNS0pZWYtiz3eLlRvQaHMyf1ULXVatSDvn1Y8OwTP3s=; b=Vm5p3bg9hOtJtn2zCNSDuZDZaIs0QsV6dIW3/DKwKt1fFw8UggkwZ86EqHt+3KqvGRUZVp hr7hWCEI3+jMmgoOhiWWQgiSjcETCIVyPRuCtJvIAghTB5vtcDhAVtRsY6JnPV6aYrYJYJ obnhLm4OnBc/yxSOA3qR4xuS+KbjR6Y= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=G3YDe+oV; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296321; a=rsa-sha256; cv=none; b=Eu/kn9CisPiXB+yqXdyGykriWuiQndPjpbuh8pfIwZNJFJa1QYYqS5C4GtsBM0nRhNA9jc t/vIGjYMUBHZXaGY9xi2V3npQu3D7uMP2Xa/01e+vKjq/2OnPMjEtiY1J5sOTmx89gN+zj 4BOtCrWf1VvRlzZIe7Al0xYXt69QhYI= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1C55E61781; Tue, 25 Jul 2023 14:45:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5012FC433CC; Tue, 25 Jul 2023 14:45:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296319; bh=yJe4lhnTjof59vWoLkGO8Qqi3dVE6gVAeA03pQuGI1M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G3YDe+oVtR6apUJALc4YfNYQt6z1JLVgTxnAGrkRupm3216gQlY++Ks/hY92BqjJx lK1+JyCzNMPvrZv76bA0BOJfX5Y4OV4IKCqseaLL7xJRulSmctBr/Ak6wLo8puTr8q UcZrMxhvyfyAq3hmwP78ssi5mVKCizMSls8JW9BE2CJLC2bVTY97xRF4x3dnRhhKi0 AHrQApjyWCSeoI0Ew0yMmw8VsRpDHfEpudr6/JmcPTeIetxs34ZBBYydUANv8tGepi dtu5/QbhOqS1Xjd0JZkUbL2whpdy3yTii6kDWpiS0MiFapduQszQQZwHMmMcp1yaDO QF+OoP6vQFd1g== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 1/7] shmem: make shmem_inode_acct_block() return error Date: Tue, 25 Jul 2023 16:45:04 +0200 Message-Id: <20230725144510.253763-2-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 45A8C160017 X-Stat-Signature: qs89ozzfd9s8mr4og4g4tbrdcf63td5s X-Rspam-User: X-HE-Tag: 1690296320-591940 X-HE-Meta: U2FsdGVkX1+f5u/xos7v7Ed9gxGwmCvVVZF+fnkS2xYK6HY5fLljSY5z7WE5zRBenzc3FfoOLsxLsytlvEMzc69WXMb+V7h7Jm+cCWKmVlh967eOlx8RYk+7RFrXvVChIex7jXI/nOCeNRzs6V+b7GYr9qHGBeT99CsP39QlIW9r/7SS28KE7+RjnkYBkMnleRMvebxvYrxe/MYEwsQA1/JU8xIxUzOh5gD+lq+KKhGdci01CViHeCL2qdxp31MrJmrblRvkEK7LKr/2sx5TSRo/KsdtKS6Fk71iI2A1xl+K5Ss7HKfzXiXZY1pn6TkDwoSgeCtdsWRJQEDUB42KCyno0T6VYmggXoWkzvXQN/jj3BmJEqVZUDLqZg3L5xFkNF29SA+TY2vCbxdG262xiyKHuL5VD6KuPg1pqrITtqCTquwoeKi7CCQIU2fUzf6QV0xNyjOmw24C6qYMcdpZL5z6IGoB9GicfsLveEq1q/BE1Y6nZCG97GY1tzLXcHTsO2RCKvtC5bae0Uur+KiMaAvAwUHBxKNdQwVzImwa9ybsDjVwlSkr/IVSROb7wtlYIhGOflL6fgG+YaoZqDiGExrzgZ9ro529l/dl88pI/78Nn8ZGbJ8NBUmvBYUlY1GNLpWStGzTfiHGnWjljr9aDOIgkV4ZMHHkBPKwENwufTZ2l9a3+NCb0be035h5gRedOxF3I5tQ+vI3L1Zvc28EamYJap+/TbwPmFTR1eZQAugYUksp44BTIOLxpjapoYkLyfAtx40+WD1QSdlZzz6BjvxCf8jhV913ltfwC3G5/gtM3Mcd8GxlDmHaLEUvFQfFlI/oISoN5yyzYfG8CEOjxM4qUSuI7vmmzsXL1R+rUqcjmXiiQ1/0byULGoUIcEn/vRMIwkl69nu1iIGcw4F6xNY8GykPDTrFoYtBtcG4MIKGqvtXulQg9B73jv/BY9AUx+nKUFD+y+wEcMdb2VS QP4pAXeO jcnRw8EDaKzocHIiNLwNlc5BNREP6FaSnkQBh1d7XXNIZIu9kgBK8yc2Kcl/9TG1kBEXaSrPIGIhdMM+OJr34hfYr3hpomvU6/KIReadfi9KbEDjLL+9d3G1RbZ/LDDrxBZbu+fmgjoAHES0WLcj4TKRNvPwpamA90eBJ1GhyPCyYAz3KAPVldlNctOTg8kWhfUDfc8Kta6MJpCfxlDl1Cqlsih9UodJkkgAa8Rr3qgXdrVIsq6HcA5pWtQBOLOC+VyuChgYTv+94PGprE+9RhRrJFpRGID9B/e3lhShyh/FvyUAZq6i0zzM0AMZwyugchXPigz9ykbCXSZUHXYH3yzJl7tcPJkuYKmLH9hDYdZ855HRSlSop0rPUHSOxNNRbl+Ji X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Lukas Czerner Make shmem_inode_acct_block() return proper error code instead of bool. This will be useful later when we introduce quota support. There should be no functional change. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- mm/shmem.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 2f2e0e618072..51d17655a6e1 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -199,13 +199,14 @@ static inline void shmem_unacct_blocks(unsigned long flags, long pages) vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); } -static inline bool shmem_inode_acct_block(struct inode *inode, long pages) +static inline int shmem_inode_acct_block(struct inode *inode, long pages) { struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); + int err = -ENOSPC; if (shmem_acct_block(info->flags, pages)) - return false; + return err; if (sbinfo->max_blocks) { if (percpu_counter_compare(&sbinfo->used_blocks, @@ -214,11 +215,11 @@ static inline bool shmem_inode_acct_block(struct inode *inode, long pages) percpu_counter_add(&sbinfo->used_blocks, pages); } - return true; + return 0; unacct: shmem_unacct_blocks(info->flags, pages); - return false; + return err; } static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) @@ -370,7 +371,7 @@ bool shmem_charge(struct inode *inode, long pages) struct shmem_inode_info *info = SHMEM_I(inode); unsigned long flags; - if (!shmem_inode_acct_block(inode, pages)) + if (shmem_inode_acct_block(inode, pages)) return false; /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ @@ -1588,13 +1589,14 @@ static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, struct shmem_inode_info *info = SHMEM_I(inode); struct folio *folio; int nr; - int err = -ENOSPC; + int err; if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) huge = false; nr = huge ? HPAGE_PMD_NR : 1; - if (!shmem_inode_acct_block(inode, nr)) + err = shmem_inode_acct_block(inode, nr); + if (err) goto failed; if (huge) @@ -2445,7 +2447,7 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, int ret; pgoff_t max_off; - if (!shmem_inode_acct_block(inode, 1)) { + if (shmem_inode_acct_block(inode, 1)) { /* * We may have got a page, returned -ENOENT triggering a retry, * and now we find ourselves with -ENOMEM. Release the page, to From patchwork Tue Jul 25 14:45:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDEF5C001DE for ; Tue, 25 Jul 2023 14:45:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 504496B0075; Tue, 25 Jul 2023 10:45:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43FE08D0001; Tue, 25 Jul 2023 10:45:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E23E6B007B; Tue, 25 Jul 2023 10:45:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 09C836B0075 for ; Tue, 25 Jul 2023 10:45:26 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C3FA7160E26 for ; Tue, 25 Jul 2023 14:45:25 +0000 (UTC) X-FDA: 81050407410.17.DE858BA Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id C10518001C for ; Tue, 25 Jul 2023 14:45:23 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Mc42U+wz; spf=pass (imf30.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296323; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Wq32InAH2gBYYoHF9Ofd9XzMSlQQWj+R7t4StC/4UDA=; b=alKLbOHmvemwn0vJQBcaiWmmH9Wznp+fWyuua3zhushC5rSr0jUFOea3QYVIFO4wUqIwMM B8qbGHo4ZmnUZPeOuNseHt5NEb18D68lEetPT9qfPn6uCmMOApWM8Nc1Xatj0lOCiLgD0R VXFGZhqPVUDym20YBYyyLpkmuX9IMdw= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Mc42U+wz; spf=pass (imf30.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296323; a=rsa-sha256; cv=none; b=tep4LZuMiKa0xw7dA9OtEVcnj7OUzTB1ikqVfJ+g/rEWBN+hi1WhWx+3kx7WT6LKApSjUi SB7yhOIa6JPF0L9sbtlZN/Frd07SE0jUPRHvJDD1grdvbCpgitmy+qzABRMXAtzFZ9z0Ay HjVc4aVGKHA3pHTz6cT1P9TnyOc2QRQ= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D6A0961703; Tue, 25 Jul 2023 14:45:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 176E8C433C7; Tue, 25 Jul 2023 14:45:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296322; bh=HyjiaT6OkD+tH9G9Qx+9mbfsJI7UgpYW54K83e9Gg3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mc42U+wz5YazH6HAaKB0YC1ST3KlRN5PEhh3OAQx/YzJsfHQTO/fsmzJ1YApZu789 VbSwUPavIhcOjTvfa1Mn39smRwibF0db+a1bSskaFMt63cxOG/kiI+uDhaCRwiLAVr moWXbmHaF7qNq/ZcPglY32VvZEVHlnzO+sq0pvE53heCkfcIZoW477IAzQGUB7ci/A knXxpnQPAi8+SfnBno6YWwngWkg9eCOek/dEKvEtB3U6tvVin6ccp/IyoJ5nmEQhVA gGv2NlWeMqFSv+ZeWyR0+9hVP5rGKMOrmBH2EjaZsdBbI5LFWsSdUX5CodYKR88tCd lWmSZSTb2BL2g== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 2/7] shmem: make shmem_get_inode() return ERR_PTR instead of NULL Date: Tue, 25 Jul 2023 16:45:05 +0200 Message-Id: <20230725144510.253763-3-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C10518001C X-Rspam-User: X-Stat-Signature: te456817b9d63gykuq1j9rcqcht49t4f X-Rspamd-Server: rspam01 X-HE-Tag: 1690296323-54037 X-HE-Meta: U2FsdGVkX18eTPSHrOj4QfaUHaYP1b4nzCoLmd2Ag1PC3aD3Im++e8x3bam+DGVfXDy54ehXoHH6AQCNCyGGIZqsGH59pZyk3rQwHRDPmZrX95P9AmmRO8dNNmT9QMVPWWtzXVh/++qD9wkJ0Txejb4peaYh+EhUt7znKaTcita9YzdIeLf1pRwh2Umx8fyDmzMXFieKshfxrOR2zmr34seAYdEYmDVJ/ZHZyIQnLX+ZC8cKKe3kdit4/15FFN3Ib5ntp0spLcQLoZFkv6dvBTwCwvBodKKWCEw/ihb23Xbl1e9N6Sz7G2KAghx1P9TeBcifNtGAQ+X+u61nnfhKrmTxj+buPHb2szbsxeQj79rFoTlKFJg39Hybyq0X9iIxOpMfn04m2ELMVvdNMMOcqbIDZWRx/wdPCBLLiPu+h4tEpdKcJCH1DhffYRE18oj72lTRTFF6fsdwWvEycy33nrpW8snzYccpQLmOWRHJgNB87lr+tCqdFzK9weBGYijgnqVDziHyW1CbJcyf7cuvRjEyJbCyuLd1+pS5zKIwVR21ZBduFpt9V8jglM+txwZ4nzkZuA52J/+uMfsQFFoFLBCc4XlEPbJTpQoU6rTFmqDYju7Svw+KmEBgAtlq893TX2f25EDshZMNlSzOJlAI4FFTo4X7gX39ck2J4OTW5W5KJv+sdYS0w8c1zx0pEyF/NcJuVYh+zAbmUzkOWtyT315rMgM//Oi731luw8ZoLYylC3Bh7b3GQnOSj2TJ2XpUHOWNF0sATd5FMqcwwRdXBzMsi9gIM3zdg0vSMwR+VQPaHl2WSfOev/E+bEGyCs5ANnEyYFcjR0rYrV8nL7nGC0hT1Ey/hryOAhqqhb/d3TFqxvz9iG3LhwzJVJWgZ0A5/sYerWKNmy73D1mAY4NAF5C0dE3vP6o/IGW8hNOLglZvbxSe4tHg9FzHF7ZLBifln6Xn2AXi4kFh0cLhn1b 0lzeomX8 qsI0OkgaIX71gU6tlRhF4U9UUq4tb6DLZcu1IoKGzZDUH+M9+Op2d3BF7OxhMl++D83u/JygaoYaPpjoFfhnRWOtVWEIzx6YlQeRihMO61iOuXP8xKGOOTU2XYyrHp54q8Y0OlNm/IjdIGVLvNEzRxdTwL6Rd8+HjitWlnO9nelJWLmSfAXR5jZNvDewhY03qYc/GA0qCD+MmgrzrSk490DHpRBkhBZ5HoPN7HHGg784JobBb5AqAjYXMnfHXgDIA9Zsh3ITLvp+o1du60AXYCUx10YK13HjwdDlaCmCR6kFZhv+9KNDsovtaJnbBjxLSopStbVgc5f2tYp5INMFibv1idLKnxJndC5XLL6Z/nfEWq+fzHERBoZJp7gdQ4WId2xZLZqekidf8Kl/PaDHhfW9tJQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Carlos Maiolino Make shmem_get_inode() return ERR_PTR instead of NULL on error. This will be useful later when we introduce quota support. There should be no functional change. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- mm/shmem.c | 211 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 119 insertions(+), 92 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 51d17655a6e1..2a7b8060b6f4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2365,67 +2365,74 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block struct shmem_inode_info *info; struct shmem_sb_info *sbinfo = SHMEM_SB(sb); ino_t ino; + int err; + + err = shmem_reserve_inode(sb, &ino); + if (err) + return ERR_PTR(err); - if (shmem_reserve_inode(sb, &ino)) - return NULL; inode = new_inode(sb); - if (inode) { - inode->i_ino = ino; - inode_init_owner(idmap, inode, dir, mode); - inode->i_blocks = 0; - inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); - inode->i_generation = get_random_u32(); - info = SHMEM_I(inode); - memset(info, 0, (char *)inode - (char *)info); - spin_lock_init(&info->lock); - atomic_set(&info->stop_eviction, 0); - info->seals = F_SEAL_SEAL; - info->flags = flags & VM_NORESERVE; - info->i_crtime = inode->i_mtime; - info->fsflags = (dir == NULL) ? 0 : - SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; - if (info->fsflags) - 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); - - switch (mode & S_IFMT) { - default: - inode->i_op = &shmem_special_inode_operations; - init_special_inode(inode, mode, dev); - break; - case S_IFREG: - inode->i_mapping->a_ops = &shmem_aops; - inode->i_op = &shmem_inode_operations; - inode->i_fop = &shmem_file_operations; - mpol_shared_policy_init(&info->policy, - shmem_get_sbmpol(sbinfo)); - break; - case S_IFDIR: - inc_nlink(inode); - /* Some things misbehave if size == 0 on a directory */ - inode->i_size = 2 * BOGO_DIRENT_SIZE; - inode->i_op = &shmem_dir_inode_operations; - inode->i_fop = &simple_dir_operations; - break; - case S_IFLNK: - /* - * Must not load anything in the rbtree, - * mpol_free_shared_policy will not be called. - */ - mpol_shared_policy_init(&info->policy, NULL); - break; - } - lockdep_annotate_inode_mutex_key(inode); - } else + if (!inode) { shmem_free_inode(sb); + return ERR_PTR(-ENOSPC); + } + + inode->i_ino = ino; + inode_init_owner(idmap, inode, dir, mode); + inode->i_blocks = 0; + inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); + inode->i_generation = get_random_u32(); + info = SHMEM_I(inode); + memset(info, 0, (char *)inode - (char *)info); + spin_lock_init(&info->lock); + atomic_set(&info->stop_eviction, 0); + info->seals = F_SEAL_SEAL; + info->flags = flags & VM_NORESERVE; + info->i_crtime = inode->i_mtime; + info->fsflags = (dir == NULL) ? 0 : + SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; + if (info->fsflags) + shmem_set_inode_flags(inode, info->fsflags); + INIT_LIST_HEAD(&info->shrinklist); + INIT_LIST_HEAD(&info->swaplist); + 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); + + switch (mode & S_IFMT) { + default: + inode->i_op = &shmem_special_inode_operations; + init_special_inode(inode, mode, dev); + break; + case S_IFREG: + inode->i_mapping->a_ops = &shmem_aops; + inode->i_op = &shmem_inode_operations; + inode->i_fop = &shmem_file_operations; + mpol_shared_policy_init(&info->policy, + shmem_get_sbmpol(sbinfo)); + break; + case S_IFDIR: + inc_nlink(inode); + /* Some things misbehave if size == 0 on a directory */ + inode->i_size = 2 * BOGO_DIRENT_SIZE; + inode->i_op = &shmem_dir_inode_operations; + inode->i_fop = &simple_dir_operations; + break; + case S_IFLNK: + /* + * Must not load anything in the rbtree, + * mpol_free_shared_policy will not be called. + */ + mpol_shared_policy_init(&info->policy, NULL); + break; + } + + lockdep_annotate_inode_mutex_key(inode); return inode; } @@ -3071,27 +3078,30 @@ shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) { struct inode *inode; - int error = -ENOSPC; + int error; inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, dev, VM_NORESERVE); - if (inode) { - error = simple_acl_create(dir, inode); - if (error) - goto out_iput; - error = security_inode_init_security(inode, dir, - &dentry->d_name, - shmem_initxattrs, NULL); - if (error && error != -EOPNOTSUPP) - goto out_iput; - error = 0; - dir->i_size += BOGO_DIRENT_SIZE; - dir->i_ctime = dir->i_mtime = current_time(dir); - inode_inc_iversion(dir); - d_instantiate(dentry, inode); - dget(dentry); /* Extra count - pin the dentry in core */ - } + if (IS_ERR(inode)) + return PTR_ERR(inode); + + error = simple_acl_create(dir, inode); + if (error) + goto out_iput; + error = security_inode_init_security(inode, dir, + &dentry->d_name, + shmem_initxattrs, NULL); + if (error && error != -EOPNOTSUPP) + goto out_iput; + + error = 0; + dir->i_size += BOGO_DIRENT_SIZE; + dir->i_ctime = dir->i_mtime = current_time(dir); + inode_inc_iversion(dir); + d_instantiate(dentry, inode); + dget(dentry); /* Extra count - pin the dentry in core */ return error; + out_iput: iput(inode); return error; @@ -3102,20 +3112,26 @@ shmem_tmpfile(struct mnt_idmap *idmap, struct inode *dir, struct file *file, umode_t mode) { struct inode *inode; - int error = -ENOSPC; + int error; inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, 0, VM_NORESERVE); - if (inode) { - error = security_inode_init_security(inode, dir, - NULL, - shmem_initxattrs, NULL); - if (error && error != -EOPNOTSUPP) - goto out_iput; - error = simple_acl_create(dir, inode); - if (error) - goto out_iput; - d_tmpfile(file, inode); + + if (IS_ERR(inode)) { + error = PTR_ERR(inode); + goto err_out; } + + error = security_inode_init_security(inode, dir, + NULL, + shmem_initxattrs, NULL); + if (error && error != -EOPNOTSUPP) + goto out_iput; + error = simple_acl_create(dir, inode); + if (error) + goto out_iput; + d_tmpfile(file, inode); + +err_out: return finish_open_simple(file, error); out_iput: iput(inode); @@ -3290,8 +3306,9 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, inode = shmem_get_inode(idmap, dir->i_sb, dir, S_IFLNK | 0777, 0, VM_NORESERVE); - if (!inode) - return -ENOSPC; + + if (IS_ERR(inode)) + return PTR_ERR(inode); error = security_inode_init_security(inode, dir, &dentry->d_name, shmem_initxattrs, NULL); @@ -3929,12 +3946,13 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) struct shmem_options *ctx = fc->fs_private; struct inode *inode; struct shmem_sb_info *sbinfo; + int error = -ENOMEM; /* Round up to L1_CACHE_BYTES to resist false sharing */ sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), L1_CACHE_BYTES), GFP_KERNEL); if (!sbinfo) - return -ENOMEM; + return error; sb->s_fs_info = sbinfo; @@ -3997,8 +4015,10 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); - if (!inode) + if (IS_ERR(inode)) { + error = PTR_ERR(inode); goto failed; + } inode->i_uid = sbinfo->uid; inode->i_gid = sbinfo->gid; sb->s_root = d_make_root(inode); @@ -4008,7 +4028,7 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) failed: shmem_put_super(sb); - return -ENOMEM; + return error; } static int shmem_get_tree(struct fs_context *fc) @@ -4377,10 +4397,16 @@ EXPORT_SYMBOL_GPL(shmem_truncate_range); #define shmem_vm_ops generic_file_vm_ops #define shmem_anon_vm_ops generic_file_vm_ops #define shmem_file_operations ramfs_file_operations -#define shmem_get_inode(idmap, sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) #define shmem_acct_size(flags, size) 0 #define shmem_unacct_size(flags, size) do {} while (0) +static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block *sb, struct inode *dir, + umode_t mode, dev_t dev, unsigned long flags) +{ + struct inode *inode = ramfs_get_inode(sb, dir, mode, dev); + return inode ? inode : ERR_PTR(-ENOSPC); +} + #endif /* CONFIG_SHMEM */ /* common code */ @@ -4405,9 +4431,10 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, l inode = shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); - if (unlikely(!inode)) { + + if (IS_ERR(inode)) { shmem_unacct_size(flags, size); - return ERR_PTR(-ENOSPC); + return ERR_CAST(inode); } inode->i_flags |= i_flags; inode->i_size = size; From patchwork Tue Jul 25 14:45:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326568 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90392C04FDF for ; Tue, 25 Jul 2023 14:45:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2BBC96B0078; Tue, 25 Jul 2023 10:45:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 21CC56B007B; Tue, 25 Jul 2023 10:45:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BCE06B007D; Tue, 25 Jul 2023 10:45:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F2B206B0078 for ; Tue, 25 Jul 2023 10:45:28 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BA9C040488 for ; Tue, 25 Jul 2023 14:45:28 +0000 (UTC) X-FDA: 81050407536.10.DA75F31 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 052CF14001E for ; Tue, 25 Jul 2023 14:45:26 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="h/x4jyji"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296327; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1IAdq4n5/oIzAGmJbwcaTZVO+ZHOV/slhcqGbtISZa0=; b=xO3N0SfmwfICxE0xpVEpCdGZ16fLy5rIP2ObAiig0hT5hwsyxMSxihSxVqe7AdptkbC3yY MKg8XsnnQaUnQKflCbIMm26MWgPr1/lBykGJmslskqt/Ve3QZP62oM02YLfiHNaNMggIMY pyJsfZAmEkbTY68w3I0X8oNUcqaRouQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="h/x4jyji"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296327; a=rsa-sha256; cv=none; b=4YfjN0856VKA9CjAHg4lfpI71tQfCTMKR3AYojbvuMweYwa6LWKf/QTXID3fYQigSei2O7 vQ3IvARUr4GbHd9ir6GrD41BuNtX5vb8IYy353EdkaGKEtqNVDPkqChdzZkk6O12vOzV27 YwY66k28A2vAc7zyPw0Y6yXMCrg9Xo8= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 854D661789; Tue, 25 Jul 2023 14:45:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7B9DC433CA; Tue, 25 Jul 2023 14:45:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296325; bh=okwg5m+9iRcDR+mIe+e593XoCBrrkAftdLKtFY7t22A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h/x4jyjitGuI2xCFW730+l91R7oGtiojJBNuA+VuT/wXG7VQE3MXHRTPWnwVG4h4d U1Gi1X3u/0A09iYDEDvbBxFwb5m2KWIJUkHQBdH4cVAZoCBtT8hVT70CGESfm/AXxM Ud4H7J+fsycphFQCrW3WJFYQOksPPLakJiqSBPRJ8RTjDs7QXBwIEOLhljPyW0Sh82 Ct/phTEbHj2/9+ZZ872hFKV+vGFXirj+P++EV2uCxSPKTPWVKpCOtIvwwjgefRVZDY xwieeKaGM1qN75kD6v+RE7Wk51Ritya/uS0EFxbzoExfhctyRVoA9IRT96NnkJUktz TzLhFNtjoH3uA== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 3/7] quota: Check presence of quota operation structures instead of ->quota_read and ->quota_write callbacks Date: Tue, 25 Jul 2023 16:45:06 +0200 Message-Id: <20230725144510.253763-4-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 052CF14001E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: oc6xsc9cdat1ttfjfk1huh3qu44ps6ah X-HE-Tag: 1690296326-848023 X-HE-Meta: U2FsdGVkX1+KGv2XnTvzbi4QeLv18U9Faxn/gn663KOARc8lSjghWZ2tcH+qXjDitDeTG9Tal9F0PMcle1/E7CgM7CTBz8HP+7sfbT2jgCc2wqaODbCpcSkYj2IlQ2Kc0l9XCISgGfaQmjnGk9P8DfYw8lJxQzsoZY9TkbunhqZAONcE1pMWUo2gPErnOteKhfkBL7EEmN7n1pEbkHg6FxfDcfcecbvWFYOoTr0dGTFEcRNar6qnXNNjzJpEs11yGDVv45L7GlxpweLHe9ONJFuIrx/w5xbaTTk6C5pCqh3k1wjtWIqZD5BTujOljCJwLTEoC+rbh14UPF4GnjJELn7cfZMftBR6fQyJwB/nJdHwAhfmPnKBE50dzQcUCYp3OZtmFGW+wPNLV0nNwWO1BOnho7QuoHGxDmwWuki6bj3Ty3hMSVB+Gt0fRE747BGBVDCbkrF/wn2Fl3mfmITvn85S97Y2Ifvwt981Obgtioxu0rJIg5g3JZx6mI+ReFdeex4e81vgAlbnfli+0fO577NeN9faf0w1zXRgVvz3T8R3pR2JKk6yWZLm6tl8YIllGEZeD1JiuUk7vUdqFxloiwsMrPW3X/CVPFWnaSGFPPe+7XomE0fbrBWzTRJ2GGtGIiyXve4eCrJzEXdzYVX6pMYvTJ0YAPU4BvxKM3skNRkqwj84hXbHk5696dyr+SLu7E9dXfuPdS9L4lh0XEmoyK23GPAbKLfdMpUPiECESNZrmvRGt6Vo88+56EBIjg0cjCCQ/5A9hB93mErF1qV5CVM7ViagQ7HxWh248lyReh63C5hSPh9L2iUHm+2MTlW6lR+D/IKSq21kctC6eEUUm7oizOJcmzOYu2iRKQ6hVbmSGswJEMVzWXVgkRf8n4sgg5dzlCm8H4XDGu/tL4MfCRcgKyG4vgd/REFeoGWJqlIhGBVWj2u/ZwcZpZtCImqhWotAy0P2j9Y5gj6cpie RL0HWQ2Y QDbx389xGdHwfbGj5JljDt1FVJTC7m+ikMw6lZOmi5OKk+MbWKVlWATPdBR7dhyV2G1mJdWahX2uMzq7Tohs6ikPTFB+ib72xpZ2bJ2PbRRcdolIv7qLSIQhrx+w4JhmM42kMn3cXjsT2Kh9TFMCXeosqApkHVd0su4/lb0hB6BTdXpioD9TE1PwkbFFstt9QrTomGK80Dyjlm0LlsKO9iZTfEEV+ORXPFojsnGxgLM5FzIlq/H+MEhO6XYsqsUBMfMwwtPhYx2Md6t5p1i1s962AzyPY+yNVpuUvqM28i6Vx0ixCD6BMdAC7t+7CALp3DsSZXsa9XcfDvhOdIqxgDcE51g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Jan Kara Currently we check whether superblock has ->quota_read and ->quota_write operations to check whether filesystem supports quotas. However for example for shmfs we will not read or write dquots so check whether quota operations are set in the superblock instead. Signed-off-by: Jan Kara Reviewed-by: Carlos Maiolino --- fs/quota/dquot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c index e3e4f4047657..4d826c369da2 100644 --- a/fs/quota/dquot.c +++ b/fs/quota/dquot.c @@ -2367,7 +2367,7 @@ int dquot_load_quota_sb(struct super_block *sb, int type, int format_id, if (!fmt) return -ESRCH; - if (!sb->s_op->quota_write || !sb->s_op->quota_read || + if (!sb->dq_op || !sb->s_qcop || (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) { error = -EINVAL; goto out_fmt; From patchwork Tue Jul 25 14:45:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326569 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BFA2C001DE for ; Tue, 25 Jul 2023 14:45:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7E876B007B; Tue, 25 Jul 2023 10:45:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C0A5B6B007D; Tue, 25 Jul 2023 10:45:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5A236B007E; Tue, 25 Jul 2023 10:45:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8F2746B007B for ; Tue, 25 Jul 2023 10:45:31 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 642F1B273A for ; Tue, 25 Jul 2023 14:45:31 +0000 (UTC) X-FDA: 81050407662.06.1468229 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 6A2D9140020 for ; Tue, 25 Jul 2023 14:45:29 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NWdcCi4K; spf=pass (imf23.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296329; a=rsa-sha256; cv=none; b=ouDyeOL/qY1uf4ztrhhINfl2uw2LjOELgOcJJUds6OqnV9DZCgZ/j1jjABTk9zu4g55cYp XHcktDgm+jM36rq++rsyFVroARjxFh00mLCmMoKz6qfb7cMjvX8C6dQMS9wJ2Bh1OtUtcr bsVqmcpILdYROMfTVnHkhBXOchI81G0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NWdcCi4K; spf=pass (imf23.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296329; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=b45lOUU24oqkHWuBwfObVOrS0h/UkBDwVdEmsnM7cTA=; b=wB3HOrpx+DEYWk1k9DXx4PYG3OA7QnZ+f9V7LUsU9nzY2yroiIqMOdw1K1eSurCkJlaGIZ UhjS7RMa01Cv4PCoo9txYR4Xl/lzYnCz+oe3VfJv6stjg2T0NENKiF+83V+9aewQL5Y3oo BP1qg58kmI0uA4/mXgLVeXNLkaADRmo= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5738C6178B; Tue, 25 Jul 2023 14:45:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E692C433C8; Tue, 25 Jul 2023 14:45:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296327; bh=n0gdfDmwA5+qGSDPEm2katroSDAcKDjqK4HA0jNlq58=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NWdcCi4KcgCNT93SjXwu55cBt+9yfRWAaFGAMuOhWlAAxXyB7ir8c+j7Aj10OsjkH nu0Omj8hU5pQ5EIv1WDtNCqMsIlxk4FaIjel9kyPBoRye+2+u9Th94gKjuVEZeuQgL 75GcNY0gysM8DVgi3UYLaV7G74Zv7cQlXYI4hRtLp3IXC9plYaMLaqMjZUrE/zNeY2 FOruDG44AG6Z3i1mdS5V2SAi4G9VZomlh70mKw+llvqEZfY1r6tdgmjKqXrRVZl0b4 kEwq6sJ9iiAfvS6mv7iDZGVrymp6kb8wfbXKoKbrabznUbknWzQNBJxOvpLtspwOSP pBkE6KHo+TKDQ== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 4/7] shmem: prepare shmem quota infrastructure Date: Tue, 25 Jul 2023 16:45:07 +0200 Message-Id: <20230725144510.253763-5-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 6A2D9140020 X-Stat-Signature: z954dxp63yircsm6g3z78brgwjwywqhn X-Rspam-User: X-HE-Tag: 1690296329-800399 X-HE-Meta: U2FsdGVkX1+T51hRkGs80U3HRMFzla5hFOV1DRRq2IhGj74ShDpX9GMkmaWQYTPnh1VYLVY4r7UTkKbvmjKY38fRs5EHHN4BObOpf3el38OrSh5jEqhFuFJgd1lDPpv68Yd3MMH516KWlQfbRGEScHeY6vob1KjqqGH/qGj29SqVoElnIV4YztqkGANvHLF6a+7C7wRcI54CEhZTQTV5h/3NAJZ13gY8ABGCvpgGIYB1gBkYoRM2f5EqV5FbuFmc8JfBjwJqsQ/Qsg9LVIK402n1YQah0b0Z1angS/2zJ7dsGqF5V5M6M28gYF4jWQ2dCRKgaFOyLiK3eQiFCzH+lYnp6Oc7N7B6FTVLxT/w4YcUaKzp8pGxeQQsVOteaZGMSX2+bPCN21WlgIUnkP6V9e20fP2bAp9Ba2odSv26zaPYkzmF/IhKr6ppBF+EwxYhaRkuSi5bMdKExqwfPKZUgJh1XXgCNicWm+lE5iqgyWJ+IAleihU+gTtWNwvRurlDwfOhimCBFpjSjC4bB4Xh1TS5k9xBvB80z4zhrmPjndkNHCk0qfrvZnbnHRujqQJDB/X2rQpHOp82+8IAH6JGFdTlQtd1Co3XPVfRGWdhO3t0+yJoWMLFTd8egkA9Dtx0VKj4PO/mjKzcP1YljHeWGXh/4V/YWUEGC+JLhz5FWmqu3knTLDnVFyQkhL9JfW0B8OjD8PRqz45OHZnSywX3mhOSaWgjkVNqOcSOvKTR1YdXIPYlyjVR26S7EKN/U5qkn8ceMRDxXoJZQmc+V8X4tBZqR/LKgwsEg4cwO3DhtVrWMMm2b5qA1mR6EXfUTNinq/XbIY8suv5tGNPlfiplL0Nq2pf+ZsBVxt3aL7yzDlAqgO2L33jHKFWujenAX4CFAZP6qV+jt0WwDMd61MJroSmJtjfTusbUU2+CeOQFaVFNYQ3/V1+kX/uEPj/3cOtIQykHCOjXA+ueNEW22cR yD7mf8Ke SPOMS1QMsuJ+lfh/2UDH0ZjBiyyqTsoNF0pl+Is3ndEm6iZer6ZEft5Z0Utb/MhQGBsVcpGWY6psxrTaw9cqATfXZORt5hEwIAnf0hsNZ6SHN5hXE1lv+/3d+YUATtpBpi8cNP1jLlC7n3joBidrfyI4u4pq01GhVkI7U26EDyxtSuVFgWOylortgKmdVfADwpx919Q14E5WlKM38/xNYsd2lYPlOgnoPl/uWW3sNPPT8K7+HI0qclmAnPQYNDy1Warnl/P+olY1PWvfy38LIaeflS2HHevkJDjOUDYfzCX30i/zVDBAH38vK8z5+o06INdmZP7oKbpGsoV8bdxHw30ZJzesHDdV7X1C1mN0GEQ+4fno= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Carlos Maiolino Add new shmem quota format, its quota_format_ops together with dquot_operations Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- fs/Kconfig | 12 ++ include/linux/shmem_fs.h | 12 ++ include/uapi/linux/quota.h | 1 + mm/Makefile | 2 +- mm/shmem_quota.c | 318 +++++++++++++++++++++++++++++++++++++ 5 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 mm/shmem_quota.c diff --git a/fs/Kconfig b/fs/Kconfig index 18d034ec7953..8218a71933f9 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -233,6 +233,18 @@ config TMPFS_INODE64 If unsure, say N. +config TMPFS_QUOTA + bool "Tmpfs quota support" + depends on TMPFS + select QUOTA + help + Quota support allows to set per user and group limits for tmpfs + usage. Say Y to enable quota support. Once enabled you can control + user and group quota enforcement with quota, usrquota and grpquota + mount options. + + If unsure, say N. + config ARCH_SUPPORTS_HUGETLBFS def_bool n diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 9029abd29b1c..7abfaf70b58a 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -13,6 +13,10 @@ /* inode in-kernel data */ +#ifdef CONFIG_TMPFS_QUOTA +#define SHMEM_MAXQUOTAS 2 +#endif + struct shmem_inode_info { spinlock_t lock; unsigned int seals; /* shmem seals */ @@ -172,4 +176,12 @@ extern int shmem_mfill_atomic_pte(pmd_t *dst_pmd, #endif /* CONFIG_SHMEM */ #endif /* CONFIG_USERFAULTFD */ +/* + * Used space is stored as unsigned 64-bit value in bytes but + * quota core supports only signed 64-bit values so use that + * as a limit + */ +#define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */ +#define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL + #endif diff --git a/include/uapi/linux/quota.h b/include/uapi/linux/quota.h index f17c9636a859..52090105b828 100644 --- a/include/uapi/linux/quota.h +++ b/include/uapi/linux/quota.h @@ -77,6 +77,7 @@ #define QFMT_VFS_V0 2 #define QFMT_OCFS2 3 #define QFMT_VFS_V1 4 +#define QFMT_SHMEM 5 /* Size of block in which space limits are passed through the quota * interface */ diff --git a/mm/Makefile b/mm/Makefile index 678530a07326..d4ee20988dd1 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -51,7 +51,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ readahead.o swap.o truncate.o vmscan.o shmem.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o percpu.o slab_common.o \ - compaction.o show_mem.o\ + compaction.o show_mem.o shmem_quota.o\ interval_tree.o list_lru.o workingset.o \ debug.o gup.o mmap_lock.o $(mmu-y) diff --git a/mm/shmem_quota.c b/mm/shmem_quota.c new file mode 100644 index 000000000000..e92b8ece9880 --- /dev/null +++ b/mm/shmem_quota.c @@ -0,0 +1,318 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * In memory quota format relies on quota infrastructure to store dquot + * information for us. While conventional quota formats for file systems + * with persistent storage can load quota information into dquot from the + * storage on-demand and hence quota dquot shrinker can free any dquot + * that is not currently being used, it must be avoided here. Otherwise we + * can lose valuable information, user provided limits, because there is + * no persistent storage to load the information from afterwards. + * + * One information that in-memory quota format needs to keep track of is + * a sorted list of ids for each quota type. This is done by utilizing + * an rb tree which root is stored in mem_dqinfo->dqi_priv for each quota + * type. + * + * This format can be used to support quota on file system without persistent + * storage such as tmpfs. + * + * Author: Lukas Czerner + * Carlos Maiolino + * + * Copyright (C) 2023 Red Hat, Inc. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef CONFIG_TMPFS_QUOTA + +/* + * The following constants define the amount of time given a user + * before the soft limits are treated as hard limits (usually resulting + * in an allocation failure). The timer is started when the user crosses + * their soft limit, it is reset when they go below their soft limit. + */ +#define SHMEM_MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */ +#define SHMEM_MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */ + +struct quota_id { + struct rb_node node; + qid_t id; + qsize_t bhardlimit; + qsize_t bsoftlimit; + qsize_t ihardlimit; + qsize_t isoftlimit; +}; + +static int shmem_check_quota_file(struct super_block *sb, int type) +{ + /* There is no real quota file, nothing to do */ + return 1; +} + +/* + * There is no real quota file. Just allocate rb_root for quota ids and + * set limits + */ +static int shmem_read_file_info(struct super_block *sb, int type) +{ + struct quota_info *dqopt = sb_dqopt(sb); + struct mem_dqinfo *info = &dqopt->info[type]; + + info->dqi_priv = kzalloc(sizeof(struct rb_root), GFP_NOFS); + if (!info->dqi_priv) + return -ENOMEM; + + info->dqi_max_spc_limit = SHMEM_QUOTA_MAX_SPC_LIMIT; + info->dqi_max_ino_limit = SHMEM_QUOTA_MAX_INO_LIMIT; + + info->dqi_bgrace = SHMEM_MAX_DQ_TIME; + info->dqi_igrace = SHMEM_MAX_IQ_TIME; + info->dqi_flags = 0; + + return 0; +} + +static int shmem_write_file_info(struct super_block *sb, int type) +{ + /* There is no real quota file, nothing to do */ + return 0; +} + +/* + * Free all the quota_id entries in the rb tree and rb_root. + */ +static int shmem_free_file_info(struct super_block *sb, int type) +{ + struct mem_dqinfo *info = &sb_dqopt(sb)->info[type]; + struct rb_root *root = info->dqi_priv; + struct quota_id *entry; + struct rb_node *node; + + info->dqi_priv = NULL; + node = rb_first(root); + while (node) { + entry = rb_entry(node, struct quota_id, node); + node = rb_next(&entry->node); + + rb_erase(&entry->node, root); + kfree(entry); + } + + kfree(root); + return 0; +} + +static int shmem_get_next_id(struct super_block *sb, struct kqid *qid) +{ + struct mem_dqinfo *info = sb_dqinfo(sb, qid->type); + struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + qid_t id = from_kqid(&init_user_ns, *qid); + struct quota_info *dqopt = sb_dqopt(sb); + struct quota_id *entry = NULL; + int ret = 0; + + if (!sb_has_quota_active(sb, qid->type)) + return -ESRCH; + + down_read(&dqopt->dqio_sem); + while (node) { + entry = rb_entry(node, struct quota_id, node); + + if (id < entry->id) + node = node->rb_left; + else if (id > entry->id) + node = node->rb_right; + else + goto got_next_id; + } + + if (!entry) { + ret = -ENOENT; + goto out_unlock; + } + + if (id > entry->id) { + node = rb_next(&entry->node); + if (!node) { + ret = -ENOENT; + goto out_unlock; + } + entry = rb_entry(node, struct quota_id, node); + } + +got_next_id: + *qid = make_kqid(&init_user_ns, qid->type, entry->id); +out_unlock: + up_read(&dqopt->dqio_sem); + return ret; +} + +/* + * Load dquot with limits from existing entry, or create the new entry if + * it does not exist. + */ +static int shmem_acquire_dquot(struct dquot *dquot) +{ + struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); + struct rb_node **n = &((struct rb_root *)info->dqi_priv)->rb_node; + struct rb_node *parent = NULL, *new_node = NULL; + struct quota_id *new_entry, *entry; + qid_t id = from_kqid(&init_user_ns, dquot->dq_id); + struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); + int ret = 0; + + mutex_lock(&dquot->dq_lock); + + down_write(&dqopt->dqio_sem); + while (*n) { + parent = *n; + entry = rb_entry(parent, struct quota_id, node); + + if (id < entry->id) + n = &(*n)->rb_left; + else if (id > entry->id) + n = &(*n)->rb_right; + else + goto found; + } + + /* We don't have entry for this id yet, create it */ + new_entry = kzalloc(sizeof(struct quota_id), GFP_NOFS); + if (!new_entry) { + ret = -ENOMEM; + goto out_unlock; + } + + new_entry->id = id; + new_node = &new_entry->node; + rb_link_node(new_node, parent, n); + rb_insert_color(new_node, (struct rb_root *)info->dqi_priv); + entry = new_entry; + +found: + /* Load the stored limits from the tree */ + spin_lock(&dquot->dq_dqb_lock); + dquot->dq_dqb.dqb_bhardlimit = entry->bhardlimit; + dquot->dq_dqb.dqb_bsoftlimit = entry->bsoftlimit; + dquot->dq_dqb.dqb_ihardlimit = entry->ihardlimit; + dquot->dq_dqb.dqb_isoftlimit = entry->isoftlimit; + + if (!dquot->dq_dqb.dqb_bhardlimit && + !dquot->dq_dqb.dqb_bsoftlimit && + !dquot->dq_dqb.dqb_ihardlimit && + !dquot->dq_dqb.dqb_isoftlimit) + set_bit(DQ_FAKE_B, &dquot->dq_flags); + spin_unlock(&dquot->dq_dqb_lock); + + /* Make sure flags update is visible after dquot has been filled */ + smp_mb__before_atomic(); + set_bit(DQ_ACTIVE_B, &dquot->dq_flags); +out_unlock: + up_write(&dqopt->dqio_sem); + mutex_unlock(&dquot->dq_lock); + return ret; +} + +/* + * Store limits from dquot in the tree unless it's fake. If it is fake + * remove the id from the tree since there is no useful information in + * there. + */ +static int shmem_release_dquot(struct dquot *dquot) +{ + struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); + struct rb_node *node = ((struct rb_root *)info->dqi_priv)->rb_node; + qid_t id = from_kqid(&init_user_ns, dquot->dq_id); + struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); + struct quota_id *entry = NULL; + + mutex_lock(&dquot->dq_lock); + /* Check whether we are not racing with some other dqget() */ + if (dquot_is_busy(dquot)) + goto out_dqlock; + + down_write(&dqopt->dqio_sem); + while (node) { + entry = rb_entry(node, struct quota_id, node); + + if (id < entry->id) + node = node->rb_left; + else if (id > entry->id) + node = node->rb_right; + else + goto found; + } + + /* We should always find the entry in the rb tree */ + WARN_ONCE(1, "quota id %u from dquot %p, not in rb tree!\n", id, dquot); + up_write(&dqopt->dqio_sem); + mutex_unlock(&dquot->dq_lock); + return -ENOENT; + +found: + if (test_bit(DQ_FAKE_B, &dquot->dq_flags)) { + /* Remove entry from the tree */ + rb_erase(&entry->node, info->dqi_priv); + kfree(entry); + } else { + /* Store the limits in the tree */ + spin_lock(&dquot->dq_dqb_lock); + entry->bhardlimit = dquot->dq_dqb.dqb_bhardlimit; + entry->bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit; + entry->ihardlimit = dquot->dq_dqb.dqb_ihardlimit; + entry->isoftlimit = dquot->dq_dqb.dqb_isoftlimit; + spin_unlock(&dquot->dq_dqb_lock); + } + + clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); + up_write(&dqopt->dqio_sem); + +out_dqlock: + mutex_unlock(&dquot->dq_lock); + return 0; +} + +static int shmem_mark_dquot_dirty(struct dquot *dquot) +{ + return 0; +} + +static int shmem_dquot_write_info(struct super_block *sb, int type) +{ + return 0; +} + +static const struct quota_format_ops shmem_format_ops = { + .check_quota_file = shmem_check_quota_file, + .read_file_info = shmem_read_file_info, + .write_file_info = shmem_write_file_info, + .free_file_info = shmem_free_file_info, +}; + +struct quota_format_type shmem_quota_format = { + .qf_fmt_id = QFMT_SHMEM, + .qf_ops = &shmem_format_ops, + .qf_owner = THIS_MODULE +}; + +const struct dquot_operations shmem_quota_operations = { + .acquire_dquot = shmem_acquire_dquot, + .release_dquot = shmem_release_dquot, + .alloc_dquot = dquot_alloc, + .destroy_dquot = dquot_destroy, + .write_info = shmem_dquot_write_info, + .mark_dirty = shmem_mark_dquot_dirty, + .get_next_id = shmem_get_next_id, +}; +#endif /* CONFIG_TMPFS_QUOTA */ From patchwork Tue Jul 25 14:45:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326570 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D82CC001DF for ; Tue, 25 Jul 2023 14:45:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BDF66B007D; Tue, 25 Jul 2023 10:45:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 06F246B007E; Tue, 25 Jul 2023 10:45:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E50ED8D0001; Tue, 25 Jul 2023 10:45:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D37456B007D for ; Tue, 25 Jul 2023 10:45:34 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6A64A160E00 for ; Tue, 25 Jul 2023 14:45:34 +0000 (UTC) X-FDA: 81050407788.06.E31AD54 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id E41C714002B for ; Tue, 25 Jul 2023 14:45:31 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IrdOwJZo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296332; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6naRwCXxuUzGCkRIF5f+GfJQXa4wH345A/1iRaT7rxs=; b=3HcqYrLV1UiiPT1nhGIK7v6VQcSz1UuocCDGz0ESSft2r+vK/KBlrBeGfrnY+T5FkzWlLw YodbwcTABUeoA/O+xEPXO4V5BZKLM8X6fEhbAsV29UiL4kunGl3AWkdRlL8JnGzDy365BF riZib5pPqmrfFQ9xrwT+UtKZbASWyDs= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IrdOwJZo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296332; a=rsa-sha256; cv=none; b=pFbW4BDmrcIoinupMnANUQ7rRTbqMCxxQS5SlFgEDq9ZkDAZ9hBGEz038F8LTDscd51UYf 5U5NhwdVekh5hIS2P4t4GGgJ54TNbbdulOULRMbnF7erWnHyuO8yOpb7kYI4QXQto2mmVn WS4ty3xYtVgFMaNm35RTK2nt/D/tKxk= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EF7016178F; Tue, 25 Jul 2023 14:45:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C60AC433CA; Tue, 25 Jul 2023 14:45:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296330; bh=Lx002CNlkWLsF2EjoxoEtNrs2ktIDBHz9wc8h2aQndk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IrdOwJZoTNXOvLVtMNlSNkikVy5s/u+/pMnIDR9Wec4AShjc0I13tXZNkA3JQktUv Bcy5nH+mce4f/GxnosWkSpIhT1AlJg1lY7/Ei2VC0Zc69gM+YSyemXBKcl+o95ilac ojFtH7BcokOHx3YgS+kIEJPKUvzP6Mt5AJ0LqHhsyPU9U5ph4wDaaEu5qjlRljL1at 1oh1mhRnpzV6tY+6K8iEc+g/AVCkKzCFDf4xrfFPZBXPBI5FGe3Esw44abAh5wQwbF 3HkJ3lF1zYK3l7HBGMFdiopokhTieClP+4FApn3kNP98mqAS2dWee5XGPRFfg2B8FQ 3MKCt4pZBUbxQ== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 5/7] shmem: quota support Date: Tue, 25 Jul 2023 16:45:08 +0200 Message-Id: <20230725144510.253763-6-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E41C714002B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 13abwdwt1ngsrjs14k9t7naxdres59x3 X-HE-Tag: 1690296331-853351 X-HE-Meta: U2FsdGVkX19jEyMPl5Igj1XMVbYdng6n7Dwv0Vxmb8RbLOfwUkEDci/cIt1MrwesA9uHIzRyin6ZWbxFVoDclW/5LzlZdLqqzp1UPMLEbXqlzsrvKSuTmRwOzECf16cOWP0pqy2cAFwzBxI3YM/Id8DCzm8q+UsA0xPhttjaiGawQRsBz6H9v0xI7MTsY0E26Mnjy7pV+BLBidj4V5KLID/Kuf41DDkLqKpZba9KxuCIdlGg9dbbl9vxL+6lkbtYSXjPSy03/jqR5cLx/K6gTRR54AMySZuropdipOG4zGHAmsV67nIrVnLM1C5amUoGpSWPGBQgxlRY6ExLCRCgafienWn973ZrlWRUF/1gOB0SSy3F3m1mJ5muSKPWQjnebik31cRm7ehqzqtOML5t7nnIMT0N5p8pghSOVAA3x8dE35zGxwCAJpjIU4IFOAfTwQUVOW/xh5zxFemmex/CYNFaD+xwd8syL7Fiv6fqcKet+5E5iy2xfM26P9kvl98tkZEUWMrcGtYBiAvf1xbQ76LgOrMfr96yJ6BAkmLNMqn+XnCYLwVN5IJVmMBxdQpJvwJ21iWqR+zwHXihRqQdWwzl1Fj1rl+h3nfNkSumN25OjzysMcNQoCglsUb+wwjsxQtbwa0UXcel5xcuzu3QKcRKtF207OpQ8T/qZGtSrY/bNJIEmj94SB9+NbAM/bGEKhD6czP7Hp4HfygI8youHHtynVaC5Awl49vcRvX78Df/ImKeELTkBjmS0VcgdrIHb0zIjWvRTK3uoFXjYBYnTP56g7LLCzhWNrCLECkZ1TX/84MiMIgMAqL+XHpGUTqzWyFcz3dAGpkBzZIhl151RNLKMeG+vMVn+ayst4zCn2Jc/L97FSzZP6qLYCS3hwzAh+dGM5QnuRtm1k2l6qnvupr7s2ZXxTJbGlurCY3fJQcW2Cdf3u5dpgzS+4YjSzBk2/+zQmUGHNbKIiJ4+zu hGCfPJUz icdegq+tngmjKrB9uI7WseYSxD/oEfKRWQXE+dvFUD6bpHL0BazH8Y+P66+5r5NKbUcbc2e6mRw+Yh4c84Tgc2Vl8qxKPzrvTiBlYCiVvH9tQv0W/c1mtGWiidqobaYARHDXcK7SgYBPspitR7/DiJmZd+ZqMy/9BG4JoTaoHHOc5QSi+LKvS4MRq0qLn/zUJrNTOGUVUhTg4k9R0qs1uy7wW0lHmR/h74l6llQUYoEoQBIqWw2XdU1yg4KlbyT0fhzaBrBege5THLaKoF7I8sjhTjAb3v+tj4ShKpPwqDZa09UG99mrtu3rSmPwiaCwlEu6U2+eguLESy6pr5dGMLVBwwNsiHC3DGek2i2nL3HVqFM0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Carlos Maiolino Now the basic infra-structure is in place, enable quota support for tmpfs. This offers user and group quotas to tmpfs (project quotas will be added later). Also, as other filesystems, the tmpfs quota is not supported within user namespaces yet, so idmapping is not translated. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- Documentation/filesystems/tmpfs.rst | 15 +++ include/linux/shmem_fs.h | 8 ++ mm/shmem.c | 186 ++++++++++++++++++++++++++-- 3 files changed, 201 insertions(+), 8 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystems/tmpfs.rst index f18f46be5c0c..0c7d8bd052f1 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -130,6 +130,21 @@ for emergency or testing purposes. The values you can set for shmem_enabled are: option, for testing == ============================================================ +tmpfs also supports quota with the following mount options + +======== ============================================================= +quota User and group quota accounting and enforcement is enabled on + the mount. Tmpfs is using hidden system quota files that are + initialized on mount. +usrquota User quota accounting and enforcement is enabled on the + mount. +grpquota Group quota accounting and enforcement is enabled on the + mount. +======== ============================================================= + +Note that tmpfs quotas do not support user namespaces so no uid/gid +translation is done if quotas are enabled inside user namespaces. + tmpfs has a mount option to set the NUMA memory allocation policy for all files in that instance (if CONFIG_NUMA is enabled) - which can be adjusted on the fly via 'mount -o remount ...' diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 7abfaf70b58a..1a568a0f542f 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -31,6 +31,9 @@ struct shmem_inode_info { atomic_t stop_eviction; /* hold when working on inode */ struct timespec64 i_crtime; /* file creation time */ unsigned int fsflags; /* flags for FS_IOC_[SG]ETFLAGS */ +#ifdef CONFIG_TMPFS_QUOTA + struct dquot *i_dquot[MAXQUOTAS]; +#endif struct inode vfs_inode; }; @@ -184,4 +187,9 @@ extern int shmem_mfill_atomic_pte(pmd_t *dst_pmd, #define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */ #define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL +#ifdef CONFIG_TMPFS_QUOTA +extern const struct dquot_operations shmem_quota_operations; +extern struct quota_format_type shmem_quota_format; +#endif /* CONFIG_TMPFS_QUOTA */ + #endif diff --git a/mm/shmem.c b/mm/shmem.c index 2a7b8060b6f4..7c75f30309ff 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -78,6 +78,7 @@ static struct vfsmount *shm_mnt; #include #include #include +#include #include @@ -116,11 +117,13 @@ struct shmem_options { int huge; int seen; bool noswap; + unsigned short quota_types; #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_QUOTA 32 }; #ifdef CONFIG_TMPFS @@ -212,7 +215,16 @@ static inline int shmem_inode_acct_block(struct inode *inode, long pages) if (percpu_counter_compare(&sbinfo->used_blocks, sbinfo->max_blocks - pages) > 0) goto unacct; + + err = dquot_alloc_block_nodirty(inode, pages); + if (err) + goto unacct; + percpu_counter_add(&sbinfo->used_blocks, pages); + } else { + err = dquot_alloc_block_nodirty(inode, pages); + if (err) + goto unacct; } return 0; @@ -227,6 +239,8 @@ static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); + dquot_free_block_nodirty(inode, pages); + if (sbinfo->max_blocks) percpu_counter_sub(&sbinfo->used_blocks, pages); shmem_unacct_blocks(info->flags, pages); @@ -255,6 +269,47 @@ bool vma_is_shmem(struct vm_area_struct *vma) static LIST_HEAD(shmem_swaplist); static DEFINE_MUTEX(shmem_swaplist_mutex); +#ifdef CONFIG_TMPFS_QUOTA + +static int shmem_enable_quotas(struct super_block *sb, + unsigned short quota_types) +{ + int type, err = 0; + + sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; + for (type = 0; type < SHMEM_MAXQUOTAS; type++) { + if (!(quota_types & (1 << type))) + continue; + err = dquot_load_quota_sb(sb, type, QFMT_SHMEM, + DQUOT_USAGE_ENABLED | + DQUOT_LIMITS_ENABLED); + if (err) + goto out_err; + } + return 0; + +out_err: + pr_warn("tmpfs: failed to enable quota tracking (type=%d, err=%d)\n", + type, err); + for (type--; type >= 0; type--) + dquot_quota_off(sb, type); + return err; +} + +static void shmem_disable_quotas(struct super_block *sb) +{ + int type; + + for (type = 0; type < SHMEM_MAXQUOTAS; type++) + dquot_quota_off(sb, type); +} + +static struct dquot **shmem_get_dquots(struct inode *inode) +{ + return SHMEM_I(inode)->i_dquot; +} +#endif /* CONFIG_TMPFS_QUOTA */ + /* * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and * produces a novel ino for the newly allocated inode. @@ -361,7 +416,6 @@ static void shmem_recalc_inode(struct inode *inode) freed = info->alloced - info->swapped - inode->i_mapping->nrpages; if (freed > 0) { info->alloced -= freed; - inode->i_blocks -= freed * BLOCKS_PER_PAGE; shmem_inode_unacct_blocks(inode, freed); } } @@ -379,7 +433,6 @@ bool shmem_charge(struct inode *inode, long pages) spin_lock_irqsave(&info->lock, flags); info->alloced += pages; - inode->i_blocks += pages * BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); @@ -395,7 +448,6 @@ void shmem_uncharge(struct inode *inode, long pages) spin_lock_irqsave(&info->lock, flags); info->alloced -= pages; - inode->i_blocks -= pages * BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irqrestore(&info->lock, flags); @@ -1141,6 +1193,21 @@ static int shmem_setattr(struct mnt_idmap *idmap, } } + if (is_quota_modification(idmap, inode, attr)) { + error = dquot_initialize(inode); + if (error) + return error; + } + + /* Transfer quota accounting */ + if (i_uid_needs_update(idmap, attr, inode) || + i_gid_needs_update(idmap, attr, inode)) { + error = dquot_transfer(idmap, inode, attr); + + if (error) + return error; + } + setattr_copy(idmap, inode, attr); if (attr->ia_valid & ATTR_MODE) error = posix_acl_chmod(idmap, dentry, inode->i_mode); @@ -1187,6 +1254,10 @@ static void shmem_evict_inode(struct inode *inode) WARN_ON(inode->i_blocks); shmem_free_inode(inode->i_sb); clear_inode(inode); +#ifdef CONFIG_TMPFS_QUOTA + dquot_free_inode(inode); + dquot_drop(inode); +#endif } static int shmem_find_swap_entries(struct address_space *mapping, @@ -1986,7 +2057,6 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, spin_lock_irq(&info->lock); info->alloced += folio_nr_pages(folio); - inode->i_blocks += (blkcnt_t)BLOCKS_PER_PAGE << folio_order(folio); shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); alloced = true; @@ -2357,9 +2427,10 @@ static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) #define shmem_initxattrs NULL #endif -static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block *sb, - struct inode *dir, umode_t mode, dev_t dev, - unsigned long flags) +static struct inode *__shmem_get_inode(struct mnt_idmap *idmap, + struct super_block *sb, + struct inode *dir, umode_t mode, + dev_t dev, unsigned long flags) { struct inode *inode; struct shmem_inode_info *info; @@ -2436,6 +2507,43 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block return inode; } +#ifdef CONFIG_TMPFS_QUOTA +static struct inode *shmem_get_inode(struct mnt_idmap *idmap, + struct super_block *sb, struct inode *dir, + umode_t mode, dev_t dev, unsigned long flags) +{ + int err; + struct inode *inode; + + inode = __shmem_get_inode(idmap, sb, dir, mode, dev, flags); + if (IS_ERR(inode)) + return inode; + + err = dquot_initialize(inode); + if (err) + goto errout; + + err = dquot_alloc_inode(inode); + if (err) { + dquot_drop(inode); + goto errout; + } + return inode; + +errout: + inode->i_flags |= S_NOQUOTA; + iput(inode); + return ERR_PTR(err); +} +#else +static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, + struct super_block *sb, struct inode *dir, + umode_t mode, dev_t dev, unsigned long flags) +{ + return __shmem_get_inode(idmap, sb, dir, mode, dev, flags); +} +#endif /* CONFIG_TMPFS_QUOTA */ + #ifdef CONFIG_USERFAULTFD int shmem_mfill_atomic_pte(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, @@ -2538,7 +2646,6 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, spin_lock_irq(&info->lock); info->alloced++; - inode->i_blocks += BLOCKS_PER_PAGE; shmem_recalc_inode(inode); spin_unlock_irq(&info->lock); @@ -3516,6 +3623,7 @@ static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) static const struct inode_operations shmem_short_symlink_operations = { .getattr = shmem_getattr, + .setattr = shmem_setattr, .get_link = simple_get_link, #ifdef CONFIG_TMPFS_XATTR .listxattr = shmem_listxattr, @@ -3524,6 +3632,7 @@ static const struct inode_operations shmem_short_symlink_operations = { static const struct inode_operations shmem_symlink_inode_operations = { .getattr = shmem_getattr, + .setattr = shmem_setattr, .get_link = shmem_get_link, #ifdef CONFIG_TMPFS_XATTR .listxattr = shmem_listxattr, @@ -3623,6 +3732,9 @@ enum shmem_param { Opt_inode32, Opt_inode64, Opt_noswap, + Opt_quota, + Opt_usrquota, + Opt_grpquota, }; static const struct constant_table shmem_param_enums_huge[] = { @@ -3645,6 +3757,11 @@ const struct fs_parameter_spec shmem_fs_parameters[] = { fsparam_flag ("inode32", Opt_inode32), fsparam_flag ("inode64", Opt_inode64), fsparam_flag ("noswap", Opt_noswap), +#ifdef CONFIG_TMPFS_QUOTA + fsparam_flag ("quota", Opt_quota), + fsparam_flag ("usrquota", Opt_usrquota), + fsparam_flag ("grpquota", Opt_grpquota), +#endif {} }; @@ -3736,6 +3853,24 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) ctx->noswap = true; ctx->seen |= SHMEM_SEEN_NOSWAP; break; + case Opt_quota: + if (fc->user_ns != &init_user_ns) + return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= (QTYPE_MASK_USR | QTYPE_MASK_GRP); + break; + case Opt_usrquota: + if (fc->user_ns != &init_user_ns) + return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_USR; + break; + case Opt_grpquota: + if (fc->user_ns != &init_user_ns) + return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); + ctx->seen |= SHMEM_SEEN_QUOTA; + ctx->quota_types |= QTYPE_MASK_GRP; + break; } return 0; @@ -3843,6 +3978,12 @@ static int shmem_reconfigure(struct fs_context *fc) goto out; } + if (ctx->seen & SHMEM_SEEN_QUOTA && + !sb_any_quota_loaded(fc->root->d_sb)) { + err = "Cannot enable quota on remount"; + goto out; + } + if (ctx->seen & SHMEM_SEEN_HUGE) sbinfo->huge = ctx->huge; if (ctx->seen & SHMEM_SEEN_INUMS) @@ -3934,6 +4075,9 @@ static void shmem_put_super(struct super_block *sb) { struct shmem_sb_info *sbinfo = SHMEM_SB(sb); +#ifdef CONFIG_TMPFS_QUOTA + shmem_disable_quotas(sb); +#endif free_percpu(sbinfo->ino_batch); percpu_counter_destroy(&sbinfo->used_blocks); mpol_put(sbinfo->mpol); @@ -4013,6 +4157,17 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) #endif uuid_gen(&sb->s_uuid); +#ifdef CONFIG_TMPFS_QUOTA + if (ctx->seen & SHMEM_SEEN_QUOTA) { + sb->dq_op = &shmem_quota_operations; + sb->s_qcop = &dquot_quotactl_sysfile_ops; + sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; + + if (shmem_enable_quotas(sb, ctx->quota_types)) + goto failed; + } +#endif /* CONFIG_TMPFS_QUOTA */ + inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); if (IS_ERR(inode)) { @@ -4188,6 +4343,9 @@ static const struct super_operations shmem_ops = { #ifdef CONFIG_TMPFS .statfs = shmem_statfs, .show_options = shmem_show_options, +#endif +#ifdef CONFIG_TMPFS_QUOTA + .get_dquots = shmem_get_dquots, #endif .evict_inode = shmem_evict_inode, .drop_inode = generic_delete_inode, @@ -4254,6 +4412,14 @@ void __init shmem_init(void) shmem_init_inodecache(); +#ifdef CONFIG_TMPFS_QUOTA + error = register_quota_format(&shmem_quota_format); + if (error < 0) { + pr_err("Could not register quota format\n"); + goto out3; + } +#endif + error = register_filesystem(&shmem_fs_type); if (error) { pr_err("Could not register tmpfs\n"); @@ -4278,6 +4444,10 @@ void __init shmem_init(void) out1: unregister_filesystem(&shmem_fs_type); out2: +#ifdef CONFIG_TMPFS_QUOTA + unregister_quota_format(&shmem_quota_format); +out3: +#endif shmem_destroy_inodecache(); shm_mnt = ERR_PTR(error); } From patchwork Tue Jul 25 14:45:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326571 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5179C04E69 for ; Tue, 25 Jul 2023 14:45:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F2416B007E; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A49E6B0080; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21C3D8D0001; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0A3E76B007E for ; Tue, 25 Jul 2023 10:45:37 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9E8DC160E50 for ; Tue, 25 Jul 2023 14:45:36 +0000 (UTC) X-FDA: 81050407872.04.DB3DDBE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 82F59180021 for ; Tue, 25 Jul 2023 14:45:34 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ICbNbwGK; spf=pass (imf06.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296334; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pxoJg+Pt00xjW1wqRTQRQVCqBr/0yKn3dxhDDQn6B8k=; b=wI8Vf3gTRUcfr79ubrCfGxOwFxFgbv3w9LrCPiG11LPrRTs63iihf4YGWiXefrs1cLwwae HmJaspkrF0lLArsrgE2SheA5Zsp810AatsTrejS3PoJS5kt3PncmHioO8g/u3GGQLP0b8S 9X53zL7V5E4uJcUyfqahwyfO05Kv6qg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296334; a=rsa-sha256; cv=none; b=d2eSoDwUlmoGTd0V6J3VBUNuB2lYTffTfvWzpo+gql9QGh7eZfusuEytnfqkuCMW6L6zDL kEKCKdxycjOr7gcGK3fYjtkYSzYKCSMnE35et7bJTDObksDnQOZ9a106W8Snyis6s5E9h9 0tJvN9uaMOdEXcNZ7+km6N6FlDXqQkE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ICbNbwGK; spf=pass (imf06.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 86A7961788; Tue, 25 Jul 2023 14:45:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDE7EC433CD; Tue, 25 Jul 2023 14:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296333; bh=lISOjIRoZ8eyNsvIWJg3pEzAgdhmgsQyuciEDHNazkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ICbNbwGK9vPDstPO+SP6gjRMfJPQmQ439ac+WmNnjrEofwPgn2a052ZfY/qgOVTJe wg6JS4Y8YJP3llQfB+CIthE7rEkkdZXUwdNC+IvP5BoiGTcnMZc3qEgwIjerzCkzgS OvXT44hXRcfvRnuo/JxCSgyha5SMALEDg9nuX1SrSgmSTbSrqWxcaFeiCkW/aAZvaI qj9imUV9s+f1KLnUWcV3ikA1Aas8emPYtwz+DwX47+xahOXLI3n6dv7X8lw7RmdPFq tKAL2tGGJsUQpGVxPqWPJF1bc2IAmFTsXztaAq+SOjlgoIfy883HaTlzclKu6eiTkD W4KsRrR4NCBzA== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 6/7] shmem: Add default quota limit mount options Date: Tue, 25 Jul 2023 16:45:09 +0200 Message-Id: <20230725144510.253763-7-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Stat-Signature: etict9skmub33synoa9c9q9o5c9iwapf X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 82F59180021 X-Rspam-User: X-HE-Tag: 1690296334-51697 X-HE-Meta: U2FsdGVkX1/ztYKVfD50XVnfHDuBipO6/IDy60O7zDRlP0ZW5WoOkIO8j8L+jk+SksmG0My42Mh+kvykbL+78QTbHixBebxrruIWiu+nH+1eIAvGwOhMvMmoHIlTLXDGRydsEPfvLNLQ8v4cF5u9qsZveiu+fC6hvpJVzkZPkxOSo1NasofosRecw6mQM6t6KhUmjvOWLOlS1WWrNOi/c/DzyoX1uE1uNyOyObVBi7dlfEUa5yJZEIJm6YEMGO/4PKGIWPJx4AUOxQCSyN534Z1PrC/gmE4+7L+XIbxgJMuojT9XJQcQpvhFhAlySEg3+nSfuGAd402djwRuinS4d0dVBcFWlywB+ZT0HMi6BnMjyk2Al/kpxT1AIJeFGWDP6TebSZeJ3F7KVTpkXRAyypWv/xUyIzpK1pyCb6AdDCGrQR4wbmBURznwKjDutyL0lWWYLIK7uG+8aFoV52RLthccFamKKTq0DNKGKaAp8ECntGK86AerKHNOJ+MWW7aZx2Ua7U50531B0wUSWdii/XFgEsuRpXr++khInQFafel6jRBRIP8dkF5zAbLx3ROCRgeoWy1QmxDpTaLfX4ev58ocui6iKW+aSvZDLPBB8taMSeYoLa5MaRnuLjW0e396SSZ9ROGdiOCrkj8iwcULtwNXTyuj9oaylm3U8HJC/iw/3QuhzvqYFDDtND5Zm6l3yJYPkxD9/6mI5fHCFpj1WcZV7yVLnYSvdn3JJH4sAn/U081JQOtMN3UMMfn1Tba43p5tQJjAbZJZtPmmsOXEaCbxsv6FlH8m6ToGjJXBX2I4Cu4RMlrZoe4sstmO2YqFkR0zcYmHJf5KxUZ8E51+lyQvXOqezf0g7DheDvdisfIXtTFjkPT/zCLy1Tqlhvt4/DG1hxr3gi/xDyEfyVr/1MdIhEFX3XwY2INBLFAYFjifNk67t97L06nI5XS0Z84w2WdowL3T6WXWhLBR2GW tZ6M/55a 1X623i92jhCaWl/HvBUwUTGBtuJpK06Igs5cVnELsbROCOfB8F+rhgah6Jk/b2UItx+xpK4W5lbvb2n9WF8JQEDNuEzNOlLleyI8ow40ienvUKpG93scQTMxghIjEUvJfBr2dFky6aofc0xoSVvsU2ZnTxetQLnf7W6aEM4NimJXpyw8frNu2JBDl3jfkEJ1bawrqSGUa7KXx3VNhhdBad31tszQzn6bBk34woyj4/c7A9johvS84XZ9CTruyrDZtPyuYyEMeRFBInPwiPLKzgbyWNFlJ3DroOwK5ocxLyq0h4l8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Lukas Czerner Allow system administrator to set default global quota limits at tmpfs mount time. Signed-off-by: Lukas Czerner Signed-off-by: Carlos Maiolino Reviewed-by: Jan Kara --- Documentation/filesystems/tmpfs.rst | 34 +++++++++++----- include/linux/shmem_fs.h | 8 ++++ mm/shmem.c | 61 +++++++++++++++++++++++++++++ mm/shmem_quota.c | 34 +++++++++++++++- 4 files changed, 127 insertions(+), 10 deletions(-) diff --git a/Documentation/filesystems/tmpfs.rst b/Documentation/filesystems/tmpfs.rst index 0c7d8bd052f1..f843dbbeb589 100644 --- a/Documentation/filesystems/tmpfs.rst +++ b/Documentation/filesystems/tmpfs.rst @@ -132,15 +132,31 @@ for emergency or testing purposes. The values you can set for shmem_enabled are: tmpfs also supports quota with the following mount options -======== ============================================================= -quota User and group quota accounting and enforcement is enabled on - the mount. Tmpfs is using hidden system quota files that are - initialized on mount. -usrquota User quota accounting and enforcement is enabled on the - mount. -grpquota Group quota accounting and enforcement is enabled on the - mount. -======== ============================================================= +======================== ================================================= +quota User and group quota accounting and enforcement + is enabled on the mount. Tmpfs is using hidden + system quota files that are initialized on mount. +usrquota User quota accounting and enforcement is enabled + on the mount. +grpquota Group quota accounting and enforcement is enabled + on the mount. +usrquota_block_hardlimit Set global user quota block hard limit. +usrquota_inode_hardlimit Set global user quota inode hard limit. +grpquota_block_hardlimit Set global group quota block hard limit. +grpquota_inode_hardlimit Set global group quota inode hard limit. +======================== ================================================= + +None of the quota related mount options can be set or changed on remount. + +Quota limit parameters accept a suffix k, m or g for kilo, mega and giga +and can't be changed on remount. Default global quota limits are taking +effect for any and all user/group/project except root the first time the +quota entry for user/group/project id is being accessed - typically the +first time an inode with a particular id ownership is being created after +the mount. In other words, instead of the limits being initialized to zero, +they are initialized with the particular value provided with these mount +options. The limits can be changed for any user/group id at any time as they +normally can be. Note that tmpfs quotas do not support user namespaces so no uid/gid translation is done if quotas are enabled inside user namespaces. diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 1a568a0f542f..c0058f3bba70 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -42,6 +42,13 @@ struct shmem_inode_info { (FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL) #define SHMEM_FL_INHERITED (FS_NODUMP_FL | FS_NOATIME_FL) +struct shmem_quota_limits { + qsize_t usrquota_bhardlimit; /* Default user quota block hard limit */ + qsize_t usrquota_ihardlimit; /* Default user quota inode hard limit */ + qsize_t grpquota_bhardlimit; /* Default group quota block hard limit */ + qsize_t grpquota_ihardlimit; /* Default group quota inode hard limit */ +}; + struct shmem_sb_info { unsigned long max_blocks; /* How many blocks are allowed */ struct percpu_counter used_blocks; /* How many are allocated */ @@ -60,6 +67,7 @@ struct shmem_sb_info { spinlock_t shrinklist_lock; /* Protects shrinklist */ struct list_head shrinklist; /* List of shinkable inodes */ unsigned long shrinklist_len; /* Length of shrinklist */ + struct shmem_quota_limits qlimits; /* Default quota limits */ }; static inline struct shmem_inode_info *SHMEM_I(struct inode *inode) diff --git a/mm/shmem.c b/mm/shmem.c index 7c75f30309ff..bd02909bacd6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -118,6 +118,7 @@ struct shmem_options { int seen; bool noswap; unsigned short quota_types; + struct shmem_quota_limits qlimits; #define SHMEM_SEEN_BLOCKS 1 #define SHMEM_SEEN_INODES 2 #define SHMEM_SEEN_HUGE 4 @@ -3735,6 +3736,10 @@ enum shmem_param { Opt_quota, Opt_usrquota, Opt_grpquota, + Opt_usrquota_block_hardlimit, + Opt_usrquota_inode_hardlimit, + Opt_grpquota_block_hardlimit, + Opt_grpquota_inode_hardlimit, }; static const struct constant_table shmem_param_enums_huge[] = { @@ -3761,6 +3766,10 @@ const struct fs_parameter_spec shmem_fs_parameters[] = { fsparam_flag ("quota", Opt_quota), fsparam_flag ("usrquota", Opt_usrquota), fsparam_flag ("grpquota", Opt_grpquota), + fsparam_string("usrquota_block_hardlimit", Opt_usrquota_block_hardlimit), + fsparam_string("usrquota_inode_hardlimit", Opt_usrquota_inode_hardlimit), + fsparam_string("grpquota_block_hardlimit", Opt_grpquota_block_hardlimit), + fsparam_string("grpquota_inode_hardlimit", Opt_grpquota_inode_hardlimit), #endif {} }; @@ -3871,6 +3880,42 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) ctx->seen |= SHMEM_SEEN_QUOTA; ctx->quota_types |= QTYPE_MASK_GRP; break; + case Opt_usrquota_block_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) + return invalfc(fc, + "User quota block hardlimit too large."); + ctx->qlimits.usrquota_bhardlimit = size; + break; + case Opt_grpquota_block_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) + return invalfc(fc, + "Group quota block hardlimit too large."); + ctx->qlimits.grpquota_bhardlimit = size; + break; + case Opt_usrquota_inode_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_INO_LIMIT) + return invalfc(fc, + "User quota inode hardlimit too large."); + ctx->qlimits.usrquota_ihardlimit = size; + break; + case Opt_grpquota_inode_hardlimit: + size = memparse(param->string, &rest); + if (*rest || !size) + goto bad_value; + if (size > SHMEM_QUOTA_MAX_INO_LIMIT) + return invalfc(fc, + "Group quota inode hardlimit too large."); + ctx->qlimits.grpquota_ihardlimit = size; + break; } return 0; @@ -3984,6 +4029,18 @@ static int shmem_reconfigure(struct fs_context *fc) goto out; } +#ifdef CONFIG_TMPFS_QUOTA +#define CHANGED_LIMIT(name) \ + (ctx->qlimits.name## hardlimit && \ + (ctx->qlimits.name## hardlimit != sbinfo->qlimits.name## hardlimit)) + + if (CHANGED_LIMIT(usrquota_b) || CHANGED_LIMIT(usrquota_i) || + CHANGED_LIMIT(grpquota_b) || CHANGED_LIMIT(grpquota_i)) { + err = "Cannot change global quota limit on remount"; + goto out; + } +#endif /* CONFIG_TMPFS_QUOTA */ + if (ctx->seen & SHMEM_SEEN_HUGE) sbinfo->huge = ctx->huge; if (ctx->seen & SHMEM_SEEN_INUMS) @@ -4163,6 +4220,10 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_qcop = &dquot_quotactl_sysfile_ops; sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; + /* Copy the default limits from ctx into sbinfo */ + memcpy(&sbinfo->qlimits, &ctx->qlimits, + sizeof(struct shmem_quota_limits)); + if (shmem_enable_quotas(sb, ctx->quota_types)) goto failed; } diff --git a/mm/shmem_quota.c b/mm/shmem_quota.c index e92b8ece9880..062d1c1097ae 100644 --- a/mm/shmem_quota.c +++ b/mm/shmem_quota.c @@ -166,6 +166,7 @@ static int shmem_acquire_dquot(struct dquot *dquot) { struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type); struct rb_node **n = &((struct rb_root *)info->dqi_priv)->rb_node; + struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info; struct rb_node *parent = NULL, *new_node = NULL; struct quota_id *new_entry, *entry; qid_t id = from_kqid(&init_user_ns, dquot->dq_id); @@ -195,6 +196,14 @@ static int shmem_acquire_dquot(struct dquot *dquot) } new_entry->id = id; + if (dquot->dq_id.type == USRQUOTA) { + new_entry->bhardlimit = sbinfo->qlimits.usrquota_bhardlimit; + new_entry->ihardlimit = sbinfo->qlimits.usrquota_ihardlimit; + } else if (dquot->dq_id.type == GRPQUOTA) { + new_entry->bhardlimit = sbinfo->qlimits.grpquota_bhardlimit; + new_entry->ihardlimit = sbinfo->qlimits.grpquota_ihardlimit; + } + new_node = &new_entry->node; rb_link_node(new_node, parent, n); rb_insert_color(new_node, (struct rb_root *)info->dqi_priv); @@ -224,6 +233,29 @@ static int shmem_acquire_dquot(struct dquot *dquot) return ret; } +static bool shmem_is_empty_dquot(struct dquot *dquot) +{ + struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info; + qsize_t bhardlimit; + qsize_t ihardlimit; + + if (dquot->dq_id.type == USRQUOTA) { + bhardlimit = sbinfo->qlimits.usrquota_bhardlimit; + ihardlimit = sbinfo->qlimits.usrquota_ihardlimit; + } else if (dquot->dq_id.type == GRPQUOTA) { + bhardlimit = sbinfo->qlimits.grpquota_bhardlimit; + ihardlimit = sbinfo->qlimits.grpquota_ihardlimit; + } + + if (test_bit(DQ_FAKE_B, &dquot->dq_flags) || + (dquot->dq_dqb.dqb_curspace == 0 && + dquot->dq_dqb.dqb_curinodes == 0 && + dquot->dq_dqb.dqb_bhardlimit == bhardlimit && + dquot->dq_dqb.dqb_ihardlimit == ihardlimit)) + return true; + + return false; +} /* * Store limits from dquot in the tree unless it's fake. If it is fake * remove the id from the tree since there is no useful information in @@ -261,7 +293,7 @@ static int shmem_release_dquot(struct dquot *dquot) return -ENOENT; found: - if (test_bit(DQ_FAKE_B, &dquot->dq_flags)) { + if (shmem_is_empty_dquot(dquot)) { /* Remove entry from the tree */ rb_erase(&entry->node, info->dqi_priv); kfree(entry); From patchwork Tue Jul 25 14:45:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 13326572 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3291CC04FDF for ; Tue, 25 Jul 2023 14:45:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2F776B0080; Tue, 25 Jul 2023 10:45:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE01B6B0081; Tue, 25 Jul 2023 10:45:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA7F08D0001; Tue, 25 Jul 2023 10:45:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9B3696B0080 for ; Tue, 25 Jul 2023 10:45:40 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6A729140323 for ; Tue, 25 Jul 2023 14:45:40 +0000 (UTC) X-FDA: 81050408040.24.38A3171 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 2526B18001A for ; Tue, 25 Jul 2023 14:45:36 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tz5z1Bt1; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690296337; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DdxOXsQ3G3Au5gSOnhH+pr9u+UFDYf2uZiQddlZmYdE=; b=vpl19GrXr7WyIsdbTUCqek2jrnJiPNyuaCf4diGqrcZhlC1F51K4jAwnzLGSiJhiTcD983 eOukbwaG3MjR02+bdXNcKL6c8dDoOxtbcVeLINmGioPQmDXqd4upEYVsaAk8gfjcAejNWT DPeIRUpjyOlC0U40y3t3u/z7mM9X9eA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tz5z1Bt1; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of cem@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cem@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690296337; a=rsa-sha256; cv=none; b=pWLIam/thN7tcYEpUepg2Dj1xeFObV42dS+oo3i7ChhS50c24MlYlqfkqbnBMPz5qnQtIQ g7JUEzf/FFod36gXTEtPf7HHAueR3yOBafxCgoaQWIQ10lp1u52x0J267t3sJAP2KkZXdV tl9/qWS05Z4Q4xgA152ewkuf2LKYZ+M= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4ED4261790; Tue, 25 Jul 2023 14:45:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76547C433C9; Tue, 25 Jul 2023 14:45:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690296335; bh=FqduMW218UjXbigOXgHtnWEu6jRYoPHoS15ts7Vta+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tz5z1Bt1rRRlUekc6O6q7KfGynmu+z+ELJkl6mLrLYvuMugTk7pV0o6kHp5yfkS52 xaHg+jlh7iYD77h5HRAjCe0bcAuub2a19HFUEAg4gBHejxGIrUNnuvzCPrFhnAg3RF bGRz4ubaQYbB7Wopc6aqu4uLS2VVLMtvnHkh4PraPGBpnWTXSxmruDXY5refk3/cGf z/Wtnqh9kcf2bExBfJwbf47O+4ullVjPLWzWBvYDKSBSOuCWInzIYMhOMU62AuPlFq k0qHd0/Vjh9HajsegiiW7cI1fUYiAKFpXEcsxqYPgBrd2XP8aaCWVVUmOn9sY3AfmS 1wTjybPZC+xyA== From: cem@kernel.org To: linux-fsdevel@vger.kernel.org Cc: jack@suse.cz, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, linux-mm@kvack.org, djwong@kernel.org, hughd@google.com, brauner@kernel.org, mcgrof@kernel.org Subject: [PATCH 7/7] shmem: fix quota lock nesting in huge hole handling Date: Tue, 25 Jul 2023 16:45:10 +0200 Message-Id: <20230725144510.253763-8-cem@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725144510.253763-1-cem@kernel.org> References: <20230725144510.253763-1-cem@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2526B18001A X-Stat-Signature: kogkyg9ip9bwjig54k8mp9ty6ticcyrd X-HE-Tag: 1690296336-257151 X-HE-Meta: U2FsdGVkX199l6+Pdnj9DSfltpK5DVO9tnp6SLGozinrLKejZuvZiE2rg1J9ZQKk8Z46/wng+K7KWVZbhZZB6Vy+W7vFVWKH4hUSqWQgPeRUC/7X01cr/OTwOi8LmFMfRphJSy+KSbbp2/XxxkAOFelYRwWVgRDcQGzcV8EOshQzR13rDIShFRaycZH7Zs254mTwwMnQesriqo0hiBVSzAGA22hOlryQ78YTZ8s/6X0RhJqR/1DPNIUG3m/jyb5BDxRo06bxj9xvflROXYeT9p0j2tB/N5i7u3+ErY1pS6cIiQ8qvWJS4GTT3VuP37HVDcg2FodLde2/1ehrXgDqSfbkN5dvgwhjuI/RSuz487M385rn9eC6MkeThdlGC9PKSDJbqn6h6tVd+7WbQoGvZ0WiC71B4wH5foDnrQkiiMK19YcdsT+XiT+3G3SlAtt8/+GvnesKmsAG75kU8MK3z5ZouWXFZuoNcEstqeNtfO+ov1Pbe/UERObWi+A54YN/WO6y0suIt2yrnfuZm+KK/VfRfKAdv+eXhQrnsabHNbtOxy4NpJnlgzjta3VqVP2eTdp6daYEgNHIF0iI9u4eNLR8zLJaQNPAgtQIOKgp0KOrMJtbs9qB3C/4PsrdNqY2scfGusf0TGYRiCFFbKYMS06Pl+6Cz/4++fTrqETmonkP2W2ub3DFmjC5Mto+sUdlMAmeh6u0ivKWp+7n+3zRb++eyzDR/cWoXNtqCUoK9tjaoHAV7reovusjpb+AnbXPvaN0SN/7DTz9wHgiBwWAxGeVoQAtz5AYvXWcv+wisbeicprBdkBbBh1sBuipkxzHLbxwXGVdtcTkScJOi3P9NPqDPYno8JZo8buie8Hd5hhjmy5b0CH2pNNq7n7B1h71NkezS1DVplrk0iGAzZbThgiDb9o5M9P2Pw6CQkB4v8OonO+mZqPaVEABbUrn1BsCUCR0yTY/i5qCLEDhW7X 9f9FBF8m 7yQf5gyq3j1uEhakQAvHDgz58DS99Vng3MKtz1OC76NmamrSN7y/WV72qtsnFKqzzlZ5pbpHm9pQ3DRqR52B5TkfEP+q1ZwUfylwIKy5OicsUO7p+ZNWyMmLu/dhFZ/e/dSu0tc/H4krhV778aAFyYkt1iHihkawFVE845hK5A8Hfxez96Ggl0pEJUFF+HrkI3pYHB+LwxUZcbIrizw0UWnJiEMzxbrfjEn4wRXKauWTy6U2PZRxUz8ihvUzt9ee5Ny2di61tJ1gYM3Yh7A8TkaWI1ypDa59UBS/gVjY2JsvsApwho3TwRuuknXATwQ0ZgjT7wQA6PSskaMpxo2fu0Myj6VymuTh/0ITx0mv330T7wEv1hg6EUJSDEfuk50M0O8p4b4anqE/564y7kzFzoEXGr3kZapzVnZEr9VNB5F+HBLHWhQcJ7P7DlkT3XRkipzyF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Hugh Dickins i_pages lock nests inside i_lock, but shmem_charge() and shmem_uncharge() were being called from THP splitting or collapsing while i_pages lock was held, and now go on to call dquot_alloc_block_nodirty() which takes i_lock to update i_blocks. We may well want to take i_lock out of this path later, in the non-quota case even if it's left in the quota case (or perhaps use i_lock instead of shmem's info->lock throughout); but don't get into that at this time. Move the shmem_charge() and shmem_uncharge() calls out from under i_pages lock, accounting the full batch of holes in a single call. Still pass the pages argument to shmem_uncharge(), but it happens now to be unused: shmem_recalc_inode() is designed to account for clean pages freed behind shmem's back, so it gets the accounting right by itself; then the later call to shmem_inode_unacct_blocks() led to imbalance (that WARN_ON(inode->i_blocks) in shmem_evict_inode()). Reported-by: syzbot+38ca19393fb3344f57e6@syzkaller.appspotmail.com Closes: https://lore.kernel.org/lkml/0000000000008e62f40600bfe080@google.com/ Reported-by: syzbot+440ff8cca06ee7a1d4db@syzkaller.appspotmail.com Closes: https://lore.kernel.org/lkml/00000000000076a7840600bfb6e8@google.com/ Signed-off-by: Hugh Dickins Reviewed-by: Carlos Maiolino Tested-by: Carlos Maiolino --- mm/huge_memory.c | 6 ++++-- mm/khugepaged.c | 13 +++++++------ mm/shmem.c | 19 +++++++++---------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index eb3678360b97..d301c323c69a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2521,7 +2521,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, struct address_space *swap_cache = NULL; unsigned long offset = 0; unsigned int nr = thp_nr_pages(head); - int i; + int i, nr_dropped = 0; /* complete memcg works before add pages to LRU */ split_page_memcg(head, nr); @@ -2546,7 +2546,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, struct folio *tail = page_folio(head + i); if (shmem_mapping(head->mapping)) - shmem_uncharge(head->mapping->host, 1); + nr_dropped++; else if (folio_test_clear_dirty(tail)) folio_account_cleaned(tail, inode_to_wb(folio->mapping->host)); @@ -2583,6 +2583,8 @@ static void __split_huge_page(struct page *page, struct list_head *list, } local_irq_enable(); + if (nr_dropped) + shmem_uncharge(head->mapping->host, nr_dropped); remap_page(folio, nr); if (PageSwapCache(head)) { diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 78c8d5d8b628..47d1d32c734f 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1955,10 +1955,6 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, goto xa_locked; } } - if (!shmem_charge(mapping->host, 1)) { - result = SCAN_FAIL; - goto xa_locked; - } nr_none++; continue; } @@ -2145,8 +2141,13 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, */ try_to_unmap_flush(); - if (result != SCAN_SUCCEED) + if (result == SCAN_SUCCEED && nr_none && + !shmem_charge(mapping->host, nr_none)) + result = SCAN_FAIL; + if (result != SCAN_SUCCEED) { + nr_none = 0; goto rollback; + } /* * The old pages are locked, so they won't change anymore. @@ -2283,8 +2284,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, if (nr_none) { xas_lock_irq(&xas); mapping->nrpages -= nr_none; - shmem_uncharge(mapping->host, nr_none); xas_unlock_irq(&xas); + shmem_uncharge(mapping->host, nr_none); } list_for_each_entry_safe(page, tmp, &pagelist, lru) { diff --git a/mm/shmem.c b/mm/shmem.c index bd02909bacd6..5f83c18abc45 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -424,18 +424,20 @@ static void shmem_recalc_inode(struct inode *inode) bool shmem_charge(struct inode *inode, long pages) { struct shmem_inode_info *info = SHMEM_I(inode); - unsigned long flags; + struct address_space *mapping = inode->i_mapping; if (shmem_inode_acct_block(inode, pages)) return false; /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ - inode->i_mapping->nrpages += pages; + xa_lock_irq(&mapping->i_pages); + mapping->nrpages += pages; + xa_unlock_irq(&mapping->i_pages); - spin_lock_irqsave(&info->lock, flags); + spin_lock_irq(&info->lock); info->alloced += pages; shmem_recalc_inode(inode); - spin_unlock_irqrestore(&info->lock, flags); + spin_unlock_irq(&info->lock); return true; } @@ -443,16 +445,13 @@ bool shmem_charge(struct inode *inode, long pages) void shmem_uncharge(struct inode *inode, long pages) { struct shmem_inode_info *info = SHMEM_I(inode); - unsigned long flags; /* nrpages adjustment done by __filemap_remove_folio() or caller */ - spin_lock_irqsave(&info->lock, flags); - info->alloced -= pages; + spin_lock_irq(&info->lock); shmem_recalc_inode(inode); - spin_unlock_irqrestore(&info->lock, flags); - - shmem_inode_unacct_blocks(inode, pages); + /* which has called shmem_inode_unacct_blocks() if necessary */ + spin_unlock_irq(&info->lock); } /*