Whamcloud - gitweb
LU-8602 gss: get rid of cfs_crypto_hash_desc
[fs/lustre-release.git] / lustre / ptlrpc / sec_bulk.c
index 20e9f4f..db87414 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -40,7 +36,7 @@
 
 #define DEBUG_SUBSYSTEM S_SEC
 
-#include <libcfs/libcfs.h>
+#include <libcfs/linux/linux-mem.h>
 
 #include <obd.h>
 #include <obd_cksum.h>
 
 #include "ptlrpc_internal.h"
 
+static int mult = 20 - PAGE_SHIFT;
+static int enc_pool_max_memory_mb;
+module_param(enc_pool_max_memory_mb, int, 0644);
+MODULE_PARM_DESC(enc_pool_max_memory_mb,
+                "Encoding pool max memory (MB), 1/8 of total physical memory by default");
+
 /****************************************
  * bulk encryption page pools           *
  ****************************************/
 
 
-#define PTRS_PER_PAGE   (PAGE_CACHE_SIZE / sizeof(void *))
+#define PTRS_PER_PAGE   (PAGE_SIZE / sizeof(void *))
 #define PAGES_PER_POOL  (PTRS_PER_PAGE)
 
 #define IDLE_IDX_MAX            (100)
@@ -91,8 +93,8 @@ static struct ptlrpc_enc_page_pool {
         unsigned long    epp_idle_idx;
 
         /* last shrink time due to mem tight */
-        long             epp_last_shrink;
-        long             epp_last_access;
+       time64_t        epp_last_shrink;
+       time64_t        epp_last_access;
 
         /*
          * in-pool pages bookkeeping
@@ -112,9 +114,10 @@ static struct ptlrpc_enc_page_pool {
         unsigned long    epp_st_missings;       /* # of cache missing */
         unsigned long    epp_st_lowfree;        /* lowest free pages reached */
         unsigned int     epp_st_max_wqlen;      /* highest waitqueue length */
-        cfs_time_t       epp_st_max_wait;       /* in jeffies */
+       ktime_t         epp_st_max_wait;        /* in nanoseconds */
+       unsigned long    epp_st_outofmem;       /* # of out of mem requests */
        /*
-        * pointers to pools
+        * pointers to pools, may be vmalloc'd
         */
        struct page    ***epp_pools;
 } page_pools;
@@ -131,53 +134,48 @@ static struct shrinker *pools_shrinker;
  */
 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
 {
-        int     rc;
-
        spin_lock(&page_pools.epp_lock);
 
-       rc = seq_printf(m,
-                      "physical pages:          %lu\n"
-                      "pages per pool:          %lu\n"
-                      "max pages:               %lu\n"
-                      "max pools:               %u\n"
-                      "total pages:             %lu\n"
-                      "total free:              %lu\n"
-                      "idle index:              %lu/100\n"
-                      "last shrink:             %lds\n"
-                      "last access:             %lds\n"
-                      "max pages reached:       %lu\n"
-                      "grows:                   %u\n"
-                      "grows failure:           %u\n"
-                      "shrinks:                 %u\n"
-                      "cache access:            %lu\n"
-                      "cache missing:           %lu\n"
-                      "low free mark:           %lu\n"
-                      "max waitqueue depth:     %u\n"
-                     "max wait time:           "CFS_TIME_T"/%lu\n"
-                      ,
-                     totalram_pages,
-                      PAGES_PER_POOL,
-                      page_pools.epp_max_pages,
-                      page_pools.epp_max_pools,
-                      page_pools.epp_total_pages,
-                      page_pools.epp_free_pages,
-                      page_pools.epp_idle_idx,
-                      cfs_time_current_sec() - page_pools.epp_last_shrink,
-                      cfs_time_current_sec() - page_pools.epp_last_access,
-                      page_pools.epp_st_max_pages,
-                      page_pools.epp_st_grows,
-                      page_pools.epp_st_grow_fails,
-                      page_pools.epp_st_shrinks,
-                      page_pools.epp_st_access,
-                     page_pools.epp_st_missings,
-                     page_pools.epp_st_lowfree,
-                     page_pools.epp_st_max_wqlen,
-                     page_pools.epp_st_max_wait,
-                     msecs_to_jiffies(MSEC_PER_SEC)
-                    );
+       seq_printf(m, "physical pages:          %lu\n"
+                  "pages per pool:          %lu\n"
+                  "max pages:               %lu\n"
+                  "max pools:               %u\n"
+                  "total pages:             %lu\n"
+                  "total free:              %lu\n"
+                  "idle index:              %lu/100\n"
+                  "last shrink:             %llds\n"
+                  "last access:             %llds\n"
+                  "max pages reached:       %lu\n"
+                  "grows:                   %u\n"
+                  "grows failure:           %u\n"
+                  "shrinks:                 %u\n"
+                  "cache access:            %lu\n"
+                  "cache missing:           %lu\n"
+                  "low free mark:           %lu\n"
+                  "max waitqueue depth:     %u\n"
+                  "max wait time ms:        %lld\n"
+                  "out of mem:              %lu\n",
+                  totalram_pages, PAGES_PER_POOL,
+                  page_pools.epp_max_pages,
+                  page_pools.epp_max_pools,
+                  page_pools.epp_total_pages,
+                  page_pools.epp_free_pages,
+                  page_pools.epp_idle_idx,
+                  ktime_get_seconds() - page_pools.epp_last_shrink,
+                  ktime_get_seconds() - page_pools.epp_last_access,
+                  page_pools.epp_st_max_pages,
+                  page_pools.epp_st_grows,
+                  page_pools.epp_st_grow_fails,
+                  page_pools.epp_st_shrinks,
+                  page_pools.epp_st_access,
+                  page_pools.epp_st_missings,
+                  page_pools.epp_st_lowfree,
+                  page_pools.epp_st_max_wqlen,
+                  ktime_to_ms(page_pools.epp_st_max_wait),
+                  page_pools.epp_st_outofmem);
 
        spin_unlock(&page_pools.epp_lock);
-       return rc;
+       return 0;
 }
 
 static void enc_pools_release_free_pages(long npages)
