From patchwork Mon Jan 9 15:24:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 90482 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp132340qgi; Mon, 9 Jan 2017 07:24:51 -0800 (PST) X-Received: by 10.55.192.137 with SMTP id v9mr84737605qkv.305.1483975491277; Mon, 09 Jan 2017 07:24:51 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id 96si56588287qtc.269.2017.01.09.07.24.50; Mon, 09 Jan 2017 07:24:51 -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 AB50D6065B; Mon, 9 Jan 2017 15:24:50 +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 422CF6050A; Mon, 9 Jan 2017 15:24:46 +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 DBBA560589; Mon, 9 Jan 2017 15:24:42 +0000 (UTC) Received: from mail-oi0-f48.google.com (mail-oi0-f48.google.com [209.85.218.48]) by lists.linaro.org (Postfix) with ESMTPS id 827D560856 for ; Mon, 9 Jan 2017 15:24:07 +0000 (UTC) Received: by mail-oi0-f48.google.com with SMTP id b126so694268139oia.2 for ; Mon, 09 Jan 2017 07:24:07 -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=fsy7kQsWzM2NCtCG5RrrWPo2hYR0aP+SyrJaoXZfpq0=; b=LAgO4qESKXwxCPuQe5q0Kfz4w7g/fpoj2eeN1dtkV731btj4l2xqqvxukfRzcT3Fhl l28er/eycO761002Sc/gK+d61RP+7PQZ9fhsyvsoH2NU/XCYL2efWVxTsNLkR11WO5MW vSSfYI45bTyUb2VP0QRGXscrDMiIhaFuIfyhs1uwZN6dhhdRjbkRgJLK1tWy9Omy8SmU kEu+rZnIk1QPH90jIyw1CyFCSL367100GX+WMUU08ZKU05okxldbZOlumB3EvvcF2P8O PannP2+El8wTFq6l2TVu/r7ySsW3xf4HJdO3i+p34HbEFENt4dIw0cvyP7HzSdrp3195 c+Hg== X-Gm-Message-State: AIkVDXK0AbOvj1t6KR/ZYPN0y7qUfRaAuSz2UBiU7WgyTczkpl8JAYzF2H01LBTotCdqlJkFSjc= X-Received: by 10.157.43.225 with SMTP id u88mr8025563ota.5.1483975447031; Mon, 09 Jan 2017 07:24:07 -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 j11sm452058oih.1.2017.01.09.07.24.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Jan 2017 07:24:06 -0800 (PST) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Mon, 9 Jan 2017 09:24:02 -0600 Message-Id: <20170109152402.8222-1-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.9.3 Subject: [lng-odp] [API-NEXT PATCHv3] linux-generic: crypto: 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 v3: - Move code from odp_init.c to odp_crypto.c as suggested by Petri and Christophe Changes for v2: - OPENSSL_INIT stage should precede CRYPTO_INIT stage since crypto uses OpenSSL platform/linux-generic/odp_crypto.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) -- 2.9.3 Reviewed-by: Christophe Milard diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c index 5808d16..4f17fd6 100644 --- a/platform/linux-generic/odp_crypto.c +++ b/platform/linux-generic/odp_crypto.c @@ -64,6 +64,7 @@ typedef struct odp_crypto_global_s odp_crypto_global_t; struct odp_crypto_global_s { odp_spinlock_t lock; + odp_ticketlock_t **openssl_lock; odp_crypto_generic_session_t *free; odp_crypto_generic_session_t sessions[0]; }; @@ -948,16 +949,35 @@ odp_crypto_operation(odp_crypto_op_param_t *param, return 0; } +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 *) + &global->openssl_lock[n]); + else + odp_ticketlock_unlock((odp_ticketlock_t *) + &global->openssl_lock[n]); +} + int odp_crypto_init_global(void) { size_t mem_size; odp_shm_t shm; int idx; + int nlocks = CRYPTO_num_locks(); /* Calculate the memory size we need */ mem_size = sizeof(*global); mem_size += (MAX_SESSIONS * sizeof(odp_crypto_generic_session_t)); + mem_size += nlocks * sizeof(odp_ticketlock_t); /* Allocate our globally shared memory */ shm = odp_shm_reserve("crypto_pool", mem_size, @@ -975,6 +995,18 @@ odp_crypto_init_global(void) } odp_spinlock_init(&global->lock); + if (nlocks > 0) { + global->openssl_lock = + (odp_ticketlock_t **)&global->sessions[MAX_SESSIONS]; + + for (idx = 0; idx < nlocks; idx++) + odp_ticketlock_init((odp_ticketlock_t *) + &global->openssl_lock[idx]); + + CRYPTO_set_id_callback(openssl_thread_id); + CRYPTO_set_locking_callback(openssl_lock); + } + return 0; } @@ -992,6 +1024,9 @@ int odp_crypto_term_global(void) rc = -1; } + CRYPTO_set_locking_callback(NULL); + CRYPTO_set_id_callback(NULL); + ret = odp_shm_free(odp_shm_lookup("crypto_pool")); if (ret < 0) { ODP_ERR("shm free failed for crypto_pool\n");