Whamcloud - gitweb
LU-7990 rpc: increase bulk size
[fs/lustre-release.git] / lustre / ptlrpc / sec_bulk.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/sec_bulk.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #include <libcfs/libcfs.h>
44
45 #include <obd.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>
53
54 #include "ptlrpc_internal.h"
55
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");
60
61
62 /****************************************
63  * bulk encryption page pools           *
64  ****************************************/
65
66
67 #define PTRS_PER_PAGE   (PAGE_CACHE_SIZE / sizeof(void *))
68 #define PAGES_PER_POOL  (PTRS_PER_PAGE)
69
70 #define IDLE_IDX_MAX            (100)
71 #define IDLE_IDX_WEIGHT         (3)
72
73 #define CACHE_QUIESCENT_PERIOD  (20)
74
75 static struct ptlrpc_enc_page_pool {
76         /*
77          * constants
78          */
79         unsigned long    epp_max_pages;   /* maximum pages can hold, const */
80         unsigned int     epp_max_pools;   /* number of pools, const */
81
82         /*
83          * wait queue in case of not enough free pages.
84          */
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 */
89
90         /*
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.
96          */
97         unsigned long    epp_idle_idx;
98
99         /* last shrink time due to mem tight */
100         long             epp_last_shrink;
101         long             epp_last_access;
102
103         /*
104          * in-pool pages bookkeeping
105          */
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 */
109
110         /*
111          * statistics
112          */
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 */
123         /*
124          * pointers to pools, may be vmalloc'd
125          */
126         struct page    ***epp_pools;
127 } page_pools;
128
129 /*
130  * memory shrinker
131  */
132 static const int pools_shrinker_seeks = DEFAULT_SEEKS;
133 static struct shrinker *pools_shrinker;
134
135
136 /*
137  * /proc/fs/lustre/sptlrpc/encrypt_page_pools
138  */
139 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
140 {
141         spin_lock(&page_pools.epp_lock);
142
143         seq_printf(m, "physical pages:          %lu\n"
144                    "pages per pool:          %lu\n"
145                    "max pages:               %lu\n"
146                    "max pools:               %u\n"
147                    "total pages:             %lu\n"
148                    "total free:              %lu\n"
149                    "idle index:              %lu/100\n"
150                    "last shrink:             %lds\n"
151                    "last access:             %lds\n"
152                    "max pages reached:       %lu\n"
153                    "grows:                   %u\n"
154                    "grows failure:           %u\n"
155                    "shrinks:                 %u\n"
156                    "cache access:            %lu\n"
157                    "cache missing:           %lu\n"
158                    "low free mark:           %lu\n"
159                    "max waitqueue depth:     %u\n"
160                    "max wait time:           "CFS_TIME_T"/%lu\n"
161                    "out of mem:             %lu\n",
162                    totalram_pages, PAGES_PER_POOL,
163                    page_pools.epp_max_pages,
164                    page_pools.epp_max_pools,
165                    page_pools.epp_total_pages,
166                    page_pools.epp_free_pages,
167                    page_pools.epp_idle_idx,
168                    cfs_time_current_sec() - page_pools.epp_last_shrink,
169                    cfs_time_current_sec() - page_pools.epp_last_access,
170                    page_pools.epp_st_max_pages,
171                    page_pools.epp_st_grows,
172                    page_pools.epp_st_grow_fails,
173                    page_pools.epp_st_shrinks,
174                    page_pools.epp_st_access,
175                    page_pools.epp_st_missings,
176                    page_pools.epp_st_lowfree,
177                    page_pools.epp_st_max_wqlen,
178                    page_pools.epp_st_max_wait,
179                    msecs_to_jiffies(MSEC_PER_SEC),
180                    page_pools.epp_st_outofmem);
181
182         spin_unlock(&page_pools.epp_lock);
183         return 0;
184 }
185
186 static void enc_pools_release_free_pages(long npages)
187 {
188         int     p_idx, g_idx;
189         int     p_idx_max1, p_idx_max2;
190
191         LASSERT(npages > 0);
192         LASSERT(npages <= page_pools.epp_free_pages);
193         LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
194
195         /* max pool index before the release */
196         p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
197
198         page_pools.epp_free_pages -= npages;
199         page_pools.epp_total_pages -= npages;
200
201         /* max pool index after the release */
202         p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
203                      ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
204
205         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
206         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
207         LASSERT(page_pools.epp_pools[p_idx]);
208
209         while (npages--) {
210                 LASSERT(page_pools.epp_pools[p_idx]);
211                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
212
213                 __free_page(page_pools.epp_pools[p_idx][g_idx]);
214                 page_pools.epp_pools[p_idx][g_idx] = NULL;
215
216                 if (++g_idx == PAGES_PER_POOL) {
217                         p_idx++;
218                         g_idx = 0;
219                 }
220         }
221
222         /* free unused pools */
223         while (p_idx_max1 < p_idx_max2) {
224                 LASSERT(page_pools.epp_pools[p_idx_max2]);
225                 OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_CACHE_SIZE);
226                 page_pools.epp_pools[p_idx_max2] = NULL;
227                 p_idx_max2--;
228         }
229 }
230
231 /*
232  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
233  */
234 static unsigned long enc_pools_shrink_count(struct shrinker *s,
235                                             struct shrink_control *sc)
236 {
237         /*
238          * if no pool access for a long time, we consider it's fully idle.
239          * a little race here is fine.
240          */
241         if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
242                      CACHE_QUIESCENT_PERIOD)) {
243                 spin_lock(&page_pools.epp_lock);
244                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
245                 spin_unlock(&page_pools.epp_lock);
246         }
247
248         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
249         return max((int)page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
250                 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
251 }
252
253 /*
254  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
255  */
256 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
257                                            struct shrink_control *sc)
258 {
259         spin_lock(&page_pools.epp_lock);
260         sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
261                               page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
262         if (sc->nr_to_scan > 0) {
263                 enc_pools_release_free_pages(sc->nr_to_scan);
264                 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
265                        (long)sc->nr_to_scan, page_pools.epp_free_pages);
266
267                 page_pools.epp_st_shrinks++;
268                 page_pools.epp_last_shrink = cfs_time_current_sec();
269         }
270         spin_unlock(&page_pools.epp_lock);
271
272         /*
273          * if no pool access for a long time, we consider it's fully idle.
274          * a little race here is fine.
275          */
276         if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
277                      CACHE_QUIESCENT_PERIOD)) {
278                 spin_lock(&page_pools.epp_lock);
279                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
280                 spin_unlock(&page_pools.epp_lock);
281         }
282
283         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
284         return sc->nr_to_scan;
285 }
286
287 #ifndef HAVE_SHRINKER_COUNT
288 /*
289  * could be called frequently for query (@nr_to_scan == 0).
290  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
291  */
292 static int enc_pools_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
293 {
294         struct shrink_control scv = {
295                 .nr_to_scan = shrink_param(sc, nr_to_scan),
296                 .gfp_mask   = shrink_param(sc, gfp_mask)
297         };
298 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
299         struct shrinker* shrinker = NULL;
300 #endif
301
302         enc_pools_shrink_scan(shrinker, &scv);
303
304         return enc_pools_shrink_count(shrinker, &scv);
305 }
306
307 #endif /* HAVE_SHRINKER_COUNT */
308
309 static inline
310 int npages_to_npools(unsigned long npages)
311 {
312         return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
313 }
314
315 /*
316  * return how many pages cleaned up.
317  */
318 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
319 {
320         unsigned long cleaned = 0;
321         int           i, j;
322
323         for (i = 0; i < npools; i++) {
324                 if (pools[i]) {
325                         for (j = 0; j < PAGES_PER_POOL; j++) {
326                                 if (pools[i][j]) {
327                                         __free_page(pools[i][j]);
328                                         cleaned++;
329                                 }
330                         }
331                         OBD_FREE(pools[i], PAGE_CACHE_SIZE);
332                         pools[i] = NULL;
333                 }
334         }
335
336         return cleaned;
337 }
338
339 /*
340  * merge @npools pointed by @pools which contains @npages new pages
341  * into current pools.
342  *
343  * we have options to avoid most memory copy with some tricks. but we choose
344  * the simplest way to avoid complexity. It's not frequently called.
345  */
346 static void enc_pools_insert(struct page ***pools, int npools, int npages)
347 {
348         int     freeslot;
349         int     op_idx, np_idx, og_idx, ng_idx;
350         int     cur_npools, end_npools;
351
352         LASSERT(npages > 0);
353         LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
354         LASSERT(npages_to_npools(npages) == npools);
355         LASSERT(page_pools.epp_growing);
356
357         spin_lock(&page_pools.epp_lock);
358
359         /*
360          * (1) fill all the free slots of current pools.
361          */
362         /* free slots are those left by rent pages, and the extra ones with
363          * index >= total_pages, locate at the tail of last pool. */
364         freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
365         if (freeslot != 0)
366                 freeslot = PAGES_PER_POOL - freeslot;
367         freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
368
369         op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
370         og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
371         np_idx = npools - 1;
372         ng_idx = (npages - 1) % PAGES_PER_POOL;
373
374         while (freeslot) {
375                 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
376                 LASSERT(pools[np_idx][ng_idx] != NULL);
377
378                 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
379                 pools[np_idx][ng_idx] = NULL;
380
381                 freeslot--;
382
383                 if (++og_idx == PAGES_PER_POOL) {
384                         op_idx++;
385                         og_idx = 0;
386                 }
387                 if (--ng_idx < 0) {
388                         if (np_idx == 0)
389                                 break;
390                         np_idx--;
391                         ng_idx = PAGES_PER_POOL - 1;
392                 }
393         }
394
395         /*
396          * (2) add pools if needed.
397          */
398         cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
399                      PAGES_PER_POOL;
400         end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
401                      PAGES_PER_POOL;
402         LASSERT(end_npools <= page_pools.epp_max_pools);
403
404         np_idx = 0;
405         while (cur_npools < end_npools) {
406                 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
407                 LASSERT(np_idx < npools);
408                 LASSERT(pools[np_idx] != NULL);
409
410                 page_pools.epp_pools[cur_npools++] = pools[np_idx];
411                 pools[np_idx++] = NULL;
412         }
413
414         page_pools.epp_total_pages += npages;
415         page_pools.epp_free_pages += npages;
416         page_pools.epp_st_lowfree = page_pools.epp_free_pages;
417
418         if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
419                 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
420
421         CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
422                page_pools.epp_total_pages);
423
424         spin_unlock(&page_pools.epp_lock);
425 }
426
427 static int enc_pools_add_pages(int npages)
428 {
429         static DEFINE_MUTEX(add_pages_mutex);
430         struct page   ***pools;
431         int             npools, alloced = 0;
432         int             i, j, rc = -ENOMEM;
433
434         if (npages < PTLRPC_MAX_BRW_PAGES)
435                 npages = PTLRPC_MAX_BRW_PAGES;
436
437         mutex_lock(&add_pages_mutex);
438
439         if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
440                 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
441         LASSERT(npages > 0);
442
443         page_pools.epp_st_grows++;
444
445         npools = npages_to_npools(npages);
446         OBD_ALLOC(pools, npools * sizeof(*pools));
447         if (pools == NULL)
448                 goto out;
449
450         for (i = 0; i < npools; i++) {
451                 OBD_ALLOC(pools[i], PAGE_CACHE_SIZE);
452                 if (pools[i] == NULL)
453                         goto out_pools;
454
455                 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
456                         pools[i][j] = alloc_page(GFP_NOFS |
457                                                  __GFP_HIGHMEM);
458                         if (pools[i][j] == NULL)
459                                 goto out_pools;
460
461                         alloced++;
462                 }
463         }
464         LASSERT(alloced == npages);
465
466         enc_pools_insert(pools, npools, npages);
467         CDEBUG(D_SEC, "added %d pages into pools\n", npages);
468         rc = 0;
469
470 out_pools:
471         enc_pools_cleanup(pools, npools);
472         OBD_FREE(pools, npools * sizeof(*pools));
473 out:
474         if (rc) {
475                 page_pools.epp_st_grow_fails++;
476                 CERROR("Failed to allocate %d enc pages\n", npages);
477         }
478
479         mutex_unlock(&add_pages_mutex);
480         return rc;
481 }
482
483 static inline void enc_pools_wakeup(void)
484 {
485         assert_spin_locked(&page_pools.epp_lock);
486
487         if (unlikely(page_pools.epp_waitqlen)) {
488                 LASSERT(waitqueue_active(&page_pools.epp_waitq));
489                 wake_up_all(&page_pools.epp_waitq);
490         }
491 }
492
493 static int enc_pools_should_grow(int page_needed, long now)
494 {
495         /* don't grow if someone else is growing the pools right now,
496          * or the pools has reached its full capacity
497          */
498         if (page_pools.epp_growing ||
499             page_pools.epp_total_pages == page_pools.epp_max_pages)
500                 return 0;
501
502         /* if total pages is not enough, we need to grow */
503         if (page_pools.epp_total_pages < page_needed)
504                 return 1;
505
506         /*
507          * we wanted to return 0 here if there was a shrink just happened
508          * moment ago, but this may cause deadlock if both client and ost
509          * live on single node.
510          */
511 #if 0
512         if (now - page_pools.epp_last_shrink < 2)
513                 return 0;
514 #endif
515
516         /*
517          * here we perhaps need consider other factors like wait queue
518          * length, idle index, etc. ?
519          */
520
521         /* grow the pools in any other cases */
522         return 1;
523 }
524
525 /*
526  * Export the number of free pages in the pool
527  */
528 int get_free_pages_in_pool(void)
529 {
530         return page_pools.epp_free_pages;
531 }
532 EXPORT_SYMBOL(get_free_pages_in_pool);
533
534 /*
535  * Let outside world know if enc_pool full capacity is reached
536  */
537 int pool_is_at_full_capacity(void)
538 {
539         return (page_pools.epp_total_pages == page_pools.epp_max_pages);
540 }
541 EXPORT_SYMBOL(pool_is_at_full_capacity);
542
543 /*
544  * we allocate the requested pages atomically.
545  */
546 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
547 {
548         wait_queue_t  waitlink;
549         unsigned long   this_idle = -1;
550         cfs_time_t      tick = 0;
551         long            now;
552         int             p_idx, g_idx;
553         int             i;
554
555         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
556         LASSERT(desc->bd_iov_count > 0);
557         LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
558
559         /* resent bulk, enc iov might have been allocated previously */
560         if (GET_ENC_KIOV(desc) != NULL)
561                 return 0;
562
563         OBD_ALLOC_LARGE(GET_ENC_KIOV(desc),
564                   desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
565         if (GET_ENC_KIOV(desc) == NULL)
566                 return -ENOMEM;
567
568         spin_lock(&page_pools.epp_lock);
569
570         page_pools.epp_st_access++;
571 again:
572         if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
573                 if (tick == 0)
574                         tick = cfs_time_current();
575
576                 now = cfs_time_current_sec();
577
578                 page_pools.epp_st_missings++;
579                 page_pools.epp_pages_short += desc->bd_iov_count;
580
581                 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
582                         page_pools.epp_growing = 1;
583
584                         spin_unlock(&page_pools.epp_lock);
585                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
586                         spin_lock(&page_pools.epp_lock);
587
588                         page_pools.epp_growing = 0;
589
590                         enc_pools_wakeup();
591                 } else {
592                         if (page_pools.epp_growing) {
593                                 if (++page_pools.epp_waitqlen >
594                                     page_pools.epp_st_max_wqlen)
595                                         page_pools.epp_st_max_wqlen =
596                                                         page_pools.epp_waitqlen;
597
598                                 set_current_state(TASK_UNINTERRUPTIBLE);
599                                 init_waitqueue_entry(&waitlink, current);
600                                 add_wait_queue(&page_pools.epp_waitq,
601                                                &waitlink);
602
603                                 spin_unlock(&page_pools.epp_lock);
604                                 schedule();
605                                 remove_wait_queue(&page_pools.epp_waitq,
606                                                   &waitlink);
607                                 LASSERT(page_pools.epp_waitqlen > 0);
608                                 spin_lock(&page_pools.epp_lock);
609                                 page_pools.epp_waitqlen--;
610                         } else {
611                                 /* ptlrpcd thread should not sleep in that case,
612                                  * or deadlock may occur!
613                                  * Instead, return -ENOMEM so that upper layers
614                                  * will put request back in queue. */
615                                 page_pools.epp_st_outofmem++;
616                                 spin_unlock(&page_pools.epp_lock);
617                                 OBD_FREE_LARGE(GET_ENC_KIOV(desc),
618                                                desc->bd_iov_count *
619                                                 sizeof(*GET_ENC_KIOV(desc)));
620                                 GET_ENC_KIOV(desc) = NULL;
621                                 return -ENOMEM;
622                         }
623                 }
624
625                 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
626                 page_pools.epp_pages_short -= desc->bd_iov_count;
627
628                 this_idle = 0;
629                 goto again;
630         }
631
632         /* record max wait time */
633         if (unlikely(tick != 0)) {
634                 tick = cfs_time_current() - tick;
635                 if (tick > page_pools.epp_st_max_wait)
636                         page_pools.epp_st_max_wait = tick;
637         }
638
639         /* proceed with rest of allocation */
640         page_pools.epp_free_pages -= desc->bd_iov_count;
641
642         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
643         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
644
645         for (i = 0; i < desc->bd_iov_count; i++) {
646                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
647                 BD_GET_ENC_KIOV(desc, i).kiov_page =
648                        page_pools.epp_pools[p_idx][g_idx];
649                 page_pools.epp_pools[p_idx][g_idx] = NULL;
650
651                 if (++g_idx == PAGES_PER_POOL) {
652                         p_idx++;
653                         g_idx = 0;
654                 }
655         }
656
657         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
658                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
659
660         /*
661          * new idle index = (old * weight + new) / (weight + 1)
662          */
663         if (this_idle == -1) {
664                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
665                             page_pools.epp_total_pages;
666         }
667         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
668                                    this_idle) /
669                                   (IDLE_IDX_WEIGHT + 1);
670
671         page_pools.epp_last_access = cfs_time_current_sec();
672
673         spin_unlock(&page_pools.epp_lock);
674         return 0;
675 }
676 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
677
678 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
679 {
680         int     p_idx, g_idx;
681         int     i;
682
683         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
684
685         if (GET_ENC_KIOV(desc) == NULL)
686                 return;
687
688         LASSERT(desc->bd_iov_count > 0);
689
690         spin_lock(&page_pools.epp_lock);
691
692         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
693         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
694
695         LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
696                 page_pools.epp_total_pages);
697         LASSERT(page_pools.epp_pools[p_idx]);
698
699         for (i = 0; i < desc->bd_iov_count; i++) {
700                 LASSERT(BD_GET_ENC_KIOV(desc, i).kiov_page != NULL);
701                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
702                 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
703
704                 page_pools.epp_pools[p_idx][g_idx] =
705                         BD_GET_ENC_KIOV(desc, i).kiov_page;
706
707                 if (++g_idx == PAGES_PER_POOL) {
708                         p_idx++;
709                         g_idx = 0;
710                 }
711         }
712
713         page_pools.epp_free_pages += desc->bd_iov_count;
714
715         enc_pools_wakeup();
716
717         spin_unlock(&page_pools.epp_lock);
718
719         OBD_FREE_LARGE(GET_ENC_KIOV(desc),
720                  desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
721         GET_ENC_KIOV(desc) = NULL;
722 }
723
724 /*
725  * we don't do much stuff for add_user/del_user anymore, except adding some
726  * initial pages in add_user() if current pools are empty, rest would be
727  * handled by the pools's self-adaption.
728  */
729 int sptlrpc_enc_pool_add_user(void)
730 {
731         int     need_grow = 0;
732
733         spin_lock(&page_pools.epp_lock);
734         if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
735                 page_pools.epp_growing = 1;
736                 need_grow = 1;
737         }
738         spin_unlock(&page_pools.epp_lock);
739
740         if (need_grow) {
741                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
742                                     PTLRPC_MAX_BRW_PAGES);
743
744                 spin_lock(&page_pools.epp_lock);
745                 page_pools.epp_growing = 0;
746                 enc_pools_wakeup();
747                 spin_unlock(&page_pools.epp_lock);
748         }
749         return 0;
750 }
751 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
752
753 int sptlrpc_enc_pool_del_user(void)
754 {
755         return 0;
756 }
757 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
758
759 static inline void enc_pools_alloc(void)
760 {
761         LASSERT(page_pools.epp_max_pools);
762         OBD_ALLOC_LARGE(page_pools.epp_pools,
763                         page_pools.epp_max_pools *
764                         sizeof(*page_pools.epp_pools));
765 }
766
767 static inline void enc_pools_free(void)
768 {
769         LASSERT(page_pools.epp_max_pools);
770         LASSERT(page_pools.epp_pools);
771
772         OBD_FREE_LARGE(page_pools.epp_pools,
773                        page_pools.epp_max_pools *
774                        sizeof(*page_pools.epp_pools));
775 }
776
777 int sptlrpc_enc_pool_init(void)
778 {
779         DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
780                          enc_pools_shrink_count, enc_pools_shrink_scan);
781
782         page_pools.epp_max_pages = totalram_pages / 8;
783         if (enc_pool_max_memory_mb > 0 &&
784             enc_pool_max_memory_mb <= (totalram_pages >> mult))
785                 page_pools.epp_max_pages = enc_pool_max_memory_mb << mult;
786
787         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
788
789         init_waitqueue_head(&page_pools.epp_waitq);
790         page_pools.epp_waitqlen = 0;
791         page_pools.epp_pages_short = 0;
792
793         page_pools.epp_growing = 0;
794
795         page_pools.epp_idle_idx = 0;
796         page_pools.epp_last_shrink = cfs_time_current_sec();
797         page_pools.epp_last_access = cfs_time_current_sec();
798
799         spin_lock_init(&page_pools.epp_lock);
800         page_pools.epp_total_pages = 0;
801         page_pools.epp_free_pages = 0;
802
803         page_pools.epp_st_max_pages = 0;
804         page_pools.epp_st_grows = 0;
805         page_pools.epp_st_grow_fails = 0;
806         page_pools.epp_st_shrinks = 0;
807         page_pools.epp_st_access = 0;
808         page_pools.epp_st_missings = 0;
809         page_pools.epp_st_lowfree = 0;
810         page_pools.epp_st_max_wqlen = 0;
811         page_pools.epp_st_max_wait = 0;
812         page_pools.epp_st_outofmem = 0;
813
814         enc_pools_alloc();
815         if (page_pools.epp_pools == NULL)
816                 return -ENOMEM;
817
818         pools_shrinker = set_shrinker(pools_shrinker_seeks, &shvar);
819         if (pools_shrinker == NULL) {
820                 enc_pools_free();
821                 return -ENOMEM;
822         }
823
824         return 0;
825 }
826
827 void sptlrpc_enc_pool_fini(void)
828 {
829         unsigned long cleaned, npools;
830
831         LASSERT(pools_shrinker);
832         LASSERT(page_pools.epp_pools);
833         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
834
835         remove_shrinker(pools_shrinker);
836
837         npools = npages_to_npools(page_pools.epp_total_pages);
838         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
839         LASSERT(cleaned == page_pools.epp_total_pages);
840
841         enc_pools_free();
842
843         if (page_pools.epp_st_access > 0) {
844                 CDEBUG(D_SEC,
845                        "max pages %lu, grows %u, grow fails %u, shrinks %u, "
846                        "access %lu, missing %lu, max qlen %u, max wait "
847                        CFS_TIME_T"/%lu, out of mem %lu\n",
848                        page_pools.epp_st_max_pages, page_pools.epp_st_grows,
849                        page_pools.epp_st_grow_fails,
850                        page_pools.epp_st_shrinks, page_pools.epp_st_access,
851                        page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
852                        page_pools.epp_st_max_wait,
853                        msecs_to_jiffies(MSEC_PER_SEC),
854                        page_pools.epp_st_outofmem);
855         }
856 }
857
858
859 static int cfs_hash_alg_id[] = {
860         [BULK_HASH_ALG_NULL]    = CFS_HASH_ALG_NULL,
861         [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
862         [BULK_HASH_ALG_CRC32]   = CFS_HASH_ALG_CRC32,
863         [BULK_HASH_ALG_MD5]     = CFS_HASH_ALG_MD5,
864         [BULK_HASH_ALG_SHA1]    = CFS_HASH_ALG_SHA1,
865         [BULK_HASH_ALG_SHA256]  = CFS_HASH_ALG_SHA256,
866         [BULK_HASH_ALG_SHA384]  = CFS_HASH_ALG_SHA384,
867         [BULK_HASH_ALG_SHA512]  = CFS_HASH_ALG_SHA512,
868 };
869 const char * sptlrpc_get_hash_name(__u8 hash_alg)
870 {
871         return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
872 }
873
874 __u8 sptlrpc_get_hash_alg(const char *algname)
875 {
876         return cfs_crypto_hash_alg(algname);
877 }
878
879 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
880 {
881         struct ptlrpc_bulk_sec_desc *bsd;
882         int                          size = msg->lm_buflens[offset];
883
884         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
885         if (bsd == NULL) {
886                 CERROR("Invalid bulk sec desc: size %d\n", size);
887                 return -EINVAL;
888         }
889
890         if (swabbed) {
891                 __swab32s(&bsd->bsd_nob);
892         }
893
894         if (unlikely(bsd->bsd_version != 0)) {
895                 CERROR("Unexpected version %u\n", bsd->bsd_version);
896                 return -EPROTO;
897         }
898
899         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
900                 CERROR("Invalid type %u\n", bsd->bsd_type);
901                 return -EPROTO;
902         }
903
904         /* FIXME more sanity check here */
905
906         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
907                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
908                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
909                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
910                 return -EPROTO;
911         }
912
913         return 0;
914 }
915 EXPORT_SYMBOL(bulk_sec_desc_unpack);
916
917 /*
918  * Compute the checksum of an RPC buffer payload.  If the return \a buflen
919  * is not large enough, truncate the result to fit so that it is possible
920  * to use a hash function with a large hash space, but only use a part of
921  * the resulting hash.
922  */
923 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
924                               void *buf, int buflen)
925 {
926         struct cfs_crypto_hash_desc     *hdesc;
927         int                             hashsize;
928         unsigned int                    bufsize;
929         int                             i, err;
930
931         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
932         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
933         LASSERT(buflen >= 4);
934
935         hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
936         if (IS_ERR(hdesc)) {
937                 CERROR("Unable to initialize checksum hash %s\n",
938                        cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
939                 return PTR_ERR(hdesc);
940         }
941
942         hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
943
944         for (i = 0; i < desc->bd_iov_count; i++) {
945                 cfs_crypto_hash_update_page(hdesc,
946                                   BD_GET_KIOV(desc, i).kiov_page,
947                                   BD_GET_KIOV(desc, i).kiov_offset &
948                                               ~PAGE_MASK,
949                                   BD_GET_KIOV(desc, i).kiov_len);
950         }
951
952         if (hashsize > buflen) {
953                 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
954
955                 bufsize = sizeof(hashbuf);
956                 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
957                          bufsize, hashsize);
958                 err = cfs_crypto_hash_final(hdesc, hashbuf, &bufsize);
959                 memcpy(buf, hashbuf, buflen);
960         } else {
961                 bufsize = buflen;
962                 err = cfs_crypto_hash_final(hdesc, buf, &bufsize);
963         }
964
965         return err;
966 }