Commit e090abd0 authored by Marcelo Ricardo Leitner's avatar Marcelo Ricardo Leitner Committed by David S. Miller

sctp: factor out stream->out allocation

There is 1 place allocating it and 2 other reallocating. Move such
procedures to a common function.
Tested-by: default avatarXin Long <lucien.xin@gmail.com>
Signed-off-by: default avatarMarcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1ae2eaaa
...@@ -35,6 +35,30 @@ ...@@ -35,6 +35,30 @@
#include <net/sctp/sctp.h> #include <net/sctp/sctp.h>
#include <net/sctp/sm.h> #include <net/sctp/sm.h>
static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
gfp_t gfp)
{
struct sctp_stream_out *out;
out = kmalloc_array(outcnt, sizeof(*out), gfp);
if (!out)
return -ENOMEM;
if (stream->out) {
memcpy(out, stream->out, min(outcnt, stream->outcnt) *
sizeof(*out));
kfree(stream->out);
}
if (outcnt > stream->outcnt)
memset(out + stream->outcnt, 0,
(outcnt - stream->outcnt) * sizeof(*out));
stream->out = out;
return 0;
}
int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt, int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
gfp_t gfp) gfp_t gfp)
{ {
...@@ -48,11 +72,9 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt, ...@@ -48,11 +72,9 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
if (outcnt == stream->outcnt) if (outcnt == stream->outcnt)
goto in; goto in;
kfree(stream->out); i = sctp_stream_alloc_out(stream, outcnt, gfp);
if (i)
stream->out = kcalloc(outcnt, sizeof(*stream->out), gfp); return i;
if (!stream->out)
return -ENOMEM;
stream->outcnt = outcnt; stream->outcnt = outcnt;
for (i = 0; i < stream->outcnt; i++) for (i = 0; i < stream->outcnt; i++)
...@@ -276,15 +298,9 @@ int sctp_send_add_streams(struct sctp_association *asoc, ...@@ -276,15 +298,9 @@ int sctp_send_add_streams(struct sctp_association *asoc,
} }
if (out) { if (out) {
struct sctp_stream_out *streamout; retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
if (retval)
streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
GFP_KERNEL);
if (!streamout)
goto out; goto out;
memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
stream->out = streamout;
} }
chunk = sctp_make_strreset_addstrm(asoc, out, in); chunk = sctp_make_strreset_addstrm(asoc, out, in);
...@@ -682,10 +698,10 @@ struct sctp_chunk *sctp_process_strreset_addstrm_in( ...@@ -682,10 +698,10 @@ struct sctp_chunk *sctp_process_strreset_addstrm_in(
struct sctp_strreset_addstrm *addstrm = param.v; struct sctp_strreset_addstrm *addstrm = param.v;
struct sctp_stream *stream = &asoc->stream; struct sctp_stream *stream = &asoc->stream;
__u32 result = SCTP_STRRESET_DENIED; __u32 result = SCTP_STRRESET_DENIED;
struct sctp_stream_out *streamout;
struct sctp_chunk *chunk = NULL; struct sctp_chunk *chunk = NULL;
__u32 request_seq, outcnt; __u32 request_seq, outcnt;
__u16 out, i; __u16 out, i;
int ret;
request_seq = ntohl(addstrm->request_seq); request_seq = ntohl(addstrm->request_seq);
if (TSN_lt(asoc->strreset_inseq, request_seq) || if (TSN_lt(asoc->strreset_inseq, request_seq) ||
...@@ -714,14 +730,10 @@ struct sctp_chunk *sctp_process_strreset_addstrm_in( ...@@ -714,14 +730,10 @@ struct sctp_chunk *sctp_process_strreset_addstrm_in(
if (!out || outcnt > SCTP_MAX_STREAM) if (!out || outcnt > SCTP_MAX_STREAM)
goto out; goto out;
streamout = krealloc(stream->out, outcnt * sizeof(*streamout), ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
GFP_ATOMIC); if (ret)
if (!streamout)
goto out; goto out;
memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
stream->out = streamout;
chunk = sctp_make_strreset_addstrm(asoc, out, 0); chunk = sctp_make_strreset_addstrm(asoc, out, 0);
if (!chunk) if (!chunk)
goto out; goto out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment