zram: optimize memory operations with clear_page()/copy_page()
[deliverable/linux.git] / drivers / staging / zram / zram_drv.c
index 3335a6ce1784986cdb6e48c7c1dc1850107b4d78..4b30fe5816f0f80f608681406986b142aa4d8985 100644 (file)
@@ -37,7 +37,7 @@
 
 /* Globals */
 static int zram_major;
-struct zram *zram_devices;
+static struct zram *zram_devices;
 
 /* Module params (documentation at end) */
 static unsigned int num_devices = 1;
@@ -128,23 +128,26 @@ static void zram_free_page(struct zram *zram, size_t index)
        meta->table[index].size = 0;
 }
 
+static inline int is_partial_io(struct bio_vec *bvec)
+{
+       return bvec->bv_len != PAGE_SIZE;
+}
+
 static void handle_zero_page(struct bio_vec *bvec)
 {
        struct page *page = bvec->bv_page;
        void *user_mem;
 
        user_mem = kmap_atomic(page);
-       memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
+       if (is_partial_io(bvec))
+               memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
+       else
+               clear_page(user_mem);
        kunmap_atomic(user_mem);
 
        flush_dcache_page(page);
 }
 
-static inline int is_partial_io(struct bio_vec *bvec)
-{
-       return bvec->bv_len != PAGE_SIZE;
-}
-
 static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
 {
        int ret = LZO_E_OK;
@@ -154,13 +157,13 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
        unsigned long handle = meta->table[index].handle;
 
        if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
-               memset(mem, 0, PAGE_SIZE);
+               clear_page(mem);
                return 0;
        }
 
        cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
        if (meta->table[index].size == PAGE_SIZE)
-               memcpy(mem, cmem, PAGE_SIZE);
+               copy_page(mem, cmem);
        else
                ret = lzo1x_decompress_safe(cmem, meta->table[index].size,
                                                mem, &clen);
@@ -272,8 +275,6 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
 
        if (page_zero_filled(uncmem)) {
                kunmap_atomic(user_mem);
-               if (is_partial_io(bvec))
-                       kfree(uncmem);
                zram->stats.pages_zero++;
                zram_set_flag(meta, index, ZRAM_ZERO);
                ret = 0;
@@ -311,11 +312,13 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
        }
        cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
 
-       if ((clen == PAGE_SIZE) && !is_partial_io(bvec))
+       if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
                src = kmap_atomic(page);
-       memcpy(cmem, src, clen);
-       if ((clen == PAGE_SIZE) && !is_partial_io(bvec))
+               copy_page(cmem, src);
                kunmap_atomic(src);
+       } else {
+               memcpy(cmem, src, clen);
+       }
 
        zs_unmap_object(meta->mem_pool, handle);
 
@@ -422,13 +425,20 @@ out:
  */
 static inline int valid_io_request(struct zram *zram, struct bio *bio)
 {
-       if (unlikely(
-               (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
-               (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
-               (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
+       u64 start, end, bound;
+       
+       /* unaligned request */
+       if (unlikely(bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
+               return 0;
+       if (unlikely(bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
+               return 0;
 
+       start = bio->bi_sector;
+       end = start + (bio->bi_size >> SECTOR_SHIFT);
+       bound = zram->disksize >> SECTOR_SHIFT;
+       /* out of range range */
+       if (unlikely(start >= bound || end >= bound || start > end))
                return 0;
-       }
 
        /* I/O request is valid */
        return 1;
@@ -674,11 +684,6 @@ static void destroy_device(struct zram *zram)
                blk_cleanup_queue(zram->queue);
 }
 
-unsigned int zram_get_num_devices(void)
-{
-       return num_devices;
-}
-
 static int __init zram_init(void)
 {
        int ret, dev_id;
This page took 0.03191 seconds and 5 git commands to generate.