xref: /freebsd/sys/contrib/openzfs/module/icp/core/kcf_sched.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This file contains the core framework routines for the
29  * kernel cryptographic framework. These routines are at the
30  * layer, between the kernel API/ioctls and the SPI.
31  */
32 
33 #include <sys/zfs_context.h>
34 #include <sys/crypto/common.h>
35 #include <sys/crypto/impl.h>
36 #include <sys/crypto/sched_impl.h>
37 #include <sys/crypto/api.h>
38 
39 /* kmem caches used by the scheduler */
40 static kmem_cache_t *kcf_context_cache;
41 
42 /*
43  * Create a new context.
44  */
45 crypto_ctx_t *
kcf_new_ctx(kcf_provider_desc_t * pd)46 kcf_new_ctx(kcf_provider_desc_t *pd)
47 {
48 	crypto_ctx_t *ctx;
49 	kcf_context_t *kcf_ctx;
50 
51 	kcf_ctx = kmem_cache_alloc(kcf_context_cache, KM_SLEEP);
52 	if (kcf_ctx == NULL)
53 		return (NULL);
54 
55 	/* initialize the context for the consumer */
56 	kcf_ctx->kc_refcnt = 1;
57 	KCF_PROV_REFHOLD(pd);
58 	kcf_ctx->kc_prov_desc = pd;
59 	kcf_ctx->kc_sw_prov_desc = NULL;
60 
61 	ctx = &kcf_ctx->kc_glbl_ctx;
62 	ctx->cc_provider_private = NULL;
63 	ctx->cc_framework_private = (void *)kcf_ctx;
64 
65 	return (ctx);
66 }
67 
68 /*
69  * We're done with this framework context, so free it. Note that freeing
70  * framework context (kcf_context) frees the global context (crypto_ctx).
71  *
72  * The provider is responsible for freeing provider private context after a
73  * final or single operation and resetting the cc_provider_private field
74  * to NULL. It should do this before it notifies the framework of the
75  * completion. We still need to call KCF_PROV_FREE_CONTEXT to handle cases
76  * like crypto_cancel_ctx(9f).
77  */
78 void
kcf_free_context(kcf_context_t * kcf_ctx)79 kcf_free_context(kcf_context_t *kcf_ctx)
80 {
81 	kcf_provider_desc_t *pd = kcf_ctx->kc_prov_desc;
82 	crypto_ctx_t *gctx = &kcf_ctx->kc_glbl_ctx;
83 
84 	if (gctx->cc_provider_private != NULL) {
85 		mutex_enter(&pd->pd_lock);
86 		if (!KCF_IS_PROV_REMOVED(pd)) {
87 			/*
88 			 * Increment the provider's internal refcnt so it
89 			 * doesn't unregister from the framework while
90 			 * we're calling the entry point.
91 			 */
92 			KCF_PROV_IREFHOLD(pd);
93 			mutex_exit(&pd->pd_lock);
94 			(void) KCF_PROV_FREE_CONTEXT(pd, gctx);
95 			KCF_PROV_IREFRELE(pd);
96 		} else {
97 			mutex_exit(&pd->pd_lock);
98 		}
99 	}
100 
101 	/* kcf_ctx->kc_prov_desc has a hold on pd */
102 	KCF_PROV_REFRELE(kcf_ctx->kc_prov_desc);
103 
104 	kmem_cache_free(kcf_context_cache, kcf_ctx);
105 }
106 
107 /*
108  * kmem_cache_alloc constructor for kcf_context structure.
109  */
110 static int
kcf_context_cache_constructor(void * buf,void * cdrarg,int kmflags)111 kcf_context_cache_constructor(void *buf, void *cdrarg, int kmflags)
112 {
113 	(void) cdrarg, (void) kmflags;
114 	kcf_context_t *kctx = (kcf_context_t *)buf;
115 
116 	kctx->kc_refcnt = 0;
117 
118 	return (0);
119 }
120 
121 static void
kcf_context_cache_destructor(void * buf,void * cdrarg)122 kcf_context_cache_destructor(void *buf, void *cdrarg)
123 {
124 	(void) cdrarg;
125 	kcf_context_t *kctx = (kcf_context_t *)buf;
126 
127 	ASSERT(kctx->kc_refcnt == 0);
128 }
129 
130 void
kcf_sched_destroy(void)131 kcf_sched_destroy(void)
132 {
133 	if (kcf_context_cache)
134 		kmem_cache_destroy(kcf_context_cache);
135 }
136 
137 /*
138  * Creates and initializes all the structures needed by the framework.
139  */
140 void
kcf_sched_init(void)141 kcf_sched_init(void)
142 {
143 	/*
144 	 * Create all the kmem caches needed by the framework. We set the
145 	 * align argument to 64, to get a slab aligned to 64-byte as well as
146 	 * have the objects (cache_chunksize) to be a 64-byte multiple.
147 	 * This helps to avoid false sharing as this is the size of the
148 	 * CPU cache line.
149 	 */
150 	kcf_context_cache = kmem_cache_create("kcf_context_cache",
151 	    sizeof (struct kcf_context), 64, kcf_context_cache_constructor,
152 	    kcf_context_cache_destructor, NULL, NULL, NULL, 0);
153 }
154