Skip to content

Commit 498c058

Browse files
nehebwesteri
authored andcommitted
thunderbolt: tunnel: Simplify allocation
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>
1 parent c3e7cc8 commit 498c058

2 files changed

Lines changed: 5 additions & 10 deletions

File tree

drivers/thunderbolt/tunnel.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,14 @@ static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
180180
{
181181
struct tb_tunnel *tunnel;
182182

183-
tunnel = kzalloc_obj(*tunnel);
183+
tunnel = kzalloc_flex(*tunnel, paths, npaths);
184184
if (!tunnel)
185185
return NULL;
186186

187-
tunnel->paths = kzalloc_objs(tunnel->paths[0], npaths);
188-
if (!tunnel->paths) {
189-
kfree(tunnel);
190-
return NULL;
191-
}
187+
tunnel->npaths = npaths;
192188

193189
INIT_LIST_HEAD(&tunnel->list);
194190
tunnel->tb = tb;
195-
tunnel->npaths = npaths;
196191
tunnel->type = type;
197192
kref_init(&tunnel->kref);
198193

@@ -219,7 +214,6 @@ static void tb_tunnel_destroy(struct kref *kref)
219214
tb_path_free(tunnel->paths[i]);
220215
}
221216

222-
kfree(tunnel->paths);
223217
kfree(tunnel);
224218
}
225219

drivers/thunderbolt/tunnel.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ enum tb_tunnel_state {
3737
* @src_port: Source port of the tunnel
3838
* @dst_port: Destination port of the tunnel. For discovered incomplete
3939
* tunnels may be %NULL or null adapter port instead.
40-
* @paths: All paths required by the tunnel
4140
* @npaths: Number of paths in @paths
4241
* @pre_activate: Optional tunnel specific initialization called before
4342
* activation. Can touch hardware.
@@ -69,13 +68,13 @@ enum tb_tunnel_state {
6968
* @dprx_work: Worker that is scheduled to poll completion of DPRX capabilities read
7069
* @callback: Optional callback called when DP tunnel is fully activated
7170
* @callback_data: Optional data for @callback
71+
* @paths: All paths required by the tunnel
7272
*/
7373
struct tb_tunnel {
7474
struct kref kref;
7575
struct tb *tb;
7676
struct tb_port *src_port;
7777
struct tb_port *dst_port;
78-
struct tb_path **paths;
7978
size_t npaths;
8079
int (*pre_activate)(struct tb_tunnel *tunnel);
8180
int (*activate)(struct tb_tunnel *tunnel, bool activate);
@@ -107,6 +106,8 @@ struct tb_tunnel {
107106
struct delayed_work dprx_work;
108107
void (*callback)(struct tb_tunnel *tunnel, void *data);
109108
void *callback_data;
109+
110+
struct tb_path *paths[] __counted_by(npaths);
110111
};
111112

112113
struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,

0 commit comments

Comments
 (0)