Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2261481rwb; Mon, 19 Sep 2022 02:14:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7cKjrchQq+O05AuYSrnLZGGfAwI85cg7aw9spLwHeN8ppS7ul0wFMS7AMnJxr8mY/gsqbU X-Received: by 2002:a63:6cc4:0:b0:41a:ff04:661f with SMTP id h187-20020a636cc4000000b0041aff04661fmr14794322pgc.600.1663578847872; Mon, 19 Sep 2022 02:14:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663578847; cv=none; d=google.com; s=arc-20160816; b=t+9bquPEQlV1EL2I4g8tr4XmIMPXNKiP4EZWc2zFM+Jz+CrOEVXpZ7xVlXJaAjXvyC uBge86PRAUjgg58lapZLupyk3DX0ofbpzWvkBKtKzV7rAPaHXLuZ+fmlmx5rTww9EOC3 ziXNnCDjkMCywqa8ndQWDGbz7ERVGaJkQtp8TGC9gMAaPgUE8t9yeFdTrUpSYJqM1mIf ucW+iRIlkH03H1H7T4x+E7txRZ+LpkfazbzfKtJleBcbVUDAyQrFOuV3pNw4+l98X/pd Y8BAS8Qb5wFZVJEhO/u5DJGgX5TuV4zrcfJSUqTeiQ7C5fwA5Pm8PGXqzXzkkczsPuAD 8SbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5BJKlMzqTcUdYafkPAVytWgSiumWycR17L6N5+K6SZA=; b=b2FiENCoHW7Tx/n1q1TM7gPKJKt44KTZdd5BY46CSwixo16DQDvzPCtm1WFL8PGtKm l0gxqKihD/UFw0ML/e119OaWZTPbDjApy4fCzdjBZA4DDh3C8AwwNxHrNuWVyOg2ZTTX xwONPyMWu1+9RyR/dzFn8TxAgvk1Rmw8YXheTFYt7rFfT0m9WJTPEBhBB2+Mgjv2pnnE d3wqSMdrcb5HwxClFjJqusKRn2Ojy1g7wPpPUlm9Ihz97xxn7Y7j0g+cRa7TqWjvcJpO vyfUFhtkYaCdPgfsVUL5yr8xlrSW+qTxEtRyweM0HrNBMVbgupf1C9svouJ4M8EbamC2 AsGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=clyPeZHw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i185-20020a636dc2000000b0042b16d0cbf9si31874930pgc.638.2022.09.19.02.13.57; Mon, 19 Sep 2022 02:14:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=clyPeZHw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229885AbiISITB (ORCPT + 99 others); Mon, 19 Sep 2022 04:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229813AbiISIS7 (ORCPT ); Mon, 19 Sep 2022 04:18:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FB24205CC; Mon, 19 Sep 2022 01:18:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D2DBD60E74; Mon, 19 Sep 2022 08:18:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41A75C433C1; Mon, 19 Sep 2022 08:18:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1663575536; bh=zyMiFNyTUZzN5Vl8V4xhRkUXsVoPi/7RrGTWVVS39HY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=clyPeZHwyvocUxt9XrhblunGcgpM1uDLI+w2I2IB1iHn0BswcvzsJAzeTG7go0Ld6 TlVLgOVC44pbj27IJ2YhYzg8CP/zvDM3EvEWO9jChcECQCv4BQSH8UQtxFt9Ly4WoV mWSedfT6BGUco0Y0yPMzO32Wf6+ZAE1Bf+pFNoHkEsj1LSvLONWDzkiOyXbBZZnCsn qdwmvyVaW0OYtIrCb097D6zXCaP+nr8wgD1qxsWCQrtUYXmDPOKu6xvGXgiGBaGl/3 Bu9E3Bbfw+aW9PJWzlyQCOzyuM4N/rxDKAHcPh3mhaoCwFklTFGC1olCAWqraBOV0h obnbA43FinXvg== Date: Mon, 19 Sep 2022 10:18:50 +0200 From: Christian Brauner To: Alexey Dobriyan Cc: Andrew Morton , Ivan Babrou , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@cloudflare.com, Kalesh Singh , Al Viro Subject: Re: [RFC] proc: report open files as size in stat() for /proc/pid/fd Message-ID: <20220919081850.4xgsuyvumi4y2xn7@wittgenstein> References: <20220916230853.49056-1-ivan@cloudflare.com> <20220916170115.35932cba34e2cc2d923b03b5@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Sep 17, 2022 at 10:15:13AM +0300, Alexey Dobriyan wrote: > On Fri, Sep 16, 2022 at 05:01:15PM -0700, Andrew Morton wrote: > > (cc's added) > > > > On Fri, 16 Sep 2022 16:08:52 -0700 Ivan Babrou wrote: > > > > > Many monitoring tools include open file count as a metric. Currently > > > the only way to get this number is to enumerate the files in /proc/pid/fd. > > > > > > The problem with the current approach is that it does many things people > > > generally don't care about when they need one number for a metric. > > > In our tests for cadvisor, which reports open file counts per cgroup, > > > we observed that reading the number of open files is slow. Out of 35.23% > > > of CPU time spent in `proc_readfd_common`, we see 29.43% spent in > > > `proc_fill_cache`, which is responsible for filling dentry info. > > > Some of this extra time is spinlock contention, but it's a contention > > > for the lock we don't want to take to begin with. > > > > > > We considered putting the number of open files in /proc/pid/stat. > > > Unfortunately, counting the number of fds involves iterating the fdtable, > > > which means that it might slow down /proc/pid/stat for processes > > > with many open files. Instead we opted to put this info in /proc/pid/fd > > > as a size member of the stat syscall result. Previously the reported > > > number was zero, so there's very little risk of breaking anything, > > > while still providing a somewhat logical way to count the open files. > > > > Documentation/filesystems/proc.rst would be an appropriate place to > > document this ;) > > > > > Previously: > > > > > > ``` > > > $ sudo stat /proc/1/fd | head -n2 > > > File: /proc/1/fd > > > Size: 0 Blocks: 0 IO Block: 1024 directory > > > ``` > > > > > > With this patch: > > > > > > ``` > > > $ sudo stat /proc/1/fd | head -n2 > > > File: /proc/1/fd > > > Size: 65 Blocks: 0 IO Block: 1024 directory > > Yes. This is natural place. > > > > ``` > > > > > > Correctness check: > > > > > > ``` > > > $ sudo ls /proc/1/fd | wc -l > > > 65 > > > ``` > > > > > > There are two alternatives to this approach that I can see: > > > > > > * Expose /proc/pid/fd_count with a count there > > > > * Make fd count acces O(1) and expose it in /proc/pid/status > > This is doable, next to FDSize. > > Below is doable too. > > > > --- a/fs/proc/fd.c > > > +++ b/fs/proc/fd.c > > > @@ -279,6 +279,29 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx, > > > return 0; > > > } > > > > > > +static int proc_readfd_count(struct inode *inode) > > > +{ > > > + struct task_struct *p = get_proc_task(inode); > > > + unsigned int fd = 0, count = 0; > > > + > > > + if (!p) > > > + return -ENOENT; > > > + > > > + rcu_read_lock(); > > > + while (task_lookup_next_fd_rcu(p, &fd)) { > > > + rcu_read_unlock(); > > > + > > > + count++; > > > + fd++; > > > + > > > + cond_resched(); > > > + rcu_read_lock(); > > > + } > > > + rcu_read_unlock(); > > > + put_task_struct(p); > > > + return count; > > > +} > > > + > > > static int proc_readfd(struct file *file, struct dir_context *ctx) > > > { > > > return proc_readfd_common(file, ctx, proc_fd_instantiate); > > > @@ -319,9 +342,33 @@ int proc_fd_permission(struct user_namespace *mnt_userns, > > > return rv; > > > } > > > > > > +int proc_fd_getattr(struct user_namespace *mnt_userns, > > > + const struct path *path, struct kstat *stat, > > > + u32 request_mask, unsigned int query_flags) > > > +{ > > > + struct inode *inode = d_inode(path->dentry); > > > + struct proc_dir_entry *de = PDE(inode); > > > + > > > + if (de) { > > > + nlink_t nlink = READ_ONCE(de->nlink); > > > + > > > + if (nlink > 0) > > > + set_nlink(inode, nlink); > > > + } > > > + > > > + generic_fillattr(&init_user_ns, inode, stat); > ^^^^^^^^^^^^^ > > Is this correct? I'm not userns guy at all. This is correct. :)