Skip to content

Commit 605c0ac

Browse files
lxindavem330
authored andcommitted
sctp: count both sk and asoc sndbuf with skb truesize and sctp_chunk size
Now it's confusing that asoc sndbuf_used is doing memory accounting with SCTP_DATA_SNDSIZE(chunk) + sizeof(sk_buff) + sizeof(sctp_chunk) while sk sk_wmem_alloc is doing that with skb->truesize + sizeof(sctp_chunk). It also causes sctp_prsctp_prune to count with a wrong freed memory when sndbuf_policy is not set. To make this right and also keep consistent between asoc sndbuf_used, sk sk_wmem_alloc and sk_wmem_queued, use skb->truesize + sizeof(sctp_chunk) for them. Signed-off-by: Xin Long <lucien.xin@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2d0f0ca commit 605c0ac

3 files changed

Lines changed: 8 additions & 26 deletions

File tree

‎include/net/sctp/constants.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ SCTP_SUBTYPE_CONSTRUCTOR(PRIMITIVE, enum sctp_event_primitive, primitive)
148148
#define sctp_chunk_is_data(a) (a->chunk_hdr->type == SCTP_CID_DATA || \
149149
a->chunk_hdr->type == SCTP_CID_I_DATA)
150150

151-
/* Calculate the actual data size in a data chunk */
152-
#define SCTP_DATA_SNDSIZE(c) ((int)((unsigned long)(c->chunk_end) - \
153-
(unsigned long)(c->chunk_hdr) - \
154-
sctp_datachk_len(&c->asoc->stream)))
155-
156151
/* Internal error codes */
157152
enum sctp_ierror {
158153
SCTP_IERROR_NO_ERROR = 0,

‎net/sctp/outqueue.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ static int sctp_prsctp_prune_sent(struct sctp_association *asoc,
385385
asoc->outqueue.outstanding_bytes -= sctp_data_size(chk);
386386
}
387387

388-
msg_len -= SCTP_DATA_SNDSIZE(chk) +
389-
sizeof(struct sk_buff) +
390-
sizeof(struct sctp_chunk);
388+
msg_len -= chk->skb->truesize + sizeof(struct sctp_chunk);
391389
if (msg_len <= 0)
392390
break;
393391
}
@@ -421,9 +419,7 @@ static int sctp_prsctp_prune_unsent(struct sctp_association *asoc,
421419
streamout->ext->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
422420
}
423421

424-
msg_len -= SCTP_DATA_SNDSIZE(chk) +
425-
sizeof(struct sk_buff) +
426-
sizeof(struct sctp_chunk);
422+
msg_len -= chk->skb->truesize + sizeof(struct sctp_chunk);
427423
sctp_chunk_free(chk);
428424
if (msg_len <= 0)
429425
break;

‎net/sctp/socket.c‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,9 @@ static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
166166
/* Save the chunk pointer in skb for sctp_wfree to use later. */
167167
skb_shinfo(chunk->skb)->destructor_arg = chunk;
168168

169-
asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
170-
sizeof(struct sk_buff) +
171-
sizeof(struct sctp_chunk);
172-
173169
refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
174-
sk->sk_wmem_queued += chunk->skb->truesize;
170+
asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
171+
sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
175172
sk_mem_charge(sk, chunk->skb->truesize);
176173
}
177174

@@ -8460,17 +8457,11 @@ static void sctp_wfree(struct sk_buff *skb)
84608457
struct sctp_association *asoc = chunk->asoc;
84618458
struct sock *sk = asoc->base.sk;
84628459

8463-
asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8464-
sizeof(struct sk_buff) +
8465-
sizeof(struct sctp_chunk);
8466-
8467-
WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
8468-
8469-
/*
8470-
* This undoes what is done via sctp_set_owner_w and sk_mem_charge
8471-
*/
8472-
sk->sk_wmem_queued -= skb->truesize;
84738460
sk_mem_uncharge(sk, skb->truesize);
8461+
sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8462+
asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8463+
WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8464+
&sk->sk_wmem_alloc));
84748465

84758466
if (chunk->shkey) {
84768467
struct sctp_shared_key *shkey = chunk->shkey;

0 commit comments

Comments
 (0)