From nobody Thu Nov 6 01:09:01 2025 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.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538295788074389.96188627387903; Sun, 30 Sep 2018 01:23:08 -0700 (PDT) Received: from localhost ([::1]:54251 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g6X0N-0001eB-18 for importer@patchew.org; Sun, 30 Sep 2018 04:23:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41249) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g6WrA-000283-Ua for qemu-devel@nongnu.org; Sun, 30 Sep 2018 04:13:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g6Wr9-00009P-QR for qemu-devel@nongnu.org; Sun, 30 Sep 2018 04:13:36 -0400 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]:54575) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g6Wr9-00007v-Hf for qemu-devel@nongnu.org; Sun, 30 Sep 2018 04:13:35 -0400 Received: by mail-wm1-x330.google.com with SMTP id r63-v6so1754738wma.4 for ; Sun, 30 Sep 2018 01:13:35 -0700 (PDT) Received: from 640k.lan (94-36-187-248.adsl-ull.clienti.tiscali.it. [94.36.187.248]) by smtp.gmail.com with ESMTPSA id u76-v6sm11369194wmd.10.2018.09.30.01.13.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 30 Sep 2018 01:13:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sHTFc8DKU+0zj3+VW+Uh2e7qBeiVKin6qklTSK0VSqs=; b=bZZvcTZBtGnUFE214gRjqKiJ2iltxc0SawEVnfRnR9aKL523FmR1y1fECzB8bM/hpu 91OAmMLh5ZIUpU2ZlKPt+8ZpO6R9ZSvp4TtUaPKJAI0mDkidkTIgq+jOeqBtFW8l+Uvm k2IGcJ93FIQoy/I0vclkXJz1gy+700tOxMxZ1JD1j+2scUsjrB/aNafSYzuSL5tGMAp3 5VNYmhK5Uegdd74U6VZR3Gnx338LIn+eo/UYDWOWKOToOE9cj0r/SQwTyqLYiM5v80lp XXmaUjrXbDgCPnVuS7PAmNnd46MX/Hxj+DGrxIS6akZuzxsbfljtGoA0C+gKxKmnhahO yZkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=sHTFc8DKU+0zj3+VW+Uh2e7qBeiVKin6qklTSK0VSqs=; b=HTC++rl4wKIAbjPiizaWsyO8VrbnjoJ64SWD7lyDDoKCLW26FME6VyDju+E+4LFPir OlMLRjGrZlE/NdbPsV24AXMdMyHcL0aeJyj71WAtppgsj9ImOzVn5wF9DRAo19ncuxrT IkvfdudsihCn2faUH088+txVGr68He+rITpcajvIOr5PSoMfDKPEg4OOinfvVRiPs0kk IhTj1CFk9NNMhCvAdzuUEIiimZbe4W4jpGKYB9SuAhrnfueVQdfDN0vgYW2vdv/4dDOU CyXmc0DwR4w2utoFsO5ZDLvDftiVVQv8uS3T9aTs+OUqNiP/5DSh+JNCvnO7wfg6A9NF F3Xw== X-Gm-Message-State: ABuFfog4fgRQx9Ay65fodCrhJExqvttnLEKNU/CRCepJXnQk4uibLrrp yPTLznrzg6km46O0Kzz8YFCddi5g X-Google-Smtp-Source: ACcGV60v2O1Y0+bCmq1LBSNUGwaiK7yV6SvYZtSz84fifILO4PcFrQSnO8L6pwKdEgPCWKAcXL8SVg== X-Received: by 2002:a1c:a794:: with SMTP id q142-v6mr2297236wme.77.1538295214190; Sun, 30 Sep 2018 01:13:34 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sun, 30 Sep 2018 10:12:14 +0200 Message-Id: <1538295197-23704-17-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1538295197-23704-1-git-send-email-pbonzini@redhat.com> References: <1538295197-23704-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::330 Subject: [Qemu-devel] [PULL 16/79] hostmem-memfd: add checks before adding hostmem-memfd & properties 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: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 From: Marc-Andr=C3=A9 Lureau Run some memfd-related checks before registering hostmem-memfd & various properties. This will help libvirt to figure out what the host is supposed to be capable of. qemu_memfd_check() is changed to a less optimized version, since it is used with various flags, it no longer caches the result. Signed-off-by: Marc-Andr=C3=83=C2=A9 Lureau Message-Id: <20180906161415.8543-1-marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini Signed-off-by: Marc-Andr=C3=A9 Lureau --- backends/hostmem-memfd.c | 32 +++++++++++++++++++------------- include/qemu/memfd.h | 18 +++++++++++++++++- tests/vhost-user-test.c | 6 +++--- util/memfd.c | 35 ++++++----------------------------- 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c index 1e20fe0..3800bd0 100644 --- a/backends/hostmem-memfd.c +++ b/backends/hostmem-memfd.c @@ -140,18 +140,22 @@ memfd_backend_class_init(ObjectClass *oc, void *data) =20 bc->alloc =3D memfd_backend_memory_alloc; =20 - object_class_property_add_bool(oc, "hugetlb", - memfd_backend_get_hugetlb, - memfd_backend_set_hugetlb, - &error_abort); - object_class_property_add(oc, "hugetlbsize", "int", - memfd_backend_get_hugetlbsize, - memfd_backend_set_hugetlbsize, - NULL, NULL, &error_abort); - object_class_property_add_bool(oc, "seal", - memfd_backend_get_seal, - memfd_backend_set_seal, - &error_abort); + if (qemu_memfd_check(MFD_HUGETLB)) { + object_class_property_add_bool(oc, "hugetlb", + memfd_backend_get_hugetlb, + memfd_backend_set_hugetlb, + &error_abort); + object_class_property_add(oc, "hugetlbsize", "int", + memfd_backend_get_hugetlbsize, + memfd_backend_set_hugetlbsize, + NULL, NULL, &error_abort); + } + if (qemu_memfd_check(MFD_ALLOW_SEALING)) { + object_class_property_add_bool(oc, "seal", + memfd_backend_get_seal, + memfd_backend_set_seal, + &error_abort); + } } =20 static const TypeInfo memfd_backend_info =3D { @@ -164,7 +168,9 @@ static const TypeInfo memfd_backend_info =3D { =20 static void register_types(void) { - type_register_static(&memfd_backend_info); + if (qemu_memfd_check(0)) { + type_register_static(&memfd_backend_info); + } } =20 type_init(register_types); diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h index 49e7963..d551c28 100644 --- a/include/qemu/memfd.h +++ b/include/qemu/memfd.h @@ -16,12 +16,28 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #endif =20 +#ifndef MFD_CLOEXEC +#define MFD_CLOEXEC 0x0001U +#endif + +#ifndef MFD_ALLOW_SEALING +#define MFD_ALLOW_SEALING 0x0002U +#endif + +#ifndef MFD_HUGETLB +#define MFD_HUGETLB 0x0004U +#endif + +#ifndef MFD_HUGE_SHIFT +#define MFD_HUGE_SHIFT 26 +#endif + int qemu_memfd_create(const char *name, size_t size, bool hugetlb, uint64_t hugetlbsize, unsigned int seals, Error **er= rp); bool qemu_memfd_alloc_check(void); void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, int *fd, Error **errp); void qemu_memfd_free(void *ptr, size_t size, int fd); -bool qemu_memfd_check(void); +bool qemu_memfd_check(unsigned int flags); =20 #endif /* QEMU_MEMFD_H */ diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index 716aff7..45d58d8 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -169,7 +169,7 @@ static char *get_qemu_cmd(TestServer *s, int mem, enum test_memfd memfd, const char *mem_= path, const char *chr_opts, const char *extra) { - if (memfd =3D=3D TEST_MEMFD_AUTO && qemu_memfd_check()) { + if (memfd =3D=3D TEST_MEMFD_AUTO && qemu_memfd_check(0)) { memfd =3D TEST_MEMFD_YES; } =20 @@ -903,7 +903,7 @@ static void test_multiqueue(void) s->queues =3D 2; test_server_listen(s); =20 - if (qemu_memfd_check()) { + if (qemu_memfd_check(0)) { cmd =3D g_strdup_printf( QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=3D%d " "-device virtio-net-pci,netdev=3Dnet0,mq=3Don,vectors=3D%d", @@ -963,7 +963,7 @@ int main(int argc, char **argv) /* run the main loop thread so the chardev may operate */ thread =3D g_thread_new(NULL, thread_function, loop); =20 - if (qemu_memfd_check()) { + if (qemu_memfd_check(0)) { qtest_add_data_func("/vhost-user/read-guest-mem/memfd", GINT_TO_POINTER(TEST_MEMFD_YES), test_read_guest_mem); diff --git a/util/memfd.c b/util/memfd.c index 6287946..8debd0d 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -45,22 +45,6 @@ static int memfd_create(const char *name, unsigned int f= lags) } #endif =20 -#ifndef MFD_CLOEXEC -#define MFD_CLOEXEC 0x0001U -#endif - -#ifndef MFD_ALLOW_SEALING -#define MFD_ALLOW_SEALING 0x0002U -#endif - -#ifndef MFD_HUGETLB -#define MFD_HUGETLB 0x0004U -#endif - -#ifndef MFD_HUGE_SHIFT -#define MFD_HUGE_SHIFT 26 -#endif - int qemu_memfd_create(const char *name, size_t size, bool hugetlb, uint64_t hugetlbsize, unsigned int seals, Error **er= rp) { @@ -201,23 +185,16 @@ bool qemu_memfd_alloc_check(void) * * Check if host supports memfd. */ -bool qemu_memfd_check(void) +bool qemu_memfd_check(unsigned int flags) { #ifdef CONFIG_LINUX - static int memfd_check =3D MEMFD_TODO; + int mfd =3D memfd_create("test", flags); =20 - if (memfd_check =3D=3D MEMFD_TODO) { - int mfd =3D memfd_create("test", 0); - if (mfd >=3D 0) { - memfd_check =3D MEMFD_OK; - close(mfd); - } else { - memfd_check =3D MEMFD_KO; - } + if (mfd >=3D 0) { + close(mfd); + return true; } +#endif =20 - return memfd_check =3D=3D MEMFD_OK; -#else return false; -#endif } --=20 1.8.3.1