From nobody Tue Apr 7 06:22:56 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 B9DEA145355 for ; Mon, 16 Mar 2026 00:41:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773621719; cv=none; b=NgvD1O97qag3CNi5/Xpl0rd5c9YTjdJtpfyIHMBkfYHO3e6w0t52IF6MTJNRoxn44dcaFk/wg0KyUgib7aCt9WSBBt/4EtFQjLuXqp5lbixfNxgHp8P5TZ9LLT2ogUMyeK9HxpakP1rfkzrKWuUzjT/XKHZNVVNcVOtggZpSyoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773621719; c=relaxed/simple; bh=kuEXRTeWf/8AKOmPoxmyZoAuGNBTvg0wBi1WKsDLmXQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=qqJZ7Z8ubk0+ik2sae5Ssx70XEWQcsGBOVFoA4LETujT3Scioowa2bIXVL+MafyFOJt/f4HYflwVTItaoaYubuxDixymZkOhjn+d38vwHsmdvGwBzTpL+ciRD9mfMZL8KKiQ/yHI7oTstDsFl7m6Z8FIMrq1ElxQfH3Z2Jsh5uk= 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=iN/r6yf1; arc=none smtp.client-ip=209.85.210.171 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="iN/r6yf1" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-82976220e97so2331564b3a.3 for ; Sun, 15 Mar 2026 17:41:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773621718; x=1774226518; 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=bpEaCXx7J3YG0Lph6zn7bmRefcBWEhcsCVFdscO+mwk=; b=iN/r6yf1iSDluF9fj7MdwRTGFLrQ1WLOE/lH83/88SqLX6uiWaTGkQUJBVOHEHDLBG uvfsRjbuhe3kAfT1XDcrUOj1mrINKj3cyvBmtIAUgBIEXxqPgPQGYHYLnrI6486gc+TS sE/ffJMaB719oNB3XArirvYl/UsqGU0FS8BSK0/fFxMI1LwjG++HtsFZ9iNZ8t9KyV3X +4JCEr/o8pkMcUdPm6cKlLSVZsobvvSlXI2MVq5feZdz1ctUkS+TqIHQcNV4vKCJE5oa RsBtQZeBD+L0fs5gRdCf0SZaGXigYobegEFShO/CcDQdRDV3zt27wpOYlb5/9TuO8WJZ qUvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773621718; x=1774226518; 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=bpEaCXx7J3YG0Lph6zn7bmRefcBWEhcsCVFdscO+mwk=; b=KXHJv3uocwaj/heeEtO2mrc1Occ/PZFMWCYj/fWOBClKcRQ4PL3t6EiYo2KLYzZ1jI zril75IA/8t0IEm7EG1FNWVO0dPpYo4ofTMC4Fh21O3Vx2X3uJjvif7hJlG4Xqsr1ppI V0C3cuVgZ2tNehK665f0RXi9ulVlVlWaIT+DjJZuEg/r+zq8JssWEz5uibOxobmmJqrs 01cjvFKczPhZ8A/5IF+mPrtqtMoCfcNxdWFALcEyglSdF7kTVfaBQmhGBisX9WRMOzPB FzsxMZw1zWTVsizp8515l46LYTpNtxjb+FkK/jJmn4CCjs72SCenZUaXaDIv8SdhCSRk qCKA== X-Gm-Message-State: AOJu0YxD7AUwQu9l5igWboZK1aR6cFYADQte073XW3ng9MACSJZ38Rx0 IbHyYcmIhbC5NOTYxD0d0JwupmZOW4Dp3f7eG8WIdggQJFit9bUHqq+Vt/6zieS+ X-Gm-Gg: ATEYQzzQVtRq1LiAhxsdp3aTzwr7CBQEGaVPXRXCEj7FScM4M/D8EuJL18C3sCHokq9 smXyJAC9LPOyXwM5d3UkYqTq8FS9SMC9Isad0X4W+t4K3qghnNs9LxpbsT9uu8MWhOn4awgRmfj kN4pD+nmLnK5goRrn/+1ot5IPs55QpJNlkjFnU7mFN6HyP/YrMXIx0rDbWs/ShLe3Gt79XbgKaj UCvMlEjdDGWKVw4K2bAoC9AxGLHel8WQKOzUqSX+xHQ2SzzB62y95AKtMzZjdW/CJldAPs5S2oH d/ivW9Lu7SdSkPUJb65/3pY6KzcJ/7mcc2hBgAHvOZE8F5uYBkzMtzAAW6s8oPxf8aK76KjUGmr J0+fpDf8SYv0nbL2cpqKWfv27YQWIMpip1ZdtH+T1KPE9FLva5afxjqGW6/wkph4KPRnuThZr5l 0BSkGunGHwTHfe+8N8m/gChGUleQD+u7B3L9BvbtsM+QHSB8JQ7QSWgqA8Q2OAxzKWmw== X-Received: by 2002:a05:6a00:94c2:b0:824:adf4:5a32 with SMTP id d2e1a72fcca58-82a198b4d94mr9901439b3a.42.1773621717792; Sun, 15 Mar 2026 17:41:57 -0700 (PDT) Received: from ryzen ([2601:644:8000:56f5::8bd]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82a07384a7dsm11975492b3a.57.2026.03.15.17.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Mar 2026 17:41:57 -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: [PATCH] hsi: hsi_core: use kzalloc_flex Date: Sun, 15 Mar 2026 17:41:39 -0700 Message-ID: <20260316004139.267912-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 --- drivers/hsi/hsi_core.c | 36 ++++++++++++++---------------------- include/linux/hsi/hsi.h | 2 +- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/drivers/hsi/hsi_core.c b/drivers/hsi/hsi_core.c index 7cb2dcb30fdb..f1b84add291a 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); =20 - kfree(hsi->port); kfree(hsi); } =20 static void hsi_port_release(struct device *dev) { - kfree(to_hsi_port(dev)); } =20 /** @@ -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; =20 if (!n_ports) return NULL; =20 - 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); =20 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); }; =20 #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, de= vice) --=20 2.53.0