From nobody Mon Apr 6 21:31:15 2026 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1940333FE33 for ; Tue, 17 Mar 2026 19:58:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773777519; cv=none; b=Ynl1aFPE5L63ngs9sj53JBOWS9Re4oO9i5DrTEAy8IZG9EQk8me4/KqnX93m8aQtryYhAu47mMbPN8l/FPcMImaSYZm0Grl/uvqTjmqNeq0DVXZD/xAfhhnDyb1Hk1uZatqqgsNf8zuYHhdes0yUpp4BHt0yR8pA1IO35xKvNT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773777519; c=relaxed/simple; bh=qsz1d/oevfNi4OpV9QErO5/xLrhS1DjMUh+I67nbjOw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=X21n42dMnZYeMQpoHCyE9yo/yeIqQqAhEhRfMO02MDI9Zw2NMDYSmRwup7G4FWnTetDNbOd8Ri+tlshwOFMPpw6Ks8kyjkArKlIpLk6kw9e/vwUGxtkCpaFKpVm3N8NnCZdBLGStUSKMQXepP2MZhG4arO7L1HVNMMwmNMEGNfY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AQPBfbAf; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AQPBfbAf" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-82a62714fe6so118427b3a.0 for ; Tue, 17 Mar 2026 12:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773777517; x=1774382317; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=IS8/enjwNEDrh+eav3dimYdFBJyNdQLwxYnBi25HGb8=; b=AQPBfbAftYIy/lqSFkqypuFUH6gtFQhf46xil0nAqlnVhCvwVf1J0AFXMX3B/oevv1 AAFkSm6xzeV1kWXq6vn3qmauuN364GJBsYs5HH9py273uFqw3CbG7OY+ux0BRxZ4iqiN JQSKit7vW3TLIl3E86aNojp6qtV7926s1GpUwXaXPTFXC4D3KgXtT3KfgaFgPSnwX69z znEEI9EIAB4nk4RDeu/pPbRLr7+Jcr8xQJ/ilD606yacgeDYAmWqRlZcy3wOjo89X1Ud iZqXn24Y3tpEncRGp5w9BPT5DIFpEd/QydcoVxHRC8h4NoqAo1FoN8Z865MIr/BB21Ac 4LrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773777517; x=1774382317; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=IS8/enjwNEDrh+eav3dimYdFBJyNdQLwxYnBi25HGb8=; b=SyA7tROOwyJ5ERtZRYc0+WlGjxSC0GKQ7Aw3Ob53dUFgLbNmPE/hD2Mm5spuhSWaVr KNET3W9EFBaIIBcfKnWdXnt+r6qiIMoM3IVQHn7g6u4DOjNwNZrJZYU1o7uGZTi1euO6 0bo6nXVnBgg98/sQsoT5bxfSjyVnn63Vv2iyIQfC1nZ181l9fuWAVmetAYmMezC+Vxo3 MID2MMAC2kYCQu0q0GE2YM0cKQcEN7SfbjO5rK92PwMl8ipvv5jBync23KSBFvSy0D9i UDT+VzfHPzYkuZTFak03YSjY8zm2WL8pLmX9LlkMvCquX0qDonKQ52jXzs6zaD2e5IwP +n9g== X-Gm-Message-State: AOJu0Yw0q+ahwOZtGQU3jt6mzPEWwkLzjrEL9MF00U0PCAJ8/o7FjljX wrAwkVHDS9KKGcSDRrxu1QJU6t+GLR0NjAD78NzMmeK8T0+SPtS054Wipo+coibC X-Gm-Gg: ATEYQzydds3hfU0VQgcwz9TuTlFykHgwl+EAXAZVA2BJGrLXv4wqEjATfNk+ycGVcZh hU3Tei60hgF9BQjcKQxj6l/oftTZZ3W37rOXXtZyCh9hXGC6ES7v/R2gI+2fJ0xDz9wIqVQeu06 1h2uaQ0GVGfM+Eym8IlacNCbtyiaMputhzoPFUF02tdBQ4g26RGsY87u9pB3BXsKi7IBeJsEpnu Y58AbF/4HbZ7FIToobVHo11AUXpbiVbjH4iq0chMEwlx+V7EJQXn/haIHqSA4e+z08CzEDVeYNK k/CsUddIxwsXcCRFPfI3BwoufceCHXdWLiRXpIknyBqat26FE8cXgIwr+Igr1CTDFTHkh7KoWrs Cx3/a9W2HWQSLJTFDZGm6sdCmaMsHh2A4uZ01hbVZPk91vp6i79jFedb4ryp48EzwsfelwjCUyQ KGeRNmay8d3TXAoGzi4Wa6MCA/g6VtpUr+Cq51Y4OAnYE1462C55vdXoM= X-Received: by 2002:a05:6a00:1a92:b0:827:33cb:c7a3 with SMTP id d2e1a72fcca58-82a5603a9demr4141015b3a.10.1773777517025; Tue, 17 Mar 2026 12:58:37 -0700 (PDT) Received: from ryzen ([2601:644:8000:56f5::8bd]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82a6b565930sm280444b3a.14.2026.03.17.12.58.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Mar 2026 12:58:36 -0700 (PDT) From: Rosen Penev To: linux-kernel@vger.kernel.org Cc: Sebastian Reichel , Kees Cook , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be)?\b) Subject: [PATCHv2] hsi: hsi_core: use kzalloc_flex Date: Tue, 17 Mar 2026 12:58:19 -0700 Message-ID: <20260317195819.15496-1-rosenp@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Simplifies allocations by using a flexible array member in this struct. Add __counted_by to get extra runtime analysis. Signed-off-by: Rosen Penev --- v2: remove wrong null check drivers/hsi/hsi_core.c | 38 +++++++++++++++----------------------- include/linux/hsi/hsi.h | 2 +- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/drivers/hsi/hsi_core.c b/drivers/hsi/hsi_core.c index 7cb2dcb30fdb..001bcadf4b8e 100644 --- a/drivers/hsi/hsi_core.c +++ b/drivers/hsi/hsi_core.c @@ -342,13 +342,11 @@ static void hsi_controller_release(struct device *dev) { struct hsi_controller *hsi =3D to_hsi_controller(dev); - kfree(hsi->port); kfree(hsi); } static void hsi_port_release(struct device *dev) { - kfree(to_hsi_port(dev)); } /** @@ -446,7 +444,7 @@ void hsi_put_controller(struct hsi_controller *hsi) return; for (i =3D 0; i < hsi->num_ports; i++) - if (hsi->port && hsi->port[i]) + if (hsi->port[i]) put_device(&hsi->port[i]->device); put_device(&hsi->device); } @@ -462,39 +460,33 @@ EXPORT_SYMBOL_GPL(hsi_put_controller); struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t fl= ags) { struct hsi_controller *hsi; - struct hsi_port **port; unsigned int i; if (!n_ports) return NULL; - hsi =3D kzalloc_obj(*hsi, flags); + hsi =3D kzalloc_flex(*hsi, port, n_ports, flags); if (!hsi) return NULL; - port =3D kzalloc_objs(*port, n_ports, flags); - if (!port) { - kfree(hsi); - return NULL; - } + hsi->num_ports =3D n_ports; - hsi->port =3D port; hsi->device.release =3D hsi_controller_release; device_initialize(&hsi->device); for (i =3D 0; i < n_ports; i++) { - port[i] =3D kzalloc_obj(**port, flags); - if (port[i] =3D=3D NULL) + hsi->port[i] =3D kzalloc_obj(**hsi->port, flags); + if (hsi->port[i] =3D=3D NULL) goto out; - port[i]->num =3D i; - port[i]->async =3D hsi_dummy_msg; - port[i]->setup =3D hsi_dummy_cl; - port[i]->flush =3D hsi_dummy_cl; - port[i]->start_tx =3D hsi_dummy_cl; - port[i]->stop_tx =3D hsi_dummy_cl; - port[i]->release =3D hsi_dummy_cl; - mutex_init(&port[i]->lock); - BLOCKING_INIT_NOTIFIER_HEAD(&port[i]->n_head); - dev_set_name(&port[i]->device, "port%d", i); + hsi->port[i]->num =3D i; + hsi->port[i]->async =3D hsi_dummy_msg; + hsi->port[i]->setup =3D hsi_dummy_cl; + hsi->port[i]->flush =3D hsi_dummy_cl; + hsi->port[i]->start_tx =3D hsi_dummy_cl; + hsi->port[i]->stop_tx =3D hsi_dummy_cl; + hsi->port[i]->release =3D hsi_dummy_cl; + mutex_init(&hsi->port[i]->lock); + BLOCKING_INIT_NOTIFIER_HEAD(&hsi->port[i]->n_head); + dev_set_name(&hsi->port[i]->device, "port%d", i); hsi->port[i]->device.release =3D hsi_port_release; device_initialize(&hsi->port[i]->device); } diff --git a/include/linux/hsi/hsi.h b/include/linux/hsi/hsi.h index 6ca92bff02c6..ea6bef9b6012 100644 --- a/include/linux/hsi/hsi.h +++ b/include/linux/hsi/hsi.h @@ -271,7 +271,7 @@ struct hsi_controller { struct module *owner; unsigned int id; unsigned int num_ports; - struct hsi_port **port; + struct hsi_port *port[] __counted_by(num_ports); }; #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, de= vice) -- 2.53.0