Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5717082rwb; Wed, 21 Sep 2022 11:20:32 -0700 (PDT) X-Google-Smtp-Source: AMsMyM48GcheTBEG2nEJeNyu2SYQYceW6JlJdlaYrPQlHSoBJpDMOjTwWU7sPLhaVUt+4IJPDL++ X-Received: by 2002:a17:902:d2c2:b0:177:ed66:798 with SMTP id n2-20020a170902d2c200b00177ed660798mr6024655plc.76.1663784432562; Wed, 21 Sep 2022 11:20:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663784432; cv=none; d=google.com; s=arc-20160816; b=P9ETIWr2muk5Z6iLqFEm3rJclUQqrX+0bFGFPK/ybNEfIN1/+rVENbCtqcXwEvxL5+ w2GmuiO8GY1htRgBcs0/7mCSyHHnaCul7Xq1QpbcxbxbG92kuWSvEPE5JQcHefId5j1h AzY4ncCCrU7rjuPRhsWDJJVyuFXtvf/5oRMaW1TybY1KdN9j+gXzBlIef7z5Ca2PWfhV YPvNC3Eq0tbet6Pqe3l8k96Dsg+nHsF1XHCNv3boxqzCE7zo6WGso0NB7pRhQ//4acw+ ppXk4ZEnvRH4ZPX1V0XuAuNelWfJi5v9TZrhqw4pLW23P5pT6SNnch6dR9FaPGFu6Alj 5dRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zgcz1Ul6i9PXeI/P/ZQ+OncdDs4YJohaTrFfyIJlyi8=; b=nbEcdMoZqDdWh6bNIHAfli9OHhBmk23v5nrep7fFSqB4D4IEKao+vBTKFCOiIrOZSz FOkjTYuWYWIxdENNqYv9TXNYmkxNeuqBL3rp1GhkAQiD+5u5mKZrp5ScbNfNdxAWBCFr yeTp4lf+KSN3kSlljhSQJLtFCHvMpoc81/em+fZ14aI4+Lbt/2CzFzXzHHp/8Oc1OR60 Ri0dUPUaxgSGTdAK8rVurBFSNCRsA9p4FdlbnWromBs/lNRwg+0CcQ34qNkWzfFpmk+h ncRYOQ4sU6PZ837pIwa4+eyDlhLnew4J0aT82+1LpIIpNJBVhWTPnMCJlFgeQUOJZuN0 uSUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b="L/CajF85"; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c18-20020a170902f31200b00177efb5648asi3177403ple.278.2022.09.21.11.20.16; Wed, 21 Sep 2022 11:20:32 -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=@cloudflare.com header.s=google header.b="L/CajF85"; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229789AbiIUSIv (ORCPT + 99 others); Wed, 21 Sep 2022 14:08:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229669AbiIUSIu (ORCPT ); Wed, 21 Sep 2022 14:08:50 -0400 Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF3777D1CA for ; Wed, 21 Sep 2022 11:08:48 -0700 (PDT) Received: by mail-io1-xd32.google.com with SMTP id e205so5739806iof.1 for ; Wed, 21 Sep 2022 11:08:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=zgcz1Ul6i9PXeI/P/ZQ+OncdDs4YJohaTrFfyIJlyi8=; b=L/CajF85H6Pe/cI6xZHVxuCazlHeSVvBa8AG8BJwImFT1Tn1aTR82LT0SiIB63rS6+ qYanS79/LnMDGbFgcT/4tubGjl6RAoflXl+ztralKVSwtWVbJcyK2WOVUi6EqPsvmqv8 DEqSEtEbzw8qj+FsGdugIgMq8hWPAYmAWeH7c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=zgcz1Ul6i9PXeI/P/ZQ+OncdDs4YJohaTrFfyIJlyi8=; b=OhsLxuGHGckcxpKgU53Bx44j5X86DXQaNEuxVtsIfpQHnIZQoUCgAZ3ntHKRBOkdDL XdxcKQ//fQ0n7q9fY3FIvoxyAJHkuyC5BeObWQxwtVUSNudb63cZw9TqIlXp8v7iPCLq 6k9Ntpvjx/V5N74fS7pRiYPtchjG7qU7ZNX4OsJ6PgR/KEwPyrwf6UBM21eEwdnTg55r +XYlMhbiH91MfZ6RT9Tq0cyjdReMwu7l2EsWOuRXAB4DBffatZLPBgE0QfJ1SZ4m3/cH 3kc04o/PCP1/4c2RvuBGtnV9nORTgbiKePMRqRsloA3RFa/z8R5CHRvCnZ/mJ0M034wK 7LlA== X-Gm-Message-State: ACrzQf1UTzJeNh/jB70qV0nvi+mhdghad/2FHw2UgtbodwZ/PguofvMp E3nLnE3NphXQtzn1ykKCvv3ejsMKrwxWVLkZACDD8g== X-Received: by 2002:a02:1d08:0:b0:35b:9c9:f8ff with SMTP id 8-20020a021d08000000b0035b09c9f8ffmr3425999jaj.281.1663783728026; Wed, 21 Sep 2022 11:08:48 -0700 (PDT) MIME-Version: 1.0 References: <20220920190617.2539-1-ivan@cloudflare.com> In-Reply-To: From: Ivan Babrou Date: Wed, 21 Sep 2022 11:08:37 -0700 Message-ID: Subject: Re: [PATCH] proc: report open files as size in stat() for /proc/pid/fd To: David Laight Cc: "linux-fsdevel@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kernel-team@cloudflare.com" , Alexey Dobriyan , Al Viro , "Theodore Ts'o" , Jonathan Corbet , Andrew Morton , David Hildenbrand , Johannes Weiner , Christoph Anton Mitterer , Vincent Whitchurch , Mike Rapoport , Yang Shi , Paul Gortmaker , Kalesh Singh Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 Wed, Sep 21, 2022 at 3:21 AM David Laight wrote: > > From: Ivan Babrou > > Sent: 20 September 2022 20:06 > ... > > > > +static int proc_readfd_count(struct inode *inode) > > +{ > > + struct task_struct *p = get_proc_task(inode); > > + struct fdtable *fdt; > > + unsigned int i, size, open_fds = 0; > > + > > + if (!p) > > + return -ENOENT; > > + > > + if (p->files) { > > + fdt = files_fdtable(p->files); > > + size = fdt->max_fds; > > + > > + for (i = size / BITS_PER_LONG; i > 0;) > > + open_fds += hweight64(fdt->open_fds[--i]); > > + } I'm missing put_task_struct(p) here. > > + > > + return open_fds; > > +} > > + > > Doesn't that need (at least) rcu protection? Should I enclose the "if" in rcu_read_lock() / rcu_read_unlock()? files_fdtable() is this: * https://elixir.bootlin.com/linux/v6.0-rc6/source/include/linux/fdtable.h#L77 #define files_fdtable(files) \ rcu_dereference_check_fdtable((files), (files)->fdt) And rcu_dereference_check_fdtable() is #define rcu_dereference_check_fdtable(files, fdtfd) \ rcu_dereference_check((fdtfd), lockdep_is_held(&(files)->file_lock)) I definitely need some help with locking here. > There might also be issues reading p->files twice. This block for reading twice: if (p->files) { fdt = files_fdtable(p->files); Already exists in fs/proc/array.c in task_state(): * https://elixir.bootlin.com/linux/v6.0-rc6/source/fs/proc/array.c#L173 There's task_lock(p) there, so maybe that's why it's allowed. Should I run files_fdtable(p->files) unconditionally and then check the result instead? I'm happy to change it to something else if you can tell me what it should be. If there are kernel options I should enable for testing this, I'd be happy to hear them. So far we've tried running with KASAN with no issues.