All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ubifs: work around high stack usage with clang
@ 2019-03-24 19:44 ` Arnd Bergmann
  0 siblings, 0 replies; 4+ messages in thread
From: Arnd Bergmann @ 2019-03-24 19:44 UTC (permalink / raw)
  To: Richard Weinberger, Artem Bityutskiy, Adrian Hunter
  Cc: clang-built-linux, Nick Desaulniers, Nathan Chancellor,
	Arnd Bergmann, Eric Biggers, Herbert Xu, Sascha Hauer, linux-mtd,
	linux-kernel

Building this file with clang can result in large stack usage as seen from
this warning:

fs/ubifs/auth.c:78:5: error: stack frame size of 1152 bytes in function 'ubifs_prepare_auth_node'

The problem is that inlining ubifs_hash_calc_hmac() leads to
two SHASH_DESC_ON_STACK() blocks in the same function, and clang
for some reason does not reuse the stack space as it should.

Putting the first declaration into a separate basic block avoids
this problem and reduces the stack allocation to 640 bytes.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 fs/ubifs/auth.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/ubifs/auth.c b/fs/ubifs/auth.c
index 5bf5fd08879e..5d3d0b37a908 100644
--- a/fs/ubifs/auth.c
+++ b/fs/ubifs/auth.c
@@ -78,7 +78,6 @@ static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
 int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
 			     struct shash_desc *inhash)
 {
-	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
 	struct ubifs_auth_node *auth = node;
 	u8 *hash;
 	int err;
@@ -87,13 +86,17 @@ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
 	if (!hash)
 		return -ENOMEM;
 
-	hash_desc->tfm = c->hash_tfm;
-	hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
-	ubifs_shash_copy_state(c, inhash, hash_desc);
+	{
+		SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
 
-	err = crypto_shash_final(hash_desc, hash);
-	if (err)
-		goto out;
+		hash_desc->tfm = c->hash_tfm;
+		hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
+		ubifs_shash_copy_state(c, inhash, hash_desc);
+
+		err = crypto_shash_final(hash_desc, hash);
+		if (err)
+			goto out;
+	}
 
 	err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
 	if (err)
-- 
2.20.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] ubifs: work around high stack usage with clang
@ 2019-03-24 19:44 ` Arnd Bergmann
  0 siblings, 0 replies; 4+ messages in thread
From: Arnd Bergmann @ 2019-03-24 19:44 UTC (permalink / raw)
  To: Richard Weinberger, Artem Bityutskiy, Adrian Hunter
  Cc: Herbert Xu, Arnd Bergmann, Eric Biggers, Sascha Hauer,
	Nick Desaulniers, linux-kernel, clang-built-linux, linux-mtd,
	Nathan Chancellor

Building this file with clang can result in large stack usage as seen from
this warning:

fs/ubifs/auth.c:78:5: error: stack frame size of 1152 bytes in function 'ubifs_prepare_auth_node'

The problem is that inlining ubifs_hash_calc_hmac() leads to
two SHASH_DESC_ON_STACK() blocks in the same function, and clang
for some reason does not reuse the stack space as it should.

Putting the first declaration into a separate basic block avoids
this problem and reduces the stack allocation to 640 bytes.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 fs/ubifs/auth.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/ubifs/auth.c b/fs/ubifs/auth.c
index 5bf5fd08879e..5d3d0b37a908 100644
--- a/fs/ubifs/auth.c
+++ b/fs/ubifs/auth.c
@@ -78,7 +78,6 @@ static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
 int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
 			     struct shash_desc *inhash)
 {
-	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
 	struct ubifs_auth_node *auth = node;
 	u8 *hash;
 	int err;
@@ -87,13 +86,17 @@ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
 	if (!hash)
 		return -ENOMEM;
 
-	hash_desc->tfm = c->hash_tfm;
-	hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
-	ubifs_shash_copy_state(c, inhash, hash_desc);
+	{
+		SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
 
-	err = crypto_shash_final(hash_desc, hash);
-	if (err)
-		goto out;
+		hash_desc->tfm = c->hash_tfm;
+		hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
+		ubifs_shash_copy_state(c, inhash, hash_desc);
+
+		err = crypto_shash_final(hash_desc, hash);
+		if (err)
+			goto out;
+	}
 
 	err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
 	if (err)
-- 
2.20.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ubifs: work around high stack usage with clang
  2019-03-24 19:44 ` Arnd Bergmann
@ 2019-03-25 17:57   ` Nick Desaulniers
  -1 siblings, 0 replies; 4+ messages in thread
From: Nick Desaulniers @ 2019-03-25 17:57 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Richard Weinberger, Artem Bityutskiy, Adrian Hunter,
	clang-built-linux, Nathan Chancellor, Eric Biggers, Herbert Xu,
	Sascha Hauer, linux-mtd, LKML

On Sun, Mar 24, 2019 at 12:45 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> Building this file with clang can result in large stack usage as seen from
> this warning:
>
> fs/ubifs/auth.c:78:5: error: stack frame size of 1152 bytes in function 'ubifs_prepare_auth_node'
>
> The problem is that inlining ubifs_hash_calc_hmac() leads to
> two SHASH_DESC_ON_STACK() blocks in the same function, and clang
> for some reason does not reuse the stack space as it should.
>
> Putting the first declaration into a separate basic block avoids
> this problem and reduces the stack allocation to 640 bytes.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

LGTM; in general, it can be useful to always include a comment before
a new scope like:

/* New scope to limit lifetime of large stack allocated hash_desc. */

but folks can always refer to the git blame/log for reasoning as to
why a new scope was added.

I plan on working on a pass in LLVM to improve stack slot reuse
sometime in 2019Q2.  Thanks for this patch.
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>  fs/ubifs/auth.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/fs/ubifs/auth.c b/fs/ubifs/auth.c
> index 5bf5fd08879e..5d3d0b37a908 100644
> --- a/fs/ubifs/auth.c
> +++ b/fs/ubifs/auth.c
> @@ -78,7 +78,6 @@ static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
>  int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
>                              struct shash_desc *inhash)
>  {
> -       SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
>         struct ubifs_auth_node *auth = node;
>         u8 *hash;
>         int err;
> @@ -87,13 +86,17 @@ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
>         if (!hash)
>                 return -ENOMEM;
>
> -       hash_desc->tfm = c->hash_tfm;
> -       hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
> -       ubifs_shash_copy_state(c, inhash, hash_desc);
> +       {
> +               SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
>
> -       err = crypto_shash_final(hash_desc, hash);
> -       if (err)
> -               goto out;
> +               hash_desc->tfm = c->hash_tfm;
> +               hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
> +               ubifs_shash_copy_state(c, inhash, hash_desc);
> +
> +               err = crypto_shash_final(hash_desc, hash);
> +               if (err)
> +                       goto out;
> +       }
>
>         err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
>         if (err)
> --
> 2.20.0
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ubifs: work around high stack usage with clang
@ 2019-03-25 17:57   ` Nick Desaulniers
  0 siblings, 0 replies; 4+ messages in thread
From: Nick Desaulniers @ 2019-03-25 17:57 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Herbert Xu, Eric Biggers, Richard Weinberger, Sascha Hauer,
	Artem Bityutskiy, Adrian Hunter, LKML, clang-built-linux,
	linux-mtd, Nathan Chancellor

On Sun, Mar 24, 2019 at 12:45 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> Building this file with clang can result in large stack usage as seen from
> this warning:
>
> fs/ubifs/auth.c:78:5: error: stack frame size of 1152 bytes in function 'ubifs_prepare_auth_node'
>
> The problem is that inlining ubifs_hash_calc_hmac() leads to
> two SHASH_DESC_ON_STACK() blocks in the same function, and clang
> for some reason does not reuse the stack space as it should.
>
> Putting the first declaration into a separate basic block avoids
> this problem and reduces the stack allocation to 640 bytes.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

LGTM; in general, it can be useful to always include a comment before
a new scope like:

/* New scope to limit lifetime of large stack allocated hash_desc. */

but folks can always refer to the git blame/log for reasoning as to
why a new scope was added.

I plan on working on a pass in LLVM to improve stack slot reuse
sometime in 2019Q2.  Thanks for this patch.
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>  fs/ubifs/auth.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/fs/ubifs/auth.c b/fs/ubifs/auth.c
> index 5bf5fd08879e..5d3d0b37a908 100644
> --- a/fs/ubifs/auth.c
> +++ b/fs/ubifs/auth.c
> @@ -78,7 +78,6 @@ static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
>  int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
>                              struct shash_desc *inhash)
>  {
> -       SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
>         struct ubifs_auth_node *auth = node;
>         u8 *hash;
>         int err;
> @@ -87,13 +86,17 @@ int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
>         if (!hash)
>                 return -ENOMEM;
>
> -       hash_desc->tfm = c->hash_tfm;
> -       hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
> -       ubifs_shash_copy_state(c, inhash, hash_desc);
> +       {
> +               SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
>
> -       err = crypto_shash_final(hash_desc, hash);
> -       if (err)
> -               goto out;
> +               hash_desc->tfm = c->hash_tfm;
> +               hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
> +               ubifs_shash_copy_state(c, inhash, hash_desc);
> +
> +               err = crypto_shash_final(hash_desc, hash);
> +               if (err)
> +                       goto out;
> +       }
>
>         err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
>         if (err)
> --
> 2.20.0
>


-- 
Thanks,
~Nick Desaulniers

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-03-25 17:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-24 19:44 [PATCH] ubifs: work around high stack usage with clang Arnd Bergmann
2019-03-24 19:44 ` Arnd Bergmann
2019-03-25 17:57 ` Nick Desaulniers
2019-03-25 17:57   ` Nick Desaulniers

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.