From: Kees Cook <kees@kernel.org>
To: Rosen Penev <rosenp@gmail.com>
Cc: linux-usb@vger.kernel.org,
Andreas Noever <andreas.noever@gmail.com>,
Mika Westerberg <westeri@kernel.org>,
Yehezkel Bernat <YehezkelShB@gmail.com>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
open list <linux-kernel@vger.kernel.org>,
"open list:KERNEL HARDENING (not covered by other
areas):Keyword:b__counted_by(_le|_be)?b"
<linux-hardening@vger.kernel.org>
Subject: Re: [PATCHv3] thunderbolt: use kzalloc_flex
Date: Fri, 20 Mar 2026 11:48:59 -0700 [thread overview]
Message-ID: <202603201148.DAE09FC@keescook> (raw)
In-Reply-To: <20260318185237.4742-1-rosenp@gmail.com>
On Wed, Mar 18, 2026 at 11:52:37AM -0700, Rosen Penev wrote:
> Simplifies allocation by using a flexible arraay member.
>
> Added __counted_by for extra runtime analysis.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
I don't see struct tb_path composed anywhere else; looks good to me.
Reviewed-by: Kees Cook <kees@kernel.org>
-Kees
> ---
> v3: fix kdoc
> v2: remove extra kfree to fix kernel test bot.
> drivers/thunderbolt/path.c | 28 +++++++---------------------
> drivers/thunderbolt/tb.h | 5 +++--
> 2 files changed, 10 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c
> index 22fb4a1e1acd..8713ea0f47c1 100644
> --- a/drivers/thunderbolt/path.c
> +++ b/drivers/thunderbolt/path.c
> @@ -150,22 +150,17 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
> num_hops++;
> }
>
> - path = kzalloc_obj(*path);
> + path = kzalloc_flex(*path, hops, num_hops);
> if (!path)
> return NULL;
>
> + path->path_length = num_hops;
> +
> path->name = name;
> path->tb = src->sw->tb;
> - path->path_length = num_hops;
> path->activated = true;
> path->alloc_hopid = alloc_hopid;
>
> - path->hops = kzalloc_objs(*path->hops, num_hops);
> - if (!path->hops) {
> - kfree(path);
> - return NULL;
> - }
> -
> tb_dbg(path->tb, "discovering %s path starting from %llx:%u\n",
> path->name, tb_route(src->sw), src->port);
>
> @@ -245,10 +240,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
> size_t num_hops;
> int i, ret;
>
> - path = kzalloc_obj(*path);
> - if (!path)
> - return NULL;
> -
> first_port = last_port = NULL;
> i = 0;
> tb_for_each_port_on_path(src, dst, in_port) {
> @@ -259,20 +250,17 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
> }
>
> /* Check that src and dst are reachable */
> - if (first_port != src || last_port != dst) {
> - kfree(path);
> + if (first_port != src || last_port != dst)
> return NULL;
> - }
>
> /* Each hop takes two ports */
> num_hops = i / 2;
>
> - path->hops = kzalloc_objs(*path->hops, num_hops);
> - if (!path->hops) {
> - kfree(path);
> + path = kzalloc_flex(*path, hops, num_hops);
> + if (!path)
> return NULL;
> - }
>
> + path->path_length = num_hops;
> path->alloc_hopid = true;
>
> in_hopid = src_hopid;
> @@ -339,7 +327,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
> }
>
> path->tb = tb;
> - path->path_length = num_hops;
> path->name = name;
>
> return path;
> @@ -372,7 +359,6 @@ void tb_path_free(struct tb_path *path)
> }
> }
>
> - kfree(path->hops);
> kfree(path);
> }
>
> diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
> index e96474f17067..217c3114bec8 100644
> --- a/drivers/thunderbolt/tb.h
> +++ b/drivers/thunderbolt/tb.h
> @@ -419,9 +419,9 @@ enum tb_path_port {
> * @activated: Is the path active
> * @clear_fc: Clear all flow control from the path config space entries
> * when deactivating this path
> - * @hops: Path hops
> * @path_length: How many hops the path uses
> * @alloc_hopid: Does this path consume port HopID
> + * @hops: Path hops
> *
> * A path consists of a number of hops (see &struct tb_path_hop). To
> * establish a PCIe tunnel two paths have to be created between the two
> @@ -440,9 +440,10 @@ struct tb_path {
> bool drop_packages;
> bool activated;
> bool clear_fc;
> - struct tb_path_hop *hops;
> int path_length;
> bool alloc_hopid;
> +
> + struct tb_path_hop hops[] __counted_by(path_length);
> };
>
> /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
> --
> 2.53.0
>
--
Kees Cook
next prev parent reply other threads:[~2026-03-20 18:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-18 18:52 [PATCHv3] thunderbolt: use kzalloc_flex Rosen Penev
2026-03-20 18:48 ` Kees Cook [this message]
2026-03-23 5:53 ` Mika Westerberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202603201148.DAE09FC@keescook \
--to=kees@kernel.org \
--cc=YehezkelShB@gmail.com \
--cc=andreas.noever@gmail.com \
--cc=gustavoars@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=rosenp@gmail.com \
--cc=westeri@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox