Linux 4.7-rc6
[cascardo/linux.git] / drivers / staging / lustre / 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 "../../include/linux/libcfs/libcfs.h"
44
45 #include "../include/obd.h"
46 #include "../include/obd_cksum.h"
47 #include "../include/obd_class.h"
48 #include "../include/obd_support.h"
49 #include "../include/lustre_net.h"
50 #include "../include/lustre_import.h"
51 #include "../include/lustre_dlm.h"
52 #include "../include/lustre_sec.h"
53
54 #include "ptlrpc_internal.h"
55
56 /****************************************
57  * bulk encryption page pools      *
58  ****************************************/
59
60 #define POINTERS_PER_PAGE       (PAGE_SIZE / sizeof(void *))
61 #define PAGES_PER_POOL          (POINTERS_PER_PAGE)
62
63 #define IDLE_IDX_MAX     (100)
64 #define IDLE_IDX_WEIGHT  (3)
65
66 #define CACHE_QUIESCENT_PERIOD  (20)
67
68 static struct ptlrpc_enc_page_pool {
69         /*
70          * constants
71          */
72         unsigned long    epp_max_pages;   /* maximum pages can hold, const */
73         unsigned int     epp_max_pools;   /* number of pools, const */
74
75         /*
76          * wait queue in case of not enough free pages.
77          */
78         wait_queue_head_t      epp_waitq;       /* waiting threads */
79         unsigned int     epp_waitqlen;    /* wait queue length */
80         unsigned long    epp_pages_short; /* # of pages wanted of in-q users */
81         unsigned int     epp_growing:1;   /* during adding pages */
82
83         /*
84          * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
85          * this is counted based on each time when getting pages from
86          * the pools, not based on time. which means in case that system
87          * is idled for a while but the idle_idx might still be low if no
88          * activities happened in the pools.
89          */
90         unsigned long    epp_idle_idx;
91
92         /* last shrink time due to mem tight */
93         time64_t         epp_last_shrink;
94         time64_t         epp_last_access;
95
96         /*
97          * in-pool pages bookkeeping
98          */
99         spinlock_t       epp_lock;         /* protect following fields */
100         unsigned long    epp_total_pages; /* total pages in pools */
101         unsigned long    epp_free_pages;  /* current pages available */
102
103         /*
104          * statistics
105          */
106         unsigned long    epp_st_max_pages;      /* # of pages ever reached */
107         unsigned int     epp_st_grows;    /* # of grows */
108         unsigned int     epp_st_grow_fails;     /* # of add pages failures */
109         unsigned int     epp_st_shrinks;        /* # of shrinks */
110         unsigned long    epp_st_access;  /* # of access */
111         unsigned long    epp_st_missings;       /* # of cache missing */
112         unsigned long    epp_st_lowfree;        /* lowest free pages reached */
113         unsigned int     epp_st_max_wqlen;      /* highest waitqueue length */
114         unsigned long       epp_st_max_wait;       /* in jiffies */
115         /*
116          * pointers to pools
117          */
118         struct page    ***epp_pools;
119 } page_pools;
120
121 /*
122  * /sys/kernel/debug/lustre/sptlrpc/encrypt_page_pools
123  */
124 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
125 {
126         spin_lock(&page_pools.epp_lock);
127
128         seq_printf(m,
129                    "physical pages:       %lu\n"
130                    "pages per pool:       %lu\n"
131                    "max pages:         %lu\n"
132                    "max pools:         %u\n"
133                    "total pages:             %lu\n"
134                    "total free:       %lu\n"
135                    "idle index:       %lu/100\n"
136                    "last shrink:             %lds\n"
137                    "last access:             %lds\n"
138                    "max pages reached:       %lu\n"
139                    "grows:                 %u\n"
140                    "grows failure:         %u\n"
141                    "shrinks:             %u\n"
142                    "cache access:           %lu\n"
143                    "cache missing:         %lu\n"
144                    "low free mark:         %lu\n"
145                    "max waitqueue depth:     %u\n"
146                    "max wait time:         %ld/%u\n",
147                    totalram_pages,
148                    PAGES_PER_POOL,
149                    page_pools.epp_max_pages,
150                    page_pools.epp_max_pools,
151                    page_pools.epp_total_pages,
152                    page_pools.epp_free_pages,
153                    page_pools.epp_idle_idx,
154                    (long)(ktime_get_seconds() - page_pools.epp_last_shrink),
155                    (long)(ktime_get_seconds() - page_pools.epp_last_access),
156                    page_pools.epp_st_max_pages,
157                    page_pools.epp_st_grows,
158                    page_pools.epp_st_grow_fails,
159                    page_pools.epp_st_shrinks,
160                    page_pools.epp_st_access,
161                    page_pools.epp_st_missings,
162                    page_pools.epp_st_lowfree,
163                    page_pools.epp_st_max_wqlen,
164                    page_pools.epp_st_max_wait,
165                    HZ);
166
167         spin_unlock(&page_pools.epp_lock);
168
169         return 0;
170 }
171
172 static void enc_pools_release_free_pages(long npages)
173 {
174         int p_idx, g_idx;
175         int p_idx_max1, p_idx_max2;
176
177         LASSERT(npages > 0);
178         LASSERT(npages <= page_pools.epp_free_pages);
179         LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
180
181         /* max pool index before the release */
182         p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
183
184         page_pools.epp_free_pages -= npages;
185         page_pools.epp_total_pages -= npages;
186
187         /* max pool index after the release */
188         p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
189                      ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
190
191         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
192         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
193         LASSERT(page_pools.epp_pools[p_idx]);
194
195         while (npages--) {
196                 LASSERT(page_pools.epp_pools[p_idx]);
197                 LASSERT(page_pools.epp_pools[p_idx][g_idx]);
198
199                 __free_page(page_pools.epp_pools[p_idx][g_idx]);
200                 page_pools.epp_pools[p_idx][g_idx] = NULL;
201
202                 if (++g_idx == PAGES_PER_POOL) {
203                         p_idx++;
204                         g_idx = 0;
205                 }
206         }
207
208         /* free unused pools */
209         while (p_idx_max1 < p_idx_max2) {
210                 LASSERT(page_pools.epp_pools[p_idx_max2]);
211                 kfree(page_pools.epp_pools[p_idx_max2]);
212                 page_pools.epp_pools[p_idx_max2] = NULL;
213                 p_idx_max2--;
214         }
215 }
216
217 /*
218  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
219  */
220 static unsigned long enc_pools_shrink_count(struct shrinker *s,
221                                             struct shrink_control *sc)
222 {
223         /*
224          * if no pool access for a long time, we consider it's fully idle.
225          * a little race here is fine.
226          */
227         if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
228                      CACHE_QUIESCENT_PERIOD)) {
229                 spin_lock(&page_pools.epp_lock);
230                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
231                 spin_unlock(&page_pools.epp_lock);
232         }
233
234         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
235         return max((int)page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
236                 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
237 }
238
239 /*
240  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
241  */
242 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
243                                            struct shrink_control *sc)
244 {
245         spin_lock(&page_pools.epp_lock);
246         sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
247                               page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
248         if (sc->nr_to_scan > 0) {
249                 enc_pools_release_free_pages(sc->nr_to_scan);
250                 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
251                        (long)sc->nr_to_scan, page_pools.epp_free_pages);
252
253                 page_pools.epp_st_shrinks++;
254                 page_pools.epp_last_shrink = ktime_get_seconds();
255         }
256         spin_unlock(&page_pools.epp_lock);
257
258         /*
259          * if no pool access for a long time, we consider it's fully idle.
260          * a little race here is fine.
261          */
262         if (unlikely(ktime_get_seconds() - page_pools.epp_last_access >
263                      CACHE_QUIESCENT_PERIOD)) {
264                 spin_lock(&page_pools.epp_lock);
265                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
266                 spin_unlock(&page_pools.epp_lock);
267         }
268
269         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
270         return sc->nr_to_scan;
271 }
272
273 static inline
274 int npages_to_npools(unsigned long npages)
275 {
276         return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
277 }
278
279 /*
280  * return how many pages cleaned up.
281  */
282 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
283 {
284         unsigned long cleaned = 0;
285         int i, j;
286
287         for (i = 0; i < npools; i++) {
288                 if (pools[i]) {
289                         for (j = 0; j < PAGES_PER_POOL; j++) {
290                                 if (pools[i][j]) {
291                                         __free_page(pools[i][j]);
292                                         cleaned++;
293                                 }
294                         }
295                         kfree(pools[i]);
296                         pools[i] = NULL;
297                 }
298         }
299
300         return cleaned;
301 }
302
303 static inline void enc_pools_wakeup(void)
304 {
305         assert_spin_locked(&page_pools.epp_lock);
306
307         if (unlikely(page_pools.epp_waitqlen)) {
308                 LASSERT(waitqueue_active(&page_pools.epp_waitq));
309                 wake_up_all(&page_pools.epp_waitq);
310         }
311 }
312
313 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
314 {
315         int p_idx, g_idx;
316         int i;
317
318         if (!desc->bd_enc_iov)
319                 return;
320
321         LASSERT(desc->bd_iov_count > 0);
322
323         spin_lock(&page_pools.epp_lock);
324
325         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
326         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
327
328         LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
329                 page_pools.epp_total_pages);
330         LASSERT(page_pools.epp_pools[p_idx]);
331
332         for (i = 0; i < desc->bd_iov_count; i++) {
333                 LASSERT(desc->bd_enc_iov[i].kiov_page);
334                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
335                 LASSERT(!page_pools.epp_pools[p_idx][g_idx]);
336
337                 page_pools.epp_pools[p_idx][g_idx] =
338                                         desc->bd_enc_iov[i].kiov_page;
339
340                 if (++g_idx == PAGES_PER_POOL) {
341                         p_idx++;
342                         g_idx = 0;
343                 }
344         }
345
346         page_pools.epp_free_pages += desc->bd_iov_count;
347
348         enc_pools_wakeup();
349
350         spin_unlock(&page_pools.epp_lock);
351
352         kfree(desc->bd_enc_iov);
353         desc->bd_enc_iov = NULL;
354 }
355 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
356
357 static inline void enc_pools_alloc(void)
358 {
359         LASSERT(page_pools.epp_max_pools);
360         page_pools.epp_pools =
361                 libcfs_kvzalloc(page_pools.epp_max_pools *
362                                 sizeof(*page_pools.epp_pools),
363                                 GFP_NOFS);
364 }
365
366 static inline void enc_pools_free(void)
367 {
368         LASSERT(page_pools.epp_max_pools);
369         LASSERT(page_pools.epp_pools);
370
371         kvfree(page_pools.epp_pools);
372 }
373
374 static struct shrinker pools_shrinker = {
375         .count_objects  = enc_pools_shrink_count,
376         .scan_objects   = enc_pools_shrink_scan,
377         .seeks          = DEFAULT_SEEKS,
378 };
379
380 int sptlrpc_enc_pool_init(void)
381 {
382         /*
383          * maximum capacity is 1/8 of total physical memory.
384          * is the 1/8 a good number?
385          */
386         page_pools.epp_max_pages = totalram_pages / 8;
387         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
388
389         init_waitqueue_head(&page_pools.epp_waitq);
390         page_pools.epp_waitqlen = 0;
391         page_pools.epp_pages_short = 0;
392
393         page_pools.epp_growing = 0;
394
395         page_pools.epp_idle_idx = 0;
396         page_pools.epp_last_shrink = ktime_get_seconds();
397         page_pools.epp_last_access = ktime_get_seconds();
398
399         spin_lock_init(&page_pools.epp_lock);
400         page_pools.epp_total_pages = 0;
401         page_pools.epp_free_pages = 0;
402
403         page_pools.epp_st_max_pages = 0;
404         page_pools.epp_st_grows = 0;
405         page_pools.epp_st_grow_fails = 0;
406         page_pools.epp_st_shrinks = 0;
407         page_pools.epp_st_access = 0;
408         page_pools.epp_st_missings = 0;
409         page_pools.epp_st_lowfree = 0;
410         page_pools.epp_st_max_wqlen = 0;
411         page_pools.epp_st_max_wait = 0;
412
413         enc_pools_alloc();
414         if (!page_pools.epp_pools)
415                 return -ENOMEM;
416
417         register_shrinker(&pools_shrinker);
418
419         return 0;
420 }
421
422 void sptlrpc_enc_pool_fini(void)
423 {
424         unsigned long cleaned, npools;
425
426         LASSERT(page_pools.epp_pools);
427         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
428
429         unregister_shrinker(&pools_shrinker);
430
431         npools = npages_to_npools(page_pools.epp_total_pages);
432         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
433         LASSERT(cleaned == page_pools.epp_total_pages);
434
435         enc_pools_free();
436
437         if (page_pools.epp_st_access > 0) {
438                 CDEBUG(D_SEC,
439                        "max pages %lu, grows %u, grow fails %u, shrinks %u, access %lu, missing %lu, max qlen %u, max wait %ld/%d\n",
440                        page_pools.epp_st_max_pages, page_pools.epp_st_grows,
441                        page_pools.epp_st_grow_fails,
442                        page_pools.epp_st_shrinks, page_pools.epp_st_access,
443                        page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
444                        page_pools.epp_st_max_wait, HZ);
445         }
446 }
447
448 static int cfs_hash_alg_id[] = {
449         [BULK_HASH_ALG_NULL]    = CFS_HASH_ALG_NULL,
450         [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
451         [BULK_HASH_ALG_CRC32]   = CFS_HASH_ALG_CRC32,
452         [BULK_HASH_ALG_MD5]     = CFS_HASH_ALG_MD5,
453         [BULK_HASH_ALG_SHA1]    = CFS_HASH_ALG_SHA1,
454         [BULK_HASH_ALG_SHA256]  = CFS_HASH_ALG_SHA256,
455         [BULK_HASH_ALG_SHA384]  = CFS_HASH_ALG_SHA384,
456         [BULK_HASH_ALG_SHA512]  = CFS_HASH_ALG_SHA512,
457 };
458
459 const char *sptlrpc_get_hash_name(__u8 hash_alg)
460 {
461         return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
462 }
463 EXPORT_SYMBOL(sptlrpc_get_hash_name);
464
465 __u8 sptlrpc_get_hash_alg(const char *algname)
466 {
467         return cfs_crypto_hash_alg(algname);
468 }
469 EXPORT_SYMBOL(sptlrpc_get_hash_alg);
470
471 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
472 {
473         struct ptlrpc_bulk_sec_desc *bsd;
474         int                       size = msg->lm_buflens[offset];
475
476         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
477         if (!bsd) {
478                 CERROR("Invalid bulk sec desc: size %d\n", size);
479                 return -EINVAL;
480         }
481
482         if (swabbed)
483                 __swab32s(&bsd->bsd_nob);
484
485         if (unlikely(bsd->bsd_version != 0)) {
486                 CERROR("Unexpected version %u\n", bsd->bsd_version);
487                 return -EPROTO;
488         }
489
490         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
491                 CERROR("Invalid type %u\n", bsd->bsd_type);
492                 return -EPROTO;
493         }
494
495         /* FIXME more sanity check here */
496
497         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
498                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
499                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
500                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
501                 return -EPROTO;
502         }
503
504         return 0;
505 }
506 EXPORT_SYMBOL(bulk_sec_desc_unpack);
507
508 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
509                               void *buf, int buflen)
510 {
511         struct cfs_crypto_hash_desc *hdesc;
512         int hashsize;
513         unsigned int bufsize;
514         int i, err;
515
516         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
517         LASSERT(buflen >= 4);
518
519         hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
520         if (IS_ERR(hdesc)) {
521                 CERROR("Unable to initialize checksum hash %s\n",
522                        cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
523                 return PTR_ERR(hdesc);
524         }
525
526         hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
527
528         for (i = 0; i < desc->bd_iov_count; i++) {
529                 cfs_crypto_hash_update_page(hdesc, desc->bd_iov[i].kiov_page,
530                                   desc->bd_iov[i].kiov_offset & ~PAGE_MASK,
531                                   desc->bd_iov[i].kiov_len);
532         }
533
534         if (hashsize > buflen) {
535                 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
536
537                 bufsize = sizeof(hashbuf);
538                 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
539                          bufsize, hashsize);
540                 err = cfs_crypto_hash_final(hdesc, hashbuf, &bufsize);
541                 memcpy(buf, hashbuf, buflen);
542         } else {
543                 bufsize = buflen;
544                 err = cfs_crypto_hash_final(hdesc, buf, &bufsize);
545         }
546
547         return err;
548 }
549 EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);