diff --git a/net/socket.c b/net/socket.c
index b7a63d5bc915f83350bf89717021bbb56e4ee34e..2c1e8677ff2d4fdb2f29eaa6e06a7c323d27d981 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -2228,8 +2228,10 @@ int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
 		return err;
 
 	err = sock_error(sock->sk);
-	if (err)
+	if (err) {
+		datagrams = err;
 		goto out_put;
+	}
 
 	entry = mmsg;
 	compat_entry = (struct compat_mmsghdr __user *)mmsg;