Blob Blame History Raw
From: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Date: Tue, 3 Oct 2017 19:20:08 -0300
Subject: sctp: silence warns on sctp_stream_init allocations
Patch-mainline: v4.15-rc1
Git-commit: 1ae2eaaa229bc350b6f38fbf4ab9c873532aecfb
References: bsc#1083710

As SCTP supports up to 65535 streams, that can lead to very large
allocations in sctp_stream_init(). As Xin Long noticed, systems with
small amounts of memory are more prone to not have enough memory and
dump warnings on dmesg initiated by user actions. Thus, silence them.

Also, if the reallocation of stream->out is not necessary, skip it and
keep the memory we already have.

Reported-by: Xin Long <lucien.xin@gmail.com>
Tested-by: Xin Long <lucien.xin@gmail.com>
Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Acked-by: Michal Kubecek <mkubecek@suse.cz>

SLE15: as we do not have commit ff356414dc00 ("sctp: merge sctp_stream_new
and sctp_stream_init") which merged sctp_stream_new() into
sctp_stream_init(), we need to add __GFP_NOWARN in both.

---
 net/sctp/stream.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

--- a/net/sctp/stream.c
+++ b/net/sctp/stream.c
@@ -40,6 +40,8 @@ int sctp_stream_new(struct sctp_association *asoc, gfp_t gfp)
 	struct sctp_stream *stream;
 	int i;
 
+	gfp |= __GFP_NOWARN;
+
 	stream = kzalloc(sizeof(*stream), gfp);
 	if (!stream)
 		return -ENOMEM;
@@ -63,9 +65,14 @@ int sctp_stream_init(struct sctp_association *asoc, gfp_t gfp)
 	struct sctp_stream *stream = asoc->stream;
 	int i;
 
+	gfp |= __GFP_NOWARN;
+
 	/* Initial stream->out size may be very big, so free it and alloc
-	 * a new one with new outcnt to save memory.
+	 * a new one with new outcnt to save memory if needed.
 	 */
+	if (asoc->c.sinit_num_ostreams == stream->outcnt)
+		goto in;
+
 	kfree(stream->out);
 	stream->outcnt = asoc->c.sinit_num_ostreams;
 	stream->out = kcalloc(stream->outcnt, sizeof(*stream->out), gfp);
@@ -75,6 +82,7 @@ int sctp_stream_init(struct sctp_association *asoc, gfp_t gfp)
 	for (i = 0; i < stream->outcnt; i++)
 		stream->out[i].state = SCTP_STREAM_OPEN;
 
+in:
 	stream->incnt = asoc->c.sinit_max_instreams;
 	stream->in = kcalloc(stream->incnt, sizeof(*stream->in), gfp);
 	if (!stream->in) {