Skip to content

Commit c3e7cc8

Browse files
nehebwesteri
authored andcommitted
thunderbolt: Use kzalloc_flex() for struct tb_path allocation
Simplifies allocation of struct tb_path by using a flexible array member. Also added __counted_by for extra runtime analysis. Signed-off-by: Rosen Penev <rosenp@gmail.com> Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
1 parent 500e54d commit c3e7cc8

2 files changed

Lines changed: 10 additions & 23 deletions

File tree

drivers/thunderbolt/path.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,17 @@ struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
150150
num_hops++;
151151
}
152152

153-
path = kzalloc_obj(*path);
153+
path = kzalloc_flex(*path, hops, num_hops);
154154
if (!path)
155155
return NULL;
156156

157+
path->path_length = num_hops;
158+
157159
path->name = name;
158160
path->tb = src->sw->tb;
159-
path->path_length = num_hops;
160161
path->activated = true;
161162
path->alloc_hopid = alloc_hopid;
162163

163-
path->hops = kzalloc_objs(*path->hops, num_hops);
164-
if (!path->hops) {
165-
kfree(path);
166-
return NULL;
167-
}
168-
169164
tb_dbg(path->tb, "discovering %s path starting from %llx:%u\n",
170165
path->name, tb_route(src->sw), src->port);
171166

@@ -245,10 +240,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
245240
size_t num_hops;
246241
int i, ret;
247242

248-
path = kzalloc_obj(*path);
249-
if (!path)
250-
return NULL;
251-
252243
first_port = last_port = NULL;
253244
i = 0;
254245
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,
259250
}
260251

261252
/* Check that src and dst are reachable */
262-
if (first_port != src || last_port != dst) {
263-
kfree(path);
253+
if (first_port != src || last_port != dst)
264254
return NULL;
265-
}
266255

267256
/* Each hop takes two ports */
268257
num_hops = i / 2;
269258

270-
path->hops = kzalloc_objs(*path->hops, num_hops);
271-
if (!path->hops) {
272-
kfree(path);
259+
path = kzalloc_flex(*path, hops, num_hops);
260+
if (!path)
273261
return NULL;
274-
}
275262

263+
path->path_length = num_hops;
276264
path->alloc_hopid = true;
277265

278266
in_hopid = src_hopid;
@@ -339,7 +327,6 @@ struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
339327
}
340328

341329
path->tb = tb;
342-
path->path_length = num_hops;
343330
path->name = name;
344331

345332
return path;
@@ -372,7 +359,6 @@ void tb_path_free(struct tb_path *path)
372359
}
373360
}
374361

375-
kfree(path->hops);
376362
kfree(path);
377363
}
378364

drivers/thunderbolt/tb.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ enum tb_path_port {
419419
* @activated: Is the path active
420420
* @clear_fc: Clear all flow control from the path config space entries
421421
* when deactivating this path
422-
* @hops: Path hops
423422
* @path_length: How many hops the path uses
424423
* @alloc_hopid: Does this path consume port HopID
424+
* @hops: Path hops
425425
*
426426
* A path consists of a number of hops (see &struct tb_path_hop). To
427427
* establish a PCIe tunnel two paths have to be created between the two
@@ -440,9 +440,10 @@ struct tb_path {
440440
bool drop_packages;
441441
bool activated;
442442
bool clear_fc;
443-
struct tb_path_hop *hops;
444443
int path_length;
445444
bool alloc_hopid;
445+
446+
struct tb_path_hop hops[] __counted_by(path_length);
446447
};
447448

448449
/* HopIDs 0-7 are reserved by the Thunderbolt protocol */

0 commit comments

Comments
 (0)