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
91138static 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,
115161static 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
213257void sctp_stream_clear (struct sctp_stream * stream )
0 commit comments