Skip to content

Commit 3484f06

Browse files
q2venkuba-moo
authored andcommitted
af_unix: Detect Strongly Connected Components.
In the new GC, we use a simple graph algorithm, Tarjan's Strongly Connected Components (SCC) algorithm, to find cyclic references. The algorithm visits every vertex exactly once using depth-first search (DFS). DFS starts by pushing an input vertex to a stack and assigning it a unique number. Two fields, index and lowlink, are initialised with the number, but lowlink could be updated later during DFS. If a vertex has an edge to an unvisited inflight vertex, we visit it and do the same processing. So, we will have vertices in the stack in the order they appear and number them consecutively in the same order. If a vertex has a back-edge to a visited vertex in the stack, we update the predecessor's lowlink with the successor's index. After iterating edges from the vertex, we check if its index equals its lowlink. If the lowlink is different from the index, it shows there was a back-edge. Then, we go backtracking and propagate the lowlink to its predecessor and resume the previous edge iteration from the next edge. If the lowlink is the same as the index, we pop vertices before and including the vertex from the stack. Then, the set of vertices is SCC, possibly forming a cycle. At the same time, we move the vertices to unix_visited_vertices. When we finish the algorithm, all vertices in each SCC will be linked via unix_vertex.scc_entry. Let's take an example. We have a graph including five inflight vertices (F is not inflight): A -> B -> C -> D -> E (-> F) ^ | `---------' Suppose that we start DFS from C. We will visit C, D, and B first and initialise their index and lowlink. Then, the stack looks like this: > B = (3, 3) (index, lowlink) D = (2, 2) C = (1, 1) When checking B's edge to C, we update B's lowlink with C's index and propagate it to D. B = (3, 1) (index, lowlink) > D = (2, 1) C = (1, 1) Next, we visit E, which has no edge to an inflight vertex. > E = (4, 4) (index, lowlink) B = (3, 1) D = (2, 1) C = (1, 1) When we leave from E, its index and lowlink are the same, so we pop E from the stack as single-vertex SCC. Next, we leave from B and D but do nothing because their lowlink are different from their index. B = (3, 1) (index, lowlink) D = (2, 1) > C = (1, 1) Then, we leave from C, whose index and lowlink are the same, so we pop B, D and C as SCC. Last, we do DFS for the rest of vertices, A, which is also a single-vertex SCC. Finally, each unix_vertex.scc_entry is linked as follows: A -. B -> C -> D E -. ^ | ^ | ^ | `--' `---------' `--' We use SCC later to decide whether we can garbage-collect the sockets. Note that we still cannot detect SCC properly if an edge points to an embryo socket. The following two patches will sort it out. 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]>
1 parent 6ba76fd commit 3484f06

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

include/net/af_unix.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ void wait_for_unix_gc(struct scm_fp_list *fpl);
3232
struct unix_vertex {
3333
struct list_head edges;
3434
struct list_head entry;
35+
struct list_head scc_entry;
3536
unsigned long out_degree;
3637
unsigned long index;
38+
unsigned long lowlink;
39+
bool on_stack;
3740
};
3841

3942
struct unix_edge {

net/unix/garbage.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,19 @@ static LIST_HEAD(unix_visited_vertices);
251251
static void __unix_walk_scc(struct unix_vertex *vertex)
252252
{
253253
unsigned long index = UNIX_VERTEX_INDEX_START;
254+
LIST_HEAD(vertex_stack);
254255
struct unix_edge *edge;
255256
LIST_HEAD(edge_stack);
256257

257258
next_vertex:
259+
/* Push vertex to vertex_stack.
260+
* The vertex will be popped when finalising SCC later.
261+
*/
262+
vertex->on_stack = true;
263+
list_add(&vertex->scc_entry, &vertex_stack);
264+
258265
vertex->index = index;
266+
vertex->lowlink = index;
259267
index++;
260268

261269
/* Explore neighbour vertices (receivers of the current vertex's fd). */
@@ -283,12 +291,46 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
283291
edge = list_first_entry(&edge_stack, typeof(*edge), stack_entry);
284292
list_del_init(&edge->stack_entry);
285293

294+
next_vertex = vertex;
286295
vertex = edge->predecessor->vertex;
296+
297+
/* If the successor has a smaller lowlink, two vertices
298+
* are in the same SCC, so propagate the smaller lowlink
299+
* to skip SCC finalisation.
300+
*/
301+
vertex->lowlink = min(vertex->lowlink, next_vertex->lowlink);
302+
} else if (next_vertex->on_stack) {
303+
/* Loop detected by a back/cross edge.
304+
*
305+
* The successor is on vertex_stack, so two vertices are
306+
* in the same SCC. If the successor has a smaller index,
307+
* propagate it to skip SCC finalisation.
308+
*/
309+
vertex->lowlink = min(vertex->lowlink, next_vertex->index);
310+
} else {
311+
/* The successor was already grouped as another SCC */
287312
}
288313
}
289314

290-
/* Don't restart DFS from this vertex in unix_walk_scc(). */
291-
list_move_tail(&vertex->entry, &unix_visited_vertices);
315+
if (vertex->index == vertex->lowlink) {
316+
struct list_head scc;
317+
318+
/* SCC finalised.
319+
*
320+
* If the lowlink was not updated, all the vertices above on
321+
* vertex_stack are in the same SCC. Group them using scc_entry.
322+
*/
323+
__list_cut_position(&scc, &vertex_stack, &vertex->scc_entry);
324+
325+
list_for_each_entry_reverse(vertex, &scc, scc_entry) {
326+
/* Don't restart DFS from this vertex in unix_walk_scc(). */
327+
list_move_tail(&vertex->entry, &unix_visited_vertices);
328+
329+
vertex->on_stack = false;
330+
}
331+
332+
list_del(&scc);
333+
}
292334

293335
/* Need backtracking ? */
294336
if (!list_empty(&edge_stack))

0 commit comments

Comments
 (0)