Skip to content

Commit 77e5593

Browse files
q2venkuba-moo
authored andcommitted
af_unix: Skip GC if no cycle exists.
We do not need to run GC if there is no possible cyclic reference. We use unix_graph_maybe_cyclic to decide if we should run GC. If a fd of an AF_UNIX socket is passed to an already inflight AF_UNIX socket, they could form a cyclic reference. Then, we set true to unix_graph_maybe_cyclic and later run Tarjan's algorithm to group them into SCC. Once we run Tarjan's algorithm, we are 100% sure whether cyclic references exist or not. If there is no cycle, we set false to unix_graph_maybe_cyclic and can skip the entire garbage collection next time. When finalising SCC, we set true to unix_graph_maybe_cyclic if SCC consists of multiple vertices. Even if SCC is a single vertex, a cycle might exist as self-fd passing. Given the corner case is rare, we detect it by checking all edges of the vertex and set true to unix_graph_maybe_cyclic. With this change, __unix_gc() is just a spin_lock() dance in the normal usage. 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 ba31b4a commit 77e5593

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

net/unix/garbage.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ static struct unix_vertex *unix_edge_successor(struct unix_edge *edge)
112112
return edge->successor->vertex;
113113
}
114114

115+
static bool unix_graph_maybe_cyclic;
116+
117+
static void unix_update_graph(struct unix_vertex *vertex)
118+
{
119+
/* If the receiver socket is not inflight, no cyclic
120+
* reference could be formed.
121+
*/
122+
if (!vertex)
123+
return;
124+
125+
unix_graph_maybe_cyclic = true;
126+
}
127+
115128
static LIST_HEAD(unix_unvisited_vertices);
116129

117130
enum unix_vertex_index {
@@ -138,12 +151,16 @@ static void unix_add_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
138151

139152
vertex->out_degree++;
140153
list_add_tail(&edge->vertex_entry, &vertex->edges);
154+
155+
unix_update_graph(unix_edge_successor(edge));
141156
}
142157

143158
static void unix_del_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
144159
{
145160
struct unix_vertex *vertex = edge->predecessor->vertex;
146161

162+
unix_update_graph(unix_edge_successor(edge));
163+
147164
list_del(&edge->vertex_entry);
148165
vertex->out_degree--;
149166

@@ -227,6 +244,7 @@ void unix_del_edges(struct scm_fp_list *fpl)
227244
void unix_update_edges(struct unix_sock *receiver)
228245
{
229246
spin_lock(&unix_gc_lock);
247+
unix_update_graph(unix_sk(receiver->listener)->vertex);
230248
receiver->listener = NULL;
231249
spin_unlock(&unix_gc_lock);
232250
}
@@ -268,6 +286,26 @@ void unix_destroy_fpl(struct scm_fp_list *fpl)
268286
unix_free_vertices(fpl);
269287
}
270288

289+
static bool unix_scc_cyclic(struct list_head *scc)
290+
{
291+
struct unix_vertex *vertex;
292+
struct unix_edge *edge;
293+
294+
/* SCC containing multiple vertices ? */
295+
if (!list_is_singular(scc))
296+
return true;
297+
298+
vertex = list_first_entry(scc, typeof(*vertex), scc_entry);
299+
300+
/* Self-reference or a embryo-listener circle ? */
301+
list_for_each_entry(edge, &vertex->edges, vertex_entry) {
302+
if (unix_edge_successor(edge) == vertex)
303+
return true;
304+
}
305+
306+
return false;
307+
}
308+
271309
static LIST_HEAD(unix_visited_vertices);
272310
static unsigned long unix_vertex_grouped_index = UNIX_VERTEX_INDEX_MARK2;
273311

@@ -353,6 +391,9 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
353391
vertex->index = unix_vertex_grouped_index;
354392
}
355393

394+
if (!unix_graph_maybe_cyclic)
395+
unix_graph_maybe_cyclic = unix_scc_cyclic(&scc);
396+
356397
list_del(&scc);
357398
}
358399

@@ -363,6 +404,8 @@ static void __unix_walk_scc(struct unix_vertex *vertex)
363404

364405
static void unix_walk_scc(void)
365406
{
407+
unix_graph_maybe_cyclic = false;
408+
366409
/* Visit every vertex exactly once.
367410
* __unix_walk_scc() moves visited vertices to unix_visited_vertices.
368411
*/
@@ -524,6 +567,9 @@ static void __unix_gc(struct work_struct *work)
524567

525568
spin_lock(&unix_gc_lock);
526569

570+
if (!unix_graph_maybe_cyclic)
571+
goto skip_gc;
572+
527573
unix_walk_scc();
528574

529575
/* First, select candidates for garbage collection. Only
@@ -617,7 +663,7 @@ static void __unix_gc(struct work_struct *work)
617663

618664
/* All candidates should have been detached by now. */
619665
WARN_ON_ONCE(!list_empty(&gc_candidates));
620-
666+
skip_gc:
621667
/* Paired with READ_ONCE() in wait_for_unix_gc(). */
622668
WRITE_ONCE(gc_in_progress, false);
623669

0 commit comments

Comments
 (0)