From patchwork Wed Aug 11 00:05:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Hutchings X-Patchwork-Id: 12429647 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F760C4338F for ; Wed, 11 Aug 2021 00:06:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5015160EBD for ; Wed, 11 Aug 2021 00:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235608AbhHKAGW (ORCPT ); Tue, 10 Aug 2021 20:06:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235600AbhHKAGV (ORCPT ); Tue, 10 Aug 2021 20:06:21 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D29DFC061765 for ; Tue, 10 Aug 2021 17:05:58 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id x14so726573edr.12 for ; Tue, 10 Aug 2021 17:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mind.be; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=TmBklmtF6PgTKpjQdE419kaAahxa+pjIJXLUxre8OrA=; b=dUWOkifK3sn4MMtTFiJNvSMkEQ/2xwkZacFVMlQInmwPWfOdRpRahguGoy2B/EaTI+ oEBOou9ClBv1T5xQkDOOlP5+ajZrj3ymFkoVUsc3D53S9IKJLQw2OmMksvJcIF79pf9c qwv5a9+Gal5UEYZXCOvirJvJmN3HK16P5dibwKDMTlGeGTDjfIxgeWKh1IdkObwt+t6k wwJSG1f2Zb/nIOFNeb7PwXlA62gOnV3r7mKfIdip2BhGDLKglas/6TpyT6FBiACn/H5I hTSNQY5JZSV0tskD6A+9S6BGbAM6xhug+ERUVS0mWq8doe75CjzQocmjGzgeLMIWPWU9 vu8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=TmBklmtF6PgTKpjQdE419kaAahxa+pjIJXLUxre8OrA=; b=bRNBNOPQKx3TsmrC145hYCedAXRW1Uun5TebvLWT/pAP/wQBK8QBHGBoggl6x/2znw f4VMQbApqFiMSHsURMsXXVnR/TMXX5t0Bj1Bdh9Uoc61P/YlAz46koP1eVkri+cn1U/Q MwjH4px2mlubNAvuhOIYI4oVj3AiwgFbZml56oYnLmJrU7X9F4sGioIK+a7/f7kxAM+o WeM7jZz+ICfav3ueigwcT0PAnBVVnyaVZx2VNyCjmh54ELGCJ6Zvd+g2s0gotny8vI6Y 6kWzzYGnEYrJp8Z37LxG4n03jTHFojvzgao5zoF7sLUBDXX9JSJJUquXZWTNVNXtSdAP WRsQ== X-Gm-Message-State: AOAM532xqlxN3Jjtbr50GlE2qqykCq5ATuai+a4ppWP/rbKBBpl62O0r 1hJM0A6YYjjo5GVLt9wEjwkX9g== X-Google-Smtp-Source: ABdhPJyaGPw/WJKgjrkYUlq2P0fjLqHi+nuB4GcdGn/4PWX4vEAOefhRFIsBSBOg3Fc6gCoEMmCsNA== X-Received: by 2002:a50:da0e:: with SMTP id z14mr8095533edj.73.1628640357400; Tue, 10 Aug 2021 17:05:57 -0700 (PDT) Received: from cephalopod (168.7-181-91.adsl-dyn.isp.belgacom.be. [91.181.7.168]) by smtp.gmail.com with ESMTPSA id t5sm3976119ejr.37.2021.08.10.17.05.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Aug 2021 17:05:57 -0700 (PDT) Date: Wed, 11 Aug 2021 02:05:55 +0200 From: Ben Hutchings To: Herbert Xu , "David S. Miller" Cc: linux-crypto@vger.kernel.org, linux-omap@vger.kernel.org, Lokesh Vutla , Tero Kristo , Francis Le Bourse Subject: [PATCH 1/2] crypto: omap - Avoid redundant copy when using truncated sg list Message-ID: <20210811000554.GB22095@cephalopod> References: <20210811000333.GA22095@cephalopod> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210811000333.GA22095@cephalopod> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org omap_crypto_cleanup() currently copies data from sg to orig if either copy flag is set. However OMAP_CRYPTO_SG_COPIED means that sg refers to the same pages as orig, truncated to len bytes. There is no need to copy in this case. Only copy data if the OMAP_CRYPTO_DATA_COPIED flag is set. Fixes: 74ed87e7e7f7 ("crypto: omap - add base support library for common ...") Signed-off-by: Ben Hutchings --- This change was previously submitted as part of the patch at https://patchwork.kernel.org/project/linux-crypto/patch/1522164573-12259-1-git-send-email-francis.lebourse@sfr.fr/ . I couldn't find the full discussion of that in mailing list archives. Ben. drivers/crypto/omap-crypto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/crypto/omap-crypto.c b/drivers/crypto/omap-crypto.c index 31bdb1d76d11..a4cc6bf146ec 100644 --- a/drivers/crypto/omap-crypto.c +++ b/drivers/crypto/omap-crypto.c @@ -210,7 +210,7 @@ void omap_crypto_cleanup(struct scatterlist *sg, struct scatterlist *orig, buf = sg_virt(sg); pages = get_order(len); - if (orig && (flags & OMAP_CRYPTO_COPY_MASK)) + if (orig && (flags & OMAP_CRYPTO_DATA_COPIED)) omap_crypto_copy_data(sg, orig, offset, len); if (flags & OMAP_CRYPTO_DATA_COPIED) From patchwork Wed Aug 11 00:06:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Hutchings X-Patchwork-Id: 12429649 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39309C4320A for ; Wed, 11 Aug 2021 00:06:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E36B60FDA for ; Wed, 11 Aug 2021 00:06:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235600AbhHKAGg (ORCPT ); Tue, 10 Aug 2021 20:06:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235664AbhHKAGg (ORCPT ); Tue, 10 Aug 2021 20:06:36 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32948C061765 for ; Tue, 10 Aug 2021 17:06:13 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id n12so758397edx.8 for ; Tue, 10 Aug 2021 17:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mind.be; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=kKZrKj1gK2JVjgpkPE9ohM228G2m2HBu952S74N44Vs=; b=Cux/gMvrRNO2HATdjnob6imt8mMVAGTQmT86dWANq4iTKsHzaVtKLDmmajLU8ikNDL KMdFWJ2GFrRGcmalqcthro7bxL2seNe88P6+mTkdyC8bmeVwCX6JjwgQGxZ1KaGwbwdW L+uZNCiNyYIbBdzfnznVirDH0NrrvdG3K3Q81cYBUFauLqmzG8yAMCIKYEgjmpU4QuQf Em/sPSslWDHyWKSaELyR5McDMutSBSVnlptlLFrID/ilzAxNm2NopzyvH+zieFCihu78 BYUi6bxtjObr3uePU4rACZovdT7S12zycw/W/YPKtfIj8XGHvIUGo7wuR6JFqwakrisX SCHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=kKZrKj1gK2JVjgpkPE9ohM228G2m2HBu952S74N44Vs=; b=B5kQ9rVY3KJI7Q4mONweAPkpOGyP4/3d+2mouwy7nXW+fdwWJZIoOswZ6PrhlPduf2 TB75ufZKWM9vwQdsrv/Y3H/Rc7bkJQMyMbIgwiNNN8TaHvCkW9M6tVWBnUxx3POap6c3 l2nLCNcPXBr0GDivdrKb5uBmlCgpOG5nV/FZNLz1cdr4Aq0BxCCo3bh5q87q5DNl7kDn gq/EH94oqfdJebYo3OniLZPLb6W8b0AaODZnpzBcKHW/tnhI5ci/XIoktIShgAE/bzpK JLo0s4kwL0ifxr+nbvknVxuWM7+p30my+b+4FJ8B9X9284t22PQ3EGrxSWxh3LK/sgv6 RmmA== X-Gm-Message-State: AOAM5333GJ5ArEj8I7nRNEmyTuxQgjTJX6QCY/Crd0swhAlBS3bUtM0Z BVtpYKdsTi/YiQauFq85CxdYfg== X-Google-Smtp-Source: ABdhPJz7HdfIddaOCIvK/ddHtTxrTrM+ZkjX/SBf9dxbShsEj3/D57ML44PxbJ/ZfWi4TfkRg5Gmpw== X-Received: by 2002:aa7:cf98:: with SMTP id z24mr8140689edx.136.1628640371733; Tue, 10 Aug 2021 17:06:11 -0700 (PDT) Received: from cephalopod (168.7-181-91.adsl-dyn.isp.belgacom.be. [91.181.7.168]) by smtp.gmail.com with ESMTPSA id h19sm10369056edt.87.2021.08.10.17.06.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Aug 2021 17:06:11 -0700 (PDT) Date: Wed, 11 Aug 2021 02:06:09 +0200 From: Ben Hutchings To: Herbert Xu , "David S. Miller" Cc: linux-crypto@vger.kernel.org, linux-omap@vger.kernel.org, Lokesh Vutla , Tero Kristo Subject: [PATCH 2/2] crypto: omap - Fix inconsistent locking of device lists Message-ID: <20210811000608.GC22095@cephalopod> References: <20210811000333.GA22095@cephalopod> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210811000333.GA22095@cephalopod> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org lockdep complains that in omap-aes, the list_lock is taken both with softirqs enabled at probe time, and also in softirq context, which could lead to a deadlock: ================================ WARNING: inconsistent lock state 5.14.0-rc1-00035-gc836005b01c5-dirty #69 Not tainted -------------------------------- inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage. ksoftirqd/0/7 [HC0[0]:SC1[3]:HE1:SE0] takes: bf00e014 (list_lock){+.?.}-{2:2}, at: omap_aes_find_dev+0x18/0x54 [omap_aes_driver] {SOFTIRQ-ON-W} state was registered at: _raw_spin_lock+0x40/0x50 omap_aes_probe+0x1d4/0x664 [omap_aes_driver] platform_probe+0x58/0xb8 really_probe+0xbc/0x314 __driver_probe_device+0x80/0xe4 driver_probe_device+0x30/0xc8 __driver_attach+0x70/0xf4 bus_for_each_dev+0x70/0xb4 bus_add_driver+0xf0/0x1d4 driver_register+0x74/0x108 do_one_initcall+0x84/0x2e4 do_init_module+0x5c/0x240 load_module+0x221c/0x2584 sys_finit_module+0xb0/0xec ret_fast_syscall+0x0/0x2c 0xbed90b30 irq event stamp: 111800 hardirqs last enabled at (111800): [] __kmalloc+0x484/0x5ec hardirqs last disabled at (111799): [] __kmalloc+0x490/0x5ec softirqs last enabled at (111776): [] __do_softirq+0x2b8/0x4d0 softirqs last disabled at (111781): [] run_ksoftirqd+0x34/0x50 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(list_lock); lock(list_lock); *** DEADLOCK *** 2 locks held by ksoftirqd/0/7: #0: c0f5e8c8 (rcu_read_lock){....}-{1:2}, at: netif_receive_skb+0x6c/0x260 #1: c0f5e8c8 (rcu_read_lock){....}-{1:2}, at: ip_local_deliver_finish+0x2c/0xdc stack backtrace: CPU: 0 PID: 7 Comm: ksoftirqd/0 Not tainted 5.14.0-rc1-00035-gc836005b01c5-dirty #69 Hardware name: Generic AM43 (Flattened Device Tree) [] (unwind_backtrace) from [] (show_stack+0x10/0x14) [] (show_stack) from [] (mark_lock.part.17+0x5bc/0xd04) [] (mark_lock.part.17) from [] (__lock_acquire+0x960/0x2fa4) [] (__lock_acquire) from [] (lock_acquire+0x10c/0x358) [] (lock_acquire) from [] (_raw_spin_lock_bh+0x44/0x58) [] (_raw_spin_lock_bh) from [] (omap_aes_find_dev+0x18/0x54 [omap_aes_driver]) [] (omap_aes_find_dev [omap_aes_driver]) from [] (omap_aes_crypt+0x94/0xd4 [omap_aes_driver]) [] (omap_aes_crypt [omap_aes_driver]) from [] (esp_input+0x1b0/0x2c8) [] (esp_input) from [] (xfrm_input+0x410/0x1290) [] (xfrm_input) from [] (xfrm4_esp_rcv+0x54/0x11c) [] (xfrm4_esp_rcv) from [] (ip_protocol_deliver_rcu+0x48/0x3bc) [] (ip_protocol_deliver_rcu) from [] (ip_local_deliver_finish+0x9c/0xdc) [] (ip_local_deliver_finish) from [] (ip_local_deliver+0x148/0x1b0) [] (ip_local_deliver) from [] (ip_rcv+0x11c/0x180) [] (ip_rcv) from [] (__netif_receive_skb_one_core+0x54/0x74) [] (__netif_receive_skb_one_core) from [] (netif_receive_skb+0xa8/0x260) [] (netif_receive_skb) from [] (cpsw_rx_handler+0x224/0x2fc) [] (cpsw_rx_handler) from [] (__cpdma_chan_process+0xf4/0x188) [] (__cpdma_chan_process) from [] (cpdma_chan_process+0x3c/0x5c) [] (cpdma_chan_process) from [] (cpsw_rx_mq_poll+0x44/0x98) [] (cpsw_rx_mq_poll) from [] (__napi_poll+0x28/0x268) [] (__napi_poll) from [] (net_rx_action+0xcc/0x204) [] (net_rx_action) from [] (__do_softirq+0x140/0x4d0) [] (__do_softirq) from [] (run_ksoftirqd+0x34/0x50) [] (run_ksoftirqd) from [] (smpboot_thread_fn+0xf4/0x1d8) [] (smpboot_thread_fn) from [] (kthread+0x14c/0x174) [] (kthread) from [] (ret_from_fork+0x14/0x38) ... The omap-des and omap-sham drivers appear to have a similar issue. Fix this by using spin_{,un}lock_bh() around device list access in all the probe and remove functions. Signed-off-by: Ben Hutchings --- drivers/crypto/omap-aes.c | 8 ++++---- drivers/crypto/omap-des.c | 8 ++++---- drivers/crypto/omap-sham.c | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/drivers/crypto/omap-aes.c b/drivers/crypto/omap-aes.c index 0dd4c6b157de..9b968ac4ee7b 100644 --- a/drivers/crypto/omap-aes.c +++ b/drivers/crypto/omap-aes.c @@ -1175,9 +1175,9 @@ static int omap_aes_probe(struct platform_device *pdev) spin_lock_init(&dd->lock); INIT_LIST_HEAD(&dd->list); - spin_lock(&list_lock); + spin_lock_bh(&list_lock); list_add_tail(&dd->list, &dev_list); - spin_unlock(&list_lock); + spin_unlock_bh(&list_lock); /* Initialize crypto engine */ dd->engine = crypto_engine_alloc_init(dev, 1); @@ -1264,9 +1264,9 @@ static int omap_aes_remove(struct platform_device *pdev) if (!dd) return -ENODEV; - spin_lock(&list_lock); + spin_lock_bh(&list_lock); list_del(&dd->list); - spin_unlock(&list_lock); + spin_unlock_bh(&list_lock); for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) { diff --git a/drivers/crypto/omap-des.c b/drivers/crypto/omap-des.c index bc8631363d72..be77656864e3 100644 --- a/drivers/crypto/omap-des.c +++ b/drivers/crypto/omap-des.c @@ -1033,9 +1033,9 @@ static int omap_des_probe(struct platform_device *pdev) INIT_LIST_HEAD(&dd->list); - spin_lock(&list_lock); + spin_lock_bh(&list_lock); list_add_tail(&dd->list, &dev_list); - spin_unlock(&list_lock); + spin_unlock_bh(&list_lock); /* Initialize des crypto engine */ dd->engine = crypto_engine_alloc_init(dev, 1); @@ -1094,9 +1094,9 @@ static int omap_des_remove(struct platform_device *pdev) if (!dd) return -ENODEV; - spin_lock(&list_lock); + spin_lock_bh(&list_lock); list_del(&dd->list); - spin_unlock(&list_lock); + spin_unlock_bh(&list_lock); for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) diff --git a/drivers/crypto/omap-sham.c b/drivers/crypto/omap-sham.c index 735abd8f68d6..f6bf53c00b61 100644 --- a/drivers/crypto/omap-sham.c +++ b/drivers/crypto/omap-sham.c @@ -2127,9 +2127,9 @@ static int omap_sham_probe(struct platform_device *pdev) (rev & dd->pdata->major_mask) >> dd->pdata->major_shift, (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift); - spin_lock(&sham.lock); + spin_lock_bh(&sham.lock); list_add_tail(&dd->list, &sham.dev_list); - spin_unlock(&sham.lock); + spin_unlock_bh(&sham.lock); dd->engine = crypto_engine_alloc_init(dev, 1); if (!dd->engine) { @@ -2177,9 +2177,9 @@ static int omap_sham_probe(struct platform_device *pdev) err_engine_start: crypto_engine_exit(dd->engine); err_engine: - spin_lock(&sham.lock); + spin_lock_bh(&sham.lock); list_del(&dd->list); - spin_unlock(&sham.lock); + spin_unlock_bh(&sham.lock); err_pm: pm_runtime_dont_use_autosuspend(dev); pm_runtime_disable(dev); @@ -2199,9 +2199,9 @@ static int omap_sham_remove(struct platform_device *pdev) dd = platform_get_drvdata(pdev); if (!dd) return -ENODEV; - spin_lock(&sham.lock); + spin_lock_bh(&sham.lock); list_del(&dd->list); - spin_unlock(&sham.lock); + spin_unlock_bh(&sham.lock); for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) { crypto_unregister_ahash(