Skip to content

Commit 9dde27d

Browse files
lxindavem330
authored andcommitted
sctp: implement memory accounting on rx path
sk_forward_alloc's updating is also done on rx path, but to be consistent we change to use sk_mem_charge() in sctp_skb_set_owner_r(). In sctp_eat_data(), it's not enough to check sctp_memory_pressure only, which doesn't work for mem_cgroup_sockets_enabled, so we change to use sk_under_memory_pressure(). When it's under memory pressure, sk_mem_reclaim() and sk_rmem_schedule() should be called on both RENEGE or CHUNK DELIVERY path exit the memory pressure status as soon as possible. Note that sk_rmem_schedule() is using datalen to make things easy there. Reported-by: Matteo Croce <[email protected]> Tested-by: Matteo Croce <[email protected]> Acked-by: Neil Horman <[email protected]> Acked-by: Marcelo Ricardo Leitner <[email protected]> Signed-off-by: Xin Long <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1033990 commit 9dde27d

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

include/net/sctp/sctp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static inline void sctp_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
421421
/*
422422
* This mimics the behavior of skb_set_owner_r
423423
*/
424-
sk->sk_forward_alloc -= event->rmem_len;
424+
sk_mem_charge(sk, event->rmem_len);
425425
}
426426

427427
/* Tests if the list has one and only one entry. */

net/sctp/sm_statefuns.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6412,13 +6412,15 @@ static int sctp_eat_data(const struct sctp_association *asoc,
64126412
* in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
64136413
* memory usage too much
64146414
*/
6415-
if (*sk->sk_prot_creator->memory_pressure) {
6415+
if (sk_under_memory_pressure(sk)) {
64166416
if (sctp_tsnmap_has_gap(map) &&
64176417
(sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
64186418
pr_debug("%s: under pressure, reneging for tsn:%u\n",
64196419
__func__, tsn);
64206420
deliver = SCTP_CMD_RENEGE;
6421-
}
6421+
} else {
6422+
sk_mem_reclaim(sk);
6423+
}
64226424
}
64236425

64246426
/*

net/sctp/ulpevent.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,9 @@ struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
634634
gfp_t gfp)
635635
{
636636
struct sctp_ulpevent *event = NULL;
637-
struct sk_buff *skb;
638-
size_t padding, len;
637+
struct sk_buff *skb = chunk->skb;
638+
struct sock *sk = asoc->base.sk;
639+
size_t padding, datalen;
639640
int rx_count;
640641

641642
/*
@@ -646,15 +647,12 @@ struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
646647
if (asoc->ep->rcvbuf_policy)
647648
rx_count = atomic_read(&asoc->rmem_alloc);
648649
else
649-
rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
650+
rx_count = atomic_read(&sk->sk_rmem_alloc);
650651

651-
if (rx_count >= asoc->base.sk->sk_rcvbuf) {
652+
datalen = ntohs(chunk->chunk_hdr->length);
652653

653-
if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
654-
(!sk_rmem_schedule(asoc->base.sk, chunk->skb,
655-
chunk->skb->truesize)))
656-
goto fail;
657-
}
654+
if (rx_count >= sk->sk_rcvbuf || !sk_rmem_schedule(sk, skb, datalen))
655+
goto fail;
658656

659657
/* Clone the original skb, sharing the data. */
660658
skb = skb_clone(chunk->skb, gfp);
@@ -681,8 +679,7 @@ struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
681679
* The sender should never pad with more than 3 bytes. The receiver
682680
* MUST ignore the padding bytes.
683681
*/
684-
len = ntohs(chunk->chunk_hdr->length);
685-
padding = SCTP_PAD4(len) - len;
682+
padding = SCTP_PAD4(datalen) - datalen;
686683

687684
/* Fixup cloned skb with just this chunks data. */
688685
skb_trim(skb, chunk->chunk_end - padding - skb->data);

net/sctp/ulpqueue.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,8 @@ void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
11041104
freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
11051105
}
11061106
/* If able to free enough room, accept this chunk. */
1107-
if (freed >= needed) {
1107+
if (sk_rmem_schedule(asoc->base.sk, chunk->skb, needed) &&
1108+
freed >= needed) {
11081109
int retval = sctp_ulpq_tail_data(ulpq, chunk, gfp);
11091110
/*
11101111
* Enter partial delivery if chunk has not been

0 commit comments

Comments
 (0)