From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 CBE73282F2A; Mon, 13 Jul 2026 16:53:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783961625; cv=none; b=mCgfQ0g+ry+q+5ZBsqyq0pN0ymLsqdc/0qNeDjl81wdRrHRZXMwh1syecJU2IbMIO11npUwQqH4yO6Ylr27xPUbytytASsm2odYwS0JkpKW5kLsqTygLZnz6DjPaTYiEV6XcytnpfSRBxJD0iwvcHWperEgjQ3eUMUpHZVhUQg4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783961625; c=relaxed/simple; bh=926p0u3nm4Rc43IssV43boOeRaldBeDb7NqTo5rgqGc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Po+IOWj+LPPrwC4q26EAsWgYf1wGvxypNNNq67dLiBySi0ucCXGMqJ1O7XW7RWpwv2vz2ElAJMQrHGojtZiucqSkog/8BqKyHl/+jUzVNelQsTh3/FQBJlp+RYhGQzROafobJj3nmMA0XhG9H/yhN5G9Zw5q+hYjrgGCGXFqalY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QwlCxO0T; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QwlCxO0T" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4381E1F000E9; Mon, 13 Jul 2026 16:53:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783961624; bh=RUky49EZFMYWVAE4S2Lncj1iWmkgCf5kuOOfACz5DKk=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=QwlCxO0TNXvbai08wj9mItkuAJ900VTA+rygyaR2RP6rN7HGawq1A+/fZ0K6vrd+2 XxACCZ/t4zcCLMBvSvculez3o+YELTcxId09BMSupkMYtaXlqPcFtEHUWez6wkdzFk 6MlrriLDnfTq9fwG7S7GUp+XVXoKnWQFyHY3kPsgaoT+cu3gSlew72sFcT6tUMuCDb 8tTXEzPpgbviFGztKnUXF1wirPFHi7NSlimIj+5RzpkjhfyuLLRUVg3nGB3a3ksxIh Qr1KGPdiibbcSQ7AiL/ugLL8D5Zrb/maM0DZ2sl2x2m0dhZhmT28Tuv/CvbYlHGfOP 5u8+fQRnQ0S4w== Message-ID: Date: Mon, 13 Jul 2026 18:53:40 +0200 Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH] slab: don't assume alignment on allocators that may return ZERO_SIZE_PTR Content-Language: en-US To: Harry Yoo , Karl Mehltretter , Andrew Morton Cc: Rasmus Villemoes , Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Catalin Marinas References: <20260712120728.96628-1-kmehltretter@gmail.com> From: "Vlastimil Babka (SUSE)" Autocrypt: addr=vbabka@kernel.org; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9Ctkn3trYDNnat0eoAcfPIP2OZ+ 9oe9I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N+7tJ5Tsq42zVJ4jxsT8b9YplavCMsnBgDeRWhcbYhCyttoL7nYISyWg4kQYZ/P wIV3OuNv2f8iKYsxNsRuClOAF82+gvqOy1/1pprFjy8uo2pkoOrb63aOP3vO5VHnRKgra6dq NcaZ+c6J4H+nEJGi2SkHAUJz5oBzuThvPudLvPA/SK8sKoM01IRxSihev/S/5WLazXB1PGem OCbvzC1IjWJJraxiDJ5IygokapUa2RP7+WBR22skQ3SSl6G107QgWKSyTOGWEaRmV53vxQLV jXuCmzSSasTL60zq5yGrT4/DYQVSNEUiUbG4pYekxJujNeEDkUlky0Y= In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 7/13/26 07:18, Harry Yoo wrote: > > [+Cc Catalin for ARCH_KMALLOC_MINALIGN bits] > > On 7/12/26 9:07 PM, Karl Mehltretter wrote: >> __kmalloc_noprof(), __kmalloc_node_noprof(), and __kmalloc_flags_noprof() >> are annotated with __assume_kmalloc_alignment, which expands to >> __assume_aligned(ARCH_KMALLOC_MINALIGN). All three can return >> ZERO_SIZE_PTR, currently (void *)16, for zero-sized requests. When >> ARCH_KMALLOC_MINALIGN exceeds 16, this contradicts the annotation. > > Ouch. > >> Compilers may use this false assumption to reduce ZERO_OR_NULL_PTR() to a >> NULL check, losing recognition of ZERO_SIZE_PTR. Current GCC and Clang >> retain the existing range check through kmalloc's inline wrapper, so no >> functional miscompile was reproduced with the current source form. However, >> both eliminate an exact ZERO_SIZE_PTR comparison on the same return value. >> With Clang, UBSAN also reports the invalid alignment assumption at boot. >> >> Changing ZERO_SIZE_PTR to satisfy the annotation would make its value and >> the range accepted by ZERO_OR_NULL_PTR() architecture-dependent. Avoid that >> semantic change by dropping the annotation from the general kmalloc entry >> points. Retain it on the cache helpers, which cannot return the sentinel. >> Allocation behavior is unchanged. >> >> Kernels before v7.2 do not have __kmalloc_flags_noprof(). >> Backports to those kernels only need the include/linux/slab.h change. >> >> Fixes: 94a58c360a45 ("slab.h: sprinkle __assume_aligned attributes") >> Fixes: f6d50ab29afd ("mm/slab: introduce kmalloc_flags()") >> Cc: # needs adjustment before v7.2 >> Assisted-by: Claude:claude-opus-4-8 >> Signed-off-by: Karl Mehltretter >> --- >> >> Notes: >> This is RFC because the alignment contract can be corrected in three ways: > > I think it's still worth having at least a weaker alignment guarantee > and would like to vote for option 3 (or option 2, if 3 turns out to be > infeasible) unless there's something unexpected that prevents us from > doing that. Agreed. >> 1. Remove the annotation from entry points that can return ZERO_SIZE_PTR >> (this patch). On armv5 this increases .text by 1120 bytes (0.02%); >> no measurable change was seen on arm64. >> >> 2. Cap the assumed alignment at 16. This preserves some alignment >> information but couples the annotation to the current sentinel value. >> >> 3. Change ZERO_SIZE_PTR to satisfy ARCH_KMALLOC_MINALIGN. This makes the >> long-standing sentinel architecture-dependent and either expands the >> range accepted by ZERO_OR_NULL_PTR() or requires changing that macro. > > Some code (e.g., kmem_dump_obj()) performs (addr < PAGE_SIZE) check > either NULL or ZERO_SIZE_PTR) to see if the address is valid. > > Bumping ZERO_SIZE_PTR to something smaller than PAGE_SIZE should still > work: (addr < PAGE_SIZE) check still works, and accessing it still > causes a fault. No arch should have ARCH_KMALLOC_MINALIGN >= PAGE_SIZE? Yeah, I don't see why anything should mind if the value changes to a larger one, if it's still well below PAGE_SIZE.