From nobody Mon Feb 9 08:11:59 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1658405389; cv=none; d=zohomail.com; s=zohoarc; b=MczRpTE3MfxZIfnK/Lp6v5Kknjk5dSIAsTGHdv1YmjHvIPhFGrT9zSb4n3LJs271AYGd59WK3/5u//u2nHeg78k6DFawlZYHybGF5dSKit7p41Sw4y1SJeBDxGAEKLbLjFe+KLk3m/XDdG2scClgPg2F2rzFl8lAsn3D/olxylE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658405389; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ooPoKSQ4/PhB9OVPcFTiTnhYHJAUVTgVz4SNGMupjzY=; b=LWfAWsv+D+1fSr/bE1+Ujcmbh8Z+2CXYi9kLAaGcfhs5eM0CcHL3U8IddNOydSPdtzFpGU/0v/n+XcSFScwOig/e8NT+bFJVbo/W9uTw1VYykMN0NBf9OQYwvjZqqbEghU2JY6wncdKh7aY4yMcIpx0FrKnKTRZ2l9xmweLlxiM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658405389532605.3514327650147; Thu, 21 Jul 2022 05:09:49 -0700 (PDT) Received: from localhost ([::1]:57540 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEV00-0004bR-Av for importer@patchew.org; Thu, 21 Jul 2022 08:09:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59212) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEUy8-0001or-Ax for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:07:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45445) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEUy6-0000wa-BT for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:07:51 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-156-LwuXejt6MjO82ucCylOBnQ-1; Thu, 21 Jul 2022 08:07:39 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8257985A587; Thu, 21 Jul 2022 12:07:39 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.193.182]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D5B12026D64; Thu, 21 Jul 2022 12:07:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658405269; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ooPoKSQ4/PhB9OVPcFTiTnhYHJAUVTgVz4SNGMupjzY=; b=CzYf4LWj9WwIOpUHV34ZA+xR9FEUVsFZ3x0q+WiSax8NN5uFYET4cG5yaY4P8CWkSE4We+ BXQtNoeokp06hBeLWtannWtMN0IJChDfRiC/pSlPZswS8ByyrKKdp8eTzb/KRFdc6/4SjX r04+gqE04WU1PdYzG1aRBWVDY8eTfoE= X-MC-Unique: LwuXejt6MjO82ucCylOBnQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Michal Privoznik , Igor Mammedov , "Michael S. Tsirkin" , Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost , "Dr . David Alan Gilbert" , Eric Blake , Markus Armbruster , Richard Henderson , Stefan Weil Subject: [PATCH RFC 1/7] util: Cleanup and rename os_mem_prealloc() Date: Thu, 21 Jul 2022 14:07:26 +0200 Message-Id: <20220721120732.118133-2-david@redhat.com> In-Reply-To: <20220721120732.118133-1-david@redhat.com> References: <20220721120732.118133-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1658405391327100001 Content-Type: text/plain; charset="utf-8" Let's * give the function a "qemu_*" style name * make sure the parameters in the implementation match the prototype * rename smp_cpus to max_threads, which makes the semantics of that parameter clearer ... and add a function documentation. Signed-off-by: David Hildenbrand --- backends/hostmem.c | 6 +++--- hw/virtio/virtio-mem.c | 2 +- include/qemu/osdep.h | 17 +++++++++++++++-- softmmu/cpus.c | 2 +- util/oslib-posix.c | 24 ++++++++++++------------ util/oslib-win32.c | 8 ++++---- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/backends/hostmem.c b/backends/hostmem.c index 624bb7ecd3..caff42d3a5 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -232,7 +232,7 @@ static void host_memory_backend_set_prealloc(Object *ob= j, bool value, void *ptr =3D memory_region_get_ram_ptr(&backend->mr); uint64_t sz =3D memory_region_size(&backend->mr); =20 - os_mem_prealloc(fd, ptr, sz, backend->prealloc_threads, &local_err= ); + qemu_prealloc_mem(fd, ptr, sz, backend->prealloc_threads, &local_e= rr); if (local_err) { error_propagate(errp, local_err); return; @@ -393,8 +393,8 @@ host_memory_backend_memory_complete(UserCreatable *uc, = Error **errp) * specified NUMA policy in place. */ if (backend->prealloc) { - os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz, - backend->prealloc_threads, &local_err); + qemu_prealloc_mem(memory_region_get_fd(&backend->mr), ptr, sz, + backend->prealloc_threads, &local_err); if (local_err) { goto out; } diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 30d03e987a..0e9ef4ff19 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -467,7 +467,7 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, = uint64_t start_gpa, int fd =3D memory_region_get_fd(&vmem->memdev->mr); Error *local_err =3D NULL; =20 - os_mem_prealloc(fd, area, size, 1, &local_err); + qemu_prealloc_mem(fd, area, size, 1, &local_err); if (local_err) { static bool warned; =20 diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index b1c161c035..e556e45143 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -568,8 +568,21 @@ unsigned long qemu_getauxval(unsigned long type); =20 void qemu_set_tty_echo(int fd, bool echo); =20 -void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus, - Error **errp); +/** + * qemu_prealloc_mem: + * @fd: the fd mapped into the area, -1 for anonymous memory + * @area: start address of the are to preallocate + * @sz: the size of the area to preallocate + * @max_threads: maximum number of threads to use + * @errp: returns an error if this function fails + * + * Preallocate memory (populate/prefault page tables writable) for the vir= tual + * memory area starting at @area with the size of @sz. After a successful = call, + * each page in the area was faulted in writable at least once, for exampl= e, + * after allocating file blocks for mapped files. + */ +void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads, + Error **errp); =20 /** * qemu_get_pid_name: diff --git a/softmmu/cpus.c b/softmmu/cpus.c index 23b30484b2..cf8aa70ca5 100644 --- a/softmmu/cpus.c +++ b/softmmu/cpus.c @@ -354,7 +354,7 @@ static void qemu_init_sigbus(void) =20 /* * ALERT: when modifying this, take care that SIGBUS forwarding in - * os_mem_prealloc() will continue working as expected. + * qemu_prealloc_mem() will continue working as expected. */ memset(&action, 0, sizeof(action)); action.sa_flags =3D SA_SIGINFO; diff --git a/util/oslib-posix.c b/util/oslib-posix.c index bffec18869..75e8cdcf3e 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -314,7 +314,7 @@ static void sigbus_handler(int signal) return; } #endif /* CONFIG_LINUX */ - warn_report("os_mem_prealloc: unrelated SIGBUS detected and ignored"); + warn_report("qemu_prealloc_mem: unrelated SIGBUS detected and ignored"= ); } =20 static void *do_touch_pages(void *arg) @@ -384,13 +384,13 @@ static void *do_madv_populate_write_pages(void *arg) } =20 static inline int get_memset_num_threads(size_t hpagesize, size_t numpages, - int smp_cpus) + int max_threads) { long host_procs =3D sysconf(_SC_NPROCESSORS_ONLN); int ret =3D 1; =20 if (host_procs > 0) { - ret =3D MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), smp_cp= us); + ret =3D MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), max_th= reads); } =20 /* Especially with gigantic pages, don't create more threads than page= s. */ @@ -403,11 +403,11 @@ static inline int get_memset_num_threads(size_t hpage= size, size_t numpages, } =20 static int touch_all_pages(char *area, size_t hpagesize, size_t numpages, - int smp_cpus, bool use_madv_populate_write) + int max_threads, bool use_madv_populate_write) { static gsize initialized =3D 0; MemsetContext context =3D { - .num_threads =3D get_memset_num_threads(hpagesize, numpages, smp_c= pus), + .num_threads =3D get_memset_num_threads(hpagesize, numpages, max_t= hreads), }; size_t numpages_per_thread, leftover; void *(*touch_fn)(void *); @@ -479,13 +479,13 @@ static bool madv_populate_write_possible(char *area, = size_t pagesize) errno !=3D EINVAL; } =20 -void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, - Error **errp) +void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads, + Error **errp) { static gsize initialized; int ret; size_t hpagesize =3D qemu_fd_getpagesize(fd); - size_t numpages =3D DIV_ROUND_UP(memory, hpagesize); + size_t numpages =3D DIV_ROUND_UP(sz, hpagesize); bool use_madv_populate_write; struct sigaction act; =20 @@ -515,24 +515,24 @@ void os_mem_prealloc(int fd, char *area, size_t memor= y, int smp_cpus, if (ret) { qemu_mutex_unlock(&sigbus_mutex); error_setg_errno(errp, errno, - "os_mem_prealloc: failed to install signal handler"); + "qemu_prealloc_mem: failed to install signal handler"); return; } } =20 /* touch pages simultaneously */ - ret =3D touch_all_pages(area, hpagesize, numpages, smp_cpus, + ret =3D touch_all_pages(area, hpagesize, numpages, max_threads, use_madv_populate_write); if (ret) { error_setg_errno(errp, -ret, - "os_mem_prealloc: preallocating memory failed"); + "qemu_prealloc_mem: preallocating memory failed"); } =20 if (!use_madv_populate_write) { ret =3D sigaction(SIGBUS, &sigbus_oldact, NULL); if (ret) { /* Terminate QEMU since it can't recover from error */ - perror("os_mem_prealloc: failed to reinstall signal handler"); + perror("qemu_prealloc_mem: failed to reinstall signal handler"= ); exit(1); } qemu_mutex_unlock(&sigbus_mutex); diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 5723d3eb4c..e1cb725ecc 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -268,14 +268,14 @@ int getpagesize(void) return system_info.dwPageSize; } =20 -void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, - Error **errp) +void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads, + Error **errp) { int i; size_t pagesize =3D qemu_real_host_page_size(); =20 - memory =3D (memory + pagesize - 1) & -pagesize; - for (i =3D 0; i < memory / pagesize; i++) { + sz =3D (sz + pagesize - 1) & -pagesize; + for (i =3D 0; i < sz / pagesize; i++) { memset(area + pagesize * i, 0, 1); } } --=20 2.35.3