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/
31 * lustre/ptlrpc/sec_bulk.c
33 * Author: Eric Mei <ericm@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_SEC
38 #include <libcfs/linux/linux-mem.h>
41 #include <obd_cksum.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
45 #include <lustre_import.h>
46 #include <lustre_dlm.h>
47 #include <lustre_sec.h>
49 #include "ptlrpc_internal.h"
51 static int mult = 20 - PAGE_SHIFT;
52 static int enc_pool_max_memory_mb;
53 module_param(enc_pool_max_memory_mb, int, 0644);
54 MODULE_PARM_DESC(enc_pool_max_memory_mb,
55 "Encoding pool max memory (MB), 1/8 of total physical memory by default");
58 * bulk encryption page pools
61 #define PTRS_PER_PAGE (PAGE_SIZE / sizeof(void *))
62 #define PAGES_PER_POOL (PTRS_PER_PAGE)
64 #define IDLE_IDX_MAX (100)
65 #define IDLE_IDX_WEIGHT (3)
67 #define CACHE_QUIESCENT_PERIOD (20)
69 static struct ptlrpc_enc_page_pool {
70 unsigned long epp_max_pages; /* maximum pages can hold, const */
71 unsigned int epp_max_pools; /* number of pools, const */
74 * wait queue in case of not enough free pages.
76 wait_queue_head_t epp_waitq; /* waiting threads */
77 unsigned int epp_waitqlen; /* wait queue length */
78 unsigned long epp_pages_short; /* # of pages wanted of in-q users */
79 unsigned int epp_growing:1; /* during adding pages */
82 * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
83 * this is counted based on each time when getting pages from
84 * the pools, not based on time. which means in case that system
85 * is idled for a while but the idle_idx might still be low if no
86 * activities happened in the pools.
88 unsigned long epp_idle_idx;
90 /* last shrink time due to mem tight */
91 time64_t epp_last_shrink;
92 time64_t epp_last_access;
94 /* in-pool pages bookkeeping */
95 spinlock_t epp_lock; /* protect following fields */
96 unsigned long epp_total_pages; /* total pages in pools */
97 unsigned long epp_free_pages; /* current pages available */
100 unsigned long epp_st_max_pages; /* # of pages ever reached */
101 unsigned int epp_st_grows; /* # of grows */
102 unsigned int epp_st_grow_fails; /* # of add pages failures */
103 unsigned int epp_st_shrinks; /* # of shrinks */
104 unsigned long epp_st_access; /* # of access */
105 unsigned long epp_st_missings; /* # of cache missing */
106 unsigned long epp_st_lowfree; /* lowest free pages reached */
107 unsigned int epp_st_max_wqlen; /* highest waitqueue length */
108 ktime_t epp_st_max_wait; /* in nanoseconds */
109 unsigned long epp_st_outofmem; /* # of out of mem requests */
111 * pointers to pools, may be vmalloc'd
113 struct page ***epp_pools;
117 * /proc/fs/lustre/sptlrpc/encrypt_page_pools
119 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
121 spin_lock(&page_pools.epp_lock);
123 seq_printf(m, "physical pages: %lu\n"
124 "pages per pool: %lu\n"
129 "idle index: %lu/100\n"
130 "last shrink: %llds\n"
131 "last access: %llds\n"
132 "max pages reached: %lu\n"
134 "grows failure: %u\n"
136 "cache access: %lu\n"
137 "cache missing: %lu\n"
138 "low free mark: %lu\n"
139 "max waitqueue depth: %u\n"
140 "max wait time ms: %lld\n"
142 cfs_totalram_pages(), PAGES_PER_POOL,
143 page_pools.epp_max_pages,
144 page_pools.epp_max_pools,
145 page_pools.epp_total_pages,
146 page_pools.epp_free_pages,
147 page_pools.epp_idle_idx,
148 ktime_get_seconds() - page_pools.epp_last_shrink,
149 ktime_get_seconds() - page_pools.epp_last_access,
150 page_pools.epp_st_max_pages,
151 page_pools.epp_st_grows,
152 page_pools.epp_st_grow_fails,
153 page_pools.epp_st_shrinks,
154 page_pools.epp_st_access,
155 page_pools.epp_st_missings,
156 page_pools.epp_st_lowfree,
157 page_pools.epp_st_max_wqlen,
158 ktime_to_ms(page_pools.epp_st_max_wait),
159 page_pools.epp_st_outofmem);
161 spin_unlock(&page_pools.epp_lock);
165 static void enc_pools_release_free_pages(long npages)
168 int p_idx_max1, p_idx_max2;
171 LASSERT(npages <= page_pools.epp_free_pages);
172 LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
174 /* max pool index before the release */
175 p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
177 page_pools.epp_free_pages -= npages;
178 page_pools.epp_total_pages -= npages;
180 /* max pool index after the release */
181 p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
182 ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
184 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
185 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
186 LASSERT(page_pools.epp_pools[p_idx]);
189 LASSERT(page_pools.epp_pools[p_idx]);
190 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
192 __free_page(page_pools.epp_pools[p_idx][g_idx]);
193 page_pools.epp_pools[p_idx][g_idx] = NULL;
195 if (++g_idx == PAGES_PER_POOL) {
201 /* free unused pools */
202 while (p_idx_max1 < p_idx_max2) {
203 LASSERT(page_pools.epp_pools[p_idx_max2]);
204 OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_SIZE);
205 page_pools.epp_pools[p_idx_max2] = NULL;
211 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
213 static unsigned long enc_pools_shrink_count(struct shrinker *s,
214 struct shrink_control *sc)
217 * if no pool access for a long time, we consider it's fully idle.
218 * a little race here is fine.
220 if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
221 CACHE_QUIESCENT_PERIOD)) {
222 spin_lock(&page_pools.epp_lock);
223 page_pools.epp_idle_idx = IDLE_IDX_MAX;
224 spin_unlock(&page_pools.epp_lock);
227 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
228 return (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES) ? 0 :
229 (page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES) *
230 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
234 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
236 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
237 struct shrink_control *sc)
239 spin_lock(&page_pools.epp_lock);
240 if (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES)
243 sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
244 page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
245 if (sc->nr_to_scan > 0) {
246 enc_pools_release_free_pages(sc->nr_to_scan);
247 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
248 (long)sc->nr_to_scan, page_pools.epp_free_pages);
250 page_pools.epp_st_shrinks++;
251 page_pools.epp_last_shrink = ktime_get_seconds();
253 spin_unlock(&page_pools.epp_lock);
256 * if no pool access for a long time, we consider it's fully idle.
257 * a little race here is fine.
259 if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
260 CACHE_QUIESCENT_PERIOD)) {
261 spin_lock(&page_pools.epp_lock);
262 page_pools.epp_idle_idx = IDLE_IDX_MAX;
263 spin_unlock(&page_pools.epp_lock);
266 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
267 return sc->nr_to_scan;
270 #ifdef HAVE_SHRINKER_COUNT
271 static struct shrinker pools_shrinker = {
272 .count_objects = enc_pools_shrink_count,
273 .scan_objects = enc_pools_shrink_scan,
274 .seeks = DEFAULT_SEEKS,
278 * could be called frequently for query (@nr_to_scan == 0).
279 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
281 static int enc_pools_shrink(struct shrinker *shrinker,
282 struct shrink_control *sc)
284 enc_pools_shrink_scan(shrinker, sc);
286 return enc_pools_shrink_count(shrinker, sc);
289 static struct shrinker pools_shrinker = {
290 .shrink = enc_pools_shrink,
291 .seeks = DEFAULT_SEEKS,
293 #endif /* HAVE_SHRINKER_COUNT */
296 int npages_to_npools(unsigned long npages)
298 return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
302 * return how many pages cleaned up.
304 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
306 unsigned long cleaned = 0;
309 for (i = 0; i < npools; i++) {
311 for (j = 0; j < PAGES_PER_POOL; j++) {
313 __free_page(pools[i][j]);
317 OBD_FREE(pools[i], PAGE_SIZE);
326 * merge @npools pointed by @pools which contains @npages new pages
327 * into current pools.
329 * we have options to avoid most memory copy with some tricks. but we choose
330 * the simplest way to avoid complexity. It's not frequently called.
332 static void enc_pools_insert(struct page ***pools, int npools, int npages)
335 int op_idx, np_idx, og_idx, ng_idx;
336 int cur_npools, end_npools;
339 LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
340 LASSERT(npages_to_npools(npages) == npools);
341 LASSERT(page_pools.epp_growing);
343 spin_lock(&page_pools.epp_lock);
346 * (1) fill all the free slots of current pools.
349 * free slots are those left by rent pages, and the extra ones with
350 * index >= total_pages, locate at the tail of last pool.
352 freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
354 freeslot = PAGES_PER_POOL - freeslot;
355 freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
357 op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
358 og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
360 ng_idx = (npages - 1) % PAGES_PER_POOL;
363 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
364 LASSERT(pools[np_idx][ng_idx] != NULL);
366 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
367 pools[np_idx][ng_idx] = NULL;
371 if (++og_idx == PAGES_PER_POOL) {
379 ng_idx = PAGES_PER_POOL - 1;
384 * (2) add pools if needed.
386 cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
388 end_npools = (page_pools.epp_total_pages + npages +
389 PAGES_PER_POOL - 1) / PAGES_PER_POOL;
390 LASSERT(end_npools <= page_pools.epp_max_pools);
393 while (cur_npools < end_npools) {
394 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
395 LASSERT(np_idx < npools);
396 LASSERT(pools[np_idx] != NULL);
398 page_pools.epp_pools[cur_npools++] = pools[np_idx];
399 pools[np_idx++] = NULL;
402 page_pools.epp_total_pages += npages;
403 page_pools.epp_free_pages += npages;
404 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
406 if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
407 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
409 CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
410 page_pools.epp_total_pages);
412 spin_unlock(&page_pools.epp_lock);
415 static int enc_pools_add_pages(int npages)
417 static DEFINE_MUTEX(add_pages_mutex);
418 struct page ***pools;
419 int npools, alloced = 0;
420 int i, j, rc = -ENOMEM;
422 if (npages < PTLRPC_MAX_BRW_PAGES)
423 npages = PTLRPC_MAX_BRW_PAGES;
425 mutex_lock(&add_pages_mutex);
427 if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
428 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
431 page_pools.epp_st_grows++;
433 npools = npages_to_npools(npages);
434 OBD_ALLOC_PTR_ARRAY(pools, npools);
438 for (i = 0; i < npools; i++) {
439 OBD_ALLOC(pools[i], PAGE_SIZE);
440 if (pools[i] == NULL)
443 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
444 pools[i][j] = alloc_page(GFP_NOFS |
446 if (pools[i][j] == NULL)
452 LASSERT(alloced == npages);
454 enc_pools_insert(pools, npools, npages);
455 CDEBUG(D_SEC, "added %d pages into pools\n", npages);
459 enc_pools_cleanup(pools, npools);
460 OBD_FREE_PTR_ARRAY(pools, npools);
463 page_pools.epp_st_grow_fails++;
464 CERROR("Failed to allocate %d enc pages\n", npages);
467 mutex_unlock(&add_pages_mutex);
471 static inline void enc_pools_wakeup(void)
473 assert_spin_locked(&page_pools.epp_lock);
475 /* waitqueue_active */
476 if (unlikely(waitqueue_active(&page_pools.epp_waitq)))
477 wake_up(&page_pools.epp_waitq);
480 static int enc_pools_should_grow(int page_needed, time64_t now)
483 * don't grow if someone else is growing the pools right now,
484 * or the pools has reached its full capacity
486 if (page_pools.epp_growing ||
487 page_pools.epp_total_pages == page_pools.epp_max_pages)
490 /* if total pages is not enough, we need to grow */
491 if (page_pools.epp_total_pages < page_needed)
495 * we wanted to return 0 here if there was a shrink just
496 * happened a moment ago, but this may cause deadlock if both
497 * client and ost live on single node.
501 * here we perhaps need consider other factors like wait queue
502 * length, idle index, etc. ?
505 /* grow the pools in any other cases */
510 * Export the number of free pages in the pool
512 int get_free_pages_in_pool(void)
514 return page_pools.epp_free_pages;
516 EXPORT_SYMBOL(get_free_pages_in_pool);
519 * Let outside world know if enc_pool full capacity is reached
521 int pool_is_at_full_capacity(void)
523 return (page_pools.epp_total_pages == page_pools.epp_max_pages);
525 EXPORT_SYMBOL(pool_is_at_full_capacity);
527 static inline struct page **page_from_bulkdesc(void *array, int index)
529 struct ptlrpc_bulk_desc *desc = (struct ptlrpc_bulk_desc *)array;
531 return &desc->bd_enc_vec[index].bv_page;
534 static inline struct page **page_from_pagearray(void *array, int index)
536 struct page **pa = (struct page **)array;
542 * we allocate the requested pages atomically.
544 static inline int __sptlrpc_enc_pool_get_pages(void *array, unsigned int count,
545 struct page **(*page_from)(void *, int))
547 wait_queue_entry_t waitlink;
548 unsigned long this_idle = -1;
554 if (!array || count <= 0 || count > page_pools.epp_max_pages)
557 spin_lock(&page_pools.epp_lock);
559 page_pools.epp_st_access++;
561 if (unlikely(page_pools.epp_free_pages < count)) {
563 tick_ns = ktime_get_ns();
565 now = ktime_get_real_seconds();
567 page_pools.epp_st_missings++;
568 page_pools.epp_pages_short += count;
570 if (enc_pools_should_grow(count, now)) {
571 page_pools.epp_growing = 1;
573 spin_unlock(&page_pools.epp_lock);
574 enc_pools_add_pages(page_pools.epp_pages_short / 2);
575 spin_lock(&page_pools.epp_lock);
577 page_pools.epp_growing = 0;
581 if (page_pools.epp_growing) {
582 if (++page_pools.epp_waitqlen >
583 page_pools.epp_st_max_wqlen)
584 page_pools.epp_st_max_wqlen =
585 page_pools.epp_waitqlen;
587 set_current_state(TASK_UNINTERRUPTIBLE);
588 init_wait(&waitlink);
589 add_wait_queue(&page_pools.epp_waitq,
592 spin_unlock(&page_pools.epp_lock);
594 remove_wait_queue(&page_pools.epp_waitq,
596 spin_lock(&page_pools.epp_lock);
597 page_pools.epp_waitqlen--;
600 * ptlrpcd thread should not sleep in that case,
601 * or deadlock may occur!
602 * Instead, return -ENOMEM so that upper layers
603 * will put request back in queue.
605 page_pools.epp_st_outofmem++;
606 GOTO(out_unlock, rc = -ENOMEM);
610 if (page_pools.epp_pages_short < count)
611 GOTO(out_unlock, rc = -EPROTO);
612 page_pools.epp_pages_short -= count;
618 /* record max wait time */
619 if (unlikely(tick_ns)) {
620 ktime_t tick = ktime_sub_ns(ktime_get(), tick_ns);
622 if (ktime_after(tick, page_pools.epp_st_max_wait))
623 page_pools.epp_st_max_wait = tick;
626 /* proceed with rest of allocation */
627 page_pools.epp_free_pages -= count;
629 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
630 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
632 for (i = 0; i < count; i++) {
633 struct page **pagep = page_from(array, i);
635 if (page_pools.epp_pools[p_idx][g_idx] == NULL)
636 GOTO(out_unlock, rc = -EPROTO);
637 *pagep = page_pools.epp_pools[p_idx][g_idx];
638 page_pools.epp_pools[p_idx][g_idx] = NULL;
640 if (++g_idx == PAGES_PER_POOL) {
646 if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
647 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
650 * new idle index = (old * weight + new) / (weight + 1)
652 if (this_idle == -1) {
653 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
654 page_pools.epp_total_pages;
656 page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
658 (IDLE_IDX_WEIGHT + 1);
660 page_pools.epp_last_access = ktime_get_seconds();
663 spin_unlock(&page_pools.epp_lock);
667 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
671 LASSERT(desc->bd_iov_count > 0);
672 LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
674 /* resent bulk, enc iov might have been allocated previously */
675 if (desc->bd_enc_vec != NULL)
678 OBD_ALLOC_LARGE(desc->bd_enc_vec,
679 desc->bd_iov_count * sizeof(*desc->bd_enc_vec));
680 if (desc->bd_enc_vec == NULL)
683 rc = __sptlrpc_enc_pool_get_pages((void *)desc, desc->bd_iov_count,
686 OBD_FREE_LARGE(desc->bd_enc_vec,
688 sizeof(*desc->bd_enc_vec));
689 desc->bd_enc_vec = NULL;
693 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
695 int sptlrpc_enc_pool_get_pages_array(struct page **pa, unsigned int count)
697 return __sptlrpc_enc_pool_get_pages((void *)pa, count,
698 page_from_pagearray);
700 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages_array);
702 static int __sptlrpc_enc_pool_put_pages(void *array, unsigned int count,
703 struct page **(*page_from)(void *, int))
708 if (!array || count <= 0)
711 spin_lock(&page_pools.epp_lock);
713 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
714 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
716 if (page_pools.epp_free_pages + count > page_pools.epp_total_pages)
717 GOTO(out_unlock, rc = -EPROTO);
718 if (!page_pools.epp_pools[p_idx])
719 GOTO(out_unlock, rc = -EPROTO);
721 for (i = 0; i < count; i++) {
722 struct page **pagep = page_from(array, i);
725 page_pools.epp_pools[p_idx][g_idx] != NULL)
726 GOTO(out_unlock, rc = -EPROTO);
728 page_pools.epp_pools[p_idx][g_idx] = *pagep;
729 if (++g_idx == PAGES_PER_POOL) {
735 page_pools.epp_free_pages += count;
739 spin_unlock(&page_pools.epp_lock);
743 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
747 if (desc->bd_enc_vec == NULL)
750 rc = __sptlrpc_enc_pool_put_pages((void *)desc, desc->bd_iov_count,
753 CDEBUG(D_SEC, "error putting pages in enc pool: %d\n", rc);
755 OBD_FREE_LARGE(desc->bd_enc_vec,
756 desc->bd_iov_count * sizeof(*desc->bd_enc_vec));
757 desc->bd_enc_vec = NULL;
760 void sptlrpc_enc_pool_put_pages_array(struct page **pa, unsigned int count)
764 rc = __sptlrpc_enc_pool_put_pages((void *)pa, count,
765 page_from_pagearray);
768 CDEBUG(D_SEC, "error putting pages in enc pool: %d\n", rc);
770 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages_array);
773 * we don't do much stuff for add_user/del_user anymore, except adding some
774 * initial pages in add_user() if current pools are empty, rest would be
775 * handled by the pools's self-adaption.
777 int sptlrpc_enc_pool_add_user(void)
781 spin_lock(&page_pools.epp_lock);
782 if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
783 page_pools.epp_growing = 1;
786 spin_unlock(&page_pools.epp_lock);
789 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
790 PTLRPC_MAX_BRW_PAGES);
792 spin_lock(&page_pools.epp_lock);
793 page_pools.epp_growing = 0;
795 spin_unlock(&page_pools.epp_lock);
799 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
801 static inline void enc_pools_alloc(void)
803 LASSERT(page_pools.epp_max_pools);
804 OBD_ALLOC_LARGE(page_pools.epp_pools,
805 page_pools.epp_max_pools *
806 sizeof(*page_pools.epp_pools));
809 static inline void enc_pools_free(void)
811 LASSERT(page_pools.epp_max_pools);
812 LASSERT(page_pools.epp_pools);
814 OBD_FREE_LARGE(page_pools.epp_pools,
815 page_pools.epp_max_pools *
816 sizeof(*page_pools.epp_pools));
819 int sptlrpc_enc_pool_init(void)
823 page_pools.epp_max_pages = cfs_totalram_pages() / 8;
824 if (enc_pool_max_memory_mb > 0 &&
825 enc_pool_max_memory_mb <= (cfs_totalram_pages() >> mult))
826 page_pools.epp_max_pages = enc_pool_max_memory_mb << mult;
828 page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
830 init_waitqueue_head(&page_pools.epp_waitq);
831 page_pools.epp_waitqlen = 0;
832 page_pools.epp_pages_short = 0;
834 page_pools.epp_growing = 0;
836 page_pools.epp_idle_idx = 0;
837 page_pools.epp_last_shrink = ktime_get_seconds();
838 page_pools.epp_last_access = ktime_get_seconds();
840 spin_lock_init(&page_pools.epp_lock);
841 page_pools.epp_total_pages = 0;
842 page_pools.epp_free_pages = 0;
844 page_pools.epp_st_max_pages = 0;
845 page_pools.epp_st_grows = 0;
846 page_pools.epp_st_grow_fails = 0;
847 page_pools.epp_st_shrinks = 0;
848 page_pools.epp_st_access = 0;
849 page_pools.epp_st_missings = 0;
850 page_pools.epp_st_lowfree = 0;
851 page_pools.epp_st_max_wqlen = 0;
852 page_pools.epp_st_max_wait = ktime_set(0, 0);
853 page_pools.epp_st_outofmem = 0;
856 if (page_pools.epp_pools == NULL)
859 rc = register_shrinker(&pools_shrinker);
866 void sptlrpc_enc_pool_fini(void)
868 unsigned long cleaned, npools;
870 LASSERT(page_pools.epp_pools);
871 LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
873 unregister_shrinker(&pools_shrinker);
875 npools = npages_to_npools(page_pools.epp_total_pages);
876 cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
877 LASSERT(cleaned == page_pools.epp_total_pages);
881 if (page_pools.epp_st_access > 0) {
883 "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",
884 page_pools.epp_st_max_pages, page_pools.epp_st_grows,
885 page_pools.epp_st_grow_fails,
886 page_pools.epp_st_shrinks, page_pools.epp_st_access,
887 page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
888 ktime_to_ms(page_pools.epp_st_max_wait),
889 page_pools.epp_st_outofmem);
894 static int cfs_hash_alg_id[] = {
895 [BULK_HASH_ALG_NULL] = CFS_HASH_ALG_NULL,
896 [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
897 [BULK_HASH_ALG_CRC32] = CFS_HASH_ALG_CRC32,
898 [BULK_HASH_ALG_MD5] = CFS_HASH_ALG_MD5,
899 [BULK_HASH_ALG_SHA1] = CFS_HASH_ALG_SHA1,
900 [BULK_HASH_ALG_SHA256] = CFS_HASH_ALG_SHA256,
901 [BULK_HASH_ALG_SHA384] = CFS_HASH_ALG_SHA384,
902 [BULK_HASH_ALG_SHA512] = CFS_HASH_ALG_SHA512,
904 const char *sptlrpc_get_hash_name(__u8 hash_alg)
906 return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
909 __u8 sptlrpc_get_hash_alg(const char *algname)
911 return cfs_crypto_hash_alg(algname);
914 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
916 struct ptlrpc_bulk_sec_desc *bsd;
917 int size = msg->lm_buflens[offset];
919 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
921 CERROR("Invalid bulk sec desc: size %d\n", size);
926 __swab32s(&bsd->bsd_nob);
928 if (unlikely(bsd->bsd_version != 0)) {
929 CERROR("Unexpected version %u\n", bsd->bsd_version);
933 if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
934 CERROR("Invalid type %u\n", bsd->bsd_type);
938 /* FIXME more sanity check here */
940 if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
941 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
942 bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
943 CERROR("Invalid svc %u\n", bsd->bsd_svc);
949 EXPORT_SYMBOL(bulk_sec_desc_unpack);
952 * Compute the checksum of an RPC buffer payload. If the return \a buflen
953 * is not large enough, truncate the result to fit so that it is possible
954 * to use a hash function with a large hash space, but only use a part of
955 * the resulting hash.
957 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
958 void *buf, int buflen)
960 struct ahash_request *req;
962 unsigned int bufsize;
965 LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
966 LASSERT(buflen >= 4);
968 req = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
970 CERROR("Unable to initialize checksum hash %s\n",
971 cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
975 hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
977 for (i = 0; i < desc->bd_iov_count; i++) {
978 cfs_crypto_hash_update_page(req,
979 desc->bd_vec[i].bv_page,
980 desc->bd_vec[i].bv_offset &
982 desc->bd_vec[i].bv_len);
985 if (hashsize > buflen) {
986 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
988 bufsize = sizeof(hashbuf);
989 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
991 err = cfs_crypto_hash_final(req, hashbuf, &bufsize);
992 memcpy(buf, hashbuf, buflen);
995 err = cfs_crypto_hash_final(req, buf, &bufsize);