tools headers UAPI: Sync kvm.h headers with the kernel sources
[linux-2.6-microblaze.git] / mm / zswap.c
index 182f6ad..578d9f2 100644 (file)
@@ -935,13 +935,19 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
        struct scatterlist input, output;
        struct crypto_acomp_ctx *acomp_ctx;
 
-       u8 *src;
+       u8 *src, *tmp = NULL;
        unsigned int dlen;
        int ret;
        struct writeback_control wbc = {
                .sync_mode = WB_SYNC_NONE,
        };
 
+       if (!zpool_can_sleep_mapped(pool)) {
+               tmp = kmalloc(PAGE_SIZE, GFP_ATOMIC);
+               if (!tmp)
+                       return -ENOMEM;
+       }
+
        /* extract swpentry from data */
        zhdr = zpool_map_handle(pool, handle, ZPOOL_MM_RO);
        swpentry = zhdr->swpentry; /* here */
@@ -955,6 +961,7 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
                /* entry was invalidated */
                spin_unlock(&tree->lock);
                zpool_unmap_handle(pool, handle);
+               kfree(tmp);
                return 0;
        }
        spin_unlock(&tree->lock);
@@ -979,6 +986,14 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
                dlen = PAGE_SIZE;
                src = (u8 *)zhdr + sizeof(struct zswap_header);
 
+               if (!zpool_can_sleep_mapped(pool)) {
+
+                       memcpy(tmp, src, entry->length);
+                       src = tmp;
+
+                       zpool_unmap_handle(pool, handle);
+               }
+
                mutex_lock(acomp_ctx->mutex);
                sg_init_one(&input, src, entry->length);
                sg_init_table(&output, 1);
@@ -1022,10 +1037,10 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
 
        /*
        * if we get here due to ZSWAP_SWAPCACHE_EXIST
-       * a load may happening concurrently
-       * it is safe and okay to not free the entry
+       * a load may be happening concurrently.
+       * it is safe and okay to not free the entry.
        * if we free the entry in the following put
-       * it it either okay to return !0
+       * it is also okay to return !0
        */
 fail:
        spin_lock(&tree->lock);
@@ -1033,7 +1048,11 @@ fail:
        spin_unlock(&tree->lock);
 
 end:
-       zpool_unmap_handle(pool, handle);
+       if (zpool_can_sleep_mapped(pool))
+               zpool_unmap_handle(pool, handle);
+       else
+               kfree(tmp);
+
        return ret;
 }
 
@@ -1235,7 +1254,7 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
        struct zswap_entry *entry;
        struct scatterlist input, output;
        struct crypto_acomp_ctx *acomp_ctx;
-       u8 *src, *dst;
+       u8 *src, *dst, *tmp;
        unsigned int dlen;
        int ret;
 
@@ -1253,15 +1272,33 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
                dst = kmap_atomic(page);
                zswap_fill_page(dst, entry->value);
                kunmap_atomic(dst);
+               ret = 0;
                goto freeentry;
        }
 
+       if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
+
+               tmp = kmalloc(entry->length, GFP_ATOMIC);
+               if (!tmp) {
+                       ret = -ENOMEM;
+                       goto freeentry;
+               }
+       }
+
        /* decompress */
        dlen = PAGE_SIZE;
        src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO);
        if (zpool_evictable(entry->pool->zpool))
                src += sizeof(struct zswap_header);
 
+       if (!zpool_can_sleep_mapped(entry->pool->zpool)) {
+
+               memcpy(tmp, src, entry->length);
+               src = tmp;
+
+               zpool_unmap_handle(entry->pool->zpool, entry->handle);
+       }
+
        acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
        mutex_lock(acomp_ctx->mutex);
        sg_init_one(&input, src, entry->length);
@@ -1271,7 +1308,11 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
        ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
        mutex_unlock(acomp_ctx->mutex);
 
-       zpool_unmap_handle(entry->pool->zpool, entry->handle);
+       if (zpool_can_sleep_mapped(entry->pool->zpool))
+               zpool_unmap_handle(entry->pool->zpool, entry->handle);
+       else
+               kfree(tmp);
+
        BUG_ON(ret);
 
 freeentry:
@@ -1279,7 +1320,7 @@ freeentry:
        zswap_entry_put(tree, entry);
        spin_unlock(&tree->lock);
 
-       return 0;
+       return ret;
 }
 
 /* frees an entry in zswap */