Whamcloud - gitweb
c091dd681667734c43284db5eb2be6ef9c450b18
[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         int     rc;
142
143         spin_lock(&page_pools.epp_lock);
144
145         rc = seq_printf(m,
146                       "physical pages:          %lu\n"
147                       "pages per pool:          %lu\n"
148                       "max pages:               %lu\n"
149                       "max pools:               %u\n"
150                       "total pages:             %lu\n"
151                       "total free:              %lu\n"
152                       "idle index:              %lu/100\n"
153                       "last shrink:             %lds\n"
154                       "last access:             %lds\n"
155                       "max pages reached:       %lu\n"
156                       "grows:                   %u\n"
157                       "grows failure:           %u\n"
158                       "shrinks:                 %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"
164                       "out of mem:             %lu\n"
165                       ,
166                       totalram_pages,
167                       PAGES_PER_POOL,
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
186                      );
187
188         spin_unlock(&page_pools.epp_lock);
189         return rc;
190 }
191
192 static void enc_pools_release_free_pages(long npages)
193 {
194         int     p_idx, g_idx;
195         int     p_idx_max1, p_idx_max2;
196
197         LASSERT(npages > 0);
198         LASSERT(npages <= page_pools.epp_free_pages);
199         LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
200
201         /* max pool index before the release */
202         p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
203
204         page_pools.epp_free_pages -= npages;
205         page_pools.epp_total_pages -= npages;
206
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);
210
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]);
214
215         while (npages--) {
216                 LASSERT(page_pools.epp_pools[p_idx]);
217                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
218
219                 __free_page(page_pools.epp_pools[p_idx][g_idx]);
220                 page_pools.epp_pools[p_idx][g_idx] = NULL;
221
222                 if (++g_idx == PAGES_PER_POOL) {
223                         p_idx++;
224                         g_idx = 0;
225                 }
226         }
227
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;
233                 p_idx_max2--;
234         }
235 }
236
237 /*
238  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
239  */
240 static unsigned long enc_pools_shrink_count(struct shrinker *s,
241                                             struct shrink_control *sc)
242 {
243         /*
244          * if no pool access for a long time, we consider it's fully idle.
245          * a little race here is fine.
246          */
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);
252         }
253
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;
257 }
258
259 /*
260  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
261  */
262 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
263                                            struct shrink_control *sc)
264 {
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);
272
273                 page_pools.epp_st_shrinks++;
274                 page_pools.epp_last_shrink = cfs_time_current_sec();
275         }
276         spin_unlock(&page_pools.epp_lock);
277
278         /*
279          * if no pool access for a long time, we consider it's fully idle.
280          * a little race here is fine.
281          */
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);
287         }
288
289         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
290         return sc->nr_to_scan;
291 }
292
293 #ifndef HAVE_SHRINKER_COUNT
294 /*
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.
297  */
298 static int enc_pools_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
299 {
300         struct shrink_control scv = {
301                 .nr_to_scan = shrink_param(sc, nr_to_scan),
302                 .gfp_mask   = shrink_param(sc, gfp_mask)
303         };
304 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
305         struct shrinker* shrinker = NULL;
306 #endif
307
308         enc_pools_shrink_scan(shrinker, &scv);
309
310         return enc_pools_shrink_count(shrinker, &scv);
311 }
312
313 #endif /* HAVE_SHRINKER_COUNT */
314
315 static inline
316 int npages_to_npools(unsigned long npages)
317 {
318         return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
319 }
320
321 /*
322  * return how many pages cleaned up.
323  */
324 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
325 {
326         unsigned long cleaned = 0;
327         int           i, j;
328
329         for (i = 0; i < npools; i++) {
330                 if (pools[i]) {
331                         for (j = 0; j < PAGES_PER_POOL; j++) {
332                                 if (pools[i][j]) {
333                                         __free_page(pools[i][j]);
334                                         cleaned++;
335                                 }
336                         }
337                         OBD_FREE(pools[i], PAGE_CACHE_SIZE);
338                         pools[i] = NULL;
339                 }
340         }
341
342         return cleaned;
343 }
344
345 /*
346  * merge @npools pointed by @pools which contains @npages new pages
347  * into current pools.
348  *
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.
351  */
352 static void enc_pools_insert(struct page ***pools, int npools, int npages)
353 {
354         int     freeslot;
355         int     op_idx, np_idx, og_idx, ng_idx;
356         int     cur_npools, end_npools;
357
358         LASSERT(npages > 0);
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);
362
363         spin_lock(&page_pools.epp_lock);
364
365         /*
366          * (1) fill all the free slots of current pools.
367          */
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;
371         if (freeslot != 0)
372                 freeslot = PAGES_PER_POOL - freeslot;
373         freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
374
375         op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
376         og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
377         np_idx = npools - 1;
378         ng_idx = (npages - 1) % PAGES_PER_POOL;
379
380         while (freeslot) {
381                 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
382                 LASSERT(pools[np_idx][ng_idx] != NULL);
383
384                 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
385                 pools[np_idx][ng_idx] = NULL;
386
387                 freeslot--;
388
389                 if (++og_idx == PAGES_PER_POOL) {
390                         op_idx++;
391                         og_idx = 0;
392                 }
393                 if (--ng_idx < 0) {
394                         if (np_idx == 0)
395                                 break;
396                         np_idx--;
397                         ng_idx = PAGES_PER_POOL - 1;
398                 }
399         }
400
401         /*
402          * (2) add pools if needed.
403          */
404         cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
405                      PAGES_PER_POOL;
406         end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
407                      PAGES_PER_POOL;
408         LASSERT(end_npools <= page_pools.epp_max_pools);
409
410         np_idx = 0;
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);
415
416                 page_pools.epp_pools[cur_npools++] = pools[np_idx];
417                 pools[np_idx++] = NULL;
418         }
419
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;
423
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;
426
427         CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
428                page_pools.epp_total_pages);
429
430         spin_unlock(&page_pools.epp_lock);
431 }
432
433 static int enc_pools_add_pages(int npages)
434 {
435         static DEFINE_MUTEX(add_pages_mutex);
436         struct page   ***pools;
437         int             npools, alloced = 0;
438         int             i, j, rc = -ENOMEM;
439
440         if (npages < PTLRPC_MAX_BRW_PAGES)
441                 npages = PTLRPC_MAX_BRW_PAGES;
442
443         mutex_lock(&add_pages_mutex);
444
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;
447         LASSERT(npages > 0);
448
449         page_pools.epp_st_grows++;
450
451         npools = npages_to_npools(npages);
452         OBD_ALLOC(pools, npools * sizeof(*pools));
453         if (pools == NULL)
454                 goto out;
455
456         for (i = 0; i < npools; i++) {
457                 OBD_ALLOC(pools[i], PAGE_CACHE_SIZE);
458                 if (pools[i] == NULL)
459                         goto out_pools;
460
461                 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
462                         pools[i][j] = alloc_page(GFP_NOFS |
463                                                  __GFP_HIGHMEM);
464                         if (pools[i][j] == NULL)
465                                 goto out_pools;
466
467                         alloced++;
468                 }
469         }
470         LASSERT(alloced == npages);
471
472         enc_pools_insert(pools, npools, npages);
473         CDEBUG(D_SEC, "added %d pages into pools\n", npages);
474         rc = 0;
475
476 out_pools:
477         enc_pools_cleanup(pools, npools);
478         OBD_FREE(pools, npools * sizeof(*pools));
479 out:
480         if (rc) {
481                 page_pools.epp_st_grow_fails++;
482                 CERROR("Failed to allocate %d enc pages\n", npages);
483         }
484
485         mutex_unlock(&add_pages_mutex);
486         return rc;
487 }
488
489 static inline void enc_pools_wakeup(void)
490 {
491         assert_spin_locked(&page_pools.epp_lock);
492
493         if (unlikely(page_pools.epp_waitqlen)) {
494                 LASSERT(waitqueue_active(&page_pools.epp_waitq));
495                 wake_up_all(&page_pools.epp_waitq);
496         }
497 }
498
499 static int enc_pools_should_grow(int page_needed, long now)
500 {
501         /* don't grow if someone else is growing the pools right now,
502          * or the pools has reached its full capacity
503          */
504         if (page_pools.epp_growing ||
505             page_pools.epp_total_pages == page_pools.epp_max_pages)
506                 return 0;
507
508         /* if total pages is not enough, we need to grow */
509         if (page_pools.epp_total_pages < page_needed)
510                 return 1;
511
512         /*
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.
516          */
517 #if 0
518         if (now - page_pools.epp_last_shrink < 2)
519                 return 0;
520 #endif
521
522         /*
523          * here we perhaps need consider other factors like wait queue
524          * length, idle index, etc. ?
525          */
526
527         /* grow the pools in any other cases */
528         return 1;
529 }
530
531 /*
532  * Export the number of free pages in the pool
533  */
534 int get_free_pages_in_pool(void)
535 {
536         return page_pools.epp_free_pages;
537 }
538 EXPORT_SYMBOL(get_free_pages_in_pool);
539
540 /*
541  * Let outside world know if enc_pool full capacity is reached
542  */
543 int pool_is_at_full_capacity(void)
544 {
545         return (page_pools.epp_total_pages == page_pools.epp_max_pages);
546 }
547 EXPORT_SYMBOL(pool_is_at_full_capacity);
548
549 /*
550  * we allocate the requested pages atomically.
551  */
552 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
553 {
554         wait_queue_t  waitlink;
555         unsigned long   this_idle = -1;
556         cfs_time_t      tick = 0;
557         long            now;
558         int             p_idx, g_idx;
559         int             i;
560
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);
564
565         /* resent bulk, enc iov might have been allocated previously */
566         if (GET_ENC_KIOV(desc) != NULL)
567                 return 0;
568
569         OBD_ALLOC(GET_ENC_KIOV(desc),
570                   desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
571         if (GET_ENC_KIOV(desc) == NULL)
572                 return -ENOMEM;
573
574         spin_lock(&page_pools.epp_lock);
575
576         page_pools.epp_st_access++;
577 again:
578         if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
579                 if (tick == 0)
580                         tick = cfs_time_current();
581
582                 now = cfs_time_current_sec();
583
584                 page_pools.epp_st_missings++;
585                 page_pools.epp_pages_short += desc->bd_iov_count;
586
587                 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
588                         page_pools.epp_growing = 1;
589
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);
593
594                         page_pools.epp_growing = 0;
595
596                         enc_pools_wakeup();
597                 } else {
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;
603
604                                 set_current_state(TASK_UNINTERRUPTIBLE);
605                                 init_waitqueue_entry(&waitlink, current);
606                                 add_wait_queue(&page_pools.epp_waitq,
607                                                &waitlink);
608
609                                 spin_unlock(&page_pools.epp_lock);
610                                 schedule();
611                                 remove_wait_queue(&page_pools.epp_waitq,
612                                                   &waitlink);
613                                 LASSERT(page_pools.epp_waitqlen > 0);
614                                 spin_lock(&page_pools.epp_lock);
615                                 page_pools.epp_waitqlen--;
616                         } else {
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),
624                                          desc->bd_iov_count *
625                                                 sizeof(*GET_ENC_KIOV(desc)));
626                                 GET_ENC_KIOV(desc) = NULL;
627                                 return -ENOMEM;
628                         }
629                 }
630
631                 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
632                 page_pools.epp_pages_short -= desc->bd_iov_count;
633
634                 this_idle = 0;
635                 goto again;
636         }
637
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;
643         }
644
645         /* proceed with rest of allocation */
646         page_pools.epp_free_pages -= desc->bd_iov_count;
647
648         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
649         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
650
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;
656
657                 if (++g_idx == PAGES_PER_POOL) {
658                         p_idx++;
659                         g_idx = 0;
660                 }
661         }
662
663         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
664                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
665
666         /*
667          * new idle index = (old * weight + new) / (weight + 1)
668          */
669         if (this_idle == -1) {
670                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
671                             page_pools.epp_total_pages;
672         }
673         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
674                                    this_idle) /
675                                   (IDLE_IDX_WEIGHT + 1);
676
677         page_pools.epp_last_access = cfs_time_current_sec();
678
679         spin_unlock(&page_pools.epp_lock);
680         return 0;
681 }
682 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
683
684 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
685 {
686         int     p_idx, g_idx;
687         int     i;
688
689         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
690
691         if (GET_ENC_KIOV(desc) == NULL)
692                 return;
693
694         LASSERT(desc->bd_iov_count > 0);
695
696         spin_lock(&page_pools.epp_lock);
697
698         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
699         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
700
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]);
704
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);
709
710                 page_pools.epp_pools[p_idx][g_idx] =
711                         BD_GET_ENC_KIOV(desc, i).kiov_page;
712
713                 if (++g_idx == PAGES_PER_POOL) {
714                         p_idx++;
715                         g_idx = 0;
716                 }
717         }
718
719         page_pools.epp_free_pages += desc->bd_iov_count;
720
721         enc_pools_wakeup();
722
723         spin_unlock(&page_pools.epp_lock);
724
725         OBD_FREE(GET_ENC_KIOV(desc),
726                  desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
727         GET_ENC_KIOV(desc) = NULL;
728 }
729
730 /*
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.
734  */
735 int sptlrpc_enc_pool_add_user(void)
736 {
737         int     need_grow = 0;
738
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;
742                 need_grow = 1;
743         }
744         spin_unlock(&page_pools.epp_lock);
745
746         if (need_grow) {
747                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
748                                     PTLRPC_MAX_BRW_PAGES);
749
750                 spin_lock(&page_pools.epp_lock);
751                 page_pools.epp_growing = 0;
752                 enc_pools_wakeup();
753                 spin_unlock(&page_pools.epp_lock);
754         }
755         return 0;
756 }
757 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
758
759 int sptlrpc_enc_pool_del_user(void)
760 {
761         return 0;
762 }
763 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
764
765 static inline void enc_pools_alloc(void)
766 {
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));
771 }
772
773 static inline void enc_pools_free(void)
774 {
775         LASSERT(page_pools.epp_max_pools);
776         LASSERT(page_pools.epp_pools);
777
778         OBD_FREE_LARGE(page_pools.epp_pools,
779                        page_pools.epp_max_pools *
780                        sizeof(*page_pools.epp_pools));
781 }
782
783 int sptlrpc_enc_pool_init(void)
784 {
785         DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
786                          enc_pools_shrink_count, enc_pools_shrink_scan);
787
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;
792
793         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
794
795         init_waitqueue_head(&page_pools.epp_waitq);
796         page_pools.epp_waitqlen = 0;
797         page_pools.epp_pages_short = 0;
798
799         page_pools.epp_growing = 0;
800
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();
804
805         spin_lock_init(&page_pools.epp_lock);
806         page_pools.epp_total_pages = 0;
807         page_pools.epp_free_pages = 0;
808
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;
819
820         enc_pools_alloc();
821         if (page_pools.epp_pools == NULL)
822                 return -ENOMEM;
823
824         pools_shrinker = set_shrinker(pools_shrinker_seeks, &shvar);
825         if (pools_shrinker == NULL) {
826                 enc_pools_free();
827                 return -ENOMEM;
828         }
829
830         return 0;
831 }
832
833 void sptlrpc_enc_pool_fini(void)
834 {
835         unsigned long cleaned, npools;
836
837         LASSERT(pools_shrinker);
838         LASSERT(page_pools.epp_pools);
839         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
840
841         remove_shrinker(pools_shrinker);
842
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);
846
847         enc_pools_free();
848
849         if (page_pools.epp_st_access > 0) {
850                 CDEBUG(D_SEC,
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);
861         }
862 }
863
864
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,
874 };
875 const char * sptlrpc_get_hash_name(__u8 hash_alg)
876 {
877         return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
878 }
879
880 __u8 sptlrpc_get_hash_alg(const char *algname)
881 {
882         return cfs_crypto_hash_alg(algname);
883 }
884
885 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
886 {
887         struct ptlrpc_bulk_sec_desc *bsd;
888         int                          size = msg->lm_buflens[offset];
889
890         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
891         if (bsd == NULL) {
892                 CERROR("Invalid bulk sec desc: size %d\n", size);
893                 return -EINVAL;
894         }
895
896         if (swabbed) {
897                 __swab32s(&bsd->bsd_nob);
898         }
899
900         if (unlikely(bsd->bsd_version != 0)) {
901                 CERROR("Unexpected version %u\n", bsd->bsd_version);
902                 return -EPROTO;
903         }
904
905         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
906                 CERROR("Invalid type %u\n", bsd->bsd_type);
907                 return -EPROTO;
908         }
909
910         /* FIXME more sanity check here */
911
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);
916                 return -EPROTO;
917         }
918
919         return 0;
920 }
921 EXPORT_SYMBOL(bulk_sec_desc_unpack);
922
923 /*
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.
928  */
929 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
930                               void *buf, int buflen)
931 {
932         struct cfs_crypto_hash_desc     *hdesc;
933         int                             hashsize;
934         unsigned int                    bufsize;
935         int                             i, err;
936
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);
940
941         hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
942         if (IS_ERR(hdesc)) {
943                 CERROR("Unable to initialize checksum hash %s\n",
944                        cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
945                 return PTR_ERR(hdesc);
946         }
947
948         hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
949
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 &
954                                               ~PAGE_MASK,
955                                   BD_GET_KIOV(desc, i).kiov_len);
956         }
957
958         if (hashsize > buflen) {
959                 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
960
961                 bufsize = sizeof(hashbuf);
962                 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
963                          bufsize, hashsize);
964                 err = cfs_crypto_hash_final(hdesc, hashbuf, &bufsize);
965                 memcpy(buf, hashbuf, buflen);
966         } else {
967                 bufsize = buflen;
968                 err = cfs_crypto_hash_final(hdesc, buf, &bufsize);
969         }
970
971         return err;
972 }