Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1117693rwn; Thu, 8 Sep 2022 13:49:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR6EyJAgduIp0Pmz8p6+qZrkJRiVmFW8UD4wMnWr3hEIPTdv3FfEdpzSF+eYrKC7SLNysVJT X-Received: by 2002:a17:902:6ac8:b0:175:44b5:5264 with SMTP id i8-20020a1709026ac800b0017544b55264mr10564027plt.19.1662670153975; Thu, 08 Sep 2022 13:49:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662670153; cv=none; d=google.com; s=arc-20160816; b=yz+59TEcxHm6vvBu8iUqCN/MbfZDWzcpDJHkM+BpO4IsL2CR8Fq9S/KDVfQybZLf9U Cja+XUjhkbg99nbnJ2LKJmuKLP9kibMlzUnBwchqvBJwfbPfWRD6dBkQmyzuHuh11CK1 ypNXAYFx/fwHISa43/sKml+pRH/6hHKM/H8ZyT6Y33nmRgrrfIAovT94jsw56Z5M/7ed an1HzwTLks1ljBFspAywPZLr3bpTnLocR2gGcWA5UXlqJrl7/mAgJwstErQo4Y4PREN5 fPLM1tTXyr3KDGOP8lA/qT/lsqgwlZDnpk+1HLMubPOUegrGXJClhuo2pmOOycL5dB77 KQ0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=N7g/nrKk4FWjwmFplbgfTlkh1JxoTuZ+IDBcg+lvo2g=; b=zmh+7cEk3fbcBmj8o0pYeD5P8w/18IYztXTB9iPj+D7FY+yHuk2P6br6wRnkFHugIx kSBBXDd0ghNupS3/Nh+uqNNOw262w4Gsj/GZdOao90pz90bgSzQBcvxYncojzsNrnsZ/ eBxln2nHah4icR08ampSajYzHfqPotNoFDx1dKSmpIFw7v7zpX0YUHHy23x3e5GU9uA/ hfWKhCNaGYtCQnBx1K6otYY1l/vOCufV/YziUsQ1KdgeKf+as4gNszMV26UbRCaNYs1Z UOhR1j3JbSxIPlxrSNxQitMJhcX0bCqv2PSw6AqkaHLvXKlf8wD3v9/dojSKJxj79PsW G+zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=J+NBqSIO; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn18-20020a17090ac79200b001fd7b0552b0si1014821pjb.37.2022.09.08.13.48.59; Thu, 08 Sep 2022 13:49:13 -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=@linux-foundation.org header.s=korg header.b=J+NBqSIO; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229786AbiIHUpx (ORCPT + 99 others); Thu, 8 Sep 2022 16:45:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229492AbiIHUpw (ORCPT ); Thu, 8 Sep 2022 16:45:52 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B3F7C5789; Thu, 8 Sep 2022 13:45:51 -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 sin.source.kernel.org (Postfix) with ESMTPS id 3492FCE20A9; Thu, 8 Sep 2022 20:45:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 258FCC433D6; Thu, 8 Sep 2022 20:45:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1662669947; bh=9J1C2KFcH969Z3x2PZqo6s1jyIaCFtzY0yxbCJi9HH0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=J+NBqSIOd4nP66l7soHJqD67V7Ou0UKZW6aS3MEFeWbxGEZALOxpZzHnFCVf15uCq aHbQv6TiWuMjPR2xKMLkV85Fbjru5rpcW/SktGCRoa06o2Aa5NMW8IPxbXwWrlINWj mnpeHSkCjmB/0ihI6jJfJUv3fT0yIV9XMhj4bLT0= Date: Thu, 8 Sep 2022 13:45:46 -0700 From: Andrew Morton To: Alexey Dobriyan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH] proc: give /proc/cmdline size Message-Id: <20220908134546.6054f611243da37b4f067938@linux-foundation.org> In-Reply-To: References: 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 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Thu, 8 Sep 2022 21:21:54 +0300 Alexey Dobriyan wrote: > Most /proc files don't have length (in fstat sense). This leads > to inefficiencies when reading such files with APIs commonly found in > modern programming languages. They open file, then fstat descriptor, > get st_size == 0 and either assume file is empty or start reading > without knowing target size. > > cat(1) does OK because it uses large enough buffer by default. > But naive programs copy-pasted from SO aren't: What is "SO"? > let mut f = std::fs::File::open("/proc/cmdline").unwrap(); > let mut buf: Vec = Vec::new(); > f.read_to_end(&mut buf).unwrap(); > > will result in > > openat(AT_FDCWD, "/proc/cmdline", O_RDONLY|O_CLOEXEC) = 3 > statx(0, NULL, AT_STATX_SYNC_AS_STAT, STATX_ALL, NULL) = -1 EFAULT (Bad address) > statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0444, stx_size=0, ...}) = 0 > lseek(3, 0, SEEK_CUR) = 0 > read(3, "BOOT_IMAGE=(hd3,gpt2)/vmlinuz-5.", 32) = 32 > read(3, "19.6-100.fc35.x86_64 root=/dev/m", 32) = 32 > read(3, "apper/fedora_localhost--live-roo"..., 64) = 64 > read(3, "ocalhost--live-swap rd.lvm.lv=fe"..., 128) = 116 > read(3, "", 12) > > open/stat is OK, lseek looks silly but there are 3 unnecessary reads > because Rust starts with 32 bytes per Vec and grows from there. > > In case of /proc/cmdline, the length is known precisely. > > Make variables readonly while I'm at it. It seems arbitrary. Why does /proc/cmdline in particular get this treatment?