[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210203041636.38555-8-xiyou.wangcong@gmail.com>
Date: Tue, 2 Feb 2021 20:16:24 -0800
From: Cong Wang <xiyou.wangcong@...il.com>
To: netdev@...r.kernel.org
Cc: bpf@...r.kernel.org, duanxiongchun@...edance.com,
wangdongdong.6@...edance.com, jiang.wang@...edance.com,
Cong Wang <cong.wang@...edance.com>,
John Fastabend <john.fastabend@...il.com>,
Daniel Borkmann <daniel@...earbox.net>,
Jakub Sitnicki <jakub@...udflare.com>,
Lorenz Bauer <lmb@...udflare.com>
Subject: [Patch bpf-next 07/19] udp: implement ->sendmsg_locked()
From: Cong Wang <cong.wang@...edance.com>
UDP already has udp_sendmsg() which takes lock_sock() inside.
We have to build ->sendmsg_locked() on top of it, by adding
a new parameter for whether the sock has been locked.
Cc: John Fastabend <john.fastabend@...il.com>
Cc: Daniel Borkmann <daniel@...earbox.net>
Cc: Jakub Sitnicki <jakub@...udflare.com>
Cc: Lorenz Bauer <lmb@...udflare.com>
Signed-off-by: Cong Wang <cong.wang@...edance.com>
---
include/net/udp.h | 1 +
net/ipv4/af_inet.c | 1 +
net/ipv4/udp.c | 30 +++++++++++++++++++++++-------
3 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/include/net/udp.h b/include/net/udp.h
index e3e5dfc8e0f0..13f9354dbd3e 100644
--- a/include/net/udp.h
+++ b/include/net/udp.h
@@ -289,6 +289,7 @@ int udp_get_port(struct sock *sk, unsigned short snum,
int udp_err(struct sk_buff *, u32);
int udp_abort(struct sock *sk, int err);
int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
+int udp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len);
int udp_push_pending_frames(struct sock *sk);
void udp_flush_pending_frames(struct sock *sk);
int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size);
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index aaa94bea19c3..d184d9379a92 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -1071,6 +1071,7 @@ const struct proto_ops inet_dgram_ops = {
.setsockopt = sock_common_setsockopt,
.getsockopt = sock_common_getsockopt,
.sendmsg = inet_sendmsg,
+ .sendmsg_locked = udp_sendmsg_locked,
.recvmsg = inet_recvmsg,
.mmap = sock_no_mmap,
.sendpage = inet_sendpage,
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 84ab4f2e874a..635e1e8b2968 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1018,7 +1018,7 @@ int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size)
}
EXPORT_SYMBOL_GPL(udp_cmsg_send);
-int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
+static int __udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len, bool locked)
{
struct inet_sock *inet = inet_sk(sk);
struct udp_sock *up = udp_sk(sk);
@@ -1057,15 +1057,18 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
* There are pending frames.
* The socket lock must be held while it's corked.
*/
- lock_sock(sk);
+ if (!locked)
+ lock_sock(sk);
if (likely(up->pending)) {
if (unlikely(up->pending != AF_INET)) {
- release_sock(sk);
+ if (!locked)
+ release_sock(sk);
return -EINVAL;
}
goto do_append_data;
}
- release_sock(sk);
+ if (!locked)
+ release_sock(sk);
}
ulen += sizeof(struct udphdr);
@@ -1235,11 +1238,13 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
goto out;
}
- lock_sock(sk);
+ if (!locked)
+ lock_sock(sk);
if (unlikely(up->pending)) {
/* The socket is already corked while preparing it. */
/* ... which is an evident application bug. --ANK */
- release_sock(sk);
+ if (!locked)
+ release_sock(sk);
net_dbg_ratelimited("socket already corked\n");
err = -EINVAL;
@@ -1266,7 +1271,8 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
err = udp_push_pending_frames(sk);
else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
up->pending = 0;
- release_sock(sk);
+ if (!locked)
+ release_sock(sk);
out:
ip_rt_put(rt);
@@ -1296,8 +1302,18 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
err = 0;
goto out;
}
+
+int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
+{
+ return __udp_sendmsg(sk, msg, len, false);
+}
EXPORT_SYMBOL(udp_sendmsg);
+int udp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len)
+{
+ return __udp_sendmsg(sk, msg, len, true);
+}
+
int udp_sendpage(struct sock *sk, struct page *page, int offset,
size_t size, int flags)
{
--
2.25.1
Powered by blists - more mailing lists