From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7198728A72F for ; Thu, 21 May 2026 13:47:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779371255; cv=none; b=Nf24nrgvT5zihMh3MK4Kl8LNynNza9j5uKO18L0OxMOEZpm3UUeB9cLgw3ETOtPFdbCuo3gIGaG3SF5WqVfEaRBayJ79SbbrHVAAF1GLKnlDm4+nmvw40l8SkA0xusN74UerRON6UjDamaAtG2kIDJ7ot4dXB9bngzfM6wHVuz4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779371255; c=relaxed/simple; bh=+w8QZ4JdVUyNtIBPo5ZywCeAfAPKqdYMGAH56OsbWXA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=slaQbvaf/mYn9wdAPiHkSKWgcWVDvlAcjwGqUhVddLU1wxzB2UxBODA5wUM3k4Zr7yBw71Y8JZW0vGZUa84pTGTPOc4QXo1oIC8yw6Lu1lHpbU7r2mXn9gu/86YMdwh5COw9xfC7HfgwnXBiiFJW4m3v6VBoPAVj/T+7dyDA7+c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=JvHvMHUv; arc=none smtp.client-ip=91.218.175.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="JvHvMHUv" Date: Thu, 21 May 2026 15:47:25 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779371251; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=lXj32I55bWmtT0XwdpfkCPw1jMJbMSf4hLniENsiCes=; b=JvHvMHUvTP5iSDiOj3NmWHnaT83RnAK19Alv9f7afp4sZ0NXK0L2jmVkmDKuPmJFXmiYm+ casCmMD030Nvz5zbTIFfYiRBv1/NN36btEreqFXPs6c5htGbxoD22i2x9xOaAxik1NwnYv r06SIwnQ3hzHMppGpoY04xaurC26kRo= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Thorsten Blum To: Lorenzo Stoakes Cc: Andrew Morton , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Andy Lutomirski , Thomas Gleixner , Vincenzo Frascino , Kees Cook , Andy Shevchenko , Yury Norov , David Laight , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] string: use offset_in_page() in sized_strscpy() Message-ID: References: <20260521090655.160282-4-thorsten.blum@linux.dev> <20260521090655.160282-6-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT On Thu, May 21, 2026 at 12:31:25PM +0100, Lorenzo Stoakes wrote: > On Thu, May 21, 2026 at 11:06:58AM +0200, Thorsten Blum wrote: > > Replace the open-coded implementation with offset_in_page() to simplify > > sized_strscpy(). > > > > Signed-off-by: Thorsten Blum > > I feel this patch actually makes sized_strscpy() more difficult to understand > unfortunately, so not really in favour of us taking it. > > > --- > > lib/string.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/lib/string.c b/lib/string.c > > index 1f9297e9776a..7c72adc7377c 100644 > > --- a/lib/string.c > > +++ b/lib/string.c > > @@ -25,6 +25,7 @@ > > #include > > #include > > #include > > +#include > > > > #include > > #include > > @@ -127,7 +128,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) > > * since we don't know if the next page is mapped. > > */ > > if ((long)src & (sizeof(long) - 1)) > > - max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max); > > + max = min(PAGE_SIZE - offset_in_page(src), max); > > This isn't strictly the same code, offset_in_page() is: > > #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK) > > So this is now, effectively: > > - max = min(PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)), max); > + max = min(PAGE_SIZE - ((unsigned long)src & (PAGE_SIZE -1)), max); > > So there could be some issues here with type conversions at least in theory. I think this should have used unsigned long from the start, as long seems a bit odd for a pointer, and PAGE_SIZE - offset_in_page() is a common kernel idiom for this exact calculation. Andrew also commented [1] on this line recently. > But in any case you're now making the logic inconsistent (the next line uses > bitwise operations directly). Agreed, maybe there are other helpers we can use here too, but that should probably be a follow-up patch. > So I'd rather we didn't make this change. FWIW, using offset_in_page() in lib/string.c (as suggested by Andy [2]) was the initial motivation for moving it out of mm.h, so I'd prefer to keep the use-site in this series if possible. Thanks, Thorsten [1] https://lore.kernel.org/lkml/20260519123740.458d905f958f39d41cb130fd@linux-foundation.org/ [2] https://lore.kernel.org/lkml/CAHp75VfQNkqEYsO4Uup0c-uiYuVyAWit=tmCz2BsYLp-sjXsZw@mail.gmail.com/