From nobody Wed Dec 17 03:27:46 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97F471D63DF; Wed, 21 May 2025 14:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747839111; cv=none; b=tfKLW5c8xSDGrbJtLkfOSUEu/+F4R3RirIf4lAH5ZX3xAEQvceoezlHEWdvr8/SSxUoBi5Bqkm0TS5qpo5upbb9v8AsjmyDo1z30w1lQf8l71/K2cdzxf05KwDdurTbMDrg14HjjDLUUGQNw6hzO+TXz1twzKw6TwTEkZvWDtqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747839111; c=relaxed/simple; bh=H95I4bzN8IvuAVxZK25u/MkkXfxt/gt6nKI4Kx6IHGo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V3yRr1RfeiY7zswB1lN2p5knmd2dCZY10n8v3DXYiNaLoGae46wh+40VaUuy4TmnbZG+7A1wGRlu+p/rgptyybmCcpugAMLcybIdTSP64qML++nf4tdxmaqqF741NWptSH44Xwz3ini0afDL3Qu+FhaT5k7l0W55gLUmmH83Fd4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PIMXwtux; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PIMXwtux" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE1DBC4CEE4; Wed, 21 May 2025 14:51:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747839111; bh=H95I4bzN8IvuAVxZK25u/MkkXfxt/gt6nKI4Kx6IHGo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PIMXwtuxgQ5RE0ya9CA+SWwwdjTvLfBvCtPFlwhuWcga+4dn9hlFKNQjIB2qkRQhD MMXFF+GMH/qdqoNIVU7apl/e8CM6xfUQKy8h9CApcL3vMBJoJOEAZam7HEdwsk4Sul UntrAunmnTcppShU+tTtffBlCzgU7PnXfLMoson3oh9nfGGPSAiTL0th4mh8JD30xi +r2/S27WO1qXkG5kJnCVF3Z+7+G8ttWFpVd9D3RQtUWPe/fVd7US7wSg11DT/yTLg6 zIZEOhlDlsCEPpkXE7Hy/HN7slOIN+CoV9dxtrKXegvj6+sgiKFJe0cxqqZKY846VL 5UWlyTLt3byLQ== From: Lee Jones To: lee@kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Kuniyuki Iwashima , Jens Axboe , Sasha Levin , Michal Luczaj , Rao Shoaib , Simon Horman , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: stable@vger.kernel.org Subject: [PATCH v6.6 19/26] af_unix: Detect dead SCC. Date: Wed, 21 May 2025 14:45:27 +0000 Message-ID: <20250521144803.2050504-20-lee@kernel.org> X-Mailer: git-send-email 2.49.0.1112.g889b7c5bd8-goog In-Reply-To: <20250521144803.2050504-1-lee@kernel.org> References: <20250521144803.2050504-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kuniyuki Iwashima [ Upstream commit a15702d8b3aad8ce5268c565bd29f0e02fd2db83 ] 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 Acked-by: Paolo Abeni Link: https://lore.kernel.org/r/20240325202425.60930-14-kuniyu@amazon.com Signed-off-by: Jakub Kicinski (cherry picked from commit a15702d8b3aad8ce5268c565bd29f0e02fd2db83) Signed-off-by: Lee Jones --- net/unix/garbage.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/net/unix/garbage.c b/net/unix/garbage.c index 2e66b57f3f0f6..1f53c25fc71b6 100644 --- a/net/unix/garbage.c +++ b/net/unix/garbage.c @@ -289,6 +289,39 @@ void unix_destroy_fpl(struct scm_fp_list *fpl) unix_free_vertices(fpl); } =20 +static bool unix_vertex_dead(struct unix_vertex *vertex) +{ + struct unix_edge *edge; + struct unix_sock *u; + long total_ref; + + list_for_each_entry(edge, &vertex->edges, vertex_entry) { + struct unix_vertex *next_vertex =3D unix_edge_successor(edge); + + /* The vertex's fd can be received by a non-inflight socket. */ + if (!next_vertex) + return false; + + /* The vertex's fd can be received by an inflight socket in + * another SCC. + */ + if (next_vertex->scc_index !=3D vertex->scc_index) + return false; + } + + /* No receiver exists out of the same SCC. */ + + edge =3D list_first_entry(&vertex->edges, typeof(*edge), vertex_entry); + u =3D edge->predecessor; + total_ref =3D file_count(u->sk.sk_socket->file); + + /* If not close()d, total_ref > out_degree. */ + if (total_ref !=3D vertex->out_degree) + return false; + + return true; +} + static bool unix_scc_cyclic(struct list_head *scc) { struct unix_vertex *vertex; @@ -377,6 +410,7 @@ static void __unix_walk_scc(struct unix_vertex *vertex,= unsigned long *last_inde =20 if (vertex->index =3D=3D vertex->scc_index) { struct list_head scc; + bool scc_dead =3D true; =20 /* SCC finalised. * @@ -391,6 +425,9 @@ static void __unix_walk_scc(struct unix_vertex *vertex,= unsigned long *last_inde =20 /* Mark vertex as off-stack. */ vertex->index =3D unix_vertex_grouped_index; + + if (scc_dead) + scc_dead =3D unix_vertex_dead(vertex); } =20 if (!unix_graph_maybe_cyclic) @@ -431,13 +468,18 @@ static void unix_walk_scc_fast(void) while (!list_empty(&unix_unvisited_vertices)) { struct unix_vertex *vertex; struct list_head scc; + bool scc_dead =3D true; =20 vertex =3D list_first_entry(&unix_unvisited_vertices, typeof(*vertex), e= ntry); list_add(&scc, &vertex->scc_entry); =20 - list_for_each_entry_reverse(vertex, &scc, scc_entry) + list_for_each_entry_reverse(vertex, &scc, scc_entry) { list_move_tail(&vertex->entry, &unix_visited_vertices); =20 + if (scc_dead) + scc_dead =3D unix_vertex_dead(vertex); + } + list_del(&scc); } =20 --=20 2.49.0.1112.g889b7c5bd8-goog