@@ -219,7 +217,7 @@ static void enc_pools_release_free_pages(long npages)
         /* free unused pools */
         while (p_idx_max1 < p_idx_max2) {
                 LASSERT(page_pools.epp_pools[p_idx_max2]);
-               OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_CACHE_SIZE);
+               OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_SIZE);
                 page_pools.epp_pools[p_idx_max2] = NULL;
                 p_idx_max2--;
         }
@@ -235,7 +233,7 @@ static unsigned long enc_pools_shrink_count(struct shrinker *s,
         * if no pool access for a long time, we consider it's fully idle.
         * a little race here is fine.
         */
-       if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
+       if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
                     CACHE_QUIESCENT_PERIOD)) {
                spin_lock(&page_pools.epp_lock);
                page_pools.epp_idle_idx = IDLE_IDX_MAX;
@@ -243,7 +241,8 @@ static unsigned long enc_pools_shrink_count(struct shrinker *s,
        }
 
        LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
-       return max((int)page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
+       return (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES) ? 0 :
+               (page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES) *
                (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
 }
 
@@ -254,7 +253,10 @@ static unsigned long enc_pools_shrink_scan(struct shrinker *s,
                                           struct shrink_control *sc)
 {
        spin_lock(&page_pools.epp_lock);
-       sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
+       if (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES)
+               sc->nr_to_scan = 0;
+       else
+               sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
                              page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
        if (sc->nr_to_scan > 0) {
                enc_pools_release_free_pages(sc->nr_to_scan);
@@ -262,7 +264,7 @@ static unsigned long enc_pools_shrink_scan(struct shrinker *s,
                       (long)sc->nr_to_scan, page_pools.epp_free_pages);
 
                page_pools.epp_st_shrinks++;
-               page_pools.epp_last_shrink = cfs_time_current_sec();
+               page_pools.epp_last_shrink = ktime_get_seconds();
        }
        spin_unlock(&page_pools.epp_lock);
 
@@ -270,7 +272,7 @@ static unsigned long enc_pools_shrink_scan(struct shrinker *s,
         * if no pool access for a long time, we consider it's fully idle.
         * a little race here is fine.
         */
-       if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
+       if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
                     CACHE_QUIESCENT_PERIOD)) {
                spin_lock(&page_pools.epp_lock);
                page_pools.epp_idle_idx = IDLE_IDX_MAX;
@@ -325,7 +327,7 @@ static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
                                        cleaned++;
                                }
                        }
-                       OBD_FREE(pools[i], PAGE_CACHE_SIZE);
+                       OBD_FREE(pools[i], PAGE_SIZE);
                        pools[i] = NULL;
                }
        }
@@ -445,7 +447,7 @@ static int enc_pools_add_pages(int npages)
                 goto out;
 
        for (i = 0; i < npools; i++) {
-               OBD_ALLOC(pools[i], PAGE_CACHE_SIZE);
+               OBD_ALLOC(pools[i], PAGE_SIZE);
                if (pools[i] == NULL)
                        goto out_pools;
 
@@ -487,60 +489,75 @@ static inline void enc_pools_wakeup(void)
        }
 }
 
-static int enc_pools_should_grow(int page_needed, long now)
+static int enc_pools_should_grow(int page_needed, time64_t now)
 {
-        /* don't grow if someone else is growing the pools right now,
-         * or the pools has reached its full capacity
-         */
-        if (page_pools.epp_growing ||
-            page_pools.epp_total_pages == page_pools.epp_max_pages)
-                return 0;
+       /* don't grow if someone else is growing the pools right now,
+        * or the pools has reached its full capacity
+        */
+       if (page_pools.epp_growing ||
+           page_pools.epp_total_pages == page_pools.epp_max_pages)
+               return 0;
 
-        /* if total pages is not enough, we need to grow */
-        if (page_pools.epp_total_pages < page_needed)
-                return 1;
+       /* if total pages is not enough, we need to grow */
+       if (page_pools.epp_total_pages < page_needed)
+               return 1;
 
-        /*
-         * we wanted to return 0 here if there was a shrink just happened
-         * moment ago, but this may cause deadlock if both client and ost
-         * live on single node.
-         */
-#if 0
-        if (now - page_pools.epp_last_shrink < 2)
-                return 0;
-#endif
+       /*
+        * we wanted to return 0 here if there was a shrink just
+        * happened a moment ago, but this may cause deadlock if both
+        * client and ost live on single node.
+        */
 
-        /*
-         * here we perhaps need consider other factors like wait queue
-         * length, idle index, etc. ?
-         */
+       /*
+        * here we perhaps need consider other factors like wait queue
+        * length, idle index, etc. ?
+        */
 
-        /* grow the pools in any other cases */
-        return 1;
+       /* grow the pools in any other cases */
+       return 1;
 }
 
 /*
+ * Export the number of free pages in the pool
+ */
+int get_free_pages_in_pool(void)
+{
+       return page_pools.epp_free_pages;
+}
+EXPORT_SYMBOL(get_free_pages_in_pool);
+
+/*
+ * Let outside world know if enc_pool full capacity is reached
+ */
+int pool_is_at_full_capacity(void)
+{
+       return (page_pools.epp_total_pages == page_pools.epp_max_pages);
+}
+EXPORT_SYMBOL(pool_is_at_full_capacity);
+
+/*
  * we allocate the requested pages atomically.
  */
 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
 {
-       wait_queue_t  waitlink;
-       unsigned long   this_idle = -1;
-       cfs_time_t      tick = 0;
-       long            now;
-       int             p_idx, g_idx;
-       int             i;
-
+       wait_queue_entry_t waitlink;
+       unsigned long this_idle = -1;
+       u64 tick_ns = 0;
+       time64_t now;
+       int p_idx, g_idx;
+       int i;
+
+       LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
        LASSERT(desc->bd_iov_count > 0);
        LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
 
        /* resent bulk, enc iov might have been allocated previously */
-       if (desc->bd_enc_iov != NULL)
+       if (GET_ENC_KIOV(desc) != NULL)
                return 0;
 
-       OBD_ALLOC(desc->bd_enc_iov,
-                 desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
-       if (desc->bd_enc_iov == NULL)
+       OBD_ALLOC_LARGE(GET_ENC_KIOV(desc),
+                 desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
+       if (GET_ENC_KIOV(desc) == NULL)
                return -ENOMEM;
 
        spin_lock(&page_pools.epp_lock);
@@ -548,10 +565,10 @@ int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
        page_pools.epp_st_access++;
 again:
        if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
-               if (tick == 0)
-                       tick = cfs_time_current();
+               if (tick_ns == 0)
+                       tick_ns = ktime_get_ns();
 
-               now = cfs_time_current_sec();
+               now = ktime_get_real_seconds();
 
                page_pools.epp_st_missings++;
                page_pools.epp_pages_short += desc->bd_iov_count;
@@ -567,21 +584,37 @@ again:
 
                        enc_pools_wakeup();
                } else {
-                       if (++page_pools.epp_waitqlen >
-                           page_pools.epp_st_max_wqlen)
-                               page_pools.epp_st_max_wqlen =
-                                               page_pools.epp_waitqlen;
-
-                       set_current_state(TASK_UNINTERRUPTIBLE);
-                       init_waitqueue_entry_current(&waitlink);
-                       add_wait_queue(&page_pools.epp_waitq, &waitlink);
-
-                       spin_unlock(&page_pools.epp_lock);
-                       waitq_wait(&waitlink, TASK_UNINTERRUPTIBLE);
-                       remove_wait_queue(&page_pools.epp_waitq, &waitlink);
-                       LASSERT(page_pools.epp_waitqlen > 0);
-                       spin_lock(&page_pools.epp_lock);
-                       page_pools.epp_waitqlen--;
+                       if (page_pools.epp_growing) {
+                               if (++page_pools.epp_waitqlen >
+                                   page_pools.epp_st_max_wqlen)
+                                       page_pools.epp_st_max_wqlen =
+                                                       page_pools.epp_waitqlen;
+
+                               set_current_state(TASK_UNINTERRUPTIBLE);
+                               init_waitqueue_entry(&waitlink, current);
+                               add_wait_queue(&page_pools.epp_waitq,
+                                              &waitlink);
+
+                               spin_unlock(&page_pools.epp_lock);
+                               schedule();
+                               remove_wait_queue(&page_pools.epp_waitq,
+                                                 &waitlink);
+                               LASSERT(page_pools.epp_waitqlen > 0);
+                               spin_lock(&page_pools.epp_lock);
+                               page_pools.epp_waitqlen--;
+                       } else {
+                               /* ptlrpcd thread should not sleep in that case,
+                                * or deadlock may occur!
+                                * Instead, return -ENOMEM so that upper layers
+                                * will put request back in queue. */
+                               page_pools.epp_st_outofmem++;
+                               spin_unlock(&page_pools.epp_lock);
+                               OBD_FREE_LARGE(GET_ENC_KIOV(desc),
+                                              desc->bd_iov_count *
+                                               sizeof(*GET_ENC_KIOV(desc)));
+                               GET_ENC_KIOV(desc) = NULL;
+                               return -ENOMEM;
+                       }
                }
 
                LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
@@ -591,12 +624,13 @@ again:
                goto again;
        }
 
-        /* record max wait time */
-        if (unlikely(tick != 0)) {
-                tick = cfs_time_current() - tick;
-                if (tick > page_pools.epp_st_max_wait)
-                        page_pools.epp_st_max_wait = tick;
-        }
+       /* record max wait time */
+       if (unlikely(tick_ns)) {
+               ktime_t tick = ktime_sub_ns(ktime_get(), tick_ns);
+
+               if (ktime_after(tick, page_pools.epp_st_max_wait))
+                       page_pools.epp_st_max_wait = tick;
+       }
 
         /* proceed with rest of allocation */
         page_pools.epp_free_pages -= desc->bd_iov_count;
@@ -604,17 +638,17 @@ again:
         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
 
-        for (i = 0; i < desc->bd_iov_count; i++) {
-                LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
-                desc->bd_enc_iov[i].kiov_page =
-                                        page_pools.epp_pools[p_idx][g_idx];
-                page_pools.epp_pools[p_idx][g_idx] = NULL;
-
-                if (++g_idx == PAGES_PER_POOL) {
-                        p_idx++;
-                        g_idx = 0;
-                }
-        }
+       for (i = 0; i < desc->bd_iov_count; i++) {
+               LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
+               BD_GET_ENC_KIOV(desc, i).kiov_page =
+                      page_pools.epp_pools[p_idx][g_idx];
+               page_pools.epp_pools[p_idx][g_idx] = NULL;
+
+               if (++g_idx == PAGES_PER_POOL) {
+                       p_idx++;
+                       g_idx = 0;
+               }
+       }
 
         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
@@ -630,7 +664,7 @@ again:
                                    this_idle) /
                                   (IDLE_IDX_WEIGHT + 1);
 
