Skip to content

Commit 61a7536

Browse files
q2vengregkh
authored andcommitted
af_unix: Remove lock dance in unix_peek_fds().
commit 118f457da9ed58a79e24b73c2ef0aa1987241f0e upstream. In the previous GC implementation, the shape of the inflight socket graph was not expected to change while GC was in progress. MSG_PEEK was tricky because it could install inflight fd silently and transform the graph. Let's say we peeked a fd, which was a listening socket, and accept()ed some embryo sockets from it. The garbage collection algorithm would have been confused because the set of sockets visited in scan_inflight() would change within the same GC invocation. That's why we placed spin_lock(&unix_gc_lock) and spin_unlock() in unix_peek_fds() with a fat comment. In the new GC implementation, we no longer garbage-collect the socket if it exists in another queue, that is, if it has a bridge to another SCC. Also, accept() will require the lock if it has edges. Thus, we need not do the complicated lock dance. Signed-off-by: Kuniyuki Iwashima <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Lee Jones <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5dfd283 commit 61a7536

File tree

3 files changed

+1
-44
lines changed

3 files changed

+1
-44
lines changed

include/net/af_unix.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ static inline struct unix_sock *unix_get_socket(struct file *filp)
1717
}
1818
#endif
1919

20-
extern spinlock_t unix_gc_lock;
2120
extern unsigned int unix_tot_inflight;
2221
void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver);
2322
void unix_del_edges(struct scm_fp_list *fpl);

net/unix/af_unix.c

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,48 +1770,6 @@ static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
17701770
static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
17711771
{
17721772
scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1773-
1774-
/*
1775-
* Garbage collection of unix sockets starts by selecting a set of
1776-
* candidate sockets which have reference only from being in flight
1777-
* (total_refs == inflight_refs). This condition is checked once during
1778-
* the candidate collection phase, and candidates are marked as such, so
1779-
* that non-candidates can later be ignored. While inflight_refs is
1780-
* protected by unix_gc_lock, total_refs (file count) is not, hence this
1781-
* is an instantaneous decision.
1782-
*
1783-
* Once a candidate, however, the socket must not be reinstalled into a
1784-
* file descriptor while the garbage collection is in progress.
1785-
*
1786-
* If the above conditions are met, then the directed graph of
1787-
* candidates (*) does not change while unix_gc_lock is held.
1788-
*
1789-
* Any operations that changes the file count through file descriptors
1790-
* (dup, close, sendmsg) does not change the graph since candidates are
1791-
* not installed in fds.
1792-
*
1793-
* Dequeing a candidate via recvmsg would install it into an fd, but
1794-
* that takes unix_gc_lock to decrement the inflight count, so it's
1795-
* serialized with garbage collection.
1796-
*
1797-
* MSG_PEEK is special in that it does not change the inflight count,
1798-
* yet does install the socket into an fd. The following lock/unlock
1799-
* pair is to ensure serialization with garbage collection. It must be
1800-
* done between incrementing the file count and installing the file into
1801-
* an fd.
1802-
*
1803-
* If garbage collection starts after the barrier provided by the
1804-
* lock/unlock, then it will see the elevated refcount and not mark this
1805-
* as a candidate. If a garbage collection is already in progress
1806-
* before the file count was incremented, then the lock/unlock pair will
1807-
* ensure that garbage collection is finished before progressing to
1808-
* installing the fd.
1809-
*
1810-
* (*) A -> B where B is on the queue of A or B is on the queue of C
1811-
* which is on the queue of listening socket A.
1812-
*/
1813-
spin_lock(&unix_gc_lock);
1814-
spin_unlock(&unix_gc_lock);
18151773
}
18161774

18171775
static void unix_destruct_scm(struct sk_buff *skb)

net/unix/garbage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static void unix_free_vertices(struct scm_fp_list *fpl)
183183
}
184184
}
185185

186-
DEFINE_SPINLOCK(unix_gc_lock);
186+
static DEFINE_SPINLOCK(unix_gc_lock);
187187
unsigned int unix_tot_inflight;
188188

189189
void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)

0 commit comments

Comments
 (0)