From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.6 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9429C43381 for ; Tue, 19 Feb 2019 05:38:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 95FD3217D7 for ; Tue, 19 Feb 2019 05:38:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IrVyG7M7" 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 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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);