From nobody Tue Apr 7 18:33:28 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 06CE339A077 for ; Thu, 12 Mar 2026 04:59:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773291581; cv=none; b=DzMpd6GScmhdHOmHm3avGeyQOhVubmOU/Re3V4XfREhwAckxaXIB9r88PNtZbrDxFWTYmpgZ/wlOwTGcVKpvDgZ2wI9NotW6GiNsQiGVsSJJm87QDJOhdajvyXMWbDHJC1Q5H/A2bsv17YtGpNOSez9Q9wkURGfmmTTMuKkqLaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773291581; c=relaxed/simple; bh=70BeD1knxFPBhbOveZaJ78TQk69t5SY8pDh27xzK/l0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=O+n7/OEjE+kpiCangLGBDRIdVOaqBqfX0w1x0nsXcewmEcrDnh3sYqj+eMRFUPMJ108Z35kGhhtmrIzxTpzubd7fod6pJL/URKGYre+uWNUbdNiR0SxGKQjGOoLTzqyypPzSXPRCyJmzfqUgQrzrL3U4sTrvb6s+PsBS2iwfn3U= 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=j+/HuwE1; arc=none smtp.client-ip=209.85.214.175 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="j+/HuwE1" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2ae50a33ff8so4954215ad.3 for ; Wed, 11 Mar 2026 21:59:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773291579; x=1773896379; 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=JjiMf+HrHgumtDrytCb9PW5t02HTgkYKwwniQ8SZW0c=; b=j+/HuwE1M0YhY0t0xFVPNRINMa8yttMhjc7bxlbmfqCpLrUQB4H040lSzT/13alyRy 6yvjNsv9LFzHDhPaS9M90ILR0gTZH0aHYkaX0fobzKcViqHHp4e7S4lzjAjVXJLMWxDw TantOQrphn0ADdBQps61nNMI4q89HnqPM82gIniyNxJ1x4a6545X0lHazNjsMeIa66iB 7ZkLB29JXwWDe4CupLPnaT08KXZYtMltovVUiejLWKkaZAwuT6iXqrO4G7SCnOwxZvOn 2VAp1bFlCGvZmfyVCBgx603IBOslw1sDbFgimSN8ThZSX5FgXHBEgS+P47IZ1YrIwaBN eaJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773291579; x=1773896379; 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=JjiMf+HrHgumtDrytCb9PW5t02HTgkYKwwniQ8SZW0c=; b=absetfyu5VFYg1o0y9MGuQDVm0pYmB1aJ1GSu0zFuSM7zQwCet0MJGiPL+C3XqFt8i SIlPg+5BSavXnQY1AGj1J53vnPppLiB+ntrAf3qPhOBmpRX5ld8FsSToe9ckay6jr2uV 6u2BzfkjoIt6Lsol8EtExPxWnGZ8p3CJ7Nbirch6VszUPvsOsvtFoFEgIQI1vkM0ydyY 68B4zq9SnSENSvnDbt2bt1Pez+9HhJ5TfpjhuyNtCIlL9Y2jxwRELGYlXoszyZoEe6be MqVtBp6JSFlkfNWgmFLNUI9Jol93kU4DhurtXhKULrfAaB4RO1ChUv5H8rpXV9xrEynI SW2g== X-Forwarded-Encrypted: i=1; AJvYcCUJ/pQCkanhzK8JB3OFDfuQ81ueJqkZCit1SO74zYRbZOA3IvMP76mfYFEhq4ojb3I7Q0LRpqYnACsxQyY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1PzUsZpq1umPothP8nUIWQkgzUh+k8Yt6Qob/rZ/jSthskPWK o9UBFs5l1pszj8WOvQQCgNJSeTAJ8mZI53PPsDZSAELzHOXNX6EsHNQo X-Gm-Gg: ATEYQzxI66sorpnSZoo9XbjpJfhKA3vpBNv/Bj8jFulVVoiM3vS144ygEgeCLbRSQ// mcMMHKZAfoeHioJCoP4E84OlozITgNce98ABHmIbFcfWw7GXDqVfL6nHjUMZTvPVALiCeqQD6Zw p5npny9/I+qYoXYjfk/BZ0lEaEmEDy4VgQwsr3LDnTJYsz3Ux8u5vsjzu8TOWEh4roSpg532+5p BTEclOOSOULviMQgDKxaoA6zOCWsU8xv5NA1zalniGzcqtdTMRSx8N2a4CTDtXokuLpqddtPT7S yZQrtMRIvot1RCko5DlIrar7Z9FBNAygXXqy0r8jrDfZxt1fM5UlFb6cKkDp9ar4huN8biEasYo oH0GT07ed+c33C32acpIn1ViEHUP7lLNYb58d7XE38eGGvw1zTLQAVJDcnaFufOzOgLd9oQ+lKt i6Q5ZxS6NzSFiMsptMOk5HHIHYgQVU3hh/oyYh0aNnWmF73tjWqHQC7Q== X-Received: by 2002:a17:902:ea11:b0:2ae:b991:a46f with SMTP id d9443c01a7336-2aeb991a868mr25734465ad.42.1773291579268; Wed, 11 Mar 2026 21:59:39 -0700 (PDT) Received: from ryzen ([2601:644:8000:56f5::8bd]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2aeae255a05sm55804755ad.36.2026.03.11.21.59.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2026 21:59:38 -0700 (PDT) From: Rosen Penev To: mhi@lists.linux.dev Cc: Manivannan Sadhasivam , Kees Cook , "Gustavo A. R. Silva" , linux-arm-msm@vger.kernel.org (open list:MHI BUS), 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)?\b) Subject: [PATCH] bus: mhi: use kzalloc_flex Date: Wed, 11 Mar 2026 21:59:21 -0700 Message-ID: <20260312045921.7663-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" Change kzalloc + kcalloc to just kzalloc with a flexible array member. Add __counted_by for extra runtime analysis when requested. Move counting assignment immediately after allocation as required by __counted_by. Move mhi_buf definition as a complete definition is needed for flex arrays. It's not a pointer anymore. Signed-off-by: Rosen Penev --- drivers/bus/mhi/host/boot.c | 20 +++----------------- include/linux/mhi.h | 34 +++++++++++++++++----------------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/drivers/bus/mhi/host/boot.c b/drivers/bus/mhi/host/boot.c index f16a1e67a667..e1d77cecd75e 100644 --- a/drivers/bus/mhi/host/boot.c +++ b/drivers/bus/mhi/host/boot.c @@ -333,15 +333,10 @@ static int mhi_alloc_bhi_buffer(struct mhi_controller= *mhi_cntrl, struct image_info *img_info; struct mhi_buf *mhi_buf; =20 - img_info =3D kzalloc_obj(*img_info); + img_info =3D kzalloc_flex(*img_info, mhi_buf, 1); if (!img_info) return -ENOMEM; =20 - /* Allocate memory for entry */ - img_info->mhi_buf =3D kzalloc_obj(*img_info->mhi_buf); - if (!img_info->mhi_buf) - goto error_alloc_mhi_buf; - /* Allocate and populate vector table */ mhi_buf =3D img_info->mhi_buf; =20 @@ -358,8 +353,6 @@ static int mhi_alloc_bhi_buffer(struct mhi_controller *= mhi_cntrl, return 0; =20 error_alloc_segment: - kfree(mhi_buf); -error_alloc_mhi_buf: kfree(img_info); =20 return -ENOMEM; @@ -375,14 +368,11 @@ int mhi_alloc_bhie_table(struct mhi_controller *mhi_c= ntrl, struct image_info *img_info; struct mhi_buf *mhi_buf; =20 - img_info =3D kzalloc_obj(*img_info); + img_info =3D kzalloc_flex(*img_info, mhi_buf, segments); if (!img_info) return -ENOMEM; =20 - /* Allocate memory for entries */ - img_info->mhi_buf =3D kzalloc_objs(*img_info->mhi_buf, segments); - if (!img_info->mhi_buf) - goto error_alloc_mhi_buf; + img_info->entries =3D segments; =20 /* Allocate and populate vector table */ mhi_buf =3D img_info->mhi_buf; @@ -402,7 +392,6 @@ int mhi_alloc_bhie_table(struct mhi_controller *mhi_cnt= rl, } =20 img_info->bhi_vec =3D img_info->mhi_buf[segments - 1].buf; - img_info->entries =3D segments; *image_info =3D img_info; =20 return 0; @@ -411,9 +400,6 @@ int mhi_alloc_bhie_table(struct mhi_controller *mhi_cnt= rl, for (--i, --mhi_buf; i >=3D 0; i--, mhi_buf--) dma_free_coherent(mhi_cntrl->cntrl_dev, mhi_buf->len, mhi_buf->buf, mhi_buf->dma_addr); - kfree(img_info->mhi_buf); - -error_alloc_mhi_buf: kfree(img_info); =20 return -ENOMEM; diff --git a/include/linux/mhi.h b/include/linux/mhi.h index 88ccb3e14f48..fb3ba639f4f8 100644 --- a/include/linux/mhi.h +++ b/include/linux/mhi.h @@ -85,17 +85,33 @@ enum mhi_ch_type { MHI_CH_TYPE_INBOUND_COALESCED =3D 3, }; =20 +/** + * struct mhi_buf - MHI Buffer description + * @buf: Virtual address of the buffer + * @name: Buffer label. For offload channel, configurations name must be: + * ECA - Event context array data + * CCA - Channel context array data + * @dma_addr: IOMMU address of the buffer + * @len: # of bytes + */ +struct mhi_buf { + void *buf; + const char *name; + dma_addr_t dma_addr; + size_t len; +}; + /** * struct image_info - Firmware and RDDM table * @mhi_buf: Buffer for firmware and RDDM table * @entries: # of entries in table */ struct image_info { - struct mhi_buf *mhi_buf; /* private: from internal.h */ struct bhi_vec_entry *bhi_vec; /* public: */ u32 entries; + struct mhi_buf mhi_buf[] __counted_by(entries); }; =20 /** @@ -488,22 +504,6 @@ struct mhi_result { int transaction_status; }; =20 -/** - * struct mhi_buf - MHI Buffer description - * @buf: Virtual address of the buffer - * @name: Buffer label. For offload channel, configurations name must be: - * ECA - Event context array data - * CCA - Channel context array data - * @dma_addr: IOMMU address of the buffer - * @len: # of bytes - */ -struct mhi_buf { - void *buf; - const char *name; - dma_addr_t dma_addr; - size_t len; -}; - /** * struct mhi_driver - Structure representing a MHI client driver * @probe: CB function for client driver probe function --=20 2.53.0