On 2/23/24 06:36, Wen Gu wrote:
One solution to this issue I can think of is to check whether filp->private_data has been changed when the sock_fasync holds the sock lock, but it inevitably changes the general code.. diff --git a/net/socket.c b/net/socket.c index ed3df2f749bf..a28435195854 100644 --- a/net/socket.c +++ b/net/socket.c @@ -1443,6 +1443,11 @@ static int sock_fasync(int fd, struct file *filp, int on) return -EINVAL; lock_sock(sk); + /* filp->private_data has changed */ + if (on && unlikely(sock != filp->private_data)) { + release_sock(sk); + return -EAGAIN; + } fasync_helper(fd, filp, on, &wq->fasync_list); if (!wq->fasync_list) Let's see if anyone else has a better idea.
IIUC this is not a solution just because it decreases the probability of the race but doesn't eliminate it completely - an underlying socket switch (e.g. changing 'filp->private_data') may happen when 'fasync_helper()' is in progress. Dmitry