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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/sec_bulk.c
38 * Author: Eric Mei <ericm@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_SEC
43 #include <libcfs/libcfs.h>
46 #include <obd_cksum.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lustre_net.h>
50 #include <lustre_import.h>
51 #include <lustre_dlm.h>
52 #include <lustre_sec.h>
54 #include "ptlrpc_internal.h"
56 static int mult = 20 - PAGE_CACHE_SHIFT;
57 static int enc_pool_max_memory_mb;
58 CFS_MODULE_PARM(enc_pool_max_memory_mb, "i", int, 0644,
59 "Encoding pool max memory (MB), 1/8 of total physical memory by default");
62 /****************************************
63 * bulk encryption page pools *
64 ****************************************/
67 #define PTRS_PER_PAGE (PAGE_CACHE_SIZE / sizeof(void *))
68 #define PAGES_PER_POOL (PTRS_PER_PAGE)
70 #define IDLE_IDX_MAX (100)
71 #define IDLE_IDX_WEIGHT (3)
73 #define CACHE_QUIESCENT_PERIOD (20)
75 static struct ptlrpc_enc_page_pool {
79 unsigned long epp_max_pages; /* maximum pages can hold, const */
80 unsigned int epp_max_pools; /* number of pools, const */
83 * wait queue in case of not enough free pages.
85 wait_queue_head_t epp_waitq; /* waiting threads */
86 unsigned int epp_waitqlen; /* wait queue length */
87 unsigned long epp_pages_short; /* # of pages wanted of in-q users */
88 unsigned int epp_growing:1; /* during adding pages */
91 * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
92 * this is counted based on each time when getting pages from
93 * the pools, not based on time. which means in case that system
94 * is idled for a while but the idle_idx might still be low if no
95 * activities happened in the pools.
97 unsigned long epp_idle_idx;
99 /* last shrink time due to mem tight */
100 long epp_last_shrink;
101 long epp_last_access;
104 * in-pool pages bookkeeping
106 spinlock_t epp_lock; /* protect following fields */
107 unsigned long epp_total_pages; /* total pages in pools */
108 unsigned long epp_free_pages; /* current pages available */
113 unsigned long epp_st_max_pages; /* # of pages ever reached */
114 unsigned int epp_st_grows; /* # of grows */
115 unsigned int epp_st_grow_fails; /* # of add pages failures */
116 unsigned int epp_st_shrinks; /* # of shrinks */
117 unsigned long epp_st_access; /* # of access */
118 unsigned long epp_st_missings; /* # of cache missing */
119 unsigned long epp_st_lowfree; /* lowest free pages reached */
120 unsigned int epp_st_max_wqlen; /* highest waitqueue length */
121 cfs_time_t epp_st_max_wait; /* in jeffies */
122 unsigned long epp_st_outofmem; /* # of out of mem requests */
126 struct page ***epp_pools;
132 static const int pools_shrinker_seeks = DEFAULT_SEEKS;
133 static struct shrinker *pools_shrinker;
137 * /proc/fs/lustre/sptlrpc/encrypt_page_pools
139 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
143 spin_lock(&page_pools.epp_lock);
146 "physical pages: %lu\n"
147 "pages per pool: %lu\n"
152 "idle index: %lu/100\n"
153 "last shrink: %lds\n"
154 "last access: %lds\n"
155 "max pages reached: %lu\n"
157 "grows failure: %u\n"
159 "cache access: %lu\n"
160 "cache missing: %lu\n"
161 "low free mark: %lu\n"
162 "max waitqueue depth: %u\n"
163 "max wait time: "CFS_TIME_T"/%lu\n"
168 page_pools.epp_max_pages,
169 page_pools.epp_max_pools,
170 page_pools.epp_total_pages,
171 page_pools.epp_free_pages,
172 page_pools.epp_idle_idx,
173 cfs_time_current_sec() - page_pools.epp_last_shrink,
174 cfs_time_current_sec() - page_pools.epp_last_access,
175 page_pools.epp_st_max_pages,
176 page_pools.epp_st_grows,
177 page_pools.epp_st_grow_fails,
178 page_pools.epp_st_shrinks,
179 page_pools.epp_st_access,
180 page_pools.epp_st_missings,
181 page_pools.epp_st_lowfree,
182 page_pools.epp_st_max_wqlen,
183 page_pools.epp_st_max_wait,
184 msecs_to_jiffies(MSEC_PER_SEC),
185 page_pools.epp_st_outofmem
188 spin_unlock(&page_pools.epp_lock);
192 static void enc_pools_release_free_pages(long npages)
195 int p_idx_max1, p_idx_max2;
198 LASSERT(npages <= page_pools.epp_free_pages);
199 LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
201 /* max pool index before the release */
202 p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
204 page_pools.epp_free_pages -= npages;
205 page_pools.epp_total_pages -= npages;
207 /* max pool index after the release */
208 p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
209 ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
211 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
212 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
213 LASSERT(page_pools.epp_pools[p_idx]);
216 LASSERT(page_pools.epp_pools[p_idx]);
217 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
219 __free_page(page_pools.epp_pools[p_idx][g_idx]);
220 page_pools.epp_pools[p_idx][g_idx] = NULL;
222 if (++g_idx == PAGES_PER_POOL) {
228 /* free unused pools */
229 while (p_idx_max1 < p_idx_max2) {
230 LASSERT(page_pools.epp_pools[p_idx_max2]);
231 OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_CACHE_SIZE);
232 page_pools.epp_pools[p_idx_max2] = NULL;
238 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
240 static unsigned long enc_pools_shrink_count(struct shrinker *s,
241 struct shrink_control *sc)
244 * if no pool access for a long time, we consider it's fully idle.
245 * a little race here is fine.
247 if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
248 CACHE_QUIESCENT_PERIOD)) {
249 spin_lock(&page_pools.epp_lock);
250 page_pools.epp_idle_idx = IDLE_IDX_MAX;
251 spin_unlock(&page_pools.epp_lock);
254 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
255 return max((int)page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
256 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
260 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
262 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
263 struct shrink_control *sc)
265 spin_lock(&page_pools.epp_lock);
266 sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
267 page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
268 if (sc->nr_to_scan > 0) {
269 enc_pools_release_free_pages(sc->nr_to_scan);
270 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
271 (long)sc->nr_to_scan, page_pools.epp_free_pages);
273 page_pools.epp_st_shrinks++;
274 page_pools.epp_last_shrink = cfs_time_current_sec();
276 spin_unlock(&page_pools.epp_lock);
279 * if no pool access for a long time, we consider it's fully idle.
280 * a little race here is fine.
282 if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
283 CACHE_QUIESCENT_PERIOD)) {
284 spin_lock(&page_pools.epp_lock);
285 page_pools.epp_idle_idx = IDLE_IDX_MAX;
286 spin_unlock(&page_pools.epp_lock);
289 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
290 return sc->nr_to_scan;
293 #ifndef HAVE_SHRINKER_COUNT
295 * could be called frequently for query (@nr_to_scan == 0).
296 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
298 static int enc_pools_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
300 struct shrink_control scv = {
301 .nr_to_scan = shrink_param(sc, nr_to_scan),
302 .gfp_mask = shrink_param(sc, gfp_mask)
304 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
305 struct shrinker* shrinker = NULL;
308 enc_pools_shrink_scan(shrinker, &scv);
310 return enc_pools_shrink_count(shrinker, &scv);
313 #endif /* HAVE_SHRINKER_COUNT */
316 int npages_to_npools(unsigned long npages)
318 return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
322 * return how many pages cleaned up.
324 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
326 unsigned long cleaned = 0;
329 for (i = 0; i < npools; i++) {
331 for (j = 0; j < PAGES_PER_POOL; j++) {
333 __free_page(pools[i][j]);
337 OBD_FREE(pools[i], PAGE_CACHE_SIZE);
346 * merge @npools pointed by @pools which contains @npages new pages
347 * into current pools.
349 * we have options to avoid most memory copy with some tricks. but we choose
350 * the simplest way to avoid complexity. It's not frequently called.
352 static void enc_pools_insert(struct page ***pools, int npools, int npages)
355 int op_idx, np_idx, og_idx, ng_idx;
356 int cur_npools, end_npools;
359 LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
360 LASSERT(npages_to_npools(npages) == npools);
361 LASSERT(page_pools.epp_growing);
363 spin_lock(&page_pools.epp_lock);
366 * (1) fill all the free slots of current pools.
368 /* free slots are those left by rent pages, and the extra ones with
369 * index >= total_pages, locate at the tail of last pool. */
370 freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
372 freeslot = PAGES_PER_POOL - freeslot;
373 freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
375 op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
376 og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
378 ng_idx = (npages - 1) % PAGES_PER_POOL;
381 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
382 LASSERT(pools[np_idx][ng_idx] != NULL);
384 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
385 pools[np_idx][ng_idx] = NULL;
389 if (++og_idx == PAGES_PER_POOL) {
397 ng_idx = PAGES_PER_POOL - 1;
402 * (2) add pools if needed.
404 cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
406 end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
408 LASSERT(end_npools <= page_pools.epp_max_pools);
411 while (cur_npools < end_npools) {
412 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
413 LASSERT(np_idx < npools);
414 LASSERT(pools[np_idx] != NULL);
416 page_pools.epp_pools[cur_npools++] = pools[np_idx];
417 pools[np_idx++] = NULL;
420 page_pools.epp_total_pages += npages;
421 page_pools.epp_free_pages += npages;
422 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
424 if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
425 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
427 CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
428 page_pools.epp_total_pages);
430 spin_unlock(&page_pools.epp_lock);
433 static int enc_pools_add_pages(int npages)
435 static DEFINE_MUTEX(add_pages_mutex);
436 struct page ***pools;
437 int npools, alloced = 0;
438 int i, j, rc = -ENOMEM;
440 if (npages < PTLRPC_MAX_BRW_PAGES)
441 npages = PTLRPC_MAX_BRW_PAGES;
443 mutex_lock(&add_pages_mutex);
445 if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
446 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
449 page_pools.epp_st_grows++;
451 npools = npages_to_npools(npages);
452 OBD_ALLOC(pools, npools * sizeof(*pools));
456 for (i = 0; i < npools; i++) {
457 OBD_ALLOC(pools[i], PAGE_CACHE_SIZE);
458 if (pools[i] == NULL)
461 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
462 pools[i][j] = alloc_page(GFP_NOFS |
464 if (pools[i][j] == NULL)
470 LASSERT(alloced == npages);
472 enc_pools_insert(pools, npools, npages);
473 CDEBUG(D_SEC, "added %d pages into pools\n", npages);
477 enc_pools_cleanup(pools, npools);
478 OBD_FREE(pools, npools * sizeof(*pools));
481 page_pools.epp_st_grow_fails++;
482 CERROR("Failed to allocate %d enc pages\n", npages);
485 mutex_unlock(&add_pages_mutex);
489 static inline void enc_pools_wakeup(void)
491 assert_spin_locked(&page_pools.epp_lock);
493 if (unlikely(page_pools.epp_waitqlen)) {
494 LASSERT(waitqueue_active(&page_pools.epp_waitq));
495 wake_up_all(&page_pools.epp_waitq);
499 static int enc_pools_should_grow(int page_needed, long now)
501 /* don't grow if someone else is growing the pools right now,
502 * or the pools has reached its full capacity
504 if (page_pools.epp_growing ||
505 page_pools.epp_total_pages == page_pools.epp_max_pages)
508 /* if total pages is not enough, we need to grow */
509 if (page_pools.epp_total_pages < page_needed)
513 * we wanted to return 0 here if there was a shrink just happened
514 * moment ago, but this may cause deadlock if both client and ost
515 * live on single node.
518 if (now - page_pools.epp_last_shrink < 2)
523 * here we perhaps need consider other factors like wait queue
524 * length, idle index, etc. ?
527 /* grow the pools in any other cases */
532 * Export the number of free pages in the pool
534 int get_free_pages_in_pool(void)
536 return page_pools.epp_free_pages;
538 EXPORT_SYMBOL(get_free_pages_in_pool);
541 * Let outside world know if enc_pool full capacity is reached
543 int pool_is_at_full_capacity(void)
545 return (page_pools.epp_total_pages == page_pools.epp_max_pages);
547 EXPORT_SYMBOL(pool_is_at_full_capacity);
550 * we allocate the requested pages atomically.
552 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
554 wait_queue_t waitlink;
555 unsigned long this_idle = -1;
561 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
562 LASSERT(desc->bd_iov_count > 0);
563 LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
565 /* resent bulk, enc iov might have been allocated previously */
566 if (GET_ENC_KIOV(desc) != NULL)
569 OBD_ALLOC(GET_ENC_KIOV(desc),
570 desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
571 if (GET_ENC_KIOV(desc) == NULL)
574 spin_lock(&page_pools.epp_lock);
576 page_pools.epp_st_access++;
578 if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
580 tick = cfs_time_current();
582 now = cfs_time_current_sec();
584 page_pools.epp_st_missings++;
585 page_pools.epp_pages_short += desc->bd_iov_count;
587 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
588 page_pools.epp_growing = 1;
590 spin_unlock(&page_pools.epp_lock);
591 enc_pools_add_pages(page_pools.epp_pages_short / 2);
592 spin_lock(&page_pools.epp_lock);
594 page_pools.epp_growing = 0;
598 if (page_pools.epp_growing) {
599 if (++page_pools.epp_waitqlen >
600 page_pools.epp_st_max_wqlen)
601 page_pools.epp_st_max_wqlen =
602 page_pools.epp_waitqlen;
604 set_current_state(TASK_UNINTERRUPTIBLE);
605 init_waitqueue_entry(&waitlink, current);
606 add_wait_queue(&page_pools.epp_waitq,
609 spin_unlock(&page_pools.epp_lock);
611 remove_wait_queue(&page_pools.epp_waitq,
613 LASSERT(page_pools.epp_waitqlen > 0);
614 spin_lock(&page_pools.epp_lock);
615 page_pools.epp_waitqlen--;
617 /* ptlrpcd thread should not sleep in that case,
618 * or deadlock may occur!
619 * Instead, return -ENOMEM so that upper layers
620 * will put request back in queue. */
621 page_pools.epp_st_outofmem++;
622 spin_unlock(&page_pools.epp_lock);
623 OBD_FREE(GET_ENC_KIOV(desc),
625 sizeof(*GET_ENC_KIOV(desc)));
626 GET_ENC_KIOV(desc) = NULL;
631 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
632 page_pools.epp_pages_short -= desc->bd_iov_count;
638 /* record max wait time */
639 if (unlikely(tick != 0)) {
640 tick = cfs_time_current() - tick;
641 if (tick > page_pools.epp_st_max_wait)
642 page_pools.epp_st_max_wait = tick;
645 /* proceed with rest of allocation */
646 page_pools.epp_free_pages -= desc->bd_iov_count;
648 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
649 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
651 for (i = 0; i < desc->bd_iov_count; i++) {
652 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
653 BD_GET_ENC_KIOV(desc, i).kiov_page =
654 page_pools.epp_pools[p_idx][g_idx];
655 page_pools.epp_pools[p_idx][g_idx] = NULL;
657 if (++g_idx == PAGES_PER_POOL) {
663 if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
664 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
667 * new idle index = (old * weight + new) / (weight + 1)
669 if (this_idle == -1) {
670 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
671 page_pools.epp_total_pages;
673 page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
675 (IDLE_IDX_WEIGHT + 1);
677 page_pools.epp_last_access = cfs_time_current_sec();
679 spin_unlock(&page_pools.epp_lock);
682 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
684 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
689 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
691 if (GET_ENC_KIOV(desc) == NULL)
694 LASSERT(desc->bd_iov_count > 0);
696 spin_lock(&page_pools.epp_lock);
698 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
699 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
701 LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
702 page_pools.epp_total_pages);
703 LASSERT(page_pools.epp_pools[p_idx]);
705 for (i = 0; i < desc->bd_iov_count; i++) {
706 LASSERT(BD_GET_ENC_KIOV(desc, i).kiov_page != NULL);
707 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
708 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
710 page_pools.epp_pools[p_idx][g_idx] =
711 BD_GET_ENC_KIOV(desc, i).kiov_page;
713 if (++g_idx == PAGES_PER_POOL) {
719 page_pools.epp_free_pages += desc->bd_iov_count;
723 spin_unlock(&page_pools.epp_lock);
725 OBD_FREE(GET_ENC_KIOV(desc),
726 desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
727 GET_ENC_KIOV(desc) = NULL;
731 * we don't do much stuff for add_user/del_user anymore, except adding some
732 * initial pages in add_user() if current pools are empty, rest would be
733 * handled by the pools's self-adaption.
735 int sptlrpc_enc_pool_add_user(void)
739 spin_lock(&page_pools.epp_lock);
740 if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
741 page_pools.epp_growing = 1;
744 spin_unlock(&page_pools.epp_lock);
747 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
748 PTLRPC_MAX_BRW_PAGES);
750 spin_lock(&page_pools.epp_lock);
751 page_pools.epp_growing = 0;
753 spin_unlock(&page_pools.epp_lock);
757 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
759 int sptlrpc_enc_pool_del_user(void)
763 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
765 static inline void enc_pools_alloc(void)
767 LASSERT(page_pools.epp_max_pools);
768 OBD_ALLOC_LARGE(page_pools.epp_pools,
769 page_pools.epp_max_pools *
770 sizeof(*page_pools.epp_pools));
773 static inline void enc_pools_free(void)
775 LASSERT(page_pools.epp_max_pools);
776 LASSERT(page_pools.epp_pools);
778 OBD_FREE_LARGE(page_pools.epp_pools,
779 page_pools.epp_max_pools *
780 sizeof(*page_pools.epp_pools));
783 int sptlrpc_enc_pool_init(void)
785 DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
786 enc_pools_shrink_count, enc_pools_shrink_scan);
788 page_pools.epp_max_pages = totalram_pages / 8;
789 if (enc_pool_max_memory_mb > 0 &&
790 enc_pool_max_memory_mb <= (totalram_pages >> mult))
791 page_pools.epp_max_pages = enc_pool_max_memory_mb << mult;
793 page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
795 init_waitqueue_head(&page_pools.epp_waitq);
796 page_pools.epp_waitqlen = 0;
797 page_pools.epp_pages_short = 0;
799 page_pools.epp_growing = 0;
801 page_pools.epp_idle_idx = 0;
802 page_pools.epp_last_shrink = cfs_time_current_sec();
803 page_pools.epp_last_access = cfs_time_current_sec();
805 spin_lock_init(&page_pools.epp_lock);
806 page_pools.epp_total_pages = 0;
807 page_pools.epp_free_pages = 0;
809 page_pools.epp_st_max_pages = 0;
810 page_pools.epp_st_grows = 0;
811 page_pools.epp_st_grow_fails = 0;
812 page_pools.epp_st_shrinks = 0;
813 page_pools.epp_st_access = 0;
814 page_pools.epp_st_missings = 0;
815 page_pools.epp_st_lowfree = 0;
816 page_pools.epp_st_max_wqlen = 0;
817 page_pools.epp_st_max_wait = 0;
818 page_pools.epp_st_outofmem = 0;
821 if (page_pools.epp_pools == NULL)
824 pools_shrinker = set_shrinker(pools_shrinker_seeks, &shvar);
825 if (pools_shrinker == NULL) {
833 void sptlrpc_enc_pool_fini(void)
835 unsigned long cleaned, npools;
837 LASSERT(pools_shrinker);
838 LASSERT(page_pools.epp_pools);
839 LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
841 remove_shrinker(pools_shrinker);
843 npools = npages_to_npools(page_pools.epp_total_pages);
844 cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
845 LASSERT(cleaned == page_pools.epp_total_pages);
849 if (page_pools.epp_st_access > 0) {
851 "max pages %lu, grows %u, grow fails %u, shrinks %u, "
852 "access %lu, missing %lu, max qlen %u, max wait "
853 CFS_TIME_T"/%lu, out of mem %lu\n",
854 page_pools.epp_st_max_pages, page_pools.epp_st_grows,
855 page_pools.epp_st_grow_fails,
856 page_pools.epp_st_shrinks, page_pools.epp_st_access,
857 page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
858 page_pools.epp_st_max_wait,
859 msecs_to_jiffies(MSEC_PER_SEC),
860 page_pools.epp_st_outofmem);
865 static int cfs_hash_alg_id[] = {
866 [BULK_HASH_ALG_NULL] = CFS_HASH_ALG_NULL,
867 [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
868 [BULK_HASH_ALG_CRC32] = CFS_HASH_ALG_CRC32,
869 [BULK_HASH_ALG_MD5] = CFS_HASH_ALG_MD5,
870 [BULK_HASH_ALG_SHA1] = CFS_HASH_ALG_SHA1,
871 [BULK_HASH_ALG_SHA256] = CFS_HASH_ALG_SHA256,
872 [BULK_HASH_ALG_SHA384] = CFS_HASH_ALG_SHA384,
873 [BULK_HASH_ALG_SHA512] = CFS_HASH_ALG_SHA512,
875 const char * sptlrpc_get_hash_name(__u8 hash_alg)
877 return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
880 __u8 sptlrpc_get_hash_alg(const char *algname)
882 return cfs_crypto_hash_alg(algname);
885 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
887 struct ptlrpc_bulk_sec_desc *bsd;
888 int size = msg->lm_buflens[offset];
890 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
892 CERROR("Invalid bulk sec desc: size %d\n", size);
897 __swab32s(&bsd->bsd_nob);
900 if (unlikely(bsd->bsd_version != 0)) {
901 CERROR("Unexpected version %u\n", bsd->bsd_version);
905 if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
906 CERROR("Invalid type %u\n", bsd->bsd_type);
910 /* FIXME more sanity check here */
912 if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
913 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
914 bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
915 CERROR("Invalid svc %u\n", bsd->bsd_svc);
921 EXPORT_SYMBOL(bulk_sec_desc_unpack);
924 * Compute the checksum of an RPC buffer payload. If the return \a buflen
925 * is not large enough, truncate the result to fit so that it is possible
926 * to use a hash function with a large hash space, but only use a part of
927 * the resulting hash.
929 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
930 void *buf, int buflen)
932 struct cfs_crypto_hash_desc *hdesc;
934 unsigned int bufsize;
937 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
938 LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
939 LASSERT(buflen >= 4);
941 hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
943 CERROR("Unable to initialize checksum hash %s\n",
944 cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
945 return PTR_ERR(hdesc);
948 hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
950 for (i = 0; i < desc->bd_iov_count; i++) {
951 cfs_crypto_hash_update_page(hdesc,
952 BD_GET_KIOV(desc, i).kiov_page,
953 BD_GET_KIOV(desc, i).kiov_offset &
955 BD_GET_KIOV(desc, i).kiov_len);
958 if (hashsize > buflen) {
959 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
961 bufsize = sizeof(hashbuf);
962 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
964 err = cfs_crypto_hash_final(hdesc, hashbuf, &bufsize);
965 memcpy(buf, hashbuf, buflen);
968 err = cfs_crypto_hash_final(hdesc, buf, &bufsize);