Skip to contentSkip to conversation

bcache: Zero initialize bucket in bch_allocator_thread

PATCH In review Nathan Chancellor 3 participants 3 messages Mar 7, 2019 – Mar 22, 2019 bcache (block layer cache)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcache: Zero initialize bucket in bch_allocator_thread

When building with -Wsometimes-uninitialized, Clang warns:

drivers/md/bcache/alloc.c:333:4: warning: variable 'bucket' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

fifo_pop expands to a conditional depending on '!fifo_empty(...)', which is the same condition as the while loop. It seems Clang can't piece together that this condition will never been false as long as we are in the while loop because they are the same condition. It's easy enough to silence this warning by just explicitly zero initializing bucket.

---
 drivers/md/bcache/alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/alloc.c +1 -1
index 5002838ea476..c72094901ac6 100644
@@ -328,7 +328,7 @@ static int bch_allocator_thread(void *arg)
 		 * the free list:
 		 */
 		while (!fifo_empty(&ca->free_inc)) {
-			long bucket;
+			long bucket = 0;
 
 			fifo_pop(&ca->free_inc, bucket);
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 6 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/md/bcache/alloc.c:333:4: warning: variable 'bucket' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

Quoted 8 lines

fifo_pop expands to a conditional depending on '!fifo_empty(...)', which is the same condition as the while loop. It seems Clang can't piece together that this condition will never been false as long as we are in the while loop because they are the same condition. It's easy enough to silence this warning by just explicitly zero initializing bucket.

Hi Nathan,

Nice catch, I will add it to my for-test. Thanks.

Coly Li

Quoted 18 lines
---
 drivers/md/bcache/alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/alloc.c +1 -1
index 5002838ea476..c72094901ac6 100644
@@ -328,7 +328,7 @@ static int bch_allocator_thread(void *arg)
 		 * the free list:
 		 */
 		while (!fifo_empty(&ca->free_inc)) {
-			long bucket;
+			long bucket = 0;
 
 			fifo_pop(&ca->free_inc, bucket);
 
Signature · 1 line
Coly Li
#2 lore raw
Quoted 13 lines · Coly Li
Quoted 12 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/md/bcache/alloc.c:333:4: warning: variable 'bucket' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

fifo_pop expands to a conditional depending on '!fifo_empty(...)', which is the same condition as the while loop. It seems Clang can't piece together that this condition will never been false as long as we are in the while loop because they are the same condition. It's easy enough to silence this warning by just explicitly zero initializing bucket.

Quoted 6 lines
Quoted 2 lines

Hi Nathan,

Nice catch, I will add it to my for-test. Thanks.

I saw the same warning but came up with a nicer (IMHO) fix, see
https://lore.kernel.org/lkml/[email protected]/T/#u

Arnd