From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3CB08379971 for ; Mon, 23 Feb 2026 22:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771885408; cv=none; b=JhbkSNi+7ZgSXjE0Wv8680daa3V+8GqeAzqlikugqc5twxotd/bAnzp/jctVEppGJwnT5i6doZ47OvSLuigOuwJDCbKi+v8pFVCXkJB1Tg7X2Nm3cg3t2p5f3qM3SPUcLBdSJ+aL0p42lZ2dAKEIcmPAOEzj0xQ+hIEyCQ4VxcA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771885408; c=relaxed/simple; bh=7d1NyQoU5vALACKboOd/mJRj2ZPC09kjjLeModp6BAY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lMInRzw+JJQYO5rcvtPTxOANQ8G248oC1GW0ofhrjRStnPKhsxVEwFNjOo3ap/2odgTWl4zwKL9kcTlopwCFiQGL4uM+cS3EB3bSk6Cw29aEAKfgd0S5aTDG5YiwNf6IftB3Ea1kGPVeQPdzRO66yMlAFUupEwNbe0PCMV/5r8o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JT4y12qZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JT4y12qZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15773C116C6; Mon, 23 Feb 2026 22:23:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771885408; bh=7d1NyQoU5vALACKboOd/mJRj2ZPC09kjjLeModp6BAY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JT4y12qZzRycuHdCtX8liDWHGvtAufy2cDvxy5Fb9MOKeu4kHF2CtEuU9ZCMZbSDg nIG4VIO/eRoZoYqHZXUzjwFhI6RH51HlDQLOi94qf26t/tn4KG0sD5PqieJlndbD/i KhxWU80+HAAmI1gE0DbgMkIjwwLAa27sk8O7DwCGLAGFWJ27QvytitjZYrnW/LJB3h RUtXs+aZEC8G7UAotAhbg/hPfygxxi3K9agvZJ28TCRWlCE5Z5PlKqf8KDa+PyYJ8A MTznOkRmSfEjh9aCDS9TROCnUTjLkmy6wjpb5pbjjgOnIQyICgz3bGN5NpcZRAlFfG 1D5zb+5Y3rmVg== Date: Mon, 23 Feb 2026 14:23:27 -0800 From: Kees Cook To: Guenter Roeck Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH] treewide: Replace kmalloc with kmalloc_obj for non-scalar types Message-ID: <202602231422.9AF121F@keescook> References: 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: On Mon, Feb 23, 2026 at 08:05:10AM -0800, Guenter Roeck wrote: > On Fri, Feb 20, 2026 at 11:49:23PM -0800, Kees Cook wrote: > > This is the result of running the Coccinelle script from > > scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to > > avoid scalar types (which need careful case-by-case checking), and > > instead replace kmalloc-family calls that allocate struct or union > > object instances: > > > > Single allocations: kmalloc(sizeof(TYPE), ...) > > are replaced with: kmalloc_obj(TYPE, ...) > > > > Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...) > > are replaced with: kmalloc_objs(TYPE, COUNT, ...) > > > > Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...) > > are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...) > > > > (where TYPE may also be *VAR) > > > > The resulting allocations no longer return "void *", instead returning > > "TYPE *". > > > > Signed-off-by: Kees Cook > > --- > > diff --git a/arch/um/drivers/ubd_kern.c b/arch/um/drivers/ubd_kern.c > > index 37455e74d314..42f392e6add3 100644 > > --- a/arch/um/drivers/ubd_kern.c > > +++ b/arch/um/drivers/ubd_kern.c > > @@ -1069,20 +1069,16 @@ static int __init ubd_init(void) > > if (register_blkdev(UBD_MAJOR, "ubd")) > > return -1; > > > > - irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE, > > - sizeof(struct io_thread_req *), > > - GFP_KERNEL > > - ); > > + irq_req_buffer = kmalloc_objs(struct io_thread_req *, > > + UBD_REQ_BUFFER_SIZE, GFP_KERNEL); > > irq_remainder = 0; > > > > if (irq_req_buffer == NULL) { > > printk(KERN_ERR "Failed to initialize ubd buffering\n"); > > return -ENOMEM; > > } > > - io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE, > > - sizeof(struct io_thread_req *), > > - GFP_KERNEL > > - ); > > + io_req_buffer = kmalloc_objs(struct io_thread_req *, > > + UBD_REQ_BUFFER_SIZE, GFP_KERNEL); > > Building um:defconfig ... failed > -------------- > Error log: > arch/um/drivers/ubd_kern.c: In function 'ubd_init': > arch/um/drivers/ubd_kern.c:1072:24: error: assignment to 'struct io_thread_req * (*)[]' from incompatible pointer type 'struct io_thread_req **' [-Wincompatible-pointer-types] > 1072 | irq_req_buffer = kmalloc_objs(struct io_thread_req *, > | ^ > arch/um/drivers/ubd_kern.c:1080:23: error: assignment to 'struct io_thread_req * (*)[]' from incompatible pointer type 'struct io_thread_req **' [-Wincompatible-pointer-types] > 1080 | io_req_buffer = kmalloc_objs(struct io_thread_req *, > > Guenter Proposed solution sent now: https://lore.kernel.org/all/20260223214341.work.846-kees@kernel.org/ -- Kees Cook