-        page_pools.epp_last_access = cfs_time_current_sec();
+       page_pools.epp_last_access = ktime_get_seconds();
 
        spin_unlock(&page_pools.epp_lock);
        return 0;
@@ -639,48 +673,49 @@ EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
 
 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
 {
-        int     p_idx, g_idx;
-        int     i;
+       int     p_idx, g_idx;
+       int     i;
 
-        if (desc->bd_enc_iov == NULL)
-                return;
+       LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
 
-        LASSERT(desc->bd_iov_count > 0);
+       if (GET_ENC_KIOV(desc) == NULL)
+               return;
+
+       LASSERT(desc->bd_iov_count > 0);
 
        spin_lock(&page_pools.epp_lock);
 
-        p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
-        g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
+       p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
+       g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
 
-        LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
-                page_pools.epp_total_pages);
-        LASSERT(page_pools.epp_pools[p_idx]);
+       LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
+               page_pools.epp_total_pages);
+       LASSERT(page_pools.epp_pools[p_idx]);
 
-        for (i = 0; i < desc->bd_iov_count; i++) {
-                LASSERT(desc->bd_enc_iov[i].kiov_page != NULL);
-                LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
-                LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
+       for (i = 0; i < desc->bd_iov_count; i++) {
+               LASSERT(BD_GET_ENC_KIOV(desc, i).kiov_page != NULL);
+               LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
+               LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
 
-                page_pools.epp_pools[p_idx][g_idx] =
-                                        desc->bd_enc_iov[i].kiov_page;
+               page_pools.epp_pools[p_idx][g_idx] =
+                       BD_GET_ENC_KIOV(desc, i).kiov_page;
 
-                if (++g_idx == PAGES_PER_POOL) {
-                        p_idx++;
-                        g_idx = 0;
-                }
-        }
+               if (++g_idx == PAGES_PER_POOL) {
+                       p_idx++;
+                       g_idx = 0;
+               }
+       }
 
