Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3763770ybi; Tue, 18 Jun 2019 06:13:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqw4Cu9M0M9JVcSqBUlCY6R68ea9uZw6gycENic7t5pyugxcXeKBosUsROobH/qSqaVv2+JA X-Received: by 2002:a63:fd50:: with SMTP id m16mr2635585pgj.192.1560863582253; Tue, 18 Jun 2019 06:13:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560863582; cv=none; d=google.com; s=arc-20160816; b=q9qha9PLA/MKUl+sfuN63KRKqlWQ9zV50LSjEIIqxsqMGMo6OCiMeT2IgE1ZStdJG+ rcRlq67GHRWXxBpFULtZOv3/qQAonaeMmSwOwAz4NRtg/BVWsPOj6yQ7Ufi0JZkNrZ9z PTeYgsXsvd7xX5uwk9B8crWwPhkO2Xd7/Ur7Ja6Bh/Qw18dBTkiYKElTCFeoZtiiTShj barxuO4INS6JF83sUKVtSBNeAYvUFeJIPwjtr9Pc2BlqRXINjAkucaEN8rVjF6AmBeo/ AkzgBnVr49RAPMg80tAD5tUEllLCCK6i6o6KGuJ8qIm5ymhmb86CSOVjeSdmJl4hZTdh xMrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=7FALWQ6cOh3JsG038fbVz+yiqqz0f79O0ClChfodEQs=; b=XkZtS3J/fRD6pnY9BTy0sI67CBK+fl+v8UgRZ9VXTKLw5zXqS0BgrjUSesNezMGDl8 aZuuLshaUR2p6MXPoyI/WXCHVyxzAZDzUxDwNbWOU5JNXEsHwj7nQ9srfU6Vnhao7d3X iWFeaNnOI+589AoYLhet8bA838egfUscX4ncWWFqSyj49vOtqLs3kfTxWpyY+NBtbmtr 8xssjr2LSviLzWfTWEXXKAk/KzNPemuIe/07CPKt/jsVhPmkBty4LBlzFGbatu95bxad QIgfm2XKSp0YICxfmmfUWXY0ytNDV7xdSre4227WQHp/XdUTHU4m/ycBMZpWDHKfK0p3 IQPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Q/DsCxTT"; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e96si13259281plb.123.2019.06.18.06.12.43; Tue, 18 Jun 2019 06:13:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Q/DsCxTT"; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726518AbfFRNMl (ORCPT + 99 others); Tue, 18 Jun 2019 09:12:41 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:36575 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725919AbfFRNMk (ORCPT ); Tue, 18 Jun 2019 09:12:40 -0400 Received: by mail-wr1-f65.google.com with SMTP id n4so5843689wrs.3; Tue, 18 Jun 2019 06:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=7FALWQ6cOh3JsG038fbVz+yiqqz0f79O0ClChfodEQs=; b=Q/DsCxTTpuF0/+fAH5Ad7SkE1PEf7KzB0fJmiSFe3Xjj6DuqgZNMeHGkeInINWD7cJ Ikx6ys+AQneaMr5n0KwcWnkfDF5TIxTviUkX6TyvNYtHd9KYqkI7bTSr+LvMGZZyjx2W 2XcDJ4eWGtV/3vi+lK6IMdcjIV/k61XxTIETIq4nloIr254y6uadVtTEjrFn3a9oJ5Zl ouXDH8nA7WssQ3LS7eJSiMwDMvY5ELgndTHzN/aANZi3r+XuE1NkLuE7ZE/bNWfND0uH 5rYsO63g3CRDprdEw7yH9nzGzIeQ4BxagJkD1FsZj+sittr3Dn+TBrqUsJrH5pbz+HYP EmyQ== 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=7FALWQ6cOh3JsG038fbVz+yiqqz0f79O0ClChfodEQs=; b=ebVSpR1cnq7niqyWVA1LJk7sabuV6OKzmhmP4MvDXT8/XB4jIWhyjZDzCpPK3htxqv bdz9AmkyHoFxfU2MgcmV5J3XtgEsx0HtR/50LwM/R/UlzMFkoHallRRcx5VK2GToEMRq p82fM3pKZobkWvWey7uX1Iw9kj3dAnduJlFp4FB+X3/qs7rhmokty0FqKSJI4USSc2CS ++9BuzvQbU1exPyHnSlDF4jchZkKDoh5WpU6fa6YaGbf4jmhCBloaReBHOD+cBIOtQYM qi1CGL7kQdIHzLnoCswUXq9A/2JaqSXltJsW31D6BMDWBtEVGSJac9uBNVjAnmhURfXm JyZA== X-Gm-Message-State: APjAAAXYV6RMacEZCM/YyZfphRuIdFzR4qAbyJ0rYZhEFh33OrxEZUrI ivHB2+827jHUrqGCIgaHE8c= X-Received: by 2002:adf:eecf:: with SMTP id a15mr7408693wrp.354.1560863557790; Tue, 18 Jun 2019 06:12:37 -0700 (PDT) Received: from Red ([2a01:cb1d:147:7200:2e56:dcff:fed2:c6d6]) by smtp.googlemail.com with ESMTPSA id f2sm21287538wrq.48.2019.06.18.06.12.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Jun 2019 06:12:37 -0700 (PDT) Date: Tue, 18 Jun 2019 15:12:34 +0200 From: Corentin Labbe To: Arnd Bergmann Cc: Herbert Xu , "David S. Miller" , Maxime Ripard , Chen-Yu Tsai , Eric Biggers , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] crypto: sun4i-ss - reduce stack usage Message-ID: <20190618131234.GA8474@Red> References: <20190617132538.2759714-1-arnd@arndb.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190617132538.2759714-1-arnd@arndb.de> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Mon, Jun 17, 2019 at 03:25:17PM +0200, Arnd Bergmann wrote: > After the latest addition, the stack usage of sun4i_ss_cipher_poll > grew beyond the warning limit when KASAN is enabled: > > drivers/crypto/sunxi-ss/sun4i-ss-cipher.c:118:12: error: stack frame size of 1152 bytes in function 'sun4i_ss_cipher_poll' [-Werror,-Wframe-larger-than=] > static int sun4i_ss_cipher_poll(struct skcipher_request *areq) > > Reduce it in three ways: > > - split out the new code into a separate function so its stack > usage can overlap that of the sun4i_ss_opti_poll() code path > - mark both special cases as noinline_for_stack, which should > ideally result in a tail call that frees the rest of the > stack > - move the buf and obuf variables into the code blocks in > which they are used. > > The three separate functions now use 144, 640 and 304 bytes of kernel > stack, respectively. > > Fixes: 0ae1f46c55f8 ("crypto: sun4i-ss - fallback when length is not multiple of blocksize") > Signed-off-by: Arnd Bergmann > --- > drivers/crypto/sunxi-ss/sun4i-ss-cipher.c | 47 +++++++++++++++-------- > 1 file changed, 30 insertions(+), 17 deletions(-) > > diff --git a/drivers/crypto/sunxi-ss/sun4i-ss-cipher.c b/drivers/crypto/sunxi-ss/sun4i-ss-cipher.c > index 7b0c42882830..4ab14d58e85b 100644 > --- a/drivers/crypto/sunxi-ss/sun4i-ss-cipher.c > +++ b/drivers/crypto/sunxi-ss/sun4i-ss-cipher.c > @@ -12,7 +12,7 @@ > */ > #include "sun4i-ss.h" > > -static int sun4i_ss_opti_poll(struct skcipher_request *areq) > +static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) > { > struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); > struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm); > @@ -114,6 +114,29 @@ static int sun4i_ss_opti_poll(struct skcipher_request *areq) > return err; > } > > + > +static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq) > +{ > + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq); > + struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm); > + struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq); > + SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm); > + int err; > + > + skcipher_request_set_sync_tfm(subreq, op->fallback_tfm); > + skcipher_request_set_callback(subreq, areq->base.flags, NULL, > + NULL); > + skcipher_request_set_crypt(subreq, areq->src, areq->dst, > + areq->cryptlen, areq->iv); > + if (ctx->mode & SS_DECRYPTION) > + err = crypto_skcipher_decrypt(subreq); > + else > + err = crypto_skcipher_encrypt(subreq); > + skcipher_request_zero(subreq); > + > + return err; > +} > + > /* Generic function that support SG with size not multiple of 4 */ > static int sun4i_ss_cipher_poll(struct skcipher_request *areq) > { > @@ -140,8 +163,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) > unsigned int todo; > struct sg_mapping_iter mi, mo; > unsigned int oi, oo; /* offset for in and out */ > - char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */ > - char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */ > unsigned int ob = 0; /* offset in buf */ > unsigned int obo = 0; /* offset in bufo*/ > unsigned int obl = 0; /* length of data in bufo */ > @@ -178,20 +199,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) > if (no_chunk == 1 && !need_fallback) > return sun4i_ss_opti_poll(areq); > > - if (need_fallback) { > - SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm); > - skcipher_request_set_sync_tfm(subreq, op->fallback_tfm); > - skcipher_request_set_callback(subreq, areq->base.flags, NULL, > - NULL); > - skcipher_request_set_crypt(subreq, areq->src, areq->dst, > - areq->cryptlen, areq->iv); > - if (ctx->mode & SS_DECRYPTION) > - err = crypto_skcipher_decrypt(subreq); > - else > - err = crypto_skcipher_encrypt(subreq); > - skcipher_request_zero(subreq); > - return err; > - } > + if (need_fallback) > + return sun4i_ss_cipher_poll_fallback(areq); > > spin_lock_irqsave(&ss->slock, flags); > > @@ -224,6 +233,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) > > while (oleft) { > if (ileft) { > + char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */ > + > /* > * todo is the number of consecutive 4byte word that we > * can read from current SG > @@ -281,6 +292,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) > oo = 0; > } > } else { > + char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */ > + > /* > * read obl bytes in bufo, we read at maximum for > * emptying the device > -- > 2.20.0 > Tested-by: Corentin LABBE