skmsg: Get rid of unncessary memset()
authorCong Wang <cong.wang@bytedance.com>
Wed, 15 Jun 2022 16:20:14 +0000 (09:20 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 20 Jun 2022 12:05:52 +0000 (14:05 +0200)
We always allocate skmsg with kzalloc(), so there is no need
to call memset(0) on it, the only thing we need from
sk_msg_init() is sg_init_marker(). So introduce a new helper
which is just kzalloc()+sg_init_marker(), this saves an
unncessary memset(0) for skmsg on fast path.

Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20220615162014.89193-5-xiyou.wangcong@gmail.com
net/core/skmsg.c

index 8b248d2..4b297d6 100644 (file)
@@ -497,23 +497,27 @@ bool sk_msg_is_readable(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(sk_msg_is_readable);
 
-static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk,
-                                                 struct sk_buff *skb)
+static struct sk_msg *alloc_sk_msg(void)
 {
        struct sk_msg *msg;
 
-       if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
+       msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_KERNEL);
+       if (unlikely(!msg))
                return NULL;
+       sg_init_marker(msg->sg.data, NR_MSG_FRAG_IDS);
+       return msg;
+}
 
-       if (!sk_rmem_schedule(sk, skb, skb->truesize))
+static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk,
+                                                 struct sk_buff *skb)
+{
+       if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
                return NULL;
 
-       msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_KERNEL);
-       if (unlikely(!msg))
+       if (!sk_rmem_schedule(sk, skb, skb->truesize))
                return NULL;
 
-       sk_msg_init(msg);
-       return msg;
+       return alloc_sk_msg();
 }
 
 static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
@@ -590,13 +594,12 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,
 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb,
                                     u32 off, u32 len)
 {
-       struct sk_msg *msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
+       struct sk_msg *msg = alloc_sk_msg();
        struct sock *sk = psock->sk;
        int err;
 
        if (unlikely(!msg))
                return -EAGAIN;
-       sk_msg_init(msg);
        skb_set_owner_r(skb, sk);
        err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg);
        if (err < 0)