Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

af_unix: Cache state->msg in unix_stream_read_generic().

In unix_stream_read_generic(), state->msg is fetched multiple times.

Let's cache it in a local variable.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20250702223606.1054680-6-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Kuniyuki Iwashima and committed by
Jakub Kicinski
8b77338e f4e1fb04

+17 -16
+17 -16
net/unix/af_unix.c
··· 2840 2840 static int unix_stream_read_generic(struct unix_stream_read_state *state, 2841 2841 bool freezable) 2842 2842 { 2843 - struct scm_cookie scm; 2843 + int noblock = state->flags & MSG_DONTWAIT; 2844 2844 struct socket *sock = state->socket; 2845 + struct msghdr *msg = state->msg; 2845 2846 struct sock *sk = sock->sk; 2846 - struct unix_sock *u = unix_sk(sk); 2847 - int copied = 0; 2847 + size_t size = state->size; 2848 2848 int flags = state->flags; 2849 - int noblock = flags & MSG_DONTWAIT; 2850 2849 bool check_creds = false; 2851 - int target; 2850 + struct scm_cookie scm; 2851 + unsigned int last_len; 2852 + struct unix_sock *u; 2853 + int copied = 0; 2852 2854 int err = 0; 2853 2855 long timeo; 2856 + int target; 2854 2857 int skip; 2855 - size_t size = state->size; 2856 - unsigned int last_len; 2857 2858 2858 2859 if (unlikely(READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)) { 2859 2860 err = -EINVAL; ··· 2873 2872 timeo = sock_rcvtimeo(sk, noblock); 2874 2873 2875 2874 memset(&scm, 0, sizeof(scm)); 2875 + 2876 + u = unix_sk(sk); 2876 2877 2877 2878 /* Lock the socket to prevent queue disordering 2878 2879 * while sleeps in memcpy_tomsg ··· 2967 2964 } 2968 2965 2969 2966 /* Copy address just once */ 2970 - if (state->msg && state->msg->msg_name) { 2971 - DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, 2972 - state->msg->msg_name); 2973 - unix_copy_addr(state->msg, skb->sk); 2967 + if (msg && msg->msg_name) { 2968 + DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name); 2974 2969 2975 - BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk, 2976 - state->msg->msg_name, 2977 - &state->msg->msg_namelen); 2970 + unix_copy_addr(msg, skb->sk); 2971 + BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk, msg->msg_name, 2972 + &msg->msg_namelen); 2978 2973 2979 2974 sunaddr = NULL; 2980 2975 } ··· 3034 3033 } while (size); 3035 3034 3036 3035 mutex_unlock(&u->iolock); 3037 - if (state->msg) 3038 - scm_recv_unix(sock, state->msg, &scm, flags); 3036 + if (msg) 3037 + scm_recv_unix(sock, msg, &scm, flags); 3039 3038 else 3040 3039 scm_destroy(&scm); 3041 3040 out: