4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/sec_bulk.c
34 * Author: Eric Mei <ericm@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_SEC
39 #include <libcfs/linux/linux-mem.h>
42 #include <obd_cksum.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_net.h>
46 #include <lustre_import.h>
47 #include <lustre_dlm.h>
48 #include <lustre_sec.h>
50 #include "ptlrpc_internal.h"
52 static int mult = 20 - PAGE_SHIFT;
53 static int enc_pool_max_memory_mb;
54 module_param(enc_pool_max_memory_mb, int, 0644);
55 MODULE_PARM_DESC(enc_pool_max_memory_mb,
56 "Encoding pool max memory (MB), 1/8 of total physical memory by default");
59 * bulk encryption page pools
62 #define PTRS_PER_PAGE (PAGE_SIZE / sizeof(void *))
63 #define PAGES_PER_POOL (PTRS_PER_PAGE)
65 #define IDLE_IDX_MAX (100)
66 #define IDLE_IDX_WEIGHT (3)
68 #define CACHE_QUIESCENT_PERIOD (20)
70 static struct ptlrpc_enc_page_pool {
71 unsigned long epp_max_pages; /* maximum pages can hold, const */
72 unsigned int epp_max_pools; /* number of pools, const */
75 * wait queue in case of not enough free pages.
77 wait_queue_head_t epp_waitq; /* waiting threads */
78 unsigned int epp_waitqlen; /* wait queue length */
79 unsigned long epp_pages_short; /* # of pages wanted of in-q users */
80 unsigned int epp_growing:1; /* during adding pages */
83 * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
84 * this is counted based on each time when getting pages from
85 * the pools, not based on time. which means in case that system
86 * is idled for a while but the idle_idx might still be low if no
87 * activities happened in the pools.
89 unsigned long epp_idle_idx;
91 /* last shrink time due to mem tight */
92 time64_t epp_last_shrink;
93 time64_t epp_last_access;
95 /* in-pool pages bookkeeping */
96 spinlock_t epp_lock; /* protect following fields */
97 unsigned long epp_total_pages; /* total pages in pools */
98 unsigned long epp_free_pages; /* current pages available */
101 unsigned long epp_st_max_pages; /* # of pages ever reached */
102 unsigned int epp_st_grows; /* # of grows */
103 unsigned int epp_st_grow_fails; /* # of add pages failures */
104 unsigned int epp_st_shrinks; /* # of shrinks */
105 unsigned long epp_st_access; /* # of access */
106 unsigned long epp_st_missings; /* # of cache missing */
107 unsigned long epp_st_lowfree; /* lowest free pages reached */
108 unsigned int epp_st_max_wqlen; /* highest waitqueue length */
109 ktime_t epp_st_max_wait; /* in nanoseconds */
110 unsigned long epp_st_outofmem; /* # of out of mem requests */
112 * pointers to pools, may be vmalloc'd
114 struct page ***epp_pools;
120 static const int pools_shrinker_seeks = DEFAULT_SEEKS;
121 static struct shrinker *pools_shrinker;
125 * /proc/fs/lustre/sptlrpc/encrypt_page_pools
127 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
129 spin_lock(&page_pools.epp_lock);
131 seq_printf(m, "physical pages: %lu\n"
132 "pages per pool: %lu\n"
137 "idle index: %lu/100\n"
138 "last shrink: %llds\n"
139 "last access: %llds\n"
140 "max pages reached: %lu\n"
142 "grows failure: %u\n"
144 "cache access: %lu\n"
145 "cache missing: %lu\n"
146 "low free mark: %lu\n"
147 "max waitqueue depth: %u\n"
148 "max wait time ms: %lld\n"
150 cfs_totalram_pages(), PAGES_PER_POOL,
151 page_pools.epp_max_pages,
152 page_pools.epp_max_pools,
153 page_pools.epp_total_pages,
154 page_pools.epp_free_pages,
155 page_pools.epp_idle_idx,
156 ktime_get_seconds() - page_pools.epp_last_shrink,
157 ktime_get_seconds() - page_pools.epp_last_access,
158 page_pools.epp_st_max_pages,
159 page_pools.epp_st_grows,
160 page_pools.epp_st_grow_fails,
161 page_pools.epp_st_shrinks,
162 page_pools.epp_st_access,
163 page_pools.epp_st_missings,
164 page_pools.epp_st_lowfree,
165 page_pools.epp_st_max_wqlen,
166 ktime_to_ms(page_pools.epp_st_max_wait),
167 page_pools.epp_st_outofmem);
169 spin_unlock(&page_pools.epp_lock);
173 static void enc_pools_release_free_pages(long npages)
176 int p_idx_max1, p_idx_max2;
179 LASSERT(npages <= page_pools.epp_free_pages);
180 LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
182 /* max pool index before the release */
183 p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
185 page_pools.epp_free_pages -= npages;
186 page_pools.epp_total_pages -= npages;
188 /* max pool index after the release */
189 p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
190 ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
192 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
193 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
194 LASSERT(page_pools.epp_pools[p_idx]);
197 LASSERT(page_pools.epp_pools[p_idx]);
198 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
200 __free_page(page_pools.epp_pools[p_idx][g_idx]);
201 page_pools.epp_pools[p_idx][g_idx] = NULL;
203 if (++g_idx == PAGES_PER_POOL) {
209 /* free unused pools */
210 while (p_idx_max1 < p_idx_max2) {
211 LASSERT(page_pools.epp_pools[p_idx_max2]);
212 OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_SIZE);
213 page_pools.epp_pools[p_idx_max2] = NULL;
219 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
221 static unsigned long enc_pools_shrink_count(struct shrinker *s,
222 struct shrink_control *sc)
225 * if no pool access for a long time, we consider it's fully idle.
226 * a little race here is fine.
228 if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
229 CACHE_QUIESCENT_PERIOD)) {
230 spin_lock(&page_pools.epp_lock);
231 page_pools.epp_idle_idx = IDLE_IDX_MAX;
232 spin_unlock(&page_pools.epp_lock);
235 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
236 return (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES) ? 0 :
237 (page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES) *
238 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
242 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
244 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
245 struct shrink_control *sc)
247 spin_lock(&page_pools.epp_lock);
248 if (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES)
251 sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
252 page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
253 if (sc->nr_to_scan > 0) {
254 enc_pools_release_free_pages(sc->nr_to_scan);
255 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
256 (long)sc->nr_to_scan, page_pools.epp_free_pages);
258 page_pools.epp_st_shrinks++;
259 page_pools.epp_last_shrink = ktime_get_seconds();
261 spin_unlock(&page_pools.epp_lock);
264 * if no pool access for a long time, we consider it's fully idle.
265 * a little race here is fine.
267 if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
268 CACHE_QUIESCENT_PERIOD)) {
269 spin_lock(&page_pools.epp_lock);
270 page_pools.epp_idle_idx = IDLE_IDX_MAX;
271 spin_unlock(&page_pools.epp_lock);
274 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
275 return sc->nr_to_scan;
278 #ifndef HAVE_SHRINKER_COUNT
280 * could be called frequently for query (@nr_to_scan == 0).
281 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
283 static int enc_pools_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
285 struct shrink_control scv = {
286 .nr_to_scan = shrink_param(sc, nr_to_scan),
287 .gfp_mask = shrink_param(sc, gfp_mask)
289 enc_pools_shrink_scan(shrinker, &scv);
291 return enc_pools_shrink_count(shrinker, &scv);
294 #endif /* HAVE_SHRINKER_COUNT */
297 int npages_to_npools(unsigned long npages)
299 return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
303 * return how many pages cleaned up.
305 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
307 unsigned long cleaned = 0;
310 for (i = 0; i < npools; i++) {
312 for (j = 0; j < PAGES_PER_POOL; j++) {
314 __free_page(pools[i][j]);
318 OBD_FREE(pools[i], PAGE_SIZE);
327 * merge @npools pointed by @pools which contains @npages new pages
328 * into current pools.
330 * we have options to avoid most memory copy with some tricks. but we choose
331 * the simplest way to avoid complexity. It's not frequently called.
333 static void enc_pools_insert(struct page ***pools, int npools, int npages)
336 int op_idx, np_idx, og_idx, ng_idx;
337 int cur_npools, end_npools;
340 LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
341 LASSERT(npages_to_npools(npages) == npools);
342 LASSERT(page_pools.epp_growing);
344 spin_lock(&page_pools.epp_lock);
347 * (1) fill all the free slots of current pools.
350 * free slots are those left by rent pages, and the extra ones with
351 * index >= total_pages, locate at the tail of last pool.
353 freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
355 freeslot = PAGES_PER_POOL - freeslot;
356 freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
358 op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
359 og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
361 ng_idx = (npages - 1) % PAGES_PER_POOL;
364 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
365 LASSERT(pools[np_idx][ng_idx] != NULL);
367 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
368 pools[np_idx][ng_idx] = NULL;
372 if (++og_idx == PAGES_PER_POOL) {
380 ng_idx = PAGES_PER_POOL - 1;
385 * (2) add pools if needed.
387 cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
389 end_npools = (page_pools.epp_total_pages + npages +
390 PAGES_PER_POOL - 1) / PAGES_PER_POOL;
391 LASSERT(end_npools <= page_pools.epp_max_pools);
394 while (cur_npools < end_npools) {
395 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
396 LASSERT(np_idx < npools);
397 LASSERT(pools[np_idx] != NULL);
399 page_pools.epp_pools[cur_npools++] = pools[np_idx];
400 pools[np_idx++] = NULL;
403 page_pools.epp_total_pages += npages;
404 page_pools.epp_free_pages += npages;
405 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
407 if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
408 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
410 CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
411 page_pools.epp_total_pages);
413 spin_unlock(&page_pools.epp_lock);
416 static int enc_pools_add_pages(int npages)
418 static DEFINE_MUTEX(add_pages_mutex);
419 struct page ***pools;
420 int npools, alloced = 0;
421 int i, j, rc = -ENOMEM;
423 if (npages < PTLRPC_MAX_BRW_PAGES)
424 npages = PTLRPC_MAX_BRW_PAGES;
426 mutex_lock(&add_pages_mutex);
428 if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
429 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
432 page_pools.epp_st_grows++;
434 npools = npages_to_npools(npages);
435 OBD_ALLOC(pools, npools * sizeof(*pools));
439 for (i = 0; i < npools; i++) {
440 OBD_ALLOC(pools[i], PAGE_SIZE);
441 if (pools[i] == NULL)
444 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
445 pools[i][j] = alloc_page(GFP_NOFS |
447 if (pools[i][j] == NULL)
453 LASSERT(alloced == npages);
455 enc_pools_insert(pools, npools, npages);
456 CDEBUG(D_SEC, "added %d pages into pools\n", npages);
460 enc_pools_cleanup(pools, npools);
461 OBD_FREE(pools, npools * sizeof(*pools));
464 page_pools.epp_st_grow_fails++;
465 CERROR("Failed to allocate %d enc pages\n", npages);
468 mutex_unlock(&add_pages_mutex);
472 static inline void enc_pools_wakeup(void)
474 assert_spin_locked(&page_pools.epp_lock);
476 if (unlikely(page_pools.epp_waitqlen)) {
477 LASSERT(waitqueue_active(&page_pools.epp_waitq));
478 wake_up_all(&page_pools.epp_waitq);
482 static int enc_pools_should_grow(int page_needed, time64_t now)
485 * don't grow if someone else is growing the pools right now,
486 * or the pools has reached its full capacity
488 if (page_pools.epp_growing ||
489 page_pools.epp_total_pages == page_pools.epp_max_pages)
492 /* if total pages is not enough, we need to grow */
493 if (page_pools.epp_total_pages < page_needed)
497 * we wanted to return 0 here if there was a shrink just
498 * happened a moment ago, but this may cause deadlock if both
499 * client and ost live on single node.
503 * here we perhaps need consider other factors like wait queue
504 * length, idle index, etc. ?
507 /* grow the pools in any other cases */
512 * Export the number of free pages in the pool
514 int get_free_pages_in_pool(void)
516 return page_pools.epp_free_pages;
518 EXPORT_SYMBOL(get_free_pages_in_pool);
521 * Let outside world know if enc_pool full capacity is reached
523 int pool_is_at_full_capacity(void)
525 return (page_pools.epp_total_pages == page_pools.epp_max_pages);
527 EXPORT_SYMBOL(pool_is_at_full_capacity);
530 * we allocate the requested pages atomically.
532 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
534 wait_queue_entry_t waitlink;
535 unsigned long this_idle = -1;
541 LASSERT(desc->bd_iov_count > 0);
542 LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
544 /* resent bulk, enc iov might have been allocated previously */
545 if (GET_ENC_KIOV(desc) != NULL)
548 OBD_ALLOC_LARGE(GET_ENC_KIOV(desc),
549 desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
550 if (GET_ENC_KIOV(desc) == NULL)
553 spin_lock(&page_pools.epp_lock);
555 page_pools.epp_st_access++;
557 if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
559 tick_ns = ktime_get_ns();
561 now = ktime_get_real_seconds();
563 page_pools.epp_st_missings++;
564 page_pools.epp_pages_short += desc->bd_iov_count;
566 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
567 page_pools.epp_growing = 1;
569 spin_unlock(&page_pools.epp_lock);
570 enc_pools_add_pages(page_pools.epp_pages_short / 2);
571 spin_lock(&page_pools.epp_lock);
573 page_pools.epp_growing = 0;
577 if (page_pools.epp_growing) {
578 if (++page_pools.epp_waitqlen >
579 page_pools.epp_st_max_wqlen)
580 page_pools.epp_st_max_wqlen =
581 page_pools.epp_waitqlen;
583 set_current_state(TASK_UNINTERRUPTIBLE);
584 init_waitqueue_entry(&waitlink, current);
585 add_wait_queue(&page_pools.epp_waitq,
588 spin_unlock(&page_pools.epp_lock);
590 remove_wait_queue(&page_pools.epp_waitq,
592 LASSERT(page_pools.epp_waitqlen > 0);
593 spin_lock(&page_pools.epp_lock);
594 page_pools.epp_waitqlen--;
597 * ptlrpcd thread should not sleep in that case,
598 * or deadlock may occur!
599 * Instead, return -ENOMEM so that upper layers
600 * will put request back in queue.
602 page_pools.epp_st_outofmem++;
603 spin_unlock(&page_pools.epp_lock);
604 OBD_FREE_LARGE(GET_ENC_KIOV(desc),
606 sizeof(*GET_ENC_KIOV(desc)));
607 GET_ENC_KIOV(desc) = NULL;
612 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
613 page_pools.epp_pages_short -= desc->bd_iov_count;
619 /* record max wait time */
620 if (unlikely(tick_ns)) {
621 ktime_t tick = ktime_sub_ns(ktime_get(), tick_ns);
623 if (ktime_after(tick, page_pools.epp_st_max_wait))
624 page_pools.epp_st_max_wait = tick;
627 /* proceed with rest of allocation */
628 page_pools.epp_free_pages -= desc->bd_iov_count;
630 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
631 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
633 for (i = 0; i < desc->bd_iov_count; i++) {
634 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
635 BD_GET_ENC_KIOV(desc, i).kiov_page =
636 page_pools.epp_pools[p_idx][g_idx];
637 page_pools.epp_pools[p_idx][g_idx] = NULL;
639 if (++g_idx == PAGES_PER_POOL) {
645 if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
646 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
649 * new idle index = (old * weight + new) / (weight + 1)
651 if (this_idle == -1) {
652 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
653 page_pools.epp_total_pages;
655 page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
657 (IDLE_IDX_WEIGHT + 1);
659 page_pools.epp_last_access = ktime_get_seconds();
661 spin_unlock(&page_pools.epp_lock);
664 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
666 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
671 if (GET_ENC_KIOV(desc) == NULL)
674 LASSERT(desc->bd_iov_count > 0);
676 spin_lock(&page_pools.epp_lock);
678 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
679 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
681 LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
682 page_pools.epp_total_pages);
683 LASSERT(page_pools.epp_pools[p_idx]);
685 for (i = 0; i < desc->bd_iov_count; i++) {
686 LASSERT(BD_GET_ENC_KIOV(desc, i).kiov_page != NULL);
687 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
688 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
690 page_pools.epp_pools[p_idx][g_idx] =
691 BD_GET_ENC_KIOV(desc, i).kiov_page;
693 if (++g_idx == PAGES_PER_POOL) {
699 page_pools.epp_free_pages += desc->bd_iov_count;
703 spin_unlock(&page_pools.epp_lock);
705 OBD_FREE_LARGE(GET_ENC_KIOV(desc),
706 desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
707 GET_ENC_KIOV(desc) = NULL;
711 * we don't do much stuff for add_user/del_user anymore, except adding some
712 * initial pages in add_user() if current pools are empty, rest would be
713 * handled by the pools's self-adaption.
715 int sptlrpc_enc_pool_add_user(void)
719 spin_lock(&page_pools.epp_lock);
720 if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
721 page_pools.epp_growing = 1;
724 spin_unlock(&page_pools.epp_lock);
727 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
728 PTLRPC_MAX_BRW_PAGES);
730 spin_lock(&page_pools.epp_lock);
731 page_pools.epp_growing = 0;
733 spin_unlock(&page_pools.epp_lock);
737 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
739 int sptlrpc_enc_pool_del_user(void)
743 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
745 static inline void enc_pools_alloc(void)
747 LASSERT(page_pools.epp_max_pools);
748 OBD_ALLOC_LARGE(page_pools.epp_pools,
749 page_pools.epp_max_pools *
750 sizeof(*page_pools.epp_pools));
753 static inline void enc_pools_free(void)
755 LASSERT(page_pools.epp_max_pools);
756 LASSERT(page_pools.epp_pools);
758 OBD_FREE_LARGE(page_pools.epp_pools,
759 page_pools.epp_max_pools *
760 sizeof(*page_pools.epp_pools));
763 int sptlrpc_enc_pool_init(void)
765 DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
766 enc_pools_shrink_count, enc_pools_shrink_scan);
768 page_pools.epp_max_pages = cfs_totalram_pages() / 8;
769 if (enc_pool_max_memory_mb > 0 &&
770 enc_pool_max_memory_mb <= (cfs_totalram_pages() >> mult))
771 page_pools.epp_max_pages = enc_pool_max_memory_mb << mult;
773 page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
775 init_waitqueue_head(&page_pools.epp_waitq);
776 page_pools.epp_waitqlen = 0;
777 page_pools.epp_pages_short = 0;
779 page_pools.epp_growing = 0;
781 page_pools.epp_idle_idx = 0;
782 page_pools.epp_last_shrink = ktime_get_seconds();
783 page_pools.epp_last_access = ktime_get_seconds();
785 spin_lock_init(&page_pools.epp_lock);
786 page_pools.epp_total_pages = 0;
787 page_pools.epp_free_pages = 0;
789 page_pools.epp_st_max_pages = 0;
790 page_pools.epp_st_grows = 0;
791 page_pools.epp_st_grow_fails = 0;
792 page_pools.epp_st_shrinks = 0;
793 page_pools.epp_st_access = 0;
794 page_pools.epp_st_missings = 0;
795 page_pools.epp_st_lowfree = 0;
796 page_pools.epp_st_max_wqlen = 0;
797 page_pools.epp_st_max_wait = ktime_set(0, 0);
798 page_pools.epp_st_outofmem = 0;
801 if (page_pools.epp_pools == NULL)
804 pools_shrinker = set_shrinker(pools_shrinker_seeks, &shvar);
805 if (pools_shrinker == NULL) {
813 void sptlrpc_enc_pool_fini(void)
815 unsigned long cleaned, npools;
817 LASSERT(pools_shrinker);
818 LASSERT(page_pools.epp_pools);
819 LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
821 remove_shrinker(pools_shrinker);
823 npools = npages_to_npools(page_pools.epp_total_pages);
824 cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
825 LASSERT(cleaned == page_pools.epp_total_pages);
829 if (page_pools.epp_st_access > 0) {
831 "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",
832 page_pools.epp_st_max_pages, page_pools.epp_st_grows,
833 page_pools.epp_st_grow_fails,
834 page_pools.epp_st_shrinks, page_pools.epp_st_access,
835 page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
836 ktime_to_ms(page_pools.epp_st_max_wait),
837 page_pools.epp_st_outofmem);
842 static int cfs_hash_alg_id[] = {
843 [BULK_HASH_ALG_NULL] = CFS_HASH_ALG_NULL,
844 [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
845 [BULK_HASH_ALG_CRC32] = CFS_HASH_ALG_CRC32,
846 [BULK_HASH_ALG_MD5] = CFS_HASH_ALG_MD5,
847 [BULK_HASH_ALG_SHA1] = CFS_HASH_ALG_SHA1,
848 [BULK_HASH_ALG_SHA256] = CFS_HASH_ALG_SHA256,
849 [BULK_HASH_ALG_SHA384] = CFS_HASH_ALG_SHA384,
850 [BULK_HASH_ALG_SHA512] = CFS_HASH_ALG_SHA512,
852 const char *sptlrpc_get_hash_name(__u8 hash_alg)
854 return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
857 __u8 sptlrpc_get_hash_alg(const char *algname)
859 return cfs_crypto_hash_alg(algname);
862 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
864 struct ptlrpc_bulk_sec_desc *bsd;
865 int size = msg->lm_buflens[offset];
867 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
869 CERROR("Invalid bulk sec desc: size %d\n", size);
874 __swab32s(&bsd->bsd_nob);
876 if (unlikely(bsd->bsd_version != 0)) {
877 CERROR("Unexpected version %u\n", bsd->bsd_version);
881 if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
882 CERROR("Invalid type %u\n", bsd->bsd_type);
886 /* FIXME more sanity check here */
888 if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
889 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
890 bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
891 CERROR("Invalid svc %u\n", bsd->bsd_svc);
897 EXPORT_SYMBOL(bulk_sec_desc_unpack);
900 * Compute the checksum of an RPC buffer payload. If the return \a buflen
901 * is not large enough, truncate the result to fit so that it is possible
902 * to use a hash function with a large hash space, but only use a part of
903 * the resulting hash.
905 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
906 void *buf, int buflen)
908 struct ahash_request *req;
910 unsigned int bufsize;
913 LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
914 LASSERT(buflen >= 4);
916 req = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
918 CERROR("Unable to initialize checksum hash %s\n",
919 cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
923 hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
925 for (i = 0; i < desc->bd_iov_count; i++) {
926 cfs_crypto_hash_update_page(req,
927 BD_GET_KIOV(desc, i).kiov_page,
928 BD_GET_KIOV(desc, i).kiov_offset &
930 BD_GET_KIOV(desc, i).kiov_len);
933 if (hashsize > buflen) {
934 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
936 bufsize = sizeof(hashbuf);
937 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
939 err = cfs_crypto_hash_final(req, hashbuf, &bufsize);
940 memcpy(buf, hashbuf, buflen);
943 err = cfs_crypto_hash_final(req, buf, &bufsize);