Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Tim Chen <tim.c.chen@linux.intel.com>
To: Hungyu Lin <dennylin0707@gmail.com>, hughd@google.com
Cc: baolin.wang@linux.alibaba.com, akpm@linux-foundation.org,
	 linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] shmem: clamp negative used-block samples in statfs
Date: Tue, 27 Oct 2026 14:30:03 -0700	[thread overview]
Message-ID: <361569b659ccb6aae7c0d9935f65e16f472eb2b6.camel@linux.intel.com> (raw)
In-Reply-To: <20261027163653.82661-1-dennylin0707@gmail.com>

On Tue, 2026-10-27 at 16:36 +0000, Hungyu Lin wrote:
> percpu_counter_sum() holds the counter lock while scanning the per-CPU
> values, but local updates can still proceed. Concurrent allocation and
> freeing on different CPUs can therefore produce a negative sample even
> though the number of used tmpfs blocks cannot be negative.
> 
> shmem_statfs() subtracts this sample from max_blocks, reporting more free
> blocks than the filesystem's total. With a one-page file being written
> and truncated on separate CPUs, GNU df reports negative used space, for
> example 67108864 total bytes, -28672 used and 67137536 available.
> 
> Use percpu_counter_sum_positive() to clamp a negative sample to zero.
> This retains the approximate accounting and does not change allocation

Nit: I wouldn't call this "approximate". percpu_counter_sum() is the
best-effort accurate variant: it adds up fbc->count and all the per-CPU
deltas under fbc->lock. The lock only excludes batch folds, though, not
the lockless local updates, so a free on one CPU racing with the scan
can make the sum briefly dip below zero. If an approximate value were
fine, percpu_counter_read() would have been used instead.

Maybe something like:

    This keeps the existing best-effort accounting in statfs and does not
    change allocation or limit enforcement.

Otherwise the change looks good to me.

Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>

Tim

> or limit enforcement.
> 
> Fixes: 7e496299d4d2 ("tmpfs: make tmpfs scalable with percpu_counter for used blocks")
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
> ---
>  mm/shmem.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/shmem.c b/mm/shmem.c
> index d97fc32e7c94..092bb45a076c 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -4005,7 +4005,7 @@ static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
>  		buf->f_blocks = sbinfo->max_blocks;
>  		buf->f_bavail =
>  		buf->f_bfree  = sbinfo->max_blocks -
> -				percpu_counter_sum(&sbinfo->used_blocks);
> +				percpu_counter_sum_positive(&sbinfo->used_blocks);
>  	}
>  	if (sbinfo->max_inodes) {
>  		buf->f_files = sbinfo->max_inodes;


      reply	other threads:[~2026-10-27 21:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 16:36 [PATCH] shmem: clamp negative used-block samples in statfs Hungyu Lin
2026-10-27 21:30 ` Tim Chen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=361569b659ccb6aae7c0d9935f65e16f472eb2b6.camel@linux.intel.com \
    --to=tim.c.chen@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=dennylin0707@gmail.com \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox