From 686e86d82d35e202902a27314667963393948525 Mon Sep 17 00:00:00 2001 From: Qu Wenruo Date: Thu, 6 Jun 2019 19:06:05 +0800 Subject: [PATCH] btrfs-progs: image: Fix a access-beyond-boundary bug when there are 32 online CPUs [BUG] When there are over 32 (in my example, 35) online CPUs, btrfs-image -c9 will just hang. [CAUSE] Btrfs-image has a hard coded limit (32) on how many threads we can use. For the "-t" option we do the up limit check. But when we don't specify "-t" option and speicified "-c" option, then btrfs-image will try to auto detect the number of online CPUs, and use it without checking if it's over the up limit. And for num_threads larger than the up limit, we will over write the adjust members of metadump_struct/mdrestore_struct, corrupting pthread_mutex_t and pthread_cond_t, causing synchronising problem. Nowadays, with SMT/HT and higher cpu core counts, it's not hard to go beyond 32 threads, and hit the bug. [FIX] Just do extra num_threads check before using the number from sysconf(). Reviewed-by: Su Yue Signed-off-by: Qu Wenruo Signed-off-by: David Sterba --- image/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/image/main.c b/image/main.c index 9a07d945..c45d5068 100644 --- a/image/main.c +++ b/image/main.c @@ -2701,6 +2701,7 @@ int main(int argc, char *argv[]) if (tmp <= 0) tmp = 1; + tmp = min_t(long, tmp, MAX_WORKER_THREADS); num_threads = tmp; } } else {