From patchwork Fri Jan 6 13:53:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 90168 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp9455889qgi; Fri, 6 Jan 2017 05:53:56 -0800 (PST) X-Received: by 10.200.47.100 with SMTP id k33mr1064305qta.241.1483710835967; Fri, 06 Jan 2017 05:53:55 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id q49si4877126qtq.287.2017.01.06.05.53.54; Fri, 06 Jan 2017 05:53:55 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 80C1D60D16; Fri, 6 Jan 2017 13:53:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 5BCFA6093D; Fri, 6 Jan 2017 13:53:49 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 6C56A60975; Fri, 6 Jan 2017 13:53:47 +0000 (UTC) Received: from mail-oi0-f44.google.com (mail-oi0-f44.google.com [209.85.218.44]) by lists.linaro.org (Postfix) with ESMTPS id 1BA1860902 for ; Fri, 6 Jan 2017 13:53:45 +0000 (UTC) Received: by mail-oi0-f44.google.com with SMTP id 128so420975208oig.0 for ; Fri, 06 Jan 2017 05:53:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=2mlQeMylx0gL/EMNOsbfK8Y9yOqQklGESt6ylhkOnpg=; b=TFv7Ytyh7oZZyD/sbsJXvWVSLO2RIOeo8uw7ywkSR1IDFQFjdpRmMwYoc4TulG1RLC RJo/KQWRqVcqYJwBS1uYsFwyBRbp6S8ZjXTQ+gI9eaQjUIcpbaUqOKPEjNEvix/qEOaF BkNdbRDdSw1W4kRkSRzfDaTTuIcXhQjI96PVPIXY6afUPkeDDtANuh5EJujdWsrkmrGn qRl/Pcb70/ErIuTqRwO+f2qAszEhmTFZ5EFDs+y7af+Nf8ID2AnFnZilokqwcxhuj4qn dhfn4JAQwIhLdA7IEohUVKgKUP03MzNs2A3iVjQDtKrhWef94E5XRiWq2zlsUNnHXFCC l4Dg== X-Gm-Message-State: AIkVDXK2KIz005Cwf26v9f7YWh8tY0et7yEaT5tbSF/60cm9kFwP5eL5SiFwEiKRUmDUD6hW/E0= X-Received: by 10.157.55.201 with SMTP id x67mr1311273otb.195.1483710824596; Fri, 06 Jan 2017 05:53:44 -0800 (PST) Received: from localhost.localdomain (cpe-70-121-83-241.austin.res.rr.com. [70.121.83.241]) by smtp.gmail.com with ESMTPSA id 31sm36834902oth.21.2017.01.06.05.53.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 06 Jan 2017 05:53:43 -0800 (PST) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Fri, 6 Jan 2017 07:53:42 -0600 Message-Id: <1483710822-15237-1-git-send-email-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.7.4 Subject: [lng-odp] [API-NEXT PATCHv2] linux-generic: init: add openssl locking support for thread safety X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Resolve Bug https://bugs.linaro.org/show_bug.cgi?id=2798 by adding OpenSSL callbacks for locking that use ticketlocks to provide thread-safety for OpenSSL calls made by ODP components such as random number generation. Signed-off-by: Bill Fischofer --- Changes for v2: - OPENSSL_INIT stage should precede CRYPTO_INIT stage since crypto uses OpenSSL platform/linux-generic/include/odp_internal.h | 5 +++ platform/linux-generic/odp_init.c | 63 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) -- 2.7.4 diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h index b313b1f..272c7cd 100644 --- a/platform/linux-generic/include/odp_internal.h +++ b/platform/linux-generic/include/odp_internal.h @@ -20,6 +20,8 @@ extern "C" { #include #include #include +#include +#include #include #include @@ -50,6 +52,8 @@ struct odp_global_data_s { odp_cpumask_t control_cpus; odp_cpumask_t worker_cpus; int num_cpus_installed; + odp_shm_t openssl_shm; + odp_ticketlock_t **openssl_lock; }; enum init_stage { @@ -65,6 +69,7 @@ enum init_stage { SCHED_INIT, PKTIO_INIT, TIMER_INIT, + OPENSSL_INIT, CRYPTO_INIT, CLASSIFICATION_INIT, TRAFFIC_MNGR_INIT, diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c index 1b0d8f8..7474ff0 100644 --- a/platform/linux-generic/odp_init.c +++ b/platform/linux-generic/odp_init.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -21,8 +23,30 @@ #define _ODP_FILES_FMT "odp-%d-" #define _ODP_TMPDIR "/tmp" +static inline int openssl_numlocks(void) +{ + return CRYPTO_num_locks(); +} + struct odp_global_data_s odp_global_data; +static unsigned long openssl_thread_id(void) +{ + return (unsigned long)odp_thread_id(); +} + +static void openssl_lock(int mode, int n, + const char *file ODP_UNUSED, + int line ODP_UNUSED) +{ + if (mode & CRYPTO_LOCK) + odp_ticketlock_lock((odp_ticketlock_t *) + &odp_global_data.openssl_lock[n]); + else + odp_ticketlock_unlock((odp_ticketlock_t *) + &odp_global_data.openssl_lock[n]); +} + /* remove all files staring with "odp-" from a directory "dir" */ static int cleanup_files(const char *dirpath, int odp_pid) { @@ -70,6 +94,8 @@ int odp_init_global(odp_instance_t *instance, const odp_platform_init_t *platform_params ODP_UNUSED) { char *hpdir; + int nlocks = openssl_numlocks(); + int i; memset(&odp_global_data, 0, sizeof(struct odp_global_data_s)); odp_global_data.main_pid = getpid(); @@ -156,6 +182,34 @@ int odp_init_global(odp_instance_t *instance, } stage = TIMER_INIT; + if (nlocks > 0) { + odp_global_data.openssl_shm = + odp_shm_reserve("ODP OpenSSL Mutexes", + nlocks * sizeof(odp_ticketlock_t), + sizeof(odp_ticketlock_t), + ODP_SHM_SW_ONLY); + if (odp_global_data.openssl_shm == ODP_SHM_INVALID) { + ODP_ERR("ODP OpenSSL init reserve failed.\n"); + goto init_failed; + } + + odp_global_data.openssl_lock = + odp_shm_addr(odp_global_data.openssl_shm); + if (odp_global_data.openssl_lock == NULL) { + odp_shm_free(odp_global_data.openssl_shm); + ODP_ERR("ODP OpenSSL init addr failed.\n"); + goto init_failed; + } + + for (i = 0; i < nlocks; i++) + odp_ticketlock_init((odp_ticketlock_t *) + &odp_global_data.openssl_lock[i]); + + CRYPTO_set_id_callback(openssl_thread_id); + CRYPTO_set_locking_callback(openssl_lock); + } + stage = OPENSSL_INIT; + if (odp_crypto_init_global()) { ODP_ERR("ODP crypto init failed.\n"); goto init_failed; @@ -231,6 +285,15 @@ int _odp_term_global(enum init_stage stage) } /* Fall through */ + case OPENSSL_INIT: + if (odp_global_data.openssl_lock) { + CRYPTO_set_locking_callback(NULL); + CRYPTO_set_id_callback(NULL); + + odp_shm_free(odp_global_data.openssl_shm); + } + /* Fall through */ + case TIMER_INIT: if (odp_timer_term_global()) { ODP_ERR("ODP timer term failed.\n");