Skip to content

Commit 2075e50

Browse files
koverstreettorvalds
authored andcommitted
sctp: convert to genradix
This also makes sctp_stream_alloc_(out|in) saner, in that they no longer allocate new flex_arrays/genradixes, they just preallocate more elements. This code does however have a suspicious lack of locking. Link: http://lkml.kernel.org/r/20181217131929.11727-7-kent.overstreet@gmail.com Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com> Cc: Vlad Yasevich <vyasevich@gmail.com> Cc: Neil Horman <nhorman@tuxdriver.com> Cc: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Eric Paris <eparis@parisplace.org> Cc: Matthew Wilcox <willy@infradead.org> Cc: Paul Moore <paul@paul-moore.com> Cc: Pravin B Shelar <pshelar@ovn.org> Cc: Shaohua Li <shli@kernel.org> Cc: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 94f8f3b commit 2075e50

File tree

3 files changed

+28
-114
lines changed

3 files changed

+28
-114
lines changed

include/net/sctp/structs.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define __sctp_structs_h__
4949

5050
#include <linux/ktime.h>
51+
#include <linux/generic-radix-tree.h>
5152
#include <linux/rhashtable-types.h>
5253
#include <linux/socket.h> /* linux/in.h needs this!! */
5354
#include <linux/in.h> /* We get struct sockaddr_in. */
@@ -57,7 +58,6 @@
5758
#include <linux/atomic.h> /* This gets us atomic counters. */
5859
#include <linux/skbuff.h> /* We need sk_buff_head. */
5960
#include <linux/workqueue.h> /* We need tq_struct. */
60-
#include <linux/flex_array.h> /* We need flex_array. */
6161
#include <linux/sctp.h> /* We need sctp* header structs. */
6262
#include <net/sctp/auth.h> /* We need auth specific structs */
6363
#include <net/ip.h> /* For inet_skb_parm */
@@ -1449,8 +1449,9 @@ struct sctp_stream_in {
14491449
};
14501450

