Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp3166125imj; Mon, 18 Feb 2019 21:38:40 -0800 (PST) X-Google-Smtp-Source: AHgI3IYGERxs3VPqZ69HBaeXK71vOCZ0wT1JwYIWz1NgpO403+5hlH9yapz/utMspK3F4nv7YC8x X-Received: by 2002:a17:902:1008:: with SMTP id b8mr28442418pla.252.1550554720737; Mon, 18 Feb 2019 21:38:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550554720; cv=none; d=google.com; s=arc-20160816; b=Yz8mXjnSNKiNyIkZQNfMAdAYnmP2RxytQiQGzNpdeIOZUf9bxFqc/v/QNxvx4RZf81 wOpkVjtE8/rvOwESr2KCIvbmovQIPPYTKZDDsRaYVLK8nEcOoKBtkaHy5wKCeQgNBkd6 lx2mJLyvQSUOqSyb8OIkBGI+DVcCgzCtIS4fwF8yjy/LmBDjvWLYnS/SVmramDM1aRe/ LZccdp6ivbep2C2BT84dNPYA/raRkqFXR69x572m9EISeovh9LMtZbZt/cuzJe5FlZvF g9X4K0RfAw0IVLSWOhpGtO/+kTXjLhah/CLJV15lNp45RHib/d0mbtp7NRgQFLyrOb/V Jb9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :subject:cc:to:from:date:dkim-signature; bh=7TgetPiKYsdjnOAeKC/fO5fqwlcjUzyiQLqeYUXldYM=; b=XW4gW9iubo2maHAtUUHFxLuxsIb/oGUlwAe58kS8sh5Qi75TvG3dYcO9nIHQmFM/lH vsHZx1WJP8bJcpL4mt8D3fL8zXV3WnQ/ktqkrnawCZNd6m2gZDRElM7e9r1WUNw9pbI4 nw7mF+HB/UGuv/AV7aP2EcFPqrcdLHNfq687ftfTlOnBcu3jmXbqDvOiILPzxVKLVrwf Wc+Yjs5s3oalZ/ypECNjWUcsXsfuQdVHIkV3CoaBSFdODXz+XWTjHDg8DwuNiefSGYK3 Nuz9YwnR2Oo8xUHbgHgBd/wQIqNeFUM/73Pfr2aI1INI/tfPlw7sElQAGGzxyyuHgPzz fGmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IrVyG7M7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v32si1950109pgn.342.2019.02.18.21.38.25; Mon, 18 Feb 2019 21:38:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IrVyG7M7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725730AbfBSFiD (ORCPT + 99 others); Tue, 19 Feb 2019 00:38:03 -0500 Received: from mail-ot1-f66.google.com ([209.85.210.66]:33036 "EHLO mail-ot1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725885AbfBSFiC (ORCPT ); Tue, 19 Feb 2019 00:38:02 -0500 Received: by mail-ot1-f66.google.com with SMTP id i20so32358728otl.0 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=hoOvYN2qYQ/h9DsCgDYos21xxIyoZVX7XuCF0MxETqnma6m/hmabqt0stSLVIX6D1U 9WPLXgTSbcEvNI1MFiMPsqVJT3lONb0czkXBoI9XcJER331p5X9tbHmZ7y2AapWHXLj+ BDOE/vh5EgvR3GgIDBDbxtunUNgKkiMngdu5/OC7Qzvk9dgAltTwX/1Gsaqz6B4h/MAc PQNdFRjgtNIRTfHlhrIEFDrI9CjOr7pgOy/NHjx7MpUOJzqA5aNgVTFsYF6T0OT/Hkam 6e7kvGoRNkJVyI/PTOgboUM8YBIMXZwxlhDqldn+9y0kIBll0zbIF9fbhQ/IfxdhD7z+ sxYw== X-Gm-Message-State: AHQUAuaa1zV7dZADDv+E1M3fn2Opf0UVYuqZLUWto9y+wt0nLO1jaIv4 cRchp4Hkrl48INORN9UvpojAZg== 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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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);