Skip to contentSkip to conversation

ubifs: work around high stack usage with clang

PATCH In review Arnd Bergmann 2 participants 2 messages Mar 24, 2019 – Mar 25, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubifs: work around high stack usage with clang

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.

---
 fs/ubifs/auth.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
fs/ubifs/auth.c +10 -7
index 5bf5fd08879e..5d3d0b37a908 100644
@@ -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)
Signature · 1 line
2.20.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Arnd Bergmann

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'

Quoted 8 lines

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.

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.

Quoted 43 lines
---
 fs/ubifs/auth.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
fs/ubifs/auth.c +10 -8
index 5bf5fd08879e..5d3d0b37a908 100644
@@ -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

Signature · 2 lines
Thanks,
~Nick Desaulniers