Skip to content

Commit d125a75

Browse files
committed
UBI: do not allocate the memory unnecessarily
UBI reserves an LEB sized buffer for various needs. We can use this buffer while scanning, instead of allocating another one. This patch was originally created by Jan Luebbe <jlu@pengutronix.de>, but then he dropped it and I picked up and tweaked a little bit. Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
1 parent 6a059ab commit d125a75

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

drivers/mtd/ubi/attach.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
322322
int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
323323
int pnum, const struct ubi_vid_hdr *vid_hdr)
324324
{
325-
void *buf;
326325
int len, err, second_is_newer, bitflips = 0, corrupted = 0;
327326
uint32_t data_crc, crc;
328327
struct ubi_vid_hdr *vh = NULL;
@@ -393,18 +392,14 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
393392
/* Read the data of the copy and check the CRC */
394393

395394
len = be32_to_cpu(vid_hdr->data_size);
396-
buf = vmalloc(len);
397-
if (!buf) {
398-
err = -ENOMEM;
399-
goto out_free_vidh;
400-
}
401395

402-
err = ubi_io_read_data(ubi, buf, pnum, 0, len);
396+
mutex_lock(&ubi->buf_mutex);
397+
err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len);
403398
if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
404-
goto out_free_buf;
399+
goto out_unlock;
405400

406401
data_crc = be32_to_cpu(vid_hdr->data_crc);
407-
crc = crc32(UBI_CRC32_INIT, buf, len);
402+
crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len);
408403
if (crc != data_crc) {
409404
dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
410405
pnum, crc, data_crc);
@@ -415,8 +410,8 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
415410
dbg_bld("PEB %d CRC is OK", pnum);
416411
bitflips = !!err;
417412
}
413+
mutex_unlock(&ubi->buf_mutex);
418414

419-
vfree(buf);
420415
ubi_free_vid_hdr(ubi, vh);
421416

422417
if (second_is_newer)
@@ -426,8 +421,8 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
426421

427422
return second_is_newer | (bitflips << 1) | (corrupted << 2);
428423

429-
out_free_buf:
430-
vfree(buf);
424+
out_unlock:
425+
mutex_unlock(&ubi->buf_mutex);
431426
out_free_vidh:
432427
ubi_free_vid_hdr(ubi, vh);
433428
return err;

0 commit comments

Comments
 (0)