From: Alexey Dobriyan <adobriyan@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH] proc: give /proc/cmdline size
Date: Fri, 9 Sep 2022 08:07:20 +0300 [thread overview]
Message-ID: <YxrKCLXE7k16j9xu@localhost.localdomain> (raw)
In-Reply-To: <20220908134546.6054f611243da37b4f067938@linux-foundation.org>
On Thu, Sep 08, 2022 at 01:45:46PM -0700, Andrew Morton wrote:
> On Thu, 8 Sep 2022 21:21:54 +0300 Alexey Dobriyan <adobriyan@gmail.com> 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"?
StackOverflow, the source of all best programs in the world!
> > let mut f = std::fs::File::open("/proc/cmdline").unwrap();
> > let mut buf: Vec<u8> = 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<u8> 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?
We can calculate its length precisely and show to userspace so why not
do it. Other /proc files are trickier.
next prev parent reply other threads:[~2022-09-09 5:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-08 18:21 [PATCH] proc: give /proc/cmdline size Alexey Dobriyan
2022-09-08 20:45 ` Andrew Morton
2022-09-09 5:07 ` Alexey Dobriyan [this message]
2022-09-09 7:37 ` David Laight
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=YxrKCLXE7k16j9xu@localhost.localdomain \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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