Skip to content

Commit 6ba76fd

Browse files
q2venkuba-moo
authored andcommitted
af_unix: Iterate all vertices by DFS.
The new GC will use a depth first search graph algorithm to find cyclic references. The algorithm visits every vertex exactly once. Here, we implement the DFS part without recursion so that no one can abuse it. unix_walk_scc() marks every vertex unvisited by initialising index as UNIX_VERTEX_INDEX_UNVISITED and iterates inflight vertices in unix_unvisited_vertices and call __unix_walk_scc() to start DFS from an arbitrary vertex. __unix_walk_scc() iterates all edges starting from the vertex and explores the neighbour vertices with DFS using edge_stack. After visiting all neighbours, __unix_walk_scc() moves the visited vertex to unix_visited_vertices so that unix_walk_scc() will not restart DFS from the visited vertex. 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 22c3c0c commit 6ba76fd

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

include/net/af_unix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ struct unix_vertex {
3333
struct list_head edges;
3434
struct list_head entry;
3535
unsigned long out_degree;
36+
unsigned long index;
3637
};
3738

3839
struct unix_edge {
3940
struct unix_sock *predecessor;
4041
struct unix_sock *successor;
4142
struct list_head vertex_entry;
43+
struct list_head stack_entry;
4244
};
4345

4446
struct sock *unix_peer_get(struct sock *sk);

net/unix/garbage.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ struct unix_sock *unix_get_socket(struct file *filp)
103103

104104
static LIST_HEAD(unix_unvisited_vertices);
105105

106+
enum unix_vertex_index {
107+
UNIX_VERTEX_INDEX_UNVISITED,
108+
UNIX_VERTEX_INDEX_START,
109+
};
110+
106111
static void unix_add_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
107112
{
108113
struct unix_vertex *vertex = edge->predecessor->vertex;
@@ -241,6 +246,73 @@ void unix_destroy_fpl(struct scm_fp_list *fpl)
241246
unix_free_vertices(fpl);
242247
}
243248

249+
static LIST_HEAD(unix_visited_vertices);
250+
251+
static void __unix_walk_scc(struct unix_vertex *vertex)
252+
{
253+
unsigned long index = UNIX_VERTEX_INDEX_START;
254+
struct unix_edge *edge;
255+
LIST_HEAD(edge_stack);
256+
257+
next_vertex:
258+
vertex->index = index;
259+
index++;
260+
261+
/* Explore neighbour vertices (receivers of the current vertex's fd). */
262+
list_for_each_entry(edge, &vertex->edges, vertex_entry) {
263+
struct unix_vertex *next_vertex = edge->successor->vertex;
264+
265+
if (!next_vertex)
266+
continue;
267+
268+
if (next_vertex->index == UNIX_VERTEX_INDEX_UNVISITED) {
269+
/* Iterative deepening depth first search
270+
*
271+
* 1. Push a forward edge to edge_stack and set
272+
* the successor to vertex for the next iteration.
273+
*/
274+
list_add(&edge->stack_entry, &edge_stack);
275+
276+
vertex = next_vertex;
277+
goto next_vertex;
278+
279+
/* 2. Pop the edge directed to the current vertex
280+
* and restore the ancestor for backtracking.
281+
*/
282+
prev_vertex:
283+
edge = list_first_entry(&edge_stack, typeof(*edge), stack_entry);
284+
list_del_init(&edge->stack_entry);
285+
286+
vertex = edge->predecessor->vertex;
287+
}
288+
}
289+
290+
/* Don't restart DFS from this vertex in unix_walk_scc(). */
291+
list_move_tail(&vertex->entry, &unix_visited_vertices);
292+
293+
/* Need backtracking ? */
294+
if (!list_empty(&edge_stack))
295+
goto prev_vertex;
296+
}
297+
298+
static void unix_walk_scc(void)
299+
{
300+
struct unix_vertex *vertex;
301+
302+
list_for_each_entry(vertex, &unix_unvisited_vertices, entry)
303+
vertex->index = UNIX_VERTEX_INDEX_UNVISITED;
304+
305+
/* Visit every vertex exactly once.
306+
* __unix_walk_scc() moves visited vertices to unix_visited_vertices.
307+
*/
308+
while (!list_empty(&unix_unvisited_vertices)) {
309+
vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
310+
__unix_walk_scc(vertex);
311+
}
312+
313+
list_replace_init(&unix_visited_vertices, &unix_unvisited_vertices);
314+
}
315+
244316
static LIST_HEAD(gc_candidates);
245317
static LIST_HEAD(gc_inflight_list);
246318

@@ -388,6 +460,8 @@ static void __unix_gc(struct work_struct *work)
388460

389461
spin_lock(&unix_gc_lock);
390462

463+
unix_walk_scc();
464+
391465
/* First, select candidates for garbage collection. Only
392466
* in-flight sockets are considered, and from those only ones
393467
* which don't have any external reference.

0 commit comments

Comments
 (0)