]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
thunderbolt: tunnel: Simplify allocation
authorRosen Penev <rosenp@gmail.com>
Wed, 1 Apr 2026 21:47:26 +0000 (14:47 -0700)
committerMika Westerberg <mika.westerberg@linux.intel.com>
Tue, 7 Apr 2026 07:00:26 +0000 (09:00 +0200)
Use a flexible array member and kzalloc_flex to combine allocations.

Add __counted_by for extra runtime analysis. Move counting variable
assignment after allocation. kzalloc_flex with GCC >= 15 does this
automatically.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/thunderbolt/tunnel.c
drivers/thunderbolt/tunnel.h

index 89676acf129056e24815fe175686ad1dc538a288..f38f7753b6e4fdffdb0acabcd706b6a75c39b9dc 100644 (file)
@@ -180,19 +180,14 @@ static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
 {
        struct tb_tunnel *tunnel;
 
-       tunnel = kzalloc_obj(*tunnel);
+       tunnel = kzalloc_flex(*tunnel, paths, npaths);
        if (!tunnel)
                return NULL;
 
-       tunnel->paths = kzalloc_objs(tunnel->paths[0], npaths);
-       if (!tunnel->paths) {
-               kfree(tunnel);
-               return NULL;
-       }
+       tunnel->npaths = npaths;
 
        INIT_LIST_HEAD(&tunnel->list);
        tunnel->tb = tb;
-       tunnel->npaths = npaths;
        tunnel->type = type;
        kref_init(&tunnel->kref);
 
@@ -219,7 +214,6 @@ static void tb_tunnel_destroy(struct kref *kref)
                        tb_path_free(tunnel->paths[i]);
        }
 
-       kfree(tunnel->paths);
        kfree(tunnel);
 }
 
index 2c44fc8a10bc45c291760835e698965a3c50f4a4..4878763a82b31826d80c5ed6a6f12a4320354930 100644 (file)
@@ -37,7 +37,6 @@ enum tb_tunnel_state {
  * @src_port: Source port of the tunnel
  * @dst_port: Destination port of the tunnel. For discovered incomplete
  *           tunnels may be %NULL or null adapter port instead.
- * @paths: All paths required by the tunnel
  * @npaths: Number of paths in @paths
  * @pre_activate: Optional tunnel specific initialization called before
  *               activation. Can touch hardware.
@@ -69,13 +68,13 @@ enum tb_tunnel_state {
  * @dprx_work: Worker that is scheduled to poll completion of DPRX capabilities read
  * @callback: Optional callback called when DP tunnel is fully activated
  * @callback_data: Optional data for @callback
+ * @paths: All paths required by the tunnel
  */
 struct tb_tunnel {
        struct kref kref;
        struct tb *tb;
        struct tb_port *src_port;
        struct tb_port *dst_port;
-       struct tb_path **paths;
        size_t npaths;
        int (*pre_activate)(struct tb_tunnel *tunnel);
        int (*activate)(struct tb_tunnel *tunnel, bool activate);
@@ -107,6 +106,8 @@ struct tb_tunnel {
        struct delayed_work dprx_work;
        void (*callback)(struct tb_tunnel *tunnel, void *data);
        void *callback_data;
+
+       struct tb_path *paths[] __counted_by(npaths);
 };
 
 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,