From nobody Tue Feb 10 04:53:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490631494890271.0009404038735; Mon, 27 Mar 2017 09:18:14 -0700 (PDT) Received: from localhost ([::1]:47944 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1csXLR-0007yA-Lh for importer@patchew.org; Mon, 27 Mar 2017 12:18:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60763) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1csXJh-0006jt-Jr for qemu-devel@nongnu.org; Mon, 27 Mar 2017 12:16:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1csXJg-0000sL-6Y for qemu-devel@nongnu.org; Mon, 27 Mar 2017 12:16:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49598) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1csXJf-0000qw-WE for qemu-devel@nongnu.org; Mon, 27 Mar 2017 12:16:24 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 239A0C60ED; Mon, 27 Mar 2017 16:16:23 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-208.ams2.redhat.com [10.36.117.208]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v2RGGF8u009176; Mon, 27 Mar 2017 12:16:22 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 239A0C60ED Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 239A0C60ED From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 27 Mar 2017 18:16:11 +0200 Message-Id: <20170327161615.19637-6-pbonzini@redhat.com> In-Reply-To: <20170327161615.19637-1-pbonzini@redhat.com> References: <20170327161615.19637-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 27 Mar 2017 16:16:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 5/9] mem-prealloc: fix sysconf(_SC_NPROCESSORS_ONLN) failure case. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jitendra Kolhe Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Jitendra Kolhe 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 Message-Id: <1490079006-32495-1-git-send-email-jitendra.kolhe@hpe.com> Signed-off-by: Paolo Bonzini --- 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 =20 -#define MAX_MEM_PREALLOC_THREAD_COUNT (MIN(sysconf(_SC_NPROCESSORS_ONLN), = 16)) +#define MAX_MEM_PREALLOC_THREAD_COUNT 16 =20 struct MemsetThread { char *addr; @@ -381,6 +381,18 @@ static void *do_touch_pages(void *arg) return NULL; } =20 +static inline int get_memset_num_threads(int smp_cpus) +{ + long host_procs =3D sysconf(_SC_NPROCESSORS_ONLN); + int ret =3D 1; + + if (host_procs > 0) { + ret =3D MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), smp_cp= us); + } + /* 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 hpagesiz= e, size_t numpages, int i =3D 0; =20 memset_thread_failed =3D false; - memset_num_threads =3D MIN(smp_cpus, MAX_MEM_PREALLOC_THREAD_COUNT); + memset_num_threads =3D get_memset_num_threads(smp_cpus); memset_thread =3D g_new0(MemsetThread, memset_num_threads); numpages_per_thread =3D (numpages / memset_num_threads); size_per_thread =3D (hpagesize * numpages_per_thread); --=20 2.9.3