Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+7 -12
+7 -12
drivers/mtd/ubi/attach.c
··· 322 322 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, 323 323 int pnum, const struct ubi_vid_hdr *vid_hdr) 324 324 { 325 - void *buf; 326 325 int len, err, second_is_newer, bitflips = 0, corrupted = 0; 327 326 uint32_t data_crc, crc; 328 327 struct ubi_vid_hdr *vh = NULL; ··· 392 393 /* Read the data of the copy and check the CRC */ 393 394 394 395 len = be32_to_cpu(vid_hdr->data_size); 395 - buf = vmalloc(len); 396 - if (!buf) { 397 - err = -ENOMEM; 398 - goto out_free_vidh; 399 - } 400 396 401 - err = ubi_io_read_data(ubi, buf, pnum, 0, len); 397 + mutex_lock(&ubi->buf_mutex); 398 + err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); 402 399 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 403 - goto out_free_buf; 400 + goto out_unlock; 404 401 405 402 data_crc = be32_to_cpu(vid_hdr->data_crc); 406 - crc = crc32(UBI_CRC32_INIT, buf, len); 403 + crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len); 407 404 if (crc != data_crc) { 408 405 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", 409 406 pnum, crc, data_crc); ··· 410 415 dbg_bld("PEB %d CRC is OK", pnum); 411 416 bitflips = !!err; 412 417 } 418 + mutex_unlock(&ubi->buf_mutex); 413 419 414 - vfree(buf); 415 420 ubi_free_vid_hdr(ubi, vh); 416 421 417 422 if (second_is_newer) ··· 421 426 422 427 return second_is_newer | (bitflips << 1) | (corrupted << 2); 423 428 424 - out_free_buf: 425 - vfree(buf); 429 + out_unlock: 430 + mutex_unlock(&ubi->buf_mutex); 426 431 out_free_vidh: 427 432 ubi_free_vid_hdr(ubi, vh); 428 433 return err;