-        page_pools.epp_free_pages += desc->bd_iov_count;
+       page_pools.epp_free_pages += desc->bd_iov_count;
 
-        enc_pools_wakeup();
+       enc_pools_wakeup();
 
        spin_unlock(&page_pools.epp_lock);
 
-       OBD_FREE(desc->bd_enc_iov,
-                desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
-       desc->bd_enc_iov = NULL;
+       OBD_FREE_LARGE(GET_ENC_KIOV(desc),
+                desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
+       GET_ENC_KIOV(desc) = NULL;
 }
-EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
 
 /*
  * we don't do much stuff for add_user/del_user anymore, except adding some
@@ -719,31 +754,32 @@ EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
 
 static inline void enc_pools_alloc(void)
 {
-        LASSERT(page_pools.epp_max_pools);
-        OBD_ALLOC_LARGE(page_pools.epp_pools,
-                        page_pools.epp_max_pools *
-                        sizeof(*page_pools.epp_pools));
+       LASSERT(page_pools.epp_max_pools);
+       OBD_ALLOC_LARGE(page_pools.epp_pools,
+                       page_pools.epp_max_pools *
+                       sizeof(*page_pools.epp_pools));
 }
 
 static inline void enc_pools_free(void)
 {
-        LASSERT(page_pools.epp_max_pools);
-        LASSERT(page_pools.epp_pools);
+       LASSERT(page_pools.epp_max_pools);
+       LASSERT(page_pools.epp_pools);
 
-        OBD_FREE_LARGE(page_pools.epp_pools,
-                       page_pools.epp_max_pools *
-                       sizeof(*page_pools.epp_pools));
+       OBD_FREE_LARGE(page_pools.epp_pools,
+                      page_pools.epp_max_pools *
+                      sizeof(*page_pools.epp_pools));
 }
 
 int sptlrpc_enc_pool_init(void)
 {
        DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
                         enc_pools_shrink_count, enc_pools_shrink_scan);
-       /*
-        * maximum capacity is 1/8 of total physical memory.
-        * is the 1/8 a good number?
-        */
+
        page_pools.epp_max_pages = totalram_pages / 8;
+       if (enc_pool_max_memory_mb > 0 &&
+           enc_pool_max_memory_mb <= (totalram_pages >> mult))
+               page_pools.epp_max_pages = enc_pool_max_memory_mb << mult;
+
        page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
 
        init_waitqueue_head(&page_pools.epp_waitq);
@@ -753,8 +789,8 @@ int sptlrpc_enc_pool_init(void)
         page_pools.epp_growing = 0;
 
         page_pools.epp_idle_idx = 0;
-        page_pools.epp_last_shrink = cfs_time_current_sec();
-        page_pools.epp_last_access = cfs_time_current_sec();
+       page_pools.epp_last_shrink = ktime_get_seconds();
+       page_pools.epp_last_access = ktime_get_seconds();
 
        spin_lock_init(&page_pools.epp_lock);
         page_pools.epp_total_pages = 0;
@@ -768,7 +804,8 @@ int sptlrpc_enc_pool_init(void)
         page_pools.epp_st_missings = 0;
         page_pools.epp_st_lowfree = 0;
         page_pools.epp_st_max_wqlen = 0;
-        page_pools.epp_st_max_wait = 0;
+       page_pools.epp_st_max_wait = ktime_set(0, 0);
+       page_pools.epp_st_outofmem = 0;
 
         enc_pools_alloc();
         if (page_pools.epp_pools == NULL)
@@ -801,15 +838,13 @@ void sptlrpc_enc_pool_fini(void)
 
        if (page_pools.epp_st_access > 0) {
                CDEBUG(D_SEC,
-                      "max pages %lu, grows %u, grow fails %u, shrinks %u, "
-                      "access %lu, missing %lu, max qlen %u, max wait "
-                      CFS_TIME_T"/%lu\n",
+                      "max pages %lu, grows %u, grow fails %u, shrinks %u, access %lu, missing %lu, max qlen %u, max wait ms %lld, out of mem %lu\n",
                       page_pools.epp_st_max_pages, page_pools.epp_st_grows,
                       page_pools.epp_st_grow_fails,
                       page_pools.epp_st_shrinks, page_pools.epp_st_access,
                       page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
-                      page_pools.epp_st_max_wait,
-                      msecs_to_jiffies(MSEC_PER_SEC));
+                      ktime_to_ms(page_pools.epp_st_max_wait),
+                      page_pools.epp_st_outofmem);
        }
 }
 
