]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/async/async.c
Refactor the async wait fd logic
[thirdparty/openssl.git] / crypto / async / async.c
1 /*
2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
3 */
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 */
52
53 /*
54 * Without this we start getting longjmp crashes because it thinks we're jumping
55 * up the stack when in fact we are jumping to an entirely different stack. The
56 * cost of this is not having certain buffer overrun/underrun checks etc for
57 * this source file :-(
58 */
59 #undef _FORTIFY_SOURCE
60
61 /* This must be the first #include file */
62 #include "async_locl.h"
63
64 #include <openssl/err.h>
65 #include <internal/cryptlib_int.h>
66 #include <string.h>
67
68 #define ASYNC_JOB_RUNNING 0
69 #define ASYNC_JOB_PAUSING 1
70 #define ASYNC_JOB_PAUSED 2
71 #define ASYNC_JOB_STOPPING 3
72
73 static void async_free_pool_internal(async_pool *pool);
74
75 static async_ctx *async_ctx_new(void)
76 {
77 async_ctx *nctx = NULL;
78
79 nctx = OPENSSL_malloc(sizeof (async_ctx));
80 if (nctx == NULL) {
81 ASYNCerr(ASYNC_F_ASYNC_CTX_NEW, ERR_R_MALLOC_FAILURE);
82 goto err;
83 }
84
85 async_fibre_init_dispatcher(&nctx->dispatcher);
86 nctx->currjob = NULL;
87 nctx->blocked = 0;
88 if (!async_set_ctx(nctx))
89 goto err;
90
91 return nctx;
92 err:
93 OPENSSL_free(nctx);
94
95 return NULL;
96 }
97
98 static async_ctx *async_get_ctx(void)
99 {
100 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
101 return NULL;
102 return async_arch_get_ctx();
103 }
104
105 static int async_ctx_free(void)
106 {
107 async_ctx *ctx;
108
109 ctx = async_get_ctx();
110
111 if (!async_set_ctx(NULL))
112 return 0;
113
114 OPENSSL_free(ctx);
115
116 return 1;
117 }
118
119 static ASYNC_JOB *async_job_new(void)
120 {
121 ASYNC_JOB *job = NULL;
122
123 job = OPENSSL_zalloc(sizeof (ASYNC_JOB));
124 if (job == NULL) {
125 ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ERR_R_MALLOC_FAILURE);
126 return NULL;
127 }
128
129 job->status = ASYNC_JOB_RUNNING;
130
131 return job;
132 }
133
134 static void async_job_free(ASYNC_JOB *job)
135 {
136 if (job != NULL) {
137 OPENSSL_free(job->funcargs);
138 async_fibre_free(&job->fibrectx);
139 OPENSSL_free(job);
140 }
141 }
142
143 static ASYNC_JOB *async_get_pool_job(void) {
144 ASYNC_JOB *job;
145 async_pool *pool;
146
147 pool = async_get_pool();
148 if (pool == NULL) {
149 /*
150 * Pool has not been initialised, so init with the defaults, i.e.
151 * no max size and no pre-created jobs
152 */
153 if (ASYNC_init_thread(0, 0) == 0)
154 return NULL;
155 pool = async_get_pool();
156 }
157
158 job = sk_ASYNC_JOB_pop(pool->jobs);
159 if (job == NULL) {
160 /* Pool is empty */
161 if ((pool->max_size != 0) && (pool->curr_size >= pool->max_size))
162 return NULL;
163
164 job = async_job_new();
165 if (job != NULL) {
166 if (! async_fibre_makecontext(&job->fibrectx)) {
167 async_job_free(job);
168 return NULL;
169 }
170 pool->curr_size++;
171 }
172 }
173 return job;
174 }
175
176 static void async_release_job(ASYNC_JOB *job) {
177 async_pool *pool;
178
179 pool = async_get_pool();
180 OPENSSL_free(job->funcargs);
181 job->funcargs = NULL;
182 sk_ASYNC_JOB_push(pool->jobs, job);
183 }
184
185 void async_start_func(void)
186 {
187 ASYNC_JOB *job;
188 async_ctx *ctx = async_get_ctx();
189
190 while (1) {
191 /* Run the job */
192 job = ctx->currjob;
193 job->ret = job->func(job->funcargs);
194
195 /* Stop the job */
196 job->status = ASYNC_JOB_STOPPING;
197 if (!async_fibre_swapcontext(&job->fibrectx,
198 &ctx->dispatcher, 1)) {
199 /*
200 * Should not happen. Getting here will close the thread...can't do
201 * much about it
202 */
203 ASYNCerr(ASYNC_F_ASYNC_START_FUNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
204 }
205 }
206 }
207
208 int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *wctx, int *ret,
209 int (*func)(void *), void *args, size_t size)
210 {
211 async_ctx *ctx = async_get_ctx();
212 if (ctx == NULL)
213 ctx = async_ctx_new();
214 if (ctx == NULL) {
215 return ASYNC_ERR;
216 }
217
218 if (*job) {
219 ctx->currjob = *job;
220 }
221
222 for (;;) {
223 if (ctx->currjob != NULL) {
224 if (ctx->currjob->status == ASYNC_JOB_STOPPING) {
225 *ret = ctx->currjob->ret;
226 ctx->currjob->waitctx = NULL;
227 async_release_job(ctx->currjob);
228 ctx->currjob = NULL;
229 *job = NULL;
230 return ASYNC_FINISH;
231 }
232
233 if (ctx->currjob->status == ASYNC_JOB_PAUSING) {
234 *job = ctx->currjob;
235 ctx->currjob->status = ASYNC_JOB_PAUSED;
236 ctx->currjob = NULL;
237 return ASYNC_PAUSE;
238 }
239
240 if (ctx->currjob->status == ASYNC_JOB_PAUSED) {
241 ctx->currjob = *job;
242 /* Resume previous job */
243 if (!async_fibre_swapcontext(&ctx->dispatcher,
244 &ctx->currjob->fibrectx, 1)) {
245 ASYNCerr(ASYNC_F_ASYNC_START_JOB,
246 ASYNC_R_FAILED_TO_SWAP_CONTEXT);
247 goto err;
248 }
249 continue;
250 }
251
252 /* Should not happen */
253 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_INTERNAL_ERROR);
254 async_release_job(ctx->currjob);
255 ctx->currjob = NULL;
256 *job = NULL;
257 return ASYNC_ERR;
258 }
259
260 /* Start a new job */
261 if ((ctx->currjob = async_get_pool_job()) == NULL) {
262 return ASYNC_NO_JOBS;
263 }
264
265 if (args != NULL) {
266 ctx->currjob->funcargs = OPENSSL_malloc(size);
267 if (ctx->currjob->funcargs == NULL) {
268 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_MALLOC_FAILURE);
269 async_release_job(ctx->currjob);
270 ctx->currjob = NULL;
271 return ASYNC_ERR;
272 }
273 memcpy(ctx->currjob->funcargs, args, size);
274 } else {
275 ctx->currjob->funcargs = NULL;
276 }
277
278 ctx->currjob->func = func;
279 ctx->currjob->waitctx = wctx;
280 if (!async_fibre_swapcontext(&ctx->dispatcher,
281 &ctx->currjob->fibrectx, 1)) {
282 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
283 goto err;
284 }
285 }
286
287 err:
288 async_release_job(ctx->currjob);
289 ctx->currjob = NULL;
290 *job = NULL;
291 return ASYNC_ERR;
292 }
293
294 int ASYNC_pause_job(void)
295 {
296 ASYNC_JOB *job;
297 async_ctx *ctx = async_get_ctx();
298
299 if (ctx == NULL
300 || ctx->currjob == NULL
301 || ctx->blocked) {
302 /*
303 * Could be we've deliberately not been started within a job so this is
304 * counted as success.
305 */
306 return 1;
307 }
308
309 job = ctx->currjob;
310 job->status = ASYNC_JOB_PAUSING;
311
312 if (!async_fibre_swapcontext(&job->fibrectx,
313 &ctx->dispatcher, 1)) {
314 ASYNCerr(ASYNC_F_ASYNC_PAUSE_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
315 return 0;
316 }
317 /* Reset counts of added and deleted fds */
318 async_wait_ctx_reset_counts(job->waitctx);
319
320 return 1;
321 }
322
323 static void async_empty_pool(async_pool *pool)
324 {
325 ASYNC_JOB *job;
326
327 if (!pool || !pool->jobs)
328 return;
329
330 do {
331 job = sk_ASYNC_JOB_pop(pool->jobs);
332 async_job_free(job);
333 } while (job);
334 }
335
336 int async_init(void)
337 {
338 if (!async_global_init())
339 return 0;
340
341 return 1;
342 }
343
344 int ASYNC_init_thread(size_t max_size, size_t init_size)
345 {
346 async_pool *pool;
347 size_t curr_size = 0;
348
349 if (init_size > max_size) {
350 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INVALID_POOL_SIZE);
351 return 0;
352 }
353
354 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL)) {
355 return 0;
356 }
357 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ASYNC)) {
358 return 0;
359 }
360
361 pool = OPENSSL_zalloc(sizeof *pool);
362 if (pool == NULL) {
363 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
364 return 0;
365 }
366
367 pool->jobs = sk_ASYNC_JOB_new_null();
368 if (pool->jobs == NULL) {
369 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
370 OPENSSL_free(pool);
371 return 0;
372 }
373
374 pool->max_size = max_size;
375
376 /* Pre-create jobs as required */
377 while (init_size--) {
378 ASYNC_JOB *job;
379 job = async_job_new();
380 if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
381 /*
382 * Not actually fatal because we already created the pool, just
383 * skip creation of any more jobs
384 */
385 async_job_free(job);
386 break;
387 }
388 job->funcargs = NULL;
389 sk_ASYNC_JOB_push(pool->jobs, job);
390 curr_size++;
391 }
392 pool->curr_size = curr_size;
393 if (!async_set_pool(pool)) {
394 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);
395 goto err;
396 }
397
398 return 1;
399 err:
400 async_free_pool_internal(pool);
401 return 0;
402 }
403
404 static void async_free_pool_internal(async_pool *pool)
405 {
406 if (pool == NULL)
407 return;
408
409 async_empty_pool(pool);
410 sk_ASYNC_JOB_free(pool->jobs);
411 OPENSSL_free(pool);
412 (void)async_set_pool(NULL);
413 async_local_cleanup();
414 async_ctx_free();
415 }
416
417 void ASYNC_cleanup_thread(void)
418 {
419 async_free_pool_internal(async_get_pool());
420 }
421
422 ASYNC_JOB *ASYNC_get_current_job(void)
423 {
424 async_ctx *ctx;
425
426 ctx = async_get_ctx();
427 if(ctx == NULL)
428 return NULL;
429
430 return ctx->currjob;
431 }
432
433 ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job)
434 {
435 return job->waitctx;
436 }
437
438 void ASYNC_block_pause(void)
439 {
440 async_ctx *ctx = async_get_ctx();
441 if (ctx == NULL || ctx->currjob == NULL) {
442 /*
443 * We're not in a job anyway so ignore this
444 */
445 return;
446 }
447 ctx->blocked++;
448 }
449
450 void ASYNC_unblock_pause(void)
451 {
452 async_ctx *ctx = async_get_ctx();
453 if (ctx == NULL || ctx->currjob == NULL) {
454 /*
455 * We're not in a job anyway so ignore this
456 */
457 return;
458 }
459 if(ctx->blocked > 0)
460 ctx->blocked--;
461 }