Skip to content

Commit 61f3d27

Browse files
q2vengregkh
authored andcommitted
af_unix: Assign a unique index to SCC.
commit bfdb01283ee8f2f3089656c3ff8f62bb072dabb2 upstream. The definition of the lowlink in Tarjan's algorithm is the smallest index of a vertex that is reachable with at most one back-edge in SCC. This is not useful for a cross-edge. If we start traversing from A in the following graph, the final lowlink of D is 3. The cross-edge here is one between D and C. A -> B -> D D = (4, 3) (index, lowlink) ^ | | C = (3, 1) | V | B = (2, 1) `--- C <--' A = (1, 1) This is because the lowlink of D is updated with the index of C. In the following patch, we detect a dead SCC by checking two conditions for each vertex. 1) vertex has no edge directed to another SCC (no bridge) 2) vertex's out_degree is the same as the refcount of its file If 1) is false, there is a receiver of all fds of the SCC and its ancestor SCC. To evaluate 1), we need to assign a unique index to each SCC and assign it to all vertices in the SCC. This patch changes the lowlink update logic for cross-edge so that in the example above, the lowlink of D is updated with the lowlink of C. A -> B -> D D = (4, 1) (index, lowlink) ^ | | C = (3, 1) | V | B = (2, 1) `--- C <--' A = (1, 1) Then, all vertices in the same SCC have the same lowlink, and we can quickly find the bridge connecting to different SCC if exists. However, it is no longer called lowlink, so we rename it to scc_index. (It's sometimes called lowpoint.) Also, we add a global variable to hold the last index used in DFS so that we do not reset the initial index in each DFS. This patch can be squashed to the SCC detection patch but is split deliberately for anyone wondering why lowlink is not used as used in the original Tarjan's algorithm and many reference implementations. 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 adfb68b commit 61f3d27

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

include/net/af_unix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct unix_vertex {
3636
struct list_head scc_entry;
3737
unsigned long out_degree;
3838
unsigned long index;
39-
unsigned long lowlink;
39+
unsigned long scc_index;
4040
};
4141

4242
struct unix_edge {

net/unix/garbage.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,8 @@ static bool unix_scc_cyclic(struct list_head *scc)
312312
static LIST_HEAD(unix_visited_vertices);
313313
static unsigned long unix_vertex_grouped_index = UNIX_VERTEX_INDEX_MARK2;
314314

315-
static void __unix_walk_scc(struct unix_vertex *vertex)
315+
static void __unix_walk_scc(struct unix_vertex *vertex, unsigned long *last_index)
316316
{
317-
unsigned long index = UNIX_VERTEX_INDEX_START;
318317
LIST_HEAD(vertex_stack);
319318
struct unix_edge *edge;
320319
LIST_HEAD(edge_stack);
@@ -326,9 +325,9 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
326325
*/
327326
list_add(&vertex->scc_entry, &vertex_stack);
328327

329-
vertex->index = index;
330-
vertex->lowlink = index;
331-
index++;
328+
vertex->index = *last_index;
329+
vertex->scc_index = *last_index;
330+
(*last_index)++;
332331

333332
/* Explore neighbour vertices (receivers of the current vertex's fd). */
334333
list_for_each_entry(edge, &vertex->edges, vertex_entry) {
@@ -358,30 +357,30 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
358357
next_vertex = vertex;
359358
vertex = edge->predecessor->vertex;
360359

361-
/* If the successor has a smaller lowlink, two vertices
362-
* are in the same SCC, so propagate the smaller lowlink
360+
/* If the successor has a smaller scc_index, two vertices
361+
* are in the same SCC, so propagate the smaller scc_index
363362
* to skip SCC finalisation.
364363
*/
365-
vertex->lowlink = min(vertex->lowlink, next_vertex->lowlink);
364+
vertex->scc_index = min(vertex->scc_index, next_vertex->scc_index);
366365
} else if (next_vertex->index != unix_vertex_grouped_index) {
367366
/* Loop detected by a back/cross edge.
368367
*
369-
* The successor is on vertex_stack, so two vertices are
370-
* in the same SCC. If the successor has a smaller index,
368+
* The successor is on vertex_stack, so two vertices are in
369+
* the same SCC. If the successor has a smaller *scc_index*,
371370
* propagate it to skip SCC finalisation.
372371
*/
373-
vertex->lowlink = min(vertex->lowlink, next_vertex->index);
372+
vertex->scc_index = min(vertex->scc_index, next_vertex->scc_index);
374373
} else {
375374
/* The successor was already grouped as another SCC */
376375
}
377376
}
378377

379-
if (vertex->index == vertex->lowlink) {
378+
if (vertex->index == vertex->scc_index) {
380379
struct list_head scc;
381380

382381
/* SCC finalised.
383382
*
384-
* If the lowlink was not updated, all the vertices above on
383+
* If the scc_index was not updated, all the vertices above on
385384
* vertex_stack are in the same SCC. Group them using scc_entry.
386385
*/
387386
__list_cut_position(&scc, &vertex_stack, &vertex->scc_entry);
@@ -407,6 +406,8 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
407406

408407
static void unix_walk_scc(void)
409408
{
409+
unsigned long last_index = UNIX_VERTEX_INDEX_START;
410+
410411
unix_graph_maybe_cyclic = false;
411412

412413
/* Visit every vertex exactly once.
@@ -416,7 +417,7 @@ static void unix_walk_scc(void)
416417
struct unix_vertex *vertex;
417418

418419
vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
419-
__unix_walk_scc(vertex);
420+
__unix_walk_scc(vertex, &last_index);
420421
}
421422

422423
list_replace_init(&unix_visited_vertices, &unix_unvisited_vertices);

0 commit comments

Comments
 (0)