Skip to content

Commit bff9285

Browse files
rfvirgilbroonie
authored andcommitted
firmware: cs_dsp: Don't allocate temporary buffer for info text
Don't allocate a temporary buffer to hold a NUL-terminated copy of the NAME/INFO string from the wmfw/bin. It can be printed directly to the log. Also limit the maximum number of characters that will be logged from this string. The NAME/INFO blocks in the firmware files are an array of characters with a length, not a NUL-terminated C string. The original code allocated a temporary buffer to make a NUL-terminated copy of the string and then passed that to dev_info(). There's no need for this: printf formatting can use "%.*s" to print a character array of a given length. Signed-off-by: Richard Fitzgerald <[email protected]> Reviewed-by: Charles Keepax <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 680e126 commit bff9285

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

drivers/firmware/cirrus/cs_dsp.c

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/ctype.h>
1313
#include <linux/debugfs.h>
1414
#include <linux/delay.h>
15+
#include <linux/minmax.h>
1516
#include <linux/module.h>
1617
#include <linux/moduleparam.h>
1718
#include <linux/seq_file.h>
@@ -1470,7 +1471,6 @@ static int cs_dsp_load(struct cs_dsp *dsp, const struct firmware *firmware,
14701471
const struct wmfw_region *region;
14711472
const struct cs_dsp_region *mem;
14721473
const char *region_name;
1473-
char *text = NULL;
14741474
struct cs_dsp_buf *buf;
14751475
unsigned int reg;
14761476
int regions = 0;
@@ -1542,27 +1542,22 @@ static int cs_dsp_load(struct cs_dsp *dsp, const struct firmware *firmware,
15421542

15431543
region_name = "Unknown";
15441544
reg = 0;
1545-
text = NULL;
15461545
offset = le32_to_cpu(region->offset) & 0xffffff;
15471546
type = be32_to_cpu(region->type) & 0xff;
15481547

15491548
switch (type) {
1549+
case WMFW_INFO_TEXT:
15501550
case WMFW_NAME_TEXT:
1551-
region_name = "Firmware name";
1552-
text = kzalloc(le32_to_cpu(region->len) + 1,
1553-
GFP_KERNEL);
1551+
region_name = "Info/Name";
1552+
cs_dsp_info(dsp, "%s: %.*s\n", file,
1553+
min(le32_to_cpu(region->len), 100), region->data);
15541554
break;
15551555
case WMFW_ALGORITHM_DATA:
15561556
region_name = "Algorithm";
15571557
ret = cs_dsp_parse_coeff(dsp, region);
15581558
if (ret != 0)
15591559
goto out_fw;
15601560
break;
1561-
case WMFW_INFO_TEXT:
1562-
region_name = "Information";
1563-
text = kzalloc(le32_to_cpu(region->len) + 1,
1564-
GFP_KERNEL);
1565-
break;
15661561
case WMFW_ABSOLUTE:
15671562
region_name = "Absolute";
15681563
reg = offset;
@@ -1596,13 +1591,6 @@ static int cs_dsp_load(struct cs_dsp *dsp, const struct firmware *firmware,
15961591
regions, le32_to_cpu(region->len), offset,
15971592
region_name);
15981593

1599-
if (text) {
1600-
memcpy(text, region->data, le32_to_cpu(region->len));
1601-
cs_dsp_info(dsp, "%s: %s\n", file, text);
1602-
kfree(text);
1603-
text = NULL;
1604-
}
1605-
16061594
if (reg) {
16071595
buf = cs_dsp_buf_alloc(region->data,
16081596
le32_to_cpu(region->len),
@@ -1644,7 +1632,6 @@ static int cs_dsp_load(struct cs_dsp *dsp, const struct firmware *firmware,
16441632
out_fw:
16451633
regmap_async_complete(regmap);
16461634
cs_dsp_buf_free(&buf_list);
1647-
kfree(text);
16481635

16491636
if (ret == -EOVERFLOW)
16501637
cs_dsp_err(dsp, "%s: file content overflows file data\n", file);
@@ -2177,7 +2164,6 @@ static int cs_dsp_load_coeff(struct cs_dsp *dsp, const struct firmware *firmware
21772164
struct cs_dsp_alg_region *alg_region;
21782165
const char *region_name;
21792166
int ret, pos, blocks, type, offset, reg, version;
2180-
char *text = NULL;
21812167
struct cs_dsp_buf *buf;
21822168

21832169
if (!firmware)
@@ -2246,7 +2232,8 @@ static int cs_dsp_load_coeff(struct cs_dsp *dsp, const struct firmware *firmware
22462232
region_name = "Unknown";
22472233
switch (type) {
22482234
case (WMFW_NAME_TEXT << 8):
2249-
text = kzalloc(le32_to_cpu(blk->len) + 1, GFP_KERNEL);
2235+
cs_dsp_info(dsp, "%s: %.*s\n", dsp->fw_name,
2236+
min(le32_to_cpu(blk->len), 100), blk->data);
22502237
break;
22512238
case (WMFW_INFO_TEXT << 8):
22522239
case (WMFW_METADATA << 8):
@@ -2318,13 +2305,6 @@ static int cs_dsp_load_coeff(struct cs_dsp *dsp, const struct firmware *firmware
23182305
break;
23192306
}
23202307

2321-
if (text) {
2322-
memcpy(text, blk->data, le32_to_cpu(blk->len));
2323-
cs_dsp_info(dsp, "%s: %s\n", dsp->fw_name, text);
2324-
kfree(text);
2325-
text = NULL;
2326-
}
2327-
23282308
if (reg) {
23292309
buf = cs_dsp_buf_alloc(blk->data,
23302310
le32_to_cpu(blk->len),
@@ -2364,7 +2344,6 @@ static int cs_dsp_load_coeff(struct cs_dsp *dsp, const struct firmware *firmware
23642344
out_fw:
23652345
regmap_async_complete(regmap);
23662346
cs_dsp_buf_free(&buf_list);
2367-
kfree(text);
23682347

23692348
if (ret == -EOVERFLOW)
23702349
cs_dsp_err(dsp, "%s: file content overflows file data\n", file);

0 commit comments

Comments
 (0)