From: Thorsten Blum <thorsten.blum@linux.dev>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Kees Cook <kees@kernel.org>, Andy Shevchenko <andy@kernel.org>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] string: use min in sized_strscpy
Date: Fri, 15 May 2026 17:09:02 +0200 [thread overview]
Message-ID: <agc3DrODtyc2rF27@linux.dev> (raw)
In-Reply-To: <CAHp75VfQNkqEYsO4Uup0c-uiYuVyAWit=tmCz2BsYLp-sjXsZw@mail.gmail.com>
On Fri, May 15, 2026 at 09:45:08AM +0300, Andy Shevchenko wrote:
> On Thu, May 14, 2026 at 7:56 PM Thorsten Blum <thorsten.blum@linux.dev> wrote:
> >
> > Use min() and drop the limit variable to simplify sized_strscpy().
>
> ...
>
> > - if ((long)src & (sizeof(long) - 1)) {
> > - size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
> > - if (limit < max)
> > - max = limit;
> > - }
> > + if ((long)src & (sizeof(long) - 1))
> > + max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max);
>
> Side note: Isn't simply
> max = min(PAGE_SIZE - offset_in_page(src), max);
>
> ? (One will need to include linux/mm.h for this, though.)
I thought about it, but wasn't sure if pulling in all of linux/mm.h into
lib/string.c is worth it.
> Moreover there are plenty of duplications to count the size in the
> first page and even the similar min() as in
> fs/iomap/buffered-io.c:869
> arch/s390/kvm/gaccess.c:976
> and many more...
>
> Perhaps a new macro with a good (famous last words!) naming?
How about adding
#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
#define bytes_to_page_end(p) (PAGE_SIZE - offset_in_page(p))
to a new header, e.g. include/linux/page_helpers.h? There are about 50+
direct replacements and possibly more.
prev parent reply other threads:[~2026-05-15 15:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-14 16:56 [PATCH] string: use min in sized_strscpy Thorsten Blum
2026-05-15 6:45 ` Andy Shevchenko
2026-05-15 15:09 ` Thorsten Blum [this message]
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=agc3DrODtyc2rF27@linux.dev \
--to=thorsten.blum@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=andy.shevchenko@gmail.com \
--cc=andy@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@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