Skip to content

Commit 2d8af4e

Browse files
committed
Merge branch 'bpf-sockmap-fix-fionread-for-sockets-without-a-verdict-program'
Mattia Meleleo says: ==================== bpf, sockmap: Fix FIONREAD for sockets without a verdict program Sockets added to a sockmap/sockhash with no stream/skb verdict program attached answer FIONREAD with 0 even when unread data is pending in sk_receive_queue. Fix tcp_bpf_ioctl() to account for the receive queue in that case, and add a selftest. Changes in v3: - Remove unused sk_psock_msg_inq() - Link to v2: https://patch.msgid.link/20260708-fionread-no-verdict-v2-0-29dd293621c7@coralogix.com Changes in v2: - Split the fix and the selftest into separate patches - Use READ_ONCE() to read the verdict program pointers - Link to v1: https://patch.msgid.link/20260707-fionread-no-verdict-v1-1-ce94a72357ec@coralogix.com Signed-off-by: Mattia Meleleo <mattia.meleleo@coralogix.com> --- ==================== Link: https://patch.msgid.link/20260708-fionread-no-verdict-v3-0-b4ee31b3af53@coralogix.com Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
2 parents 71836d0 + a42f05c commit 2d8af4e

3 files changed

Lines changed: 55 additions & 15 deletions

File tree

include/linux/skmsg.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -551,20 +551,6 @@ static inline void psock_progs_drop(struct sk_psock_progs *progs)
551551
psock_set_prog(&progs->skb_verdict, NULL);
552552
}
553553

554-
/* for tcp only, sk is locked */
555-
static inline ssize_t sk_psock_msg_inq(struct sock *sk)
556-
{
557-
struct sk_psock *psock;
558-
ssize_t inq = 0;
559-
560-
psock = sk_psock_get(sk);
561-
if (likely(psock)) {
562-
inq = sk_psock_get_msg_len_nolock(psock);
563-
sk_psock_put(sk, psock);
564-
}
565-
return inq;
566-
}
567-
568554
/* for udp only, sk is not locked */
569555
static inline ssize_t sk_msg_first_len(struct sock *sk)
570556
{

net/ipv4/tcp_bpf.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
334334

335335
static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg)
336336
{
337+
struct sk_psock *psock;
337338
bool slow;
338339

339340
if (cmd != SIOCINQ)
@@ -344,7 +345,21 @@ static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg)
344345
return -EINVAL;
345346

346347
slow = lock_sock_fast(sk);
347-
*karg = sk_psock_msg_inq(sk);
348+
psock = sk_psock_get(sk);
349+
if (unlikely(!psock)) {
350+
unlock_sock_fast(sk, slow);
351+
return tcp_ioctl(sk, cmd, karg);
352+
}
353+
*karg = sk_psock_get_msg_len_nolock(psock);
354+
/* Without a verdict program, ingress data is never diverted to
355+
* ingress_msg: it stays in sk_receive_queue and is read through
356+
* the fallback to tcp_recvmsg(), so account for it like
357+
* tcp_ioctl() does.
358+
*/
359+
if (!READ_ONCE(psock->progs.stream_verdict) &&
360+
!READ_ONCE(psock->progs.skb_verdict))
361+
*karg += tcp_inq(sk);
362+
sk_psock_put(sk, psock);
348363
unlock_sock_fast(sk, slow);
349364

350365
return 0;

tools/testing/selftests/bpf/prog_tests/sockmap_basic.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,43 @@ static void test_sockmap_multi_channels(int sotype)
13731373
test_sockmap_pass_prog__destroy(skel);
13741374
}
13751375

1376+
/* A socket in a sockmap without a verdict program keeps its ingress data
1377+
* in sk_receive_queue: FIONREAD must account for it.
1378+
*/
1379+
static void test_sockmap_no_verdict_fionread(void)
1380+
{
1381+
int err, map, zero = 0, sent, avail;
1382+
int c0 = -1, c1 = -1, p0 = -1, p1 = -1;
1383+
struct test_sockmap_pass_prog *skel;
1384+
char buf[256] = "0123456789";
1385+
1386+
skel = test_sockmap_pass_prog__open_and_load();
1387+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
1388+
return;
1389+
map = bpf_map__fd(skel->maps.sock_map_rx);
1390+
1391+
err = create_socket_pairs(AF_INET, SOCK_STREAM, &c0, &c1, &p0, &p1);
1392+
if (!ASSERT_OK(err, "create_socket_pairs()"))
1393+
goto out;
1394+
1395+
err = bpf_map_update_elem(map, &zero, &c1, BPF_NOEXIST);
1396+
if (!ASSERT_OK(err, "bpf_map_update_elem(c1)"))
1397+
goto out_close;
1398+
1399+
sent = xsend(p1, &buf, sizeof(buf), 0);
1400+
ASSERT_EQ(sent, sizeof(buf), "xsend(p1)");
1401+
avail = wait_for_fionread(c1, sizeof(buf), IO_TIMEOUT_SEC);
1402+
ASSERT_EQ(avail, sizeof(buf), "ioctl(FIONREAD)");
1403+
1404+
out_close:
1405+
close(c0);
1406+
close(p0);
1407+
close(c1);
1408+
close(p1);
1409+
out:
1410+
test_sockmap_pass_prog__destroy(skel);
1411+
}
1412+
13761413
void test_sockmap_basic(void)
13771414
{
13781415
if (test__start_subtest("sockmap create_update_free"))
@@ -1415,6 +1452,8 @@ void test_sockmap_basic(void)
14151452
test_sockmap_skb_verdict_shutdown();
14161453
if (test__start_subtest("sockmap skb_verdict fionread"))
14171454
test_sockmap_skb_verdict_fionread(true);
1455+
if (test__start_subtest("sockmap no_verdict fionread"))
1456+
test_sockmap_no_verdict_fionread();
14181457
if (test__start_subtest("sockmap skb_verdict fionread on drop"))
14191458
test_sockmap_skb_verdict_fionread(false);
14201459
if (test__start_subtest("sockmap skb_verdict change tail"))

0 commit comments

Comments
 (0)