From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796223106; b=ZYDqP+p8bReNCPn1vThv7W+C81DPrwf2t+XVabkijFejxxueyCp+Loo+2JxZ1q6Du5bD Erl4u9JxsoLdsAPyYIB8AwxXYrHLHyF7apaZiiE3DNWQVbRCBzOpbMRIuxwRJmJi7UyBY 0YN9+/g9JLcQNNsNMxteTL4Z6MZ2/Gashfdd1s1UeUWKxVBkMB+SByivh8k1NcexIu8lt IkPVBFaIOBSFgIEs1FOPfeRt3sZf0W/ezteRGnzL7eAN07a3QBTzI43Ig77qJcBOz2xF2 kpk8yZH8JuO+Q8O0vZRPToLSBHXvj5k2leep9G0ANMdCaBIi63jlC49W3JlpU0hHlsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796223106; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=OzhtANC27M494VAWoYdXT9zdugJZdz7ez+JFE/lDpAY=; b=0zzkxsXLnToc4Vo12kTTRSY0oz80a+kvQAYcZ/mXKOsHP3EyNc67MGYK4xJKK9QymBDS Rlc4iggJiFT5aE5vRFBhPeXLD1WuQwiZA/kpmdtOJCYggDUi71Xx7feqe//M//Qt9T39/ M/JTLqQRrfnG8/QZ7CpeiTiytQa2A2JqiSU4cS5XGFjmJh/Wv2tN4ZT34lMDJkj02Ankd qHfD3z0IzuvTLV1HHFJHskPIrrHk8teKSmALKV0GGxXYrXlL8pqBPJxKbZLAJf/jPHcWN Tpyk/n+uBgvd04M2jJ0YO4KU0uBAGEXbtSLOb43/RZ6p8SvI4mk6C2crGWYbwtJXWQA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=O98Xh5nG; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5923606A325 for ; Wed, 2 Dec 2026 14:51:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=O98Xh5nG Received: by smtp.kernel.org (Postfix) with ESMTPSA id C58EC1F00919; Wed, 2 Dec 2026 14:51:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796223106; bh=OzhtANC27M494VAWoYdXT9zdugJZdz7ez+JFE/lDpAY=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=O98Xh5nG4CPG0gLfvrg+TPPqOiX1j9WXZ5nWIBseHTaY/5k/vH+sp4mLGlzaUDqv3 Qh3f5Vb7iMS5wxqPwy58FPmYxofXDBlHv/8kQvH3Rg20kHCix3b4IflG6xNVXov6+A 3OfdHMCwJSUn3EmTc/oqlx8ZQ2B1wgtKVsuShfurY1F1/p4h4wvTXH0zgvnIfVGoQH ln/pSVwVhx+RAkkYb0+VHDwOMwThrB+Sa6Rum3434e/YsWpL98qTsVcniCNCbzew3X AstA5jcLdEHd0xEaHdzV1uUPTUYQxxORXHbGmyycXNIG68LZBpMlJwk7UPhv0AmFuE cokEep1bmYg+g== Date: Wed, 2 Dec 2026 14:51:44 +0000 From: Harry Yoo To: Sang-Heon Jeon Cc: Vlastimil Babka , Andrew Morton , Christoph Lameter , Danilo Krummrich , David Rientjes , Hao Li , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Roman Gushchin Subject: Re: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() Message-ID: References: <20261201165256.1286791-1-ekffu200098@gmail.com> <20261201165256.1286791-4-ekffu200098@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261201165256.1286791-4-ekffu200098@gmail.com> On Wed, Dec 02, 2026 at 01:52:51AM +0900, Sang-Heon Jeon wrote: > If SLAB_STORE_USER is enabled, the requested size is tracked and > kzalloc() zeroes only the requested size. > > But if kvrealloc() falls back to kvmalloc(), it copies the bytes beyond > the requested size up to ksize(p), which might be non-zero even if > __GFP_ZERO is set. ksize() says "This should only be used internally to query the true size of allocations. It is not meant to be a wqay to discover the usable size of an allocation after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS, and/or FORTIFY_SOURCE" > So copy only the requested size instead, like krealloc() already does. > > Fixes: 590b9d576cae ("mm: kvmalloc: align kvrealloc() with krealloc()") > Signed-off-by: Sang-Heon Jeon > --- > > mm/slub.c | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index 8e6e900fc39b..9742c2825a06 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -7272,10 +7272,18 @@ void *kvrealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(size, token), > if (!n) > return NULL; > > - if (p) { > + if (!ZERO_OR_NULL_PTR(p)) { > + struct slab *slab = virt_to_slab(p); > + size_t old_size; > + > + if (!slab) > + old_size = ksize(p); > + else > + old_size = get_orig_size(slab->slab_cache, (void *)p); > + > /* We already know that `p` is not a vmalloc address. */ > kasan_disable_current(); > - memcpy(n, kasan_reset_tag(p), min(size, ksize(p))); > + memcpy(n, kasan_reset_tag(p), min(size, old_size)); Do we really need this when kvrealloc() frees a slab object and allocates a vmalloc object(), why? > kasan_enable_current(); > > kfree(p); > -- > 2.53.0 > -- Cheers, Harry / Hyeonggon