On Fri, May 15, 2026 at 09:45:08AM +0300, Andy Shevchenko wrote: > On Thu, May 14, 2026 at 7:56 PM Thorsten Blum <[email protected]> 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.