14511451
struct sctp_stream {
1452-
struct flex_array *out;
1453-
struct flex_array *in;
1452+
GENRADIX(struct sctp_stream_out) out;
1453+
GENRADIX(struct sctp_stream_in) in;
1454+
14541455
__u16 outcnt;
14551456
__u16 incnt;
14561457
/* Current stream being sent, if any */
@@ -1473,17 +1474,17 @@ struct sctp_stream {
14731474
};
14741475

14751476
static inline struct sctp_stream_out *sctp_stream_out(
1476-
const struct sctp_stream *stream,
1477+
struct sctp_stream *stream,
14771478
__u16 sid)
14781479
{
1479-
return flex_array_get(stream->out, sid);
1480+
return genradix_ptr(&stream->out, sid);
14801481
}
14811482

14821483
static inline struct sctp_stream_in *sctp_stream_in(
1483-
const struct sctp_stream *stream,
1484+
struct sctp_stream *stream,
14841485
__u16 sid)
14851486
{
1486-
return flex_array_get(stream->in, sid);
1487+
return genradix_ptr(&stream->in, sid);
14871488
}
14881489

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

net/sctp/stream.c

Lines changed: 19 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -37,66 +37,6 @@
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-
87-
static size_t fa_index(struct flex_array *fa, void *elem, size_t count)
88-
{
89-
size_t index = 0;
90-
91-
while (count--) {
92-
if (elem == flex_array_get(fa, index))
93-
break;
94-
index++;
95-
}
96-
97-
return index;
98-
}
99-
10040
/* Migrates chunks from stream queues to new stream queues if needed,
10141
* but not across associations. Also, removes those chunks to streams
10242
* higher than the new max.
@@ -153,53 +93,32 @@ static void sctp_stream_outq_migrate(struct sctp_stream *stream,
15393
static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
15494
gfp_t gfp)
15595
{
156-
struct flex_array *out;
157-
size_t elem_size = sizeof(struct sctp_stream_out);
158-
159-
out = fa_alloc(elem_size, outcnt, gfp);
160-
if (!out)
161-
return -ENOMEM;
162-
163-
if (stream->out) {
164-
fa_copy(out, stream->out, 0, min(outcnt, stream->outcnt));
165-
if (stream->out_curr) {
166-
size_t index = fa_index(stream->out, stream->out_curr,
167-
stream->outcnt);
168-
169-
BUG_ON(index == stream->outcnt);
170-
stream->out_curr = flex_array_get(out, index);
171-
}
172-
fa_free(stream->out);
173-
}
96+
int ret;
17497

175-
if (outcnt > stream->outcnt)
176-
fa_zero(out, stream->outcnt, (outcnt - stream->outcnt));
98+
if (outcnt <= stream->outcnt)
99+
return 0;
177100

178-
stream->out = out;
101+
ret = genradix_prealloc(&stream->out, outcnt, gfp);
102+
if (ret)
103+
return ret;
179104

105+
stream->outcnt = outcnt;
180106
return 0;
181107
}
182108

183109
static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
184110
gfp_t gfp)
185111
{
186-
struct flex_array *in;
187-
size_t elem_size = sizeof(struct sctp_stream_in);
188-
189-
in = fa_alloc(elem_size, incnt, gfp);
190-
if (!in)
191-
return -ENOMEM;
192-
193-
if (stream->in) {
194-
fa_copy(in, stream->in, 0, min(incnt, stream->incnt));
195-
fa_free(stream->in);
196-
}
112+
int ret;
197113

198-
if (incnt > stream->incnt)
199-
fa_zero(in, stream->incnt, (incnt - stream->incnt));
114+
if (incnt <= stream->incnt)
115+
return 0;
200116

201-
stream->in = in;
117+
ret = genradix_prealloc(&stream->in, incnt, gfp);
118+
if (ret)
119+
return ret;
202120

121+
stream->incnt = incnt;
203122
return 0;
204123
}
205124

@@ -226,7 +145,6 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
226145
if (ret)
227146
goto out;
228147

229-
stream->outcnt = outcnt;
230148
for (i = 0; i < stream->outcnt; i++)
231149
SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
232150

@@ -238,14 +156,11 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
238156
ret = sctp_stream_alloc_in(stream, incnt, gfp);
239157
if (ret) {
240158
sched->free(stream);
241-
fa_free(stream->out);
242-
stream->out = NULL;
159+
genradix_free(&stream->out);
243160
stream->outcnt = 0;
244161
goto out;
245162
}
246163

247-
stream->incnt = incnt;
248-
249164
out:
250165
return ret;
251166
}
@@ -270,8 +185,8 @@ void sctp_stream_free(struct sctp_stream *stream)
270185
sched->free(stream);
271186
for (i = 0; i < stream->outcnt; i++)
272187
kfree(SCTP_SO(stream, i)->ext);
273-
fa_free(stream->out);
274-
fa_free(stream->in);
188+
genradix_free(&stream->out);
189+
genradix_free(&stream->in);
275190
}
276191

277192
void sctp_stream_clear(struct sctp_stream *stream)
@@ -302,8 +217,8 @@ void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
302217

303218
sched->sched_all(stream);
304219

305-
new->out = NULL;
306-
new->in = NULL;
220+
new->out.tree.root = NULL;
221+
new->in.tree.root = NULL;
307222
new->outcnt = 0;
308223
new->incnt = 0;
309224
}
@@ -555,8 +470,6 @@ int sctp_send_add_streams(struct sctp_association *asoc,
555470
goto out;
556471
}
557472

558-
stream->outcnt = outcnt;
559-
560473
asoc->strreset_outstanding = !!out + !!in;
561474

562475
out:

net/sctp/stream_interleave.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static void sctp_chunk_assign_mid(struct sctp_chunk *chunk)
101101

102102
static bool sctp_validate_data(struct sctp_chunk *chunk)
103103
{
104-
const struct sctp_stream *stream;
104+
struct sctp_stream *stream;
105105
__u16 sid, ssn;
106106

107107
if (chunk->chunk_hdr->type != SCTP_CID_DATA)

0 commit comments

Comments
 (0)