From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) (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 405383BB131 for ; Fri, 15 May 2026 15:09:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778857756; cv=none; b=ZjuuVTD91T1+ybakyv/7PEVcluf2nCxyUMqeUwBMhACBQJBc2WYhaYj8oXMopeRcHM3RCrYvfxYVqY0civy0dEwMLmljwnroPhD/fBPreNyuDjux5wley7bcpz8MeAM9dF2Q2u0DHpusWRtDtFaP/F1OwsXM/Jji6cZBSxHyAKc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778857756; c=relaxed/simple; bh=CsGkRRMWVPsm0bpA/qBbuJrDLzaJTsgMzkh70YVJz2Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tqRX5f0WBk/UF2/cUK5PvQSVq0auqjfwe37qAF/NCJNOFlmK8PL+0WzNeSjLF3TOk8K7f1ArIS0qrj+BnWPVdHTZXUpd3Lqub0f4qaE4Dx0mi/S+wGgCvjir2xXwdXXrvID0SeCZeVtvHqSJQocqMnUN+iTh/+H97G8vSYAeHRo= 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; arc=none smtp.client-ip=95.215.58.174 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 Date: Fri, 15 May 2026 17:09:02 +0200 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Thorsten Blum To: Andy Shevchenko Cc: Andrew Morton , Kees Cook , Andy Shevchenko , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] string: use min in sized_strscpy Message-ID: References: <20260514165601.527883-3-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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Migadu-Flow: FLOW_OUT On Fri, May 15, 2026 at 09:45:08AM +0300, Andy Shevchenko wrote: > On Thu, May 14, 2026 at 7:56 PM Thorsten Blum 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.