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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40D23C4332F for ; Fri, 21 Oct 2022 00:52:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229794AbiJUAwr (ORCPT ); Thu, 20 Oct 2022 20:52:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbiJUAwp (ORCPT ); Thu, 20 Oct 2022 20:52:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A5A2230811; Thu, 20 Oct 2022 17:52:44 -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 ams.source.kernel.org (Postfix) with ESMTPS id D0253B82924; Fri, 21 Oct 2022 00:52:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D3C2C433C1; Fri, 21 Oct 2022 00:52:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1666313542; bh=xhtO9l6Zw6uXqsnXZIrGAwFLvsejDmkqFMkVXTirtro=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=0CnEMEb8oHjmtcyWq34BSjGROAcPwNGzX7CSJDL29WrtVj9dIiIgPwaZbAOTmcS2S 8wTrDSwoskmNhW9iiV+IAF/IGaJXCrPimThzeyaD7cB0VrLDY5C4urs6W0sQ0oIhXh LzivxOCGuKD6302gGnWo4rc8BN09l1tBVZYrABNE= Date: Thu, 20 Oct 2022 17:52:21 -0700 From: Andrew Morton To: Brian Foster Cc: Ivan Babrou , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@cloudflare.com, Alexey Dobriyan , Al Viro , "Theodore Ts'o" , David Laight , Jonathan Corbet , David Hildenbrand , Johannes Weiner , Christoph Anton Mitterer , Mike Rapoport , Paul Gortmaker , Kalesh Singh Subject: Re: [PATCH v3] proc: report open files as size in stat() for /proc/pid/fd Message-Id: <20221020175221.534de6ad08512d9d8a590760@linux-foundation.org> In-Reply-To: References: <20221018045844.37697-1-ivan@cloudflare.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 19 Oct 2022 07:28:45 -0400 Brian Foster wrote: > On Tue, Oct 18, 2022 at 11:51:02AM -0700, Ivan Babrou wrote: > > On Tue, Oct 18, 2022 at 11:16 AM Brian Foster wrote: > > > > +static int proc_readfd_count(struct inode *inode) > > > > +{ > > > > + struct task_struct *p = get_proc_task(inode); > > > > + struct fdtable *fdt; > > > > + unsigned int open_fds = 0; > > > > + > > > > + if (!p) > > > > + return -ENOENT; > > > > > > Maybe this shouldn't happen, but do you mean to assign the error code to > > > stat->size in the caller? Otherwise this seems reasonable to me. > > > > You are right. As unlikely as it is to happen, we shouldn't return > > negative size. > > > > What's the idiomatic way to make this work? My two options are: > > > > 1. Pass &stat->size into proc_readfd_count: > > > > if (S_ISDIR(inode->i_mode)) { > > rv = proc_readfd_count(inode, &stat->size); > > if (rv < 0) > > goto out; > > } > > > > out: > > return rv; > > > > OR without a goto: > > > > if (S_ISDIR(inode->i_mode)) { > > rv = proc_readfd_count(inode, &stat->size)); > > if (rv < 0) > > return rv; > > } > > > > return rv; > > > > 2. Return negative count as error (as we don't expect negative amount > > of files open): > > > > if (S_ISDIR(inode->i_mode)) { > > size = proc_readfd_count(inode); > > if (size < 0) > > return size; > > stat->size = size; > > } > > > > I suppose the latter is less of a change to the original patch..? Either > way seems reasonable to me. I have no strong preference FWIW. If get_proc_task() failed then something has gone horridly wrong, hasn't it? Wouldn't it make sense in this situation to make the .getattr() itself return an errno, in which case the data at *stat dosen't matter - it's invalid anyway. This seems to be the general approach in procfs when get_proc_task() fails - return -ESRCH (or, seemingly randomly, -ENOENT) to the caller.