From patchwork Tue Feb 19 05:37:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 10819199 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 25C06139A for ; Tue, 19 Feb 2019 05:38:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FF0D2AC7F for ; Tue, 19 Feb 2019 05:38:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 00DDF2ACE7; Tue, 19 Feb 2019 05:38:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 994E82AC7F for ; Tue, 19 Feb 2019 05:38:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726172AbfBSFiD (ORCPT ); Tue, 19 Feb 2019 00:38:03 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:44211 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725730AbfBSFiC (ORCPT ); Tue, 19 Feb 2019 00:38:02 -0500 Received: by mail-ot1-f65.google.com with SMTP id g1so32189385otj.11 for ; Mon, 18 Feb 2019 21:38:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=7TgetPiKYsdjnOAeKC/fO5fqwlcjUzyiQLqeYUXldYM=; b=IrVyG7M7DlLpaywnYWIxg3ulWcftgdss9wryWV/2BvZLja/i4ljxEZHrgKal4VbLAE vKDmRu9QPzRujb5aUjCeADBl+RGEmWx/8Ka9B0ZGksJ3/zkIZjNe/DyR2+RFxrpGlwag gQT+PNpzA2LVvRjERFfyhlsCghH+RfA1shAvkfU9AoRANWn1WWmVwMOIvzOzwqQ9yrnn HqrD4fLNqeGnJPCgm5a/LK+ImEFnUCrD8C2SvYqGGBxIvN1CFps9mVyOl1bLT78AALQy +jHGbQB/SuAK//WF0YPJ3ZFiUMePIJ4C7iEMBTvnUDKEm9NzVmEksLmxkuyIZZkk8c9w W6xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=7TgetPiKYsdjnOAeKC/fO5fqwlcjUzyiQLqeYUXldYM=; b=UKK7SHvIHFqEOnfiD2MRdV3Sl09dnWOSgTzExvvXMWxqM+6X03cDVlEl4DfVmeSsTs c5jSyQ5sheu9aeku1g0NNI9tKlSQzd/hN3/0WY1rqAuT43XmiqgFX124KHgFCO2T+oGX ewun4u2yb7blDG8Iq9nShjh2f/S+fE/QhJKAf2Kbgxb7kFrsU8q8kjf9aUXLysXOcVZv Gu4s3/H4qL4H2pqW98KA0mya0Q+BEqoD6vsQs7MOZMmeNgfsK6LSBFYI7jHFJoH7k1lG gKrNGyAoBZanJNarp/Enk9REFTVMuHjMa90nHTzjvwaHFD9BFM0xSed2lO6QLzCp3R3H JGxw== X-Gm-Message-State: AHQUAubIU3Ys7NmOMUMNe3flQLNgF4vPCLFjIXsgqMM2rXV1V+CExNuf 96kw5HHScWciRBtks520TG0H2iXcKnY= X-Google-Smtp-Source: AHgI3IbmXH6vefHGSLsDWVj3usIfcJ+vdeCsDimIXsoxb9GUBNQc1zai148VCOMdqT2Nj1BO1MpAyw== X-Received: by 2002:a05:6830:1648:: with SMTP id h8mr8856727otr.50.1550554681150; Mon, 18 Feb 2019 21:38:01 -0800 (PST) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id n8sm6528310otl.40.2019.02.18.21.37.59 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 18 Feb 2019 21:38:00 -0800 (PST) Date: Mon, 18 Feb 2019 21:37:52 -0800 (PST) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: "Darrick J. Wong" , Hugh Dickins , Matej Kupljen , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] tmpfs: fix link accounting when a tmpfile is linked in Message-ID: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: "Darrick J. Wong" tmpfs has a peculiarity of accounting hard links as if they were separate inodes: so that when the number of inodes is limited, as it is by default, a user cannot soak up an unlimited amount of unreclaimable dcache memory just by repeatedly linking a file. But when v3.11 added O_TMPFILE, and the ability to use linkat() on the fd, we missed accommodating this new case in tmpfs: "df -i" shows that an extra "inode" remains accounted after the file is unlinked and the fd closed and the actual inode evicted. If a user repeatedly links tmpfiles into a tmpfs, the limit will be hit (ENOSPC) even after they are deleted. Just skip the extra reservation from shmem_link() in this case: there's a sense in which this first link of a tmpfile is then cheaper than a hard link of another file, but the accounting works out, and there's still good limiting, so no need to do anything more complicated. Fixes: f4e0c30c191 ("allow the temp files created by open() to be linked to") Reported-by: Matej Kupljen Signed-off-by: Darrick J. Wong Signed-off-by: Hugh Dickins --- mm/shmem.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) --- 5.0-rc7/mm/shmem.c 2019-01-06 19:15:45.764805103 -0800 +++ linux/mm/shmem.c 2019-02-18 13:56:48.388032606 -0800 @@ -2854,10 +2854,14 @@ static int shmem_link(struct dentry *old * No ordinary (disk based) filesystem counts links as inodes; * but each new link needs a new dentry, pinning lowmem, and * tmpfs dentries cannot be pruned until they are unlinked. + * But if an O_TMPFILE file is linked into the tmpfs, the + * first link must skip that, to get the accounting right. */ - ret = shmem_reserve_inode(inode->i_sb); - if (ret) - goto out; + if (inode->i_nlink) { + ret = shmem_reserve_inode(inode->i_sb); + if (ret) + goto out; + } dir->i_size += BOGO_DIRENT_SIZE; inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);