diff mbox series

[v1,net-next,6/6] af_unix: Schedule GC only if loop exists during close().

Message ID 20240503223150.6035-7-kuniyu@amazon.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series af_unix: GC cleanup and optimisation | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 950 this patch: 950
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: dhowells@redhat.com
netdev/build_clang success Errors and warnings before: 938 this patch: 938
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 962 this patch: 962
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 89 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-05-05--03-00 (tests: 1003)

Commit Message

Kuniyuki Iwashima May 3, 2024, 10:31 p.m. UTC
If unix_tot_inflight is not 0 when AF_UNIX socket is close()d, GC is
always scheduled.

However, we need not do so if we know no loop exists in the inflight
graph.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/net/af_unix.h |  3 +--
 net/unix/af_unix.c    |  3 +--
 net/unix/garbage.c    | 30 +++++++++++++++---------------
 3 files changed, 17 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/include/net/af_unix.h b/include/net/af_unix.h
index ebd1b3ca8906..1270b2c08b8f 100644
--- a/include/net/af_unix.h
+++ b/include/net/af_unix.h
@@ -17,13 +17,12 @@  static inline struct unix_sock *unix_get_socket(struct file *filp)
 }
 #endif
 
-extern unsigned int unix_tot_inflight;
 void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver);
 void unix_del_edges(struct scm_fp_list *fpl);
 void unix_update_edges(struct unix_sock *receiver);
 int unix_prepare_fpl(struct scm_fp_list *fpl);
 void unix_destroy_fpl(struct scm_fp_list *fpl);
-void unix_gc(void);
+void unix_schedule_gc(void);
 
 struct unix_vertex {
 	struct list_head edges;
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 863058be35f3..b99f7170835e 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -677,8 +677,7 @@  static void unix_release_sock(struct sock *sk, int embrion)
 	 *	  What the above comment does talk about? --ANK(980817)
 	 */
 
-	if (READ_ONCE(unix_tot_inflight))
-		unix_gc();		/* Garbage collect fds */
+	unix_schedule_gc();
 }
 
 static void init_peercred(struct sock *sk)
diff --git a/net/unix/garbage.c b/net/unix/garbage.c
index 48cea3cf4a42..2cecbb97882c 100644
--- a/net/unix/garbage.c
+++ b/net/unix/garbage.c
@@ -189,7 +189,7 @@  static void unix_free_vertices(struct scm_fp_list *fpl)
 }
 
 static DEFINE_SPINLOCK(unix_gc_lock);
-unsigned int unix_tot_inflight;
+static unsigned int unix_tot_inflight;
 
 void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
 {
@@ -577,11 +577,6 @@  static void __unix_gc(struct work_struct *work)
 
 	spin_lock(&unix_gc_lock);
 
-	if (unix_graph_state == UNIX_GRAPH_NOT_CYCLIC) {
-		spin_unlock(&unix_gc_lock);
-		goto skip_gc;
-	}
-
 	__skb_queue_head_init(&hitlist);
 
 	if (unix_graph_state == UNIX_GRAPH_CYCLIC)
@@ -597,18 +592,12 @@  static void __unix_gc(struct work_struct *work)
 	}
 
 	__skb_queue_purge(&hitlist);
-skip_gc:
+
 	WRITE_ONCE(gc_in_progress, false);
 }
 
 static DECLARE_WORK(unix_gc_work, __unix_gc);
 
-void unix_gc(void)
-{
-	WRITE_ONCE(gc_in_progress, true);
-	queue_work(system_unbound_wq, &unix_gc_work);
-}
-
 #define UNIX_INFLIGHT_SANE_CIRCLES (1 << 10)
 #define UNIX_INFLIGHT_SANE_SOCKETS (1 << 14)
 #define UNIX_INFLIGHT_SANE_USER (SCM_MAX_FD * 8)
@@ -621,6 +610,9 @@  static void __unix_schedule_gc(struct scm_fp_list *fpl)
 	if (graph_state == UNIX_GRAPH_NOT_CYCLIC)
 		return;
 
+	if (!fpl)
+		goto schedule;
+
 	/* If the number of inflight sockets or cyclic references
 	 * is insane, schedule garbage collector if not running.
 	 */
@@ -638,9 +630,17 @@  static void __unix_schedule_gc(struct scm_fp_list *fpl)
 	if (READ_ONCE(fpl->user->unix_inflight) > UNIX_INFLIGHT_SANE_USER)
 		wait = true;
 
-	if (!READ_ONCE(gc_in_progress))
-		unix_gc();
+schedule:
+	if (!READ_ONCE(gc_in_progress)) {
+		WRITE_ONCE(gc_in_progress, true);
+		queue_work(system_unbound_wq, &unix_gc_work);
+	}
 
 	if (wait)
 		flush_work(&unix_gc_work);
 }
+
+void unix_schedule_gc(void)
+{
+	__unix_schedule_gc(NULL);
+}