Skip to content

Commit 87a2fd2

Browse files
Daniel Borkmanndavem330
authored andcommitted
packet: don't unconditionally schedule() in case of MSG_DONTWAIT
In tpacket_snd(), when we've discovered a first frame that is not in status TP_STATUS_SEND_REQUEST, and return a NULL buffer, we exit the send routine in case of MSG_DONTWAIT, since we've finished traversing the mmaped send ring buffer and don't care about pending frames. While doing so, we still unconditionally call an expensive schedule() in the packet_current_frame() "error" path, which is unnecessary in this case since it's enough to just quit the function. Also, in case MSG_DONTWAIT is not set, we should rather test for need_resched() first and do schedule() only if necessary since meanwhile pending frames could already have finished processing and called skb destructor. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 902fefb commit 87a2fd2

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

net/packet/af_packet.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
21562156
int err, reserve = 0;
21572157
void *ph;
21582158
struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
2159+
bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
21592160
int tp_len, size_max;
21602161
unsigned char *addr;
21612162
int len_sum = 0;
@@ -2198,10 +2199,10 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
21982199

21992200
do {
22002201
ph = packet_current_frame(po, &po->tx_ring,
2201-
TP_STATUS_SEND_REQUEST);
2202-
2202+
TP_STATUS_SEND_REQUEST);
22032203
if (unlikely(ph == NULL)) {
2204-
schedule();
2204+
if (need_wait && need_resched())
2205+
schedule();
22052206
continue;
22062207
}
22072208

@@ -2255,10 +2256,8 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
22552256
}
22562257
packet_increment_head(&po->tx_ring);
22572258
len_sum += tp_len;
2258-
} while (likely((ph != NULL) ||
2259-
((!(msg->msg_flags & MSG_DONTWAIT)) &&
2260-
(atomic_read(&po->tx_ring.pending))))
2261-
);
2259+
} while (likely((ph != NULL) || (need_wait &&
2260+
atomic_read(&po->tx_ring.pending))));
22622261

22632262
err = len_sum;
22642263
goto out_put;

0 commit comments

Comments
 (0)