Skip to content

Commit 9734d33

Browse files
q2vengregkh
authored andcommitted
af_unix: Detect dead SCC.
commit a15702d8b3aad8ce5268c565bd29f0e02fd2db83 upstream. When iterating SCC, we call unix_vertex_dead() for each vertex to check if the vertex is close()d and has no bridge to another SCC. If both conditions are true for every vertex in SCC, we can execute garbage collection for all skb in the SCC. The actual garbage collection is done in the following patch, replacing the old implementation. Signed-off-by: Kuniyuki Iwashima <[email protected]> Acked-by: Paolo Abeni <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Lee Jones <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 61f3d27 commit 9734d33

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

net/unix/garbage.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,39 @@ void unix_destroy_fpl(struct scm_fp_list *fpl)
289289
unix_free_vertices(fpl);
290290
}
291291

292+
static bool unix_vertex_dead(struct unix_vertex *vertex)
293+
{
294+
struct unix_edge *edge;
295+
struct unix_sock *u;
296+
long total_ref;
297+
298+
list_for_each_entry(edge, &vertex->edges, vertex_entry) {
299+
struct unix_vertex *next_vertex = unix_edge_successor(edge);
300+
301+
/* The vertex's fd can be received by a non-inflight socket. */
302+
if (!next_vertex)
303+
return false;
304+
305+
/* The vertex's fd can be received by an inflight socket in
306+
* another SCC.
307+
*/
308+
if (next_vertex->scc_index != vertex->scc_index)
309+
return false;
310+
}
311+
312+
/* No receiver exists out of the same SCC. */
313+
314+
edge = list_first_entry(&vertex->edges, typeof(*edge), vertex_entry);
315+
u = edge->predecessor;
316+
total_ref = file_count(u->sk.sk_socket->file);
317+
318+
/* If not close()d, total_ref > out_degree. */
319+
if (total_ref != vertex->out_degree)
320+
return false;
321+
322+
return true;
323+
}
324+
292325
static bool unix_scc_cyclic(struct list_head *scc)
293326
{
294327
struct unix_vertex *vertex;
@@ -377,6 +410,7 @@ static void __unix_walk_scc(struct unix_vertex *vertex, unsigned long *last_inde
377410

378411
if (vertex->index == vertex->scc_index) {
379412
struct list_head scc;
413+
bool scc_dead = true;
380414

381415
/* SCC finalised.
382416
*
@@ -391,6 +425,9 @@ static void __unix_walk_scc(struct unix_vertex *vertex, unsigned long *last_inde
391425

392426
/* Mark vertex as off-stack. */
393427
vertex->index = unix_vertex_grouped_index;
428+
429+
if (scc_dead)
430+
scc_dead = unix_vertex_dead(vertex);
394431
}
395432

396433
if (!unix_graph_maybe_cyclic)
@@ -431,13 +468,18 @@ static void unix_walk_scc_fast(void)
431468
while (!list_empty(&unix_unvisited_vertices)) {
432469
struct unix_vertex *vertex;
433470
struct list_head scc;
471+
bool scc_dead = true;
434472

435473
vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
436474
list_add(&scc, &vertex->scc_entry);
437475

438-
list_for_each_entry_reverse(vertex, &scc, scc_entry)
476+
list_for_each_entry_reverse(vertex, &scc, scc_entry) {
439477
list_move_tail(&vertex->entry, &unix_visited_vertices);
440478

479+
if (scc_dead)
480+
scc_dead = unix_vertex_dead(vertex);
481+
}
482+
441483
list_del(&scc);
442484
}
443485

0 commit comments

Comments
 (0)