@@ -828,13 +863,11 @@ const char * sptlrpc_get_hash_name(__u8 hash_alg)
 {
        return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
 }
-EXPORT_SYMBOL(sptlrpc_get_hash_name);
 
 __u8 sptlrpc_get_hash_alg(const char *algname)
 {
        return cfs_crypto_hash_alg(algname);
 }
-EXPORT_SYMBOL(sptlrpc_get_hash_alg);
 
 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
 {
@@ -883,27 +916,30 @@ EXPORT_SYMBOL(bulk_sec_desc_unpack);
 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
                              void *buf, int buflen)
 {
-       struct cfs_crypto_hash_desc     *hdesc;
+       struct ahash_request           *req;
        int                             hashsize;
        unsigned int                    bufsize;
        int                             i, err;
 
+       LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
        LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
        LASSERT(buflen >= 4);
 
-       hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
-       if (IS_ERR(hdesc)) {
+       req = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
+       if (IS_ERR(req)) {
                CERROR("Unable to initialize checksum hash %s\n",
                       cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
-               return PTR_ERR(hdesc);
+               return PTR_ERR(req);
        }
 
        hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
 
        for (i = 0; i < desc->bd_iov_count; i++) {
-               cfs_crypto_hash_update_page(hdesc, desc->bd_iov[i].kiov_page,
-                                 desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK,
-                                 desc->bd_iov[i].kiov_len);
+               cfs_crypto_hash_update_page(req,
+                                 BD_GET_KIOV(desc, i).kiov_page,
+                                 BD_GET_KIOV(desc, i).kiov_offset &
+                                             ~PAGE_MASK,
+                                 BD_GET_KIOV(desc, i).kiov_len);
        }
 
        if (hashsize > buflen) {
@@ -912,13 +948,12 @@ int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
                bufsize = sizeof(hashbuf);
                LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
                         bufsize, hashsize);
-               err = cfs_crypto_hash_final(hdesc, hashbuf, &bufsize);
+               err = cfs_crypto_hash_final(req, hashbuf, &bufsize);
                memcpy(buf, hashbuf, buflen);
        } else {
                bufsize = buflen;
-               err = cfs_crypto_hash_final(hdesc, buf, &bufsize);
+               err = cfs_crypto_hash_final(req, buf, &bufsize);
        }
 
        return err;
 }
-EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);