From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (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 2004A2E1C4E for ; Mon, 8 Jun 2026 05:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780895267; cv=none; b=X/5YIwVcJKQjcndAG+oCiqvKwaAdrn5mWD8dq3e0bIm5R+uT8nWpToehqGCdEB0lu06DTJcJ9vBkY+QgN1t8FhFa5gUMXgGCG7JdxOWcW/rmyjRAFOIGV3PtpC6EnoYodNR8udYyQxddrrOlLCRtUjCnkv/eKMB/x1DnhIF9dfI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780895267; c=relaxed/simple; bh=lF68z8SNLZcBGhYrMebJCDeUa4Ic3V094gG6WdMXo4M=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=oIEvyFuDn6hyDYAFtJSM4qoId7ibuVGSA7dddY9IMdWTtsBwpdjUjf2ZhsRNKGX3rFW0ZRfI3LEDwHQjMKKoh+lUcvPOefxIFMDMecg7FJhQS375CvyKm2eStyE33Y+LIPi194khG0O0+P7ArC7U5Z43MJCjvmLtB7f81OilQm8= 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=ZNgRQ7QZ; arc=none smtp.client-ip=209.85.128.182 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="ZNgRQ7QZ" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-7dc6fbf3e86so41109967b3.3 for ; Sun, 07 Jun 2026 22:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780895264; x=1781500064; 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=2/Cp0a9gP5wzO7K0gKJb8DvCw3TdIDmmKCsdwU1/y3w=; b=ZNgRQ7QZAfWGbpcI9RK4HvnqQgz3sSNN2npUwUJfr5spl/OGGuaT40bCmfqSYgetBj ApvZTyaciElvFb049qq14wG3h2RSMNAfYhtn1WePgYShTw1rFzJBlxJXczARrb1n5T73 5bn/cqlMyIPBMsgYj+lTOoUErRVsIK9xG4WW5DR3PTSyW3KcaPBIa7zwHKGpCCas5VhK vOgo4sQ7PqQG/8SetDjrajB6mLe9bXe0GiM+O6BVjov8zxaxcfIZ6nhy6wTCqtw6hBlV rz2h4XFCb90qQ4vgQxbR3xLkRCctjAzbTeFZBjAI3m8+kaTNdABvOyyjHNx7AIL8qrjr ghDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780895264; x=1781500064; 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=2/Cp0a9gP5wzO7K0gKJb8DvCw3TdIDmmKCsdwU1/y3w=; b=Gu82CYZB6vgwqRzf5hJdBPM2goy7sFrt+Na/aRQ4Uzq2ZXS9ddygCLH9Gnsz4cV8ke qV09+yXsilWvhBXqGJRRNZ0iTmcfTEDSih28FjKIveurW0OUWV4i3PqeHQYRlCUYEUnp 3PEELqMcI5xPMOSUbmDzbBbdbILPPMNlThDT2bq7cFnCdiyj4fMa5YvuAstScmMJW78x u6nuIgpFiq5Bv441xcH3RgfOf2m/9aKc0WOd9QG7xzbvkkGs33mz9nx/VCGe0Cld+LSJ WzHEbQHbfhllpcPp+XTx3+nDxY7C15JFiEXA+DUjYaPOjYJPdiVy0DNJBcWg5cEFrKe/ XECA== X-Forwarded-Encrypted: i=1; AFNElJ9HPhnlC7n8z+Yno+JMSbDgtcF+vfC9NkV7eHXF/hJZ0/G6ebnhlJopoKIci/OgQ1EDadqeX31V42fqguQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyjWpSVPoMiRGOa4axoY9nxPqQm5sE0VlMs4QwjNdXdH34B03w3 aNaLmQRvK+DgvrLV4QPirhTS6CMMcUj7Yfh6+OOF33zL5xaxcUTT7k0Z X-Gm-Gg: Acq92OHV35IE7RJOKqlyYepiX5HKM/hw6csksxIfz1F3qY2hD1NJd9LniDUna+8Sgpd e67Kupym/9xgCo9l337nlAQ0LJ3Ga08On8TMwG4FmRef9NNfB2QtKX2AiIBYJpGTvd9N8o2AeAV vShjFuwG3AVJIwBHWd2iEcBXgd8sWTxIyWDJMNBaS0Z+1xpRI09HS0Hjq+UmJE5NM5KJjFctldr o3yetCg5NhvI+PZuNaLzpda7C+wtoU0m3m2Ceup3r51WgRsz1UtbLrYgbHmM5L474ABkS65zCmP F9b8Adp65J01DmFVcMzeKxW3P3oGHXnuILNqmVlztaWmvTZMqDf2ynPxOx1xpM4epgYFp5DYyY3 WYgVSkQ/kmRVyMHG35A9IIOm/VKrahN9C0NMCW0Px21j7kHTltTsjUqFPgD2I28sRqEOjlaiBw7 2blGnormBbL45Uvz1/PElZz6mmbAWv0TmjEVTgqtORSxvi9rqhbnIddrAktUQfehLcVcVEXIQhw LQPFnaaAkE7z5XhN5E0Q5+8mJ+9pssljrQCkzufOg9zJQ== X-Received: by 2002:a05:690c:6181:b0:7bd:73f3:7a70 with SMTP id 00721157ae682-7ed109c657bmr130266297b3.32.1780895264088; Sun, 07 Jun 2026 22:07:44 -0700 (PDT) Received: from ryzen ([2601:644:8000:5b5d:7285:c2ff:fe45:8a32]) by smtp.gmail.com with ESMTPSA id 00721157ae682-7ea20ea8179sm80663187b3.2.2026.06.07.22.07.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jun 2026 22:07:42 -0700 (PDT) From: Rosen Penev To: arm-scmi@vger.kernel.org Cc: Sudeep Holla , Cristian Marussi , Kees Cook , "Gustavo A. R. Silva" , linux-arm-kernel@lists.infradead.org (moderated list:SYSTEM CONTROL & POWER/MANAGEMENT INTERFACE (SC...), linux-kernel@vger.kernel.org (open list), linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be|_ptr)?\b) Subject: [PATCH] firmware: arm_scmi: simplify some allocations Date: Sun, 7 Jun 2026 22:07:24 -0700 Message-ID: <20260608050724.6358-1-rosenp@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Use flexible array members to combine allocations and remove kcalloc usage. Add __counted_by where appropriate for extra runtime analysis. Move counting variable assignment after allocation before any potential array access. Signed-off-by: Rosen Penev --- drivers/firmware/arm_scmi/notify.c | 42 ++++++++++++------------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c index 40ec184eedae..45cbb3d2d684 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -209,11 +209,11 @@ struct scmi_registered_events_desc; * @init_work: A work item to perform final initializations of pending handlers * @notify_wq: A reference to the allocated Kernel cmwq * @pending_mtx: A mutex to protect @pending_events_handlers + * @pending_events_handlers: An hashtable containing all pending events' + * handlers descriptors * @registered_protocols: A statically allocated array containing pointers to * all the registered protocol-level specific information * related to events' handling - * @pending_events_handlers: An hashtable containing all pending events' - * handlers descriptors * * Each platform instance, represented by a handle, has its own instance of * the notification subsystem represented by this structure. @@ -225,8 +225,8 @@ struct scmi_notify_instance { struct workqueue_struct *notify_wq; /* lock to protect pending_events_handlers */ struct mutex pending_mtx; - struct scmi_registered_events_desc **registered_protocols; DECLARE_HASHTABLE(pending_events_handlers, SCMI_PENDING_HASH_SZ); + struct scmi_registered_events_desc *registered_protocols[]; }; /** @@ -276,13 +276,13 @@ struct scmi_registered_event; * @eh_sz: Size of the pre-allocated buffer @eh * @in_flight: A reference to an in flight &struct scmi_registered_event * @num_events: Number of events in @registered_events - * @registered_events: A dynamically allocated array holding all the registered - * events' descriptors, whose fixed-size is determined at - * compile time. * @registered_mtx: A mutex to protect @registered_events_handlers * @ph: SCMI protocol handle reference * @registered_events_handlers: An hashtable containing all events' handlers * descriptors registered for this protocol + * @registered_events: A dynamically allocated array holding all the registered + * events' descriptors, whose fixed-size is determined at + * compile time. * * All protocols that register at least one event have their protocol-specific * information stored here, together with the embedded allocated events_queue. @@ -302,11 +302,11 @@ struct scmi_registered_events_desc { size_t eh_sz; void *in_flight; int num_events; - struct scmi_registered_event **registered_events; /* mutex to protect registered_events_handlers */ struct mutex registered_mtx; const struct scmi_protocol_handle *ph; DECLARE_HASHTABLE(registered_events_handlers, SCMI_REGISTERED_HASH_SZ); + struct scmi_registered_event *registered_events[] __counted_by(num_events); }; /** @@ -338,9 +338,9 @@ struct scmi_registered_event { void *report; u32 num_sources; bool not_supported_by_platform; - refcount_t *sources; /* locking to serialize the access to sources */ struct mutex sources_mtx; + refcount_t sources[] __counted_by(num_sources); }; /** @@ -706,9 +706,12 @@ scmi_allocate_registered_events_desc(struct scmi_notify_instance *ni, if (WARN_ON(ni->registered_protocols[proto_id])) return ERR_PTR(-EINVAL); - pd = devm_kzalloc(ni->handle->dev, sizeof(*pd), GFP_KERNEL); + pd = devm_kzalloc(ni->handle->dev, struct_size(pd, registered_events, num_events), + GFP_KERNEL); if (!pd) return ERR_PTR(-ENOMEM); + + pd->num_events = num_events; pd->id = proto_id; pd->ops = ops; pd->ni = ni; @@ -722,12 +725,6 @@ scmi_allocate_registered_events_desc(struct scmi_notify_instance *ni, return ERR_PTR(-ENOMEM); pd->eh_sz = eh_sz; - pd->registered_events = devm_kcalloc(ni->handle->dev, num_events, - sizeof(char *), GFP_KERNEL); - if (!pd->registered_events) - return ERR_PTR(-ENOMEM); - pd->num_events = num_events; - /* Initialize per protocol handlers table */ mutex_init(&pd->registered_mtx); hash_init(pd->registered_events_handlers); @@ -800,14 +797,11 @@ int scmi_register_protocol_events(const struct scmi_handle *handle, u8 proto_id, GFP_KERNEL); if (!r_evt) return -ENOMEM; + + r_evt->num_sources = num_sources; r_evt->proto = pd; r_evt->evt = evt; - r_evt->sources = devm_kcalloc(ni->handle->dev, num_sources, - sizeof(refcount_t), GFP_KERNEL); - if (!r_evt->sources) - return -ENOMEM; - r_evt->num_sources = num_sources; mutex_init(&r_evt->sources_mtx); r_evt->report = devm_kzalloc(ni->handle->dev, @@ -1666,18 +1660,14 @@ int scmi_notification_init(struct scmi_handle *handle) if (!gid) return -ENOMEM; - ni = devm_kzalloc(handle->dev, sizeof(*ni), GFP_KERNEL); + ni = devm_kzalloc(handle->dev, struct_size(ni, registered_protocols, SCMI_MAX_PROTO), + GFP_KERNEL); if (!ni) goto err; ni->gid = gid; ni->handle = handle; - ni->registered_protocols = devm_kcalloc(handle->dev, SCMI_MAX_PROTO, - sizeof(char *), GFP_KERNEL); - if (!ni->registered_protocols) - goto err; - ni->notify_wq = alloc_workqueue(dev_name(handle->dev), WQ_UNBOUND | WQ_FREEZABLE | WQ_SYSFS, 0); -- 2.54.0