Skip to content

Commit db2af14

Browse files
Pekka Enberggregkh
authored andcommitted
Staging: echo: fix kmalloc()/kfree() uses
This patch removes the malloc()/free() macro wrappers and converts call-sites to use kcalloc() and kzalloc() where appropriate. I also fixed up out-of-memory error handling in couple of places where it was broken. Cc: David Rowe <david@rowetel.com> Cc: Steve Underwood <steveu@coppice.org> Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent f55ccbf commit db2af14

2 files changed

Lines changed: 22 additions & 28 deletions

File tree

‎drivers/staging/echo/echo.c‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@
109109
#include <linux/module.h>
110110
#include <linux/kernel.h>
111111
#include <linux/slab.h>
112-
#define malloc(a) kmalloc((a), GFP_KERNEL)
113-
#define free(a) kfree(a)
114112

115113
#include "bit_operations.h"
116114
#include "echo.h"
@@ -238,27 +236,19 @@ struct oslec_state *oslec_create(int len, int adaption_mode)
238236
{
239237
struct oslec_state *ec;
240238
int i;
241-
int j;
242239

243-
ec = kmalloc(sizeof(*ec), GFP_KERNEL);
244-
if (ec == NULL)
245-
return NULL;
246-
memset(ec, 0, sizeof(*ec));
240+
ec = kzalloc(sizeof(*ec), GFP_KERNEL);
241+
if (!ec)
242+
return NULL;
247243

248244
ec->taps = len;
249245
ec->log2taps = top_bit(len);
250246
ec->curr_pos = ec->taps - 1;
251247

252-
for (i = 0; i < 2; i++)
253-
{
254-
if ((ec->fir_taps16[i] = (int16_t *) malloc((ec->taps)*sizeof(int16_t))) == NULL)
255-
{
256-
for (j = 0; j < i; j++)
257-
kfree(ec->fir_taps16[j]);
258-
kfree(ec);
259-
return NULL;
260-
}
261-
memset(ec->fir_taps16[i], 0, (ec->taps)*sizeof(int16_t));
248+
for (i = 0; i < 2; i++) {
249+
ec->fir_taps16[i] = kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
250+
if (!ec->fir_taps16[i])
251+
goto error_oom;
262252
}
263253

264254
fir16_create(&ec->fir_state,
@@ -275,8 +265,9 @@ struct oslec_state *oslec_create(int len, int adaption_mode)
275265
ec->cng_level = 1000;
276266
oslec_adaption_mode(ec, adaption_mode);
277267

278-
ec->snapshot = (int16_t*)malloc(ec->taps*sizeof(int16_t));
279-
memset(ec->snapshot, 0, sizeof(int16_t)*ec->taps);
268+
ec->snapshot = kcalloc(ec->taps, sizeof(int16_t), GFP_KERNEL);
269+
if (!ec->snapshot)
270+
goto error_oom;
280271

281272
ec->cond_met = 0;
282273
ec->Pstates = 0;
@@ -288,6 +279,13 @@ struct oslec_state *oslec_create(int len, int adaption_mode)
288279
ec->Lbgn_upper_acc = ec->Lbgn_upper << 13;
289280

290281
return ec;
282+
283+
error_oom:
284+
for (i = 0; i < 2; i++)
285+
kfree(ec->fir_taps16[i]);
286+
287+
kfree(ec);
288+
return NULL;
291289
}
292290
EXPORT_SYMBOL_GPL(oslec_create);
293291
/*- End of function --------------------------------------------------------*/

‎drivers/staging/echo/fir.h‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ static __inline__ const int16_t *fir16_create(fir16_state_t *fir,
117117
fir->curr_pos = taps - 1;
118118
fir->coeffs = coeffs;
119119
#if defined(USE_MMX) || defined(USE_SSE2) || defined(__bfin__)
120-
if ((fir->history = malloc(2*taps*sizeof(int16_t))))
121-
memset(fir->history, 0, 2*taps*sizeof(int16_t));
120+
fir->history = kcalloc(2*taps, sizeof(int16_t), GFP_KERNEL);
122121
#else
123-
if ((fir->history = (int16_t *) malloc(taps*sizeof(int16_t))))
124-
memset(fir->history, 0, taps*sizeof(int16_t));
122+
fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
125123
#endif
126124
return fir->history;
127125
}
@@ -139,7 +137,7 @@ static __inline__ void fir16_flush(fir16_state_t *fir)
139137

140138
static __inline__ void fir16_free(fir16_state_t *fir)
141139
{
142-
free(fir->history);
140+
kfree(fir->history);
143141
}
144142
/*- End of function --------------------------------------------------------*/
145143

@@ -275,9 +273,7 @@ static __inline__ const int16_t *fir32_create(fir32_state_t *fir,
275273
fir->taps = taps;
276274
fir->curr_pos = taps - 1;
277275
fir->coeffs = coeffs;
278-
fir->history = (int16_t *) malloc(taps*sizeof(int16_t));
279-
if (fir->history)
280-
memset(fir->history, '\0', taps*sizeof(int16_t));
276+
fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
281277
return fir->history;
282278
}
283279
/*- End of function --------------------------------------------------------*/
@@ -290,7 +286,7 @@ static __inline__ void fir32_flush(fir32_state_t *fir)
290286

291287
static __inline__ void fir32_free(fir32_state_t *fir)
292288
{
293-
free(fir->history);
289+
kfree(fir->history);
294290
}
295291
/*- End of function --------------------------------------------------------*/
296292

0 commit comments

Comments
 (0)