From mboxrd@z Thu Jan 1 00:00:00 1970 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=-4.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS autolearn=no 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 C5A90C433E2 for ; Mon, 25 May 2020 09:07:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A690E2068D for ; Mon, 25 May 2020 09:07:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590397647; bh=xDu8dLlglr8K91hawwE4cfFpH4NGPUCe5IF8bxHut2s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:List-ID:From; b=gmLpAqJNuF7lU1rLio/9+5QUFH0By17Cn0r51EsNeAksnau7Yawj5zFI76CHtb3eb +pM+dccE0xdF0jEkQ0NIcfKy/N6GfKIbJsqmgB+jyeMIFotKwY1ITcwV37QMUjlsqU /ylMCN+gJ7ynMkc7nY5aGcRWpEfSIOJHPyqo5bX4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389299AbgEYJHX (ORCPT ); Mon, 25 May 2020 05:07:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:60894 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389294AbgEYJHX (ORCPT ); Mon, 25 May 2020 05:07:23 -0400 Received: from mail-io1-f49.google.com (mail-io1-f49.google.com [209.85.166.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2C1A42084C; Mon, 25 May 2020 09:07:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590397642; bh=xDu8dLlglr8K91hawwE4cfFpH4NGPUCe5IF8bxHut2s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=upEMwLfGG5pi9p3K+vxuXs0az7TTBHDo3uDDF39m77SbroYEeZD/9pMK4LQikloap 8CL2mVxo9pGdax86qxQdcF9OuLcvTsUZDZ0epvACsdnVbtxrucAkIAkdFjlth9FrD5 g+D4mvILxfAzLGL1l4SxRtW6mkFw/KD3ZUTdkBEs= Received: by mail-io1-f49.google.com with SMTP id p20so4563429iop.11; Mon, 25 May 2020 02:07:22 -0700 (PDT) X-Gm-Message-State: AOAM5336C0Y+mSontXSUUPqg/mgEscLEZLSQvgLZuTMlykGbwjlYgXHB SZZEZQVz4GgFAimIjHAvQgZJjMmzUTi9iyaREe8= X-Google-Smtp-Source: ABdhPJyCtr20yxFYDZawW3EBVKQBnR7JS6BmYQvfftEGhtPaFgT0QoTcn8YZlJtxk8v/BozcEBSD/6TfBBAMnfTAchU= X-Received: by 2002:a02:3341:: with SMTP id k1mr19168056jak.74.1590397641457; Mon, 25 May 2020 02:07:21 -0700 (PDT) MIME-Version: 1.0 References: <20200512141113.18972-1-nicolas.toromanoff@st.com> <20200512141113.18972-6-nicolas.toromanoff@st.com> <67c25d90d9714a85b52f3d9c2070af88@SFHDAG6NODE1.st.com> In-Reply-To: From: Ard Biesheuvel Date: Mon, 25 May 2020 11:07:10 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 5/5] crypto: stm32/crc: protect from concurrent accesses To: Nicolas TOROMANOFF , Eric Biggers Cc: Herbert Xu , "David S . Miller" , Maxime Coquelin , Alexandre TORGUE , Linux Crypto Mailing List , "linux-stm32@st-md-mailman.stormreply.com" , Linux ARM , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org (+ Eric) On Mon, 25 May 2020 at 11:01, Nicolas TOROMANOFF wrote: > > > -----Original Message----- > > From: Ard Biesheuvel > > Sent: Monday, May 25, 2020 9:46 AM > > To: Nicolas TOROMANOFF > > Subject: Re: [PATCH 5/5] crypto: stm32/crc: protect from concurrent accesses > > > > On Mon, 25 May 2020 at 09:24, Nicolas TOROMANOFF > > wrote: > > > > > > Hello, > > > > > > > -----Original Message----- > > > > From: Ard Biesheuvel > > > > Sent: Friday, May 22, 2020 6:12 PM> > > > > On Tue, 12 May 2020 at 16:13, Nicolas Toromanoff > > > > wrote: > > > > > > > > > > Protect STM32 CRC device from concurrent accesses. > > > > > > > > > > As we create a spinlocked section that increase with buffer size, > > > > > we provide a module parameter to release the pressure by splitting > > > > > critical section in chunks. > > > > > > > > > > Size of each chunk is defined in burst_size module parameter. > > > > > By default burst_size=0, i.e. don't split incoming buffer. > > > > > > > > > > Signed-off-by: Nicolas Toromanoff > > > > > > > > Would you mind explaining the usage model here? It looks like you > > > > are sharing a CRC hardware accelerator with a synchronous interface > > > > between different users by using spinlocks? You are aware that this > > > > will tie up the waiting CPUs completely during this time, right? So > > > > it would be much better to use a mutex here. Or perhaps it would > > > > make more sense to fall back to a s/w based CRC routine if the h/w is tied up > > working for another task? > > > > > > I know mutex are more acceptable here, but shash _update() and _init() > > > may be call from any context, and so I cannot take a mutex. > > > And to protect my concurrent HW access I only though about spinlock. > > > Due to possible constraint on CPUs, I add a burst_size option to force > > > slitting long buffer into smaller one, and so decrease time we take the lock. > > > But I didn't though to fallback to software CRC. > > > > > > I'll do a patch on top. > > > In in the burst_update() function I'll use a spin_trylock_irqsave() and use > > software CRC32 if HW is already in use. > > > > > > > Right. I didn't even notice that you were keeping interrupts disabled the whole > > time when using the h/w block. That means that any serious use of this h/w > > block will make IRQ latency go through the roof. > > > > I recommend that you go back to the drawing board on this driver, rather than > > papering over the issues with a spin_trylock(). Perhaps it would be better to > > model it as a ahash (even though the h/w block itself is synchronous) and use a > > kthread to feed in the data. > > I thought when I updated the driver to move to a ahash interface, but the main usage > of crc32 is the ext4 fs, that calls the shash API. > Commit 877b5691f27a ("crypto: shash - remove shash_desc::flags") removed possibility > to sleep in shash callback. (before this commit and with MAY_SLEEP option set, using > a mutex may have been fine). > According to that commit's log, sleeping is never fine for shash(), since it uses kmap_atomic() when iterating over the scatterlist. > By now the solution I see is to use the spin_trylock_irqsave(), fallback to software crc *AND* capping burst_size > to ensure the locked section stay reasonable. > > Does this seems acceptable ? > If the reason for disabling interrupts is to avoid deadlocks, wouldn't the switch to trylock() with a software fallback allow us to keep interrupts enabled? From mboxrd@z Thu Jan 1 00:00:00 1970 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=-4.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=no 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 EAC91C433E0 for ; Mon, 25 May 2020 09:07:44 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 969262068D for ; Mon, 25 May 2020 09:07:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="QR7fnNoL"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="upEMwLfG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 969262068D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/7mC2EfwO2EQGbEAgg+Lh8BIeT3zyJhrrncxPXsl3TI=; b=QR7fnNoL/EN4E0 dpxXir1NeRMmStoyWitamcySQWsg9uCXtPYQShdKO4PQXvtdxmujSUVEDSPYB7YVWNG/Sy9U7+iQn u6AnKUCMO/CGWBt7wkgo1CJe7P4QBO1UGqYVnxB+Qk58mipOLmJPH4LLgQEp2s1iOhuIIipI1Y8CW GtyMlzSH7uRuwk7VyDHFeP//Yf2Vg4MZxfNy1or9306Y2wQhO6ezeUkqN4C91zaqX2XGgQs6jQ6Mn vVJvoUXFe70KYW+/FpxLWngzzZY+/87LpRK3yeakoBkRfh96njaQbETLzOVryjTHDdU95WUO+zx/P y1Cboc4l8/Cbo/KCQbIg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jd95E-0007h2-4V; Mon, 25 May 2020 09:07:44 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jd94s-0007RJ-PF for linux-arm-kernel@lists.infradead.org; Mon, 25 May 2020 09:07:24 +0000 Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 20ED02073B for ; Mon, 25 May 2020 09:07:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590397642; bh=xDu8dLlglr8K91hawwE4cfFpH4NGPUCe5IF8bxHut2s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=upEMwLfGG5pi9p3K+vxuXs0az7TTBHDo3uDDF39m77SbroYEeZD/9pMK4LQikloap 8CL2mVxo9pGdax86qxQdcF9OuLcvTsUZDZ0epvACsdnVbtxrucAkIAkdFjlth9FrD5 g+D4mvILxfAzLGL1l4SxRtW6mkFw/KD3ZUTdkBEs= Received: by mail-io1-f45.google.com with SMTP id q129so8838590iod.6 for ; Mon, 25 May 2020 02:07:22 -0700 (PDT) X-Gm-Message-State: AOAM531tipVhnI0/HTjQmFzIiexaRz0lp0YWr9BYgwFli7abReWSWXkz 6wOkUAZsxVnjKaKP9pI3hZjJqiweaV03NmAkz2o= X-Google-Smtp-Source: ABdhPJyCtr20yxFYDZawW3EBVKQBnR7JS6BmYQvfftEGhtPaFgT0QoTcn8YZlJtxk8v/BozcEBSD/6TfBBAMnfTAchU= X-Received: by 2002:a02:3341:: with SMTP id k1mr19168056jak.74.1590397641457; Mon, 25 May 2020 02:07:21 -0700 (PDT) MIME-Version: 1.0 References: <20200512141113.18972-1-nicolas.toromanoff@st.com> <20200512141113.18972-6-nicolas.toromanoff@st.com> <67c25d90d9714a85b52f3d9c2070af88@SFHDAG6NODE1.st.com> In-Reply-To: From: Ard Biesheuvel Date: Mon, 25 May 2020 11:07:10 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 5/5] crypto: stm32/crc: protect from concurrent accesses To: Nicolas TOROMANOFF , Eric Biggers X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200525_020722_852954_0FCCF24E X-CRM114-Status: GOOD ( 27.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexandre TORGUE , Linux Kernel Mailing List , "David S . Miller" , Linux Crypto Mailing List , Maxime Coquelin , "linux-stm32@st-md-mailman.stormreply.com" , Linux ARM , Herbert Xu Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org (+ Eric) On Mon, 25 May 2020 at 11:01, Nicolas TOROMANOFF wrote: > > > -----Original Message----- > > From: Ard Biesheuvel > > Sent: Monday, May 25, 2020 9:46 AM > > To: Nicolas TOROMANOFF > > Subject: Re: [PATCH 5/5] crypto: stm32/crc: protect from concurrent accesses > > > > On Mon, 25 May 2020 at 09:24, Nicolas TOROMANOFF > > wrote: > > > > > > Hello, > > > > > > > -----Original Message----- > > > > From: Ard Biesheuvel > > > > Sent: Friday, May 22, 2020 6:12 PM> > > > > On Tue, 12 May 2020 at 16:13, Nicolas Toromanoff > > > > wrote: > > > > > > > > > > Protect STM32 CRC device from concurrent accesses. > > > > > > > > > > As we create a spinlocked section that increase with buffer size, > > > > > we provide a module parameter to release the pressure by splitting > > > > > critical section in chunks. > > > > > > > > > > Size of each chunk is defined in burst_size module parameter. > > > > > By default burst_size=0, i.e. don't split incoming buffer. > > > > > > > > > > Signed-off-by: Nicolas Toromanoff > > > > > > > > Would you mind explaining the usage model here? It looks like you > > > > are sharing a CRC hardware accelerator with a synchronous interface > > > > between different users by using spinlocks? You are aware that this > > > > will tie up the waiting CPUs completely during this time, right? So > > > > it would be much better to use a mutex here. Or perhaps it would > > > > make more sense to fall back to a s/w based CRC routine if the h/w is tied up > > working for another task? > > > > > > I know mutex are more acceptable here, but shash _update() and _init() > > > may be call from any context, and so I cannot take a mutex. > > > And to protect my concurrent HW access I only though about spinlock. > > > Due to possible constraint on CPUs, I add a burst_size option to force > > > slitting long buffer into smaller one, and so decrease time we take the lock. > > > But I didn't though to fallback to software CRC. > > > > > > I'll do a patch on top. > > > In in the burst_update() function I'll use a spin_trylock_irqsave() and use > > software CRC32 if HW is already in use. > > > > > > > Right. I didn't even notice that you were keeping interrupts disabled the whole > > time when using the h/w block. That means that any serious use of this h/w > > block will make IRQ latency go through the roof. > > > > I recommend that you go back to the drawing board on this driver, rather than > > papering over the issues with a spin_trylock(). Perhaps it would be better to > > model it as a ahash (even though the h/w block itself is synchronous) and use a > > kthread to feed in the data. > > I thought when I updated the driver to move to a ahash interface, but the main usage > of crc32 is the ext4 fs, that calls the shash API. > Commit 877b5691f27a ("crypto: shash - remove shash_desc::flags") removed possibility > to sleep in shash callback. (before this commit and with MAY_SLEEP option set, using > a mutex may have been fine). > According to that commit's log, sleeping is never fine for shash(), since it uses kmap_atomic() when iterating over the scatterlist. > By now the solution I see is to use the spin_trylock_irqsave(), fallback to software crc *AND* capping burst_size > to ensure the locked section stay reasonable. > > Does this seems acceptable ? > If the reason for disabling interrupts is to avoid deadlocks, wouldn't the switch to trylock() with a software fallback allow us to keep interrupts enabled? _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel