From nobody Sun Feb 8 06:04:29 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1641478089947914.043315697157; Thu, 6 Jan 2022 06:08:09 -0800 (PST) Received: from localhost ([::1]:40686 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n5TR2-0005Cz-A2 for importer@patchew.org; Thu, 06 Jan 2022 09:08:08 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42254) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n5Sel-0005Ly-2x for qemu-devel@nongnu.org; Thu, 06 Jan 2022 08:18:15 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24302) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n5Seh-0000y1-6B for qemu-devel@nongnu.org; Thu, 06 Jan 2022 08:18:14 -0500 Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-628-yU0i45FxP9WXVASnIzULgQ-1; Thu, 06 Jan 2022 08:17:47 -0500 Received: by mail-ed1-f72.google.com with SMTP id g2-20020a056402424200b003f8ee03207eso1937988edb.7 for ; Thu, 06 Jan 2022 05:17:47 -0800 (PST) Received: from redhat.com ([2a03:c5c0:207e:991b:6857:5652:b903:a63b]) by smtp.gmail.com with ESMTPSA id oz31sm488170ejc.35.2022.01.06.05.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 05:17:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641475073; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g64Nj4WV+Irx5qvL3Yea9s2X356RYx1CceDWXAsokho=; b=SJG/vUWvYrP2OVXHr58DWwDiqBtqNoV0NWjgFbrFBGfSUCB/u8Loe7GIUR1x/uLyl0H6Wf IiVmgOUM3zCoM8y1zskDm6QNHWZhVaBsNZ/oPWxgTue630bnxBkeeqFHbG6kl2m1wLWYYe 9lWCwAG2aK16yv/vv8jPrewGuvZw8Vc= X-MC-Unique: yU0i45FxP9WXVASnIzULgQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=g64Nj4WV+Irx5qvL3Yea9s2X356RYx1CceDWXAsokho=; b=fkavEZ6nKa2/IfmikVHvZ95s3b66GiB2g9/B3xK6+lOCMJIDDE1SdJUtxzb5nj0VAk NkE/v6U49M/KhjmEJCZCgoLVeYnKRX0vawPZnn/qDABXLSO910UG1ucm3NTa+UfJ99db lnw4ZK2yo2BCyxo5p8WBKvgLLiWrbDRv9n/V9/2sykd1sbXUdxk4IGENFURoA0LDCtjI fuLAT4caZ7cmYRp3YFFAL3NWX5jG6c+H4LWJqP5RKytpknHy1wKXFAzJzZhRfwUxvecR ndgOwcvzFTE6Vqoou/s0F4jveN/AQk5scZq5FrJgrgac8cEemJIFie5aJztQnu31gElB 0zdQ== X-Gm-Message-State: AOAM532Kr2j8xRMTf/wTvBsMWwsut3V5KfZ2VY1+F5zO4IJRvTFvxhwX hc/xxjM3sn6iZ1vaMNV7WHCW97x6Xzd7cD+o96l/bKATjkSz79axIZe4WdeBV4X6WrmNI7YYi9A 3WpyfdUDE9iJcnimmLImQ2hCRRSPNsYTDvYE3KS98GLWsERXPv1U8XQIDtvIZ X-Received: by 2002:a17:907:6da2:: with SMTP id sb34mr45961670ejc.309.1641475065506; Thu, 06 Jan 2022 05:17:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJyclM1Fo9+Ne5S7PpD6Pgf+Ijxxc1OxYBZvrpHHOcerN9SJGqgE1t06uBih96hhzO7x0+E0Iw== X-Received: by 2002:a17:907:6da2:: with SMTP id sb34mr45961648ejc.309.1641475065226; Thu, 06 Jan 2022 05:17:45 -0800 (PST) Date: Thu, 6 Jan 2022 08:17:42 -0500 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Subject: [PULL 31/52] util/oslib-posix: Introduce and use MemsetContext for touch_all_pages() Message-ID: <20220106131534.423671-32-mst@redhat.com> References: <20220106131534.423671-1-mst@redhat.com> MIME-Version: 1.0 In-Reply-To: <20220106131534.423671-1-mst@redhat.com> X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mst@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.372, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michal Privoznik , Peter Maydell , Paolo Bonzini , Daniel P =?utf-8?B?LiBCZXJyYW5nw6k=?= , David Hildenbrand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1641478091205100001 From: David Hildenbrand Let's minimize the number of global variables to prepare for os_mem_prealloc() getting called concurrently and make the code a bit easier to read. The only consumer that really needs a global variable is the sigbus handler, which will require protection via a mutex in the future either way as we cannot concurrently mess with the SIGBUS handler. Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik Signed-off-by: David Hildenbrand Message-Id: <20211217134611.31172-4-david@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- util/oslib-posix.c | 73 +++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index cb89e07770..cf2ead54ad 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -73,21 +73,30 @@ =20 #define MAX_MEM_PREALLOC_THREAD_COUNT 16 =20 +struct MemsetThread; + +typedef struct MemsetContext { + bool all_threads_created; + bool any_thread_failed; + struct MemsetThread *threads; + int num_threads; +} MemsetContext; + struct MemsetThread { char *addr; size_t numpages; size_t hpagesize; QemuThread pgthread; sigjmp_buf env; + MemsetContext *context; }; typedef struct MemsetThread MemsetThread; =20 -static MemsetThread *memset_thread; -static int memset_num_threads; +/* used by sigbus_handler() */ +static MemsetContext *sigbus_memset_context; =20 static QemuMutex page_mutex; static QemuCond page_cond; -static bool threads_created_flag; =20 int qemu_get_thread_id(void) { @@ -438,10 +447,13 @@ const char *qemu_get_exec_dir(void) static void sigbus_handler(int signal) { int i; - if (memset_thread) { - for (i =3D 0; i < memset_num_threads; i++) { - if (qemu_thread_is_self(&memset_thread[i].pgthread)) { - siglongjmp(memset_thread[i].env, 1); + + if (sigbus_memset_context) { + for (i =3D 0; i < sigbus_memset_context->num_threads; i++) { + MemsetThread *thread =3D &sigbus_memset_context->threads[i]; + + if (qemu_thread_is_self(&thread->pgthread)) { + siglongjmp(thread->env, 1); } } } @@ -459,7 +471,7 @@ static void *do_touch_pages(void *arg) * clearing until all threads have been created. */ qemu_mutex_lock(&page_mutex); - while(!threads_created_flag){ + while (!memset_args->context->all_threads_created) { qemu_cond_wait(&page_cond, &page_mutex); } qemu_mutex_unlock(&page_mutex); @@ -502,7 +514,7 @@ static void *do_madv_populate_write_pages(void *arg) =20 /* See do_touch_pages(). */ qemu_mutex_lock(&page_mutex); - while (!threads_created_flag) { + while (!memset_args->context->all_threads_created) { qemu_cond_wait(&page_cond, &page_mutex); } qemu_mutex_unlock(&page_mutex); @@ -529,6 +541,9 @@ static int touch_all_pages(char *area, size_t hpagesize= , size_t numpages, int smp_cpus, bool use_madv_populate_write) { static gsize initialized =3D 0; + MemsetContext context =3D { + .num_threads =3D get_memset_num_threads(smp_cpus), + }; size_t numpages_per_thread, leftover; void *(*touch_fn)(void *); int ret =3D 0, i =3D 0; @@ -546,35 +561,41 @@ static int touch_all_pages(char *area, size_t hpagesi= ze, size_t numpages, touch_fn =3D do_touch_pages; } =20 - threads_created_flag =3D false; - 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; - leftover =3D numpages % memset_num_threads; - for (i =3D 0; i < memset_num_threads; i++) { - memset_thread[i].addr =3D addr; - memset_thread[i].numpages =3D numpages_per_thread + (i < leftover); - memset_thread[i].hpagesize =3D hpagesize; - qemu_thread_create(&memset_thread[i].pgthread, "touch_pages", - touch_fn, &memset_thread[i], + context.threads =3D g_new0(MemsetThread, context.num_threads); + numpages_per_thread =3D numpages / context.num_threads; + leftover =3D numpages % context.num_threads; + for (i =3D 0; i < context.num_threads; i++) { + context.threads[i].addr =3D addr; + context.threads[i].numpages =3D numpages_per_thread + (i < leftove= r); + context.threads[i].hpagesize =3D hpagesize; + context.threads[i].context =3D &context; + qemu_thread_create(&context.threads[i].pgthread, "touch_pages", + touch_fn, &context.threads[i], QEMU_THREAD_JOINABLE); - addr +=3D memset_thread[i].numpages * hpagesize; + addr +=3D context.threads[i].numpages * hpagesize; + } + + if (!use_madv_populate_write) { + sigbus_memset_context =3D &context; } =20 qemu_mutex_lock(&page_mutex); - threads_created_flag =3D true; + context.all_threads_created =3D true; qemu_cond_broadcast(&page_cond); qemu_mutex_unlock(&page_mutex); =20 - for (i =3D 0; i < memset_num_threads; i++) { - int tmp =3D (uintptr_t)qemu_thread_join(&memset_thread[i].pgthread= ); + for (i =3D 0; i < context.num_threads; i++) { + int tmp =3D (uintptr_t)qemu_thread_join(&context.threads[i].pgthre= ad); =20 if (tmp) { ret =3D tmp; } } - g_free(memset_thread); - memset_thread =3D NULL; + + if (!use_madv_populate_write) { + sigbus_memset_context =3D NULL; + } + g_free(context.threads); =20 return ret; } --=20 MST