From nobody Mon Apr 6 16:46:04 2026 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 7D7743EF678 for ; Wed, 18 Mar 2026 19:10:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773861057; cv=none; b=Ncz1weC+27QFl2KP3WFtYhd+u+GBJJ+iHmb5yTqAljctacDxgJYKlwxTMs9xdOHjdaaAeRdiTcZ1cmZMWVdpfBMbtc5S+QhqP6cDfuc6d6cdy2wiMVaJ4JFRIRu+SI4ldh1Xf9zxHfbfclTjqruUAAfgj2n55ufYB7bEUmksEAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773861057; c=relaxed/simple; bh=tB+Nv6pjBL7gh/YSoN1m/+MbKOlsv6e5EOr2WdQwmn0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=MQoiCxDU5K2gNqw8SkUnhpoIOI4tZsqtr/vg+ga3VOkZD1ZusvMtrL51Lc7TK0xEyMLtevHe3lu1C05WBMXCINm07S9vNOT04vGdzUsX/prDt9d2DTmXLI3IRS7zjWMFoUnp36C/aLAUPXFX5c/hIgYYccqg0+Ml/XgDqHZZCkI= 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=RXRrzcij; arc=none smtp.client-ip=209.85.216.46 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="RXRrzcij" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-35bb7afdc38so244140a91.1 for ; Wed, 18 Mar 2026 12:10:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773861055; x=1774465855; 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=N/fuMQSCwheo92q+sJzp01BF5cuisAnFpxfybDN6btA=; b=RXRrzcijFCiysqSrTKYFo5cqpEnivzGnqYa5YXfR8XTvSouCJcKH6mrbuj1lWRDXq4 AqI2hFwby6GrLaRHQfm8o+k4nRGxx2hU3GwdSVtr+OOKPJff4LCN2fo1pUBEJoJc1j4+ ubW0flsVQEuA1mzNBtxCsGEjU6BNk1keSurSG04EBa5TyEcwDxu+CKXjgaP3Vt6fEC1x BSq+UXZ7aZZBzaQLc230bQsVW3uDrnrevvT0wQlTF9zPqmoyw2kPEHkXrBPjE12GiAYX 1pt4Q9RVwkug2mkmOIE+2SYechuyB2lg8huA3k/EgpLErtLffbrelcQI+SogB978cAaG jKYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773861055; x=1774465855; 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=N/fuMQSCwheo92q+sJzp01BF5cuisAnFpxfybDN6btA=; b=GMRV+CVfMFby7evz3fuJ4Apfh0u5psmx+G+RwtdRD+A+i26nMP8jSUixlzKU42F3Cr k/DVlJEc9gKPGSGATPYyjdrkIjjQwKh40bgIe/totCLY12GnwBHi98+VJXNsMadSUgAb hE9BXPg8VnwxIxXWQUSCi4lVq48pB9Qt5cxa5NmGDvk7XM4s6Rs83gLZhsOAYhCj/sRf UQ4RqnwQYmOGkES9qTqYnLHbiay3vYOZBI/Rg56hdjksrW4x/fKSZcBWLVyvYEYJ8gnV j5xYBNBbusRCkOfAZs6F9BW663uc9F9/y4aG3osEGk0I8Dqtil7WAXlL1bYLeED8592r XXCQ== X-Gm-Message-State: AOJu0YyZYUzUD6V413SS5Od7hYcWxAJi3T/86+tNuvDUsrWF5Afcz4t7 KH6VkSdzaZO7ZLcNrKxp8XJD8muDrXwaPEKSHyn+S56ckijbwCU5/rNtE3tFnw== X-Gm-Gg: ATEYQzx4BrUmPH1M8VlDdLrv0IQOIcGsb+uhw5JLLSso1o/y6mPGG4gMfy2grVWMQsb o5ORKjruMjjC0ie/DmslormElP6g9ivXiwzoZIGe9jGzDnvAuuLAZnIHAz5Mx04rTvBzsDj91aV hFaiajYHb+aW01jOUDX6lOLU3CrQsWclTT9KB4MHrDsLyJ6RFN0KFeheAfQzPENEsh5292NGShO 5Gm71HEk6DJaSTuxS/qAkfEo0qo2KaWkSZG0nSSWNtADQDDcDhFDjxQPq/plxQAyC+56+jW3C9E 2ck9HqwfLqyBvPz8vaL8/c/oMoVxb4rkdxhf1I+qVeXlwyiXmrEmR+MMwODNNM6Iu7EOjvp2uF1 XIETlVNrvxqAgBGD0hMsIWcMvIp1x5/P1xdtUvHgsxJ1XzXs0dP7amd0uRbY3Vl4PGEZ/t3pOEz f6drrwQ+h7QGXhTDDUWQpEUzml+S1R2xsmv/p+xPl/Rs/RsZ4LCfgYlFE= X-Received: by 2002:a17:90b:5584:b0:35b:9896:cbcd with SMTP id 98e67ed59e1d1-35bb9f123bcmr3740207a91.27.1773861055321; Wed, 18 Mar 2026 12:10:55 -0700 (PDT) Received: from ryzen ([2601:644:8000:56f5::8bd]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35bc62f6003sm355894a91.8.2026.03.18.12.10.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Mar 2026 12:10:54 -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: [PATCHv3] hsi: hsi_core: use kzalloc_flex Date: Wed, 18 Mar 2026 12:10:37 -0700 Message-ID: <20260318191037.5661-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 --- v3: add back kfree for ports. v2: remove wrong null check drivers/hsi/hsi_core.c | 37 +++++++++++++++---------------------- include/linux/hsi/hsi.h | 2 +- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/drivers/hsi/hsi_core.c b/drivers/hsi/hsi_core.c index 7cb2dcb30fdb..754949f5ebd6 100644 --- a/drivers/hsi/hsi_core.c +++ b/drivers/hsi/hsi_core.c @@ -342,7 +342,6 @@ static void hsi_controller_release(struct device *dev) { struct hsi_controller *hsi =3D to_hsi_controller(dev); - kfree(hsi->port); kfree(hsi); } @@ -446,7 +445,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 +461,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