UBI: avoid unnecessary division operations
authorKyungmin Park <kyungmin.park@samsung.com>
Thu, 22 May 2008 01:32:18 +0000 (10:32 +0900)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Thu, 24 Jul 2008 10:32:54 +0000 (13:32 +0300)
UBI already checks that @min io size is the power of 2 at io_init.
It is save to use bit operations then.

Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
drivers/mtd/ubi/build.c
drivers/mtd/ubi/cdev.c
drivers/mtd/ubi/eba.c
drivers/mtd/ubi/kapi.c
drivers/mtd/ubi/misc.c
drivers/mtd/ubi/vmt.c
drivers/mtd/ubi/vtbl.c
drivers/mtd/ubi/wl.c

index 961416ac06167350bf689c631a85b0ece7f067ec..ff4425de15276c06a005f49825b637946cf64b6f 100644 (file)
@@ -530,7 +530,11 @@ static int io_init(struct ubi_device *ubi)
        ubi->min_io_size = ubi->mtd->writesize;
        ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
 
-       /* Make sure minimal I/O unit is power of 2 */
+       /*
+        * Make sure minimal I/O unit is power of 2. Note, there is no
+        * fundamental reason for this assumption. It is just an optimization
+        * which allows us to avoid costly division operations.
+        */
        if (!is_power_of_2(ubi->min_io_size)) {
                ubi_err("min. I/O unit (%d) is not power of 2",
                        ubi->min_io_size);
@@ -581,7 +585,7 @@ static int io_init(struct ubi_device *ubi)
        if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
            ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
            ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
-           ubi->leb_start % ubi->min_io_size) {
+           ubi->leb_start & (ubi->min_io_size - 1)) {
                ubi_err("bad VID header (%d) or data offsets (%d)",
                        ubi->vid_hdr_offset, ubi->leb_start);
                return -EINVAL;
index 89193ba9451e78e771740724fbb98ff04135aeb7..0cdaf9fba7b09160095119c2de729f143e8979ef 100644 (file)
@@ -295,7 +295,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
        off = do_div(tmp, vol->usable_leb_size);
        lnum = tmp;
 
-       if (off % ubi->min_io_size) {
+       if (off & (ubi->min_io_size - 1)) {
                dbg_err("unaligned position");
                return -EINVAL;
        }
@@ -304,7 +304,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
                count_save = count = vol->used_bytes - *offp;
 
        /* We can write only in fractions of the minimum I/O unit */
-       if (count % ubi->min_io_size) {
+       if (count & (ubi->min_io_size - 1)) {
                dbg_err("unaligned write length");
                return -EINVAL;
        }
@@ -564,7 +564,7 @@ static int verify_mkvol_req(const struct ubi_device *ubi,
        if (req->alignment > ubi->leb_size)
                goto bad;
 
-       n = req->alignment % ubi->min_io_size;
+       n = req->alignment & (ubi->min_io_size - 1);
        if (req->alignment != 1 && n)
                goto bad;
 
index 7ce91ca742b136c7b60ac6db25ffb3d3771d2d6b..37d7784479436842e2360163bb59003667a7af5a 100644 (file)
@@ -752,7 +752,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
                /* If this is the last LEB @len may be unaligned */
                len = ALIGN(data_size, ubi->min_io_size);
        else
-               ubi_assert(len % ubi->min_io_size == 0);
+               ubi_assert(!(len & (ubi->min_io_size - 1)));
 
        vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
        if (!vid_hdr)
index a70d58823f8d116804c4c05bfaf807cead9015e5..51508832566df7c1ad439185407f672ddc4a5201 100644 (file)
@@ -397,8 +397,8 @@ int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
                return -EROFS;
 
        if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
-           offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
-           len % ubi->min_io_size)
+           offset + len > vol->usable_leb_size ||
+           offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
                return -EINVAL;
 
        if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
@@ -447,7 +447,7 @@ int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
                return -EROFS;
 
        if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
-           len > vol->usable_leb_size || len % ubi->min_io_size)
+           len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
                return -EINVAL;
 
        if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
index 93e05281201292c4641d95593eb03158797d29a9..22ad314029452935dfffff32165d49047da2603d 100644 (file)
@@ -37,7 +37,7 @@ int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
 {
        int i;
 
-       ubi_assert(length % ubi->min_io_size == 0);
+       ubi_assert(!(length & (ubi->min_io_size - 1)));
 
        for (i = length - 1; i >= 0; i--)
                if (((const uint8_t *)buf)[i] != 0xFF)
index 5be58d85c6393c53c2be0f4693ff8a9bd13e40b3..7402025ded94a0418478340a6ba478926d1d7415 100644 (file)
@@ -727,7 +727,7 @@ static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
                goto fail;
        }
 
-       n = vol->alignment % ubi->min_io_size;
+       n = vol->alignment & (ubi->min_io_size - 1);
        if (vol->alignment != 1 && n) {
                ubi_err("alignment is not multiple of min I/O unit");
                goto fail;
index 42a7815086b7fe3abc28e701607716e3bc106e62..d9af11a8682ba3dce1b250307b2e7c33bff387c8 100644 (file)
@@ -170,7 +170,7 @@ static int vtbl_check(const struct ubi_device *ubi,
                        goto bad;
                }
 
-               n = alignment % ubi->min_io_size;
+               n = alignment & (ubi->min_io_size - 1);
                if (alignment != 1 && n) {
                        err = 5;
                        goto bad;
@@ -684,14 +684,13 @@ static int check_scanning_info(const struct ubi_device *ubi,
                return -EINVAL;
        }
 
-       if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&&
+       if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
            si->highest_vol_id < UBI_INTERNAL_VOL_START) {
                ubi_err("too large volume ID %d found by scanning",
                        si->highest_vol_id);
                return -EINVAL;
        }
 
-
        for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
                cond_resched();
 
index a471a491f0ab0c2840543594966640487e7bacd4..cc8fe2934d2ba0441c1f7f2bf161703c4ed5099d 100644 (file)
@@ -1368,7 +1368,7 @@ int ubi_thread(void *u)
                int err;
 
                if (kthread_should_stop())
-                       goto out;
+                       break;
 
                if (try_to_freeze())
                        continue;
@@ -1403,7 +1403,6 @@ int ubi_thread(void *u)
                cond_resched();
        }
 
-out:
        dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
        return 0;
 }