Thu Jul 20 09:31:36 2017 UTC ()
fix panic when using ecryption devices attached earlier than ipi_sysinit().

pointed out and tested by martin@n.o, thanks.


(knakahara)
diff -r1.92 -r1.93 src/sys/opencrypto/crypto.c

cvs diff -r1.92 -r1.93 src/sys/opencrypto/crypto.c (expand / switch to unified diff)

--- src/sys/opencrypto/crypto.c 2017/07/18 06:01:36 1.92
+++ src/sys/opencrypto/crypto.c 2017/07/20 09:31:36 1.93
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: crypto.c,v 1.92 2017/07/18 06:01:36 knakahara Exp $ */ 1/* $NetBSD: crypto.c,v 1.93 2017/07/20 09:31:36 knakahara Exp $ */
2/* $FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $ */ 2/* $FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $ */
3/* $OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $ */ 3/* $OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $ */
4 4
5/*- 5/*-
6 * Copyright (c) 2008 The NetBSD Foundation, Inc. 6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * This code is derived from software contributed to The NetBSD Foundation 9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Coyote Point Systems, Inc. 10 * by Coyote Point Systems, Inc.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
@@ -43,41 +43,42 @@ @@ -43,41 +43,42 @@
43 * Permission to use, copy, and modify this software with or without fee 43 * Permission to use, copy, and modify this software with or without fee
44 * is hereby granted, provided that this entire notice is included in 44 * is hereby granted, provided that this entire notice is included in
45 * all source code copies of any software which is or includes a copy or 45 * all source code copies of any software which is or includes a copy or
46 * modification of this software. 46 * modification of this software.
47 * 47 *
48 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 48 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
49 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 49 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
50 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 50 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
51 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 51 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
52 * PURPOSE. 52 * PURPOSE.
53 */ 53 */
54 54
55#include <sys/cdefs.h> 55#include <sys/cdefs.h>
56__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.92 2017/07/18 06:01:36 knakahara Exp $"); 56__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.93 2017/07/20 09:31:36 knakahara Exp $");
57 57
58#include <sys/param.h> 58#include <sys/param.h>
59#include <sys/reboot.h> 59#include <sys/reboot.h>
60#include <sys/systm.h> 60#include <sys/systm.h>
61#include <sys/malloc.h> 61#include <sys/malloc.h>
62#include <sys/proc.h> 62#include <sys/proc.h>
63#include <sys/pool.h> 63#include <sys/pool.h>
64#include <sys/kthread.h> 64#include <sys/kthread.h>
65#include <sys/once.h> 65#include <sys/once.h>
66#include <sys/sysctl.h> 66#include <sys/sysctl.h>
67#include <sys/intr.h> 67#include <sys/intr.h>
68#include <sys/errno.h> 68#include <sys/errno.h>
69#include <sys/module.h> 69#include <sys/module.h>
70#include <sys/xcall.h> 70#include <sys/xcall.h>
 71#include <sys/device.h>
71 72
72#if defined(_KERNEL_OPT) 73#if defined(_KERNEL_OPT)
73#include "opt_ocf.h" 74#include "opt_ocf.h"
74#endif 75#endif
75 76
76#include <opencrypto/cryptodev.h> 77#include <opencrypto/cryptodev.h>
77#include <opencrypto/xform.h> /* XXX for M_XDATA */ 78#include <opencrypto/xform.h> /* XXX for M_XDATA */
78 79
79static kmutex_t crypto_q_mtx; 80static kmutex_t crypto_q_mtx;
80static kmutex_t crypto_ret_q_mtx; 81static kmutex_t crypto_ret_q_mtx;
81 82
82/* below are kludges for residual code wrtitten to FreeBSD interfaces */ 83/* below are kludges for residual code wrtitten to FreeBSD interfaces */
83 #define SWI_CRYPTO 17 84 #define SWI_CRYPTO 17
@@ -372,26 +373,28 @@ MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", " @@ -372,26 +373,28 @@ MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "
372static void cryptointr(void); /* swi thread to dispatch ops */ 373static void cryptointr(void); /* swi thread to dispatch ops */
373static void cryptoret_softint(void *); /* kernel thread for callbacks*/ 374static void cryptoret_softint(void *); /* kernel thread for callbacks*/
374static int crypto_destroy(bool); 375static int crypto_destroy(bool);
375static int crypto_invoke(struct cryptop *crp, int hint); 376static int crypto_invoke(struct cryptop *crp, int hint);
376static int crypto_kinvoke(struct cryptkop *krp, int hint); 377static int crypto_kinvoke(struct cryptkop *krp, int hint);
377 378
378static struct cryptocap *crypto_checkdriver_lock(u_int32_t); 379static struct cryptocap *crypto_checkdriver_lock(u_int32_t);
379static struct cryptocap *crypto_checkdriver_uninit(u_int32_t); 380static struct cryptocap *crypto_checkdriver_uninit(u_int32_t);
380static struct cryptocap *crypto_checkdriver(u_int32_t); 381static struct cryptocap *crypto_checkdriver(u_int32_t);
381static void crypto_driver_lock(struct cryptocap *); 382static void crypto_driver_lock(struct cryptocap *);
382static void crypto_driver_unlock(struct cryptocap *); 383static void crypto_driver_unlock(struct cryptocap *);
383static void crypto_driver_clear(struct cryptocap *); 384static void crypto_driver_clear(struct cryptocap *);
384 385
 386static int crypto_init_finalize(device_t);
 387
385static struct cryptostats cryptostats; 388static struct cryptostats cryptostats;
386#ifdef CRYPTO_TIMING 389#ifdef CRYPTO_TIMING
387static int crypto_timing = 0; 390static int crypto_timing = 0;
388#endif 391#endif
389 392
390static struct sysctllog *sysctl_opencrypto_clog; 393static struct sysctllog *sysctl_opencrypto_clog;
391 394
392static int 395static int
393crypto_init0(void) 396crypto_init0(void)
394{ 397{
395 398
396 mutex_init(&crypto_drv_mtx, MUTEX_DEFAULT, IPL_NONE); 399 mutex_init(&crypto_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
397 mutex_init(&crypto_q_mtx, MUTEX_DEFAULT, IPL_NONE); 400 mutex_init(&crypto_q_mtx, MUTEX_DEFAULT, IPL_NONE);
@@ -407,38 +410,52 @@ crypto_init0(void) @@ -407,38 +410,52 @@ crypto_init0(void)
407 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO); 410 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
408 if (crypto_drivers == NULL) { 411 if (crypto_drivers == NULL) {
409 printf("crypto_init: cannot malloc driver table\n"); 412 printf("crypto_init: cannot malloc driver table\n");
410 return ENOMEM; 413 return ENOMEM;
411 } 414 }
412 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL; 415 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
413 416
414 softintr_cookie = register_swi(SWI_CRYPTO, cryptointr); 417 softintr_cookie = register_swi(SWI_CRYPTO, cryptointr);
415 if (softintr_cookie == NULL) { 418 if (softintr_cookie == NULL) {
416 printf("crypto_init: cannot establish request queue handler\n"); 419 printf("crypto_init: cannot establish request queue handler\n");
417 return crypto_destroy(false); 420 return crypto_destroy(false);
418 } 421 }
419 422
420 crypto_ret_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE|SOFTINT_RCPU, 423 /*
421 &cryptoret_softint, NULL); 424 * Some encryption devices(such as mvcesa) is attached before
422 if (crypto_ret_si == NULL) { 425 * ipi_sysinit(). That causes assertion in ipi_register() as
423 printf("crypto_init: cannot establish ret queue handler\n"); 426 * crypto_ret_si softint uses SOFTINT_RCPU.
 427 */
 428 if (config_finalize_register(NULL, crypto_init_finalize) != 0) {
 429 printf("crypto_init: cannot register crypto_init_finalize\n");
424 return crypto_destroy(false); 430 return crypto_destroy(false);
425 } 431 }
426 432
427 sysctl_opencrypto_setup(&sysctl_opencrypto_clog); 433 sysctl_opencrypto_setup(&sysctl_opencrypto_clog);
428 434
429 return 0; 435 return 0;
430} 436}
431 437
 438static int
 439crypto_init_finalize(device_t self __unused)
 440{
 441
 442 crypto_ret_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE|SOFTINT_RCPU,
 443 &cryptoret_softint, NULL);
 444 KASSERT(crypto_ret_si != NULL);
 445
 446 return 0;
 447}
 448
432int 449int
433crypto_init(void) 450crypto_init(void)
434{ 451{
435 static ONCE_DECL(crypto_init_once); 452 static ONCE_DECL(crypto_init_once);
436 453
437 return RUN_ONCE(&crypto_init_once, crypto_init0); 454 return RUN_ONCE(&crypto_init_once, crypto_init0);
438} 455}
439 456
440static int 457static int
441crypto_destroy(bool exit_kthread) 458crypto_destroy(bool exit_kthread)
442{ 459{
443 int i; 460 int i;
444 461