From: Paolo Bonzini <pbonzini@redhat.com>
To: Jitendra Kolhe <jitendra.kolhe@hpe.com>, qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, armbru@redhat.com, stefanha@redhat.com,
kwolf@redhat.com, renganathan.meenakshisundaram@hpe.com,
mohan_parthasarathy@hpe.com
Subject: Re: [Qemu-devel] [PATCH] mem-prealloc: fix sysconf(_SC_NPROCESSORS_ONLN) failure case.
Date: Fri, 24 Mar 2017 11:50:22 +0100 [thread overview]
Message-ID: <c1ae1dba-33eb-c325-177a-6a74888f1ebb@redhat.com> (raw)
In-Reply-To: <1490079006-32495-1-git-send-email-jitendra.kolhe@hpe.com>
On 21/03/2017 07:50, Jitendra Kolhe wrote:
> 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.)
>
> Signed-off-by: Jitendra Kolhe <jitendra.kolhe@hpe.com>
> ---
> util/oslib-posix.c | 16 ++++++++++++++--
> 1 file changed, 14 insertions(+), 2 deletions(-)
>
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 3fe6089..4d9189e 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -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 16
>
> struct 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);
>
Queued, thanks.
Paolo
prev parent reply other threads:[~2017-03-24 10:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-21 6:50 [Qemu-devel] [PATCH] mem-prealloc: fix sysconf(_SC_NPROCESSORS_ONLN) failure case Jitendra Kolhe
2017-03-24 10:50 ` Paolo Bonzini [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c1ae1dba-33eb-c325-177a-6a74888f1ebb@redhat.com \
--to=pbonzini@redhat.com \
--cc=armbru@redhat.com \
--cc=jitendra.kolhe@hpe.com \
--cc=kwolf@redhat.com \
--cc=mohan_parthasarathy@hpe.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=renganathan.meenakshisundaram@hpe.com \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).