Skip to content

Commit 1fdb8d8

Browse files
marceloleitnerdavem330
authored andcommitted
sctp: factor out stream->in allocation
There is 1 place allocating it and another reallocating. Move such procedures to a common function. v2: updated changelog Tested-by: Xin Long <[email protected]> Signed-off-by: Marcelo Ricardo Leitner <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e090abd commit 1fdb8d8

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

net/sctp/stream.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
5959
return 0;
6060
}
6161

62+
static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
63+
gfp_t gfp)
64+
{
65+
struct sctp_stream_in *in;
66+
67+
in = kmalloc_array(incnt, sizeof(*stream->in), gfp);
68+
69+
if (!in)
70+
return -ENOMEM;
71+
72+
if (stream->in) {
73+
memcpy(in, stream->in, min(incnt, stream->incnt) *
74+
sizeof(*in));
75+
kfree(stream->in);
76+
}
77+
78+
if (incnt > stream->incnt)
79+
memset(in + stream->incnt, 0,
80+
(incnt - stream->incnt) * sizeof(*in));
81+
82+
stream->in = in;
83+
84+
return 0;
85+
}
86+
6287
int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
6388
gfp_t gfp)
6489
{
@@ -84,8 +109,8 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
84109
if (!incnt)
85110
return 0;
86111

87-
stream->in = kcalloc(incnt, sizeof(*stream->in), gfp);
88-
if (!stream->in) {
112+
i = sctp_stream_alloc_in(stream, incnt, gfp);
113+
if (i) {
89114
kfree(stream->out);
90115
stream->out = NULL;
91116
return -ENOMEM;
@@ -623,7 +648,6 @@ struct sctp_chunk *sctp_process_strreset_addstrm_out(
623648
struct sctp_strreset_addstrm *addstrm = param.v;
624649
struct sctp_stream *stream = &asoc->stream;
625650
__u32 result = SCTP_STRRESET_DENIED;
626-
struct sctp_stream_in *streamin;
627651
__u32 request_seq, incnt;
628652
__u16 in, i;
629653

@@ -670,13 +694,9 @@ struct sctp_chunk *sctp_process_strreset_addstrm_out(
670694
if (!in || incnt > SCTP_MAX_STREAM)
671695
goto out;
672696

673-
streamin = krealloc(stream->in, incnt * sizeof(*streamin),
674-
GFP_ATOMIC);
675-
if (!streamin)
697+
if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
676698
goto out;
677699

678-
memset(streamin + stream->incnt, 0, in * sizeof(*streamin));
679-
stream->in = streamin;
680700
stream->incnt = incnt;
681701

682702
result = SCTP_STRRESET_PERFORMED;

0 commit comments

Comments
 (0)