Skip to content

Commit 0d493b4

Browse files
finist0davem330
authored andcommitted
net/sctp: Replace in/out stream arrays with flex_array
This path replaces physically contiguous memory arrays allocated using kmalloc_array() with flexible arrays. This enables to avoid memory allocation failures on the systems under a memory stress. Signed-off-by: Oleg Babin <obabin@virtuozzo.com> Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 05364ca commit 0d493b4

2 files changed

Lines changed: 71 additions & 26 deletions

File tree

‎include/net/sctp/structs.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <linux/atomic.h> /* This gets us atomic counters. */
5858
#include <linux/skbuff.h> /* We need sk_buff_head. */
5959
#include <linux/workqueue.h> /* We need tq_struct. */
60+
#include <linux/flex_array.h> /* We need flex_array. */
6061
#include <linux/sctp.h> /* We need sctp* header structs. */
6162
#include <net/sctp/auth.h> /* We need auth specific structs */
6263
#include <net/ip.h> /* For inet_skb_parm */
@@ -1438,8 +1439,8 @@ struct sctp_stream_in {
14381439
};
14391440

14401441
struct sctp_stream {
1441-
struct sctp_stream_out *out;
1442-
struct sctp_stream_in *in;
1442+
struct flex_array *out;
1443+
struct flex_array *in;
14431444
__u16 outcnt;
14441445
__u16 incnt;
14451446
/* Current stream being sent, if any */
@@ -1465,14 +1466,14 @@ static inline struct sctp_stream_out *sctp_stream_out(
14651466
const struct sctp_stream *stream,
14661467
__u16 sid)
14671468
{
1468-
return ((struct sctp_stream_out *)(stream->out)) + sid;
1469+
return flex_array_get(stream->out, sid);
14691470
}
14701471

14711472
static inline struct sctp_stream_in *sctp_stream_in(
14721473
const struct sctp_stream *stream,
14731474
__u16 sid)
14741475
{
1475-
return ((struct sctp_stream_in *)(stream->in)) + sid;
1476+
return flex_array_get(stream->in, sid);
14761477
}
14771478

14781479
#define SCTP_SO(s, i) sctp_stream_out((s), (i))

‎net/sctp/stream.c‎

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,53 @@
3737
#include <net/sctp/sm.h>
3838
#include <net/sctp/stream_sched.h>
3939

40+
static struct flex_array *fa_alloc(size_t elem_size, size_t elem_count,
41+
gfp_t gfp)
42+
{
43+
struct flex_array *result;
44+
int err;
45+
46+
result = flex_array_alloc(elem_size, elem_count, gfp);
47+
if (result) {
48+
err = flex_array_prealloc(result, 0, elem_count, gfp);
49+
if (err) {
50+
flex_array_free(result);
51+
result = NULL;
52+
}
53+
}
54+
55+
return result;
56+
}
57+
58+
static void fa_free(struct flex_array *fa)
59+
{
60+
if (fa)
61+
flex_array_free(fa);
62+
}
63+
64+
static void fa_copy(struct flex_array *fa, struct flex_array *from,
65+
size_t index, size_t count)
66+
{
67+
void *elem;
68+
69+
while (count--) {
70+
elem = flex_array_get(from, index);
71+
flex_array_put(fa, index, elem, 0);
72+
index++;
73+
}
74+
}
75+
76+
static void fa_zero(struct flex_array *fa, size_t index, size_t count)
77+
{
78+
void *elem;
79+
80+
while (count--) {
81+
elem = flex_array_get(fa, index);
82+
memset(elem, 0, fa->element_size);
83+
index++;
84+
}
85+
}
86+
4087
/* Migrates chunks from stream queues to new stream queues if needed,
4188
* but not across associations. Also, removes those chunks to streams
4289
* higher than the new max.
@@ -78,34 +125,33 @@ static void sctp_stream_outq_migrate(struct sctp_stream *stream,
78125
* sctp_stream_update will swap ->out pointers.
79126
*/
80127
for (i = 0; i < outcnt; i++) {
81-
kfree(new->out[i].ext);
82-
new->out[i].ext = stream->out[i].ext;
83-
stream->out[i].ext = NULL;
128+
kfree(SCTP_SO(new, i)->ext);
129+
SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
130+
SCTP_SO(stream, i)->ext = NULL;
84131
}
85132
}
86133

87134
for (i = outcnt; i < stream->outcnt; i++)
88-
kfree(stream->out[i].ext);
135+
kfree(SCTP_SO(stream, i)->ext);
89136
}
90137

91138
static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
92139
gfp_t gfp)
93140
{
94-
struct sctp_stream_out *out;
141+
struct flex_array *out;
142+
size_t elem_size = sizeof(struct sctp_stream_out);
95143

96-
out = kmalloc_array(outcnt, sizeof(*out), gfp);
144+
out = fa_alloc(elem_size, outcnt, gfp);
97145
if (!out)
98146
return -ENOMEM;
99147

100148
if (stream->out) {
101-
memcpy(out, stream->out, min(outcnt, stream->outcnt) *
102-
sizeof(*out));
103-
kfree(stream->out);
149+
fa_copy(out, stream->out, 0, min(outcnt, stream->outcnt));
150+
fa_free(stream->out);
104151
}
105152

106153
if (outcnt > stream->outcnt)
107-
memset(out + stream->outcnt, 0,
108-
(outcnt - stream->outcnt) * sizeof(*out));
154+
fa_zero(out, stream->outcnt, (outcnt - stream->outcnt));
109155

110156
stream->out = out;
111157

@@ -115,22 +161,20 @@ static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
115161
static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
116162
gfp_t gfp)
117163
{
118-
struct sctp_stream_in *in;
119-
120-
in = kmalloc_array(incnt, sizeof(*stream->in), gfp);
164+
struct flex_array *in;
165+
size_t elem_size = sizeof(struct sctp_stream_in);
121166

167+
in = fa_alloc(elem_size, incnt, gfp);
122168
if (!in)
123169
return -ENOMEM;
124170

125171
if (stream->in) {
126-
memcpy(in, stream->in, min(incnt, stream->incnt) *
127-
sizeof(*in));
128-
kfree(stream->in);
172+
fa_copy(in, stream->in, 0, min(incnt, stream->incnt));
173+
fa_free(stream->in);
129174
}
130175

131176
if (incnt > stream->incnt)
132-
memset(in + stream->incnt, 0,
133-
(incnt - stream->incnt) * sizeof(*in));
177+
fa_zero(in, stream->incnt, (incnt - stream->incnt));
134178

135179
stream->in = in;
136180

@@ -174,7 +218,7 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
174218
ret = sctp_stream_alloc_in(stream, incnt, gfp);
175219
if (ret) {
176220
sched->free(stream);
177-
kfree(stream->out);
221+
fa_free(stream->out);
178222
stream->out = NULL;
179223
stream->outcnt = 0;
180224
goto out;
@@ -206,8 +250,8 @@ void sctp_stream_free(struct sctp_stream *stream)
206250
sched->free(stream);
207251
for (i = 0; i < stream->outcnt; i++)
208252
kfree(SCTP_SO(stream, i)->ext);
209-
kfree(stream->out);
210-
kfree(stream->in);
253+
fa_free(stream->out);
254+
fa_free(stream->in);
211255
}
212256

213257
void sctp_stream_clear(struct sctp_stream *stream)

0 commit comments

Comments
 (0)