[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240503223150.6035-7-kuniyu@amazon.com>
Date: Fri, 3 May 2024 15:31:50 -0700
From: Kuniyuki Iwashima <kuniyu@...zon.com>
To: "David S. Miller" <davem@...emloft.net>, Eric Dumazet
<edumazet@...gle.com>, Jakub Kicinski <kuba@...nel.org>, Paolo Abeni
<pabeni@...hat.com>
CC: Kuniyuki Iwashima <kuniyu@...zon.com>, Kuniyuki Iwashima
<kuni1840@...il.com>, <netdev@...r.kernel.org>
Subject: [PATCH v1 net-next 6/6] af_unix: Schedule GC only if loop exists during close().
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@...zon.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 --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);
+}
--
2.30.2
Powered by blists - more mailing lists