mem-prealloc: fix sysconf(_SC_NPROCESSORS_ONLN) failure case.
PATCH 1/1 mem-prealloc: fix sysconf(_SC_NPROCESSORS_ONLN) failure case.
This was spotted by Coverity, in case where sysconf(_SC_NPROCESSORS_ONLN) fails and returns -1. This results in memset_num_threads getting set to -1. Which we then pass to g_new0().
The patch replaces MAX_MEM_PREALLOC_THREAD_COUNT macro with a function call get_memset_num_threads() to handle sysconf() failure gracefully. In case sysconf() fails, we fall back to single threaded.
(Spotted by Coverity, CID 1372465.)
--- util/oslib-posix.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
util/oslib-posix.c +14 -2
@@ -55,7 +55,7 @@#include "qemu/error-report.h" #endif -#define MAX_MEM_PREALLOC_THREAD_COUNT (MIN(sysconf(_SC_NPROCESSORS_ONLN), 16)) +#define MAX_MEM_PREALLOC_THREAD_COUNT 16struct MemsetThread {char *addr;
@@ -381,6 +381,18 @@ static void *do_touch_pages(void *arg)return NULL ; } +static inline int get_memset_num_threads (int smp_cpus) +{ +long host_procs =sysconf (_SC_NPROCESSORS_ONLN); +int ret =1 ; + +if (host_procs >0 ) { + ret =MIN (MIN (host_procs,MAX_MEM_PREALLOC_THREAD_COUNT ), smp_cpus); + } +/* In case sysconf() fails, we fall back to single threaded */ +return ret; +} +static bool touch_all_pages (char *area,size_t hpagesize,size_t numpages,int smp_cpus) {
@@ -389,7 +401,7 @@ static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages,int i =0 ; memset_thread_failed = false; - memset_num_threads =MIN (smp_cpus,MAX_MEM_PREALLOC_THREAD_COUNT ); + memset_num_threads =get_memset_num_threads (smp_cpus); memset_thread =g_new0 (MemsetThread, memset_num_threads); numpages_per_thread = (numpages / memset_num_threads); size_per_thread = (hpagesize * numpages_per_thread);
Signature · 1 line
1.8.3.1
Paolo Bonzini
AppliedQuoted 48 lines · Jitendra Kolhe
This was spotted by Coverity, in case where
sysconf(_SC_NPROCESSORS_ONLN)fails and returns -1. This results inmemset_num_threadsgetting set to -1. Which we then pass tog_new0().
The patch replacesMAX_MEM_PREALLOC_THREAD_COUNTmacro with a function callget_memset_num_threads()to handlesysconf()failure gracefully. In casesysconf()fails, we fall back to single threaded.(Spotted by Coverity, CID 1372465.)
Signed-off-by Jitendra Kolhe <[email protected]>--- util/oslib-posix.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)util/oslib-posix.c +13
-1@@ -55,7 +55,7 @@#include "qemu/error-report.h" #endif -#define MAX_MEM_PREALLOC_THREAD_COUNT (MIN(sysconf(_SC_NPROCESSORS_ONLN), 16)) +#define MAX_MEM_PREALLOC_THREAD_COUNT 16struct MemsetThread {char *addr;@@ -381,6 +381,18 @@ static void *do_touch_pages(void *arg)return NULL ; } +static inline int get_memset_num_threads (int smp_cpus) +{ +long host_procs =sysconf (_SC_NPROCESSORS_ONLN); +int ret =1 ; + +if (host_procs >0 ) { + ret =MIN (MIN (host_procs,MAX_MEM_PREALLOC_THREAD_COUNT ), smp_cpus); + } +/* In case sysconf() fails, we fall back to single threaded */ +return ret; +} +static bool touch_all_pages (char *area,size_t hpagesize,size_t numpages,int smp_cpus) {
util/oslib-posix.c static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages, · line 407
Quoted 8 lines
util/oslib-posix.c +1
-1@@ -389,7 +401,7 @@ static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages,int i =0 ; memset_thread_failed = false; - memset_num_threads =MIN (smp_cpus,MAX_MEM_PREALLOC_THREAD_COUNT ); + memset_num_threads =get_memset_num_threads (smp_cpus); memset_thread =g_new0 (MemsetThread, memset_num_threads); numpages_per_thread = (numpages / memset_num_threads); size_per_thread = (hpagesize * numpages_per_thread);
Queued, thanks.
Paolo