From nobody Fri May 3 21:29:59 2024 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; 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 151749183155683.18680432910458; Thu, 1 Feb 2018 05:30:31 -0800 (PST) Received: from localhost ([::1]:36549 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEwW-0005z0-Eg for importer@patchew.org; Thu, 01 Feb 2018 08:30:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59487) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEue-0004dj-Hu for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEub-00008n-9k for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:24 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34306) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEub-00008S-1m for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:21 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3A7E8806 for ; Thu, 1 Feb 2018 13:28:20 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 06E8C5E1BE; Thu, 1 Feb 2018 13:28:01 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:51 +0100 Message-Id: <20180201132757.23063-2-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 01 Feb 2018 13:28:20 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 1/7] memfd: add error argument, instead of perror() 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: pbonzini@redhat.com, "Michael S. Tsirkin" , ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This will allow callers to silence error report when the call is allowed to failed. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/qemu/memfd.h | 5 +++-- hw/virtio/vhost.c | 8 +++++++- util/memfd.c | 57 +++++++++++++++++++++++++++---------------------= ---- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h index 41c24d807c..b9d09873b5 100644 --- a/include/qemu/memfd.h +++ b/include/qemu/memfd.h @@ -16,9 +16,10 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #endif =20 -int qemu_memfd_create(const char *name, size_t size, unsigned int seals); +int qemu_memfd_create(const char *name, size_t size, unsigned int seals, + Error **errp); void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, - int *fd); + int *fd, Error **errp); void qemu_memfd_free(void *ptr, size_t size, int fd); bool qemu_memfd_check(void); =20 diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 386aef85be..80dc739b27 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -329,6 +329,7 @@ static uint64_t vhost_get_log_size(struct vhost_dev *de= v) =20 static struct vhost_log *vhost_log_alloc(uint64_t size, bool share) { + Error *err =3D NULL; struct vhost_log *log; uint64_t logsize =3D size * sizeof(*(log->log)); int fd =3D -1; @@ -337,7 +338,12 @@ static struct vhost_log *vhost_log_alloc(uint64_t size= , bool share) if (share) { log->log =3D qemu_memfd_alloc("vhost-log", logsize, F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_S= EAL, - &fd); + &fd, &err); + if (err) { + error_report_err(err); + g_free(log); + return NULL; + } memset(log->log, 0, logsize); } else { log->log =3D g_malloc0(logsize); diff --git a/util/memfd.c b/util/memfd.c index dce61f9d21..8d27307137 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -27,6 +27,7 @@ =20 #include "qemu/osdep.h" =20 +#include "qapi/error.h" #include "qemu/memfd.h" =20 #if defined CONFIG_LINUX && !defined CONFIG_MEMFD @@ -51,11 +52,11 @@ static int memfd_create(const char *name, unsigned int = flags) #define MFD_ALLOW_SEALING 0x0002U #endif =20 -int qemu_memfd_create(const char *name, size_t size, unsigned int seals) +int qemu_memfd_create(const char *name, size_t size, + unsigned int seals, Error **errp) { - int mfd =3D -1; - #ifdef CONFIG_LINUX + int mfd =3D -1; unsigned int flags =3D MFD_CLOEXEC; =20 if (seals) { @@ -64,23 +65,26 @@ int qemu_memfd_create(const char *name, size_t size, un= signed int seals) =20 mfd =3D memfd_create(name, flags); if (mfd < 0) { - return -1; + goto err; } =20 if (ftruncate(mfd, size) =3D=3D -1) { - perror("ftruncate"); - close(mfd); - return -1; + goto err; } =20 if (seals && fcntl(mfd, F_ADD_SEALS, seals) =3D=3D -1) { - perror("fcntl"); - close(mfd); - return -1; + goto err; } -#endif =20 return mfd; + +err: + if (mfd >=3D 0) { + close(mfd); + } +#endif + error_setg_errno(errp, errno, "failed to create memfd"); + return -1; } =20 /* @@ -90,14 +94,14 @@ int qemu_memfd_create(const char *name, size_t size, un= signed int seals) * sealing. */ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, - int *fd) + int *fd, Error **errp) { void *ptr; - int mfd =3D qemu_memfd_create(name, size, seals); + int mfd =3D qemu_memfd_create(name, size, seals, NULL); =20 /* some systems have memfd without sealing */ if (mfd =3D=3D -1) { - mfd =3D qemu_memfd_create(name, size, 0); + mfd =3D qemu_memfd_create(name, size, 0, NULL); } =20 if (mfd =3D=3D -1) { @@ -109,27 +113,26 @@ void *qemu_memfd_alloc(const char *name, size_t size,= unsigned int seals, unlink(fname); g_free(fname); =20 - if (mfd =3D=3D -1) { - perror("mkstemp"); - return NULL; - } - - if (ftruncate(mfd, size) =3D=3D -1) { - perror("ftruncate"); - close(mfd); - return NULL; + if (mfd =3D=3D -1 || + ftruncate(mfd, size) =3D=3D -1) { + goto err; } } =20 ptr =3D mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0); if (ptr =3D=3D MAP_FAILED) { - perror("mmap"); - close(mfd); - return NULL; + goto err; } =20 *fd =3D mfd; return ptr; + +err: + error_setg_errno(errp, errno, "failed to allocate shared memory"); + if (mfd >=3D 0) { + close(mfd); + } + return NULL; } =20 void qemu_memfd_free(void *ptr, size_t size, int fd) @@ -157,7 +160,7 @@ bool qemu_memfd_check(void) int fd; void *ptr; =20 - ptr =3D qemu_memfd_alloc("test", 4096, 0, &fd); + ptr =3D qemu_memfd_alloc("test", 4096, 0, &fd, NULL); memfd_check =3D ptr ? MEMFD_OK : MEMFD_KO; qemu_memfd_free(ptr, 4096, fd); } --=20 2.16.0.rc1.1.gef27df75a1 From nobody Fri May 3 21:29:59 2024 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; 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 1517491844756332.1742441963329; Thu, 1 Feb 2018 05:30:44 -0800 (PST) Received: from localhost ([::1]:36585 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEwo-0006IB-D1 for importer@patchew.org; Thu, 01 Feb 2018 08:30:38 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59598) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEup-0004mR-3L for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEuj-0000Eb-4q for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59102) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEui-0000Da-SW for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:29 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3107849023 for ; Thu, 1 Feb 2018 13:28:28 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C1C35E1BE; Thu, 1 Feb 2018 13:28:21 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:52 +0100 Message-Id: <20180201132757.23063-3-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 01 Feb 2018 13:28:28 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 2/7] memfd: add hugetlb support 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: pbonzini@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Linux commit 749df87bd7bee5a79cef073f5d032ddb2b211de8 (v4.14-rc1) added a new flag MFD_HUGETLB to memfd_create() that specify the file to be created resides in the hugetlbfs filesystem. This is the generic hugetlbfs filesystem not associated with any specific mount point. hugetlbfs does not support sealing operations in v4.14, therefore specifying MFD_ALLOW_SEALING with MFD_HUGETLB will result in EINVAL. However, I added sealing support in "[PATCH v3 0/9] memfd: add sealing to hugetlb-backed memory" series, queued in -mm tree for v4.16. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/qemu/memfd.h | 4 ++-- util/memfd.c | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h index b9d09873b5..1d3ecc7458 100644 --- a/include/qemu/memfd.h +++ b/include/qemu/memfd.h @@ -16,8 +16,8 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #endif =20 -int qemu_memfd_create(const char *name, size_t size, unsigned int seals, - Error **errp); +int qemu_memfd_create(const char *name, size_t size, bool hugetlb, + unsigned int seals, Error **errp); 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); diff --git a/util/memfd.c b/util/memfd.c index 8d27307137..7594af7089 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -52,7 +52,11 @@ static int memfd_create(const char *name, unsigned int f= lags) #define MFD_ALLOW_SEALING 0x0002U #endif =20 -int qemu_memfd_create(const char *name, size_t size, +#ifndef MFD_HUGETLB +#define MFD_HUGETLB 0x0004U +#endif + +int qemu_memfd_create(const char *name, size_t size, bool hugetlb, unsigned int seals, Error **errp) { #ifdef CONFIG_LINUX @@ -62,6 +66,9 @@ int qemu_memfd_create(const char *name, size_t size, if (seals) { flags |=3D MFD_ALLOW_SEALING; } + if (hugetlb) { + flags |=3D MFD_HUGETLB; + } =20 mfd =3D memfd_create(name, flags); if (mfd < 0) { @@ -97,11 +104,11 @@ void *qemu_memfd_alloc(const char *name, size_t size, = unsigned int seals, int *fd, Error **errp) { void *ptr; - int mfd =3D qemu_memfd_create(name, size, seals, NULL); + int mfd =3D qemu_memfd_create(name, size, false, seals, NULL); =20 /* some systems have memfd without sealing */ if (mfd =3D=3D -1) { - mfd =3D qemu_memfd_create(name, size, 0, NULL); + mfd =3D qemu_memfd_create(name, size, false, 0, NULL); } =20 if (mfd =3D=3D -1) { --=20 2.16.0.rc1.1.gef27df75a1 From nobody Fri May 3 21:29:59 2024 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; 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 1517492032090702.6531303956796; Thu, 1 Feb 2018 05:33:52 -0800 (PST) Received: from localhost ([::1]:36629 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEzv-0000Qw-8U for importer@patchew.org; Thu, 01 Feb 2018 08:33:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59637) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEuq-0004nu-83 for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEum-0000HT-QC for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33746) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEum-0000Gq-Gv for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:32 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CDD54C0587DF for ; Thu, 1 Feb 2018 13:28:31 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76F3E620AB; Thu, 1 Feb 2018 13:28:29 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:53 +0100 Message-Id: <20180201132757.23063-4-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 01 Feb 2018 13:28:31 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 3/7] memfd: add hugetlbsize argument 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: pbonzini@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Learn to specificy hugetlb size as qemu_memfd_create() argument. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/qemu/memfd.h | 2 +- util/memfd.c | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/qemu/memfd.h b/include/qemu/memfd.h index 1d3ecc7458..de10198ed6 100644 --- a/include/qemu/memfd.h +++ b/include/qemu/memfd.h @@ -17,7 +17,7 @@ #endif =20 int qemu_memfd_create(const char *name, size_t size, bool hugetlb, - unsigned int seals, Error **errp); + uint64_t hugetlbsize, unsigned int seals, Error **er= rp); 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); diff --git a/util/memfd.c b/util/memfd.c index 7594af7089..07d579ea7d 100644 --- a/util/memfd.c +++ b/util/memfd.c @@ -29,6 +29,7 @@ =20 #include "qapi/error.h" #include "qemu/memfd.h" +#include "qemu/host-utils.h" =20 #if defined CONFIG_LINUX && !defined CONFIG_MEMFD #include @@ -56,9 +57,22 @@ static int memfd_create(const char *name, unsigned int f= lags) #define MFD_HUGETLB 0x0004U #endif =20 +#ifndef MFD_HUGE_SHIFT +#define MFD_HUGE_SHIFT 26 +#endif + int qemu_memfd_create(const char *name, size_t size, bool hugetlb, - unsigned int seals, Error **errp) + uint64_t hugetlbsize, unsigned int seals, Error **er= rp) { + int htsize =3D hugetlbsize ? ctz64(hugetlbsize) : 0; + + if (htsize && 1 << htsize !=3D hugetlbsize) { + error_setg(errp, "Hugepage size must be a power of 2"); + return -1; + } + + htsize =3D htsize << MFD_HUGE_SHIFT; + #ifdef CONFIG_LINUX int mfd =3D -1; unsigned int flags =3D MFD_CLOEXEC; @@ -68,8 +82,8 @@ int qemu_memfd_create(const char *name, size_t size, bool= hugetlb, } if (hugetlb) { flags |=3D MFD_HUGETLB; + flags |=3D htsize; } - mfd =3D memfd_create(name, flags); if (mfd < 0) { goto err; @@ -104,11 +118,11 @@ void *qemu_memfd_alloc(const char *name, size_t size,= unsigned int seals, int *fd, Error **errp) { void *ptr; - int mfd =3D qemu_memfd_create(name, size, false, seals, NULL); + int mfd =3D qemu_memfd_create(name, size, false, 0, seals, NULL); =20 /* some systems have memfd without sealing */ if (mfd =3D=3D -1) { - mfd =3D qemu_memfd_create(name, size, false, 0, NULL); + mfd =3D qemu_memfd_create(name, size, false, 0, 0, NULL); } =20 if (mfd =3D=3D -1) { --=20 2.16.0.rc1.1.gef27df75a1 From nobody Fri May 3 21:29:59 2024 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; 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 1517491845276712.7014672097124; Thu, 1 Feb 2018 05:30:45 -0800 (PST) Received: from localhost ([::1]:36608 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEwt-0006NL-FH for importer@patchew.org; Thu, 01 Feb 2018 08:30:43 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59673) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEus-0004qs-Um for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEuq-0000M0-MG for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:38 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54160) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEuq-0000LH-Dg for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:36 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B70F2769F9 for ; Thu, 1 Feb 2018 13:28:35 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25A9E6A02C; Thu, 1 Feb 2018 13:28:32 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:54 +0100 Message-Id: <20180201132757.23063-5-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 01 Feb 2018 13:28:35 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 4/7] Add memfd based hostmem 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: pbonzini@redhat.com, Igor Mammedov , ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new memory backend, similar to hostmem-file, except that it doesn't need to create files. It also enforces memory sealing. This backend is mainly useful for sharing the memory with other processes. Note that Linux supports transparent huge-pages of shmem/memfd memory since 4.8. It is relatively easier to set up THP than a dedicate hugepage mount point by using "madvise" in /sys/kernel/mm/transparent_hugepage/shmem_enabled. Since 4.14, memfd allows to set hugetlb requirement explicitly. Pending for merge in 4.16 is memfd sealing support for hugetlb backed memory. Usage: -object memory-backend-memfd,id=3Dmem1,size=3D1G Signed-off-by: Marc-Andr=C3=A9 Lureau --- backends/hostmem-memfd.c | 170 +++++++++++++++++++++++++++++++++++++++++++= ++++ backends/Makefile.objs | 2 + qemu-options.hx | 22 ++++++ 3 files changed, 194 insertions(+) create mode 100644 backends/hostmem-memfd.c diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c new file mode 100644 index 0000000000..1e20fe0ba8 --- /dev/null +++ b/backends/hostmem-memfd.c @@ -0,0 +1,170 @@ +/* + * QEMU host memfd memory backend + * + * Copyright (C) 2018 Red Hat Inc + * + * Authors: + * Marc-Andr=C3=A9 Lureau + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "sysemu/hostmem.h" +#include "sysemu/sysemu.h" +#include "qom/object_interfaces.h" +#include "qemu/memfd.h" +#include "qapi/error.h" + +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" + +#define MEMORY_BACKEND_MEMFD(obj) \ + OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD) + +typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd; + +struct HostMemoryBackendMemfd { + HostMemoryBackend parent_obj; + + bool hugetlb; + uint64_t hugetlbsize; + bool seal; +}; + +static void +memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) +{ + HostMemoryBackendMemfd *m =3D MEMORY_BACKEND_MEMFD(backend); + char *name; + int fd; + + if (!backend->size) { + error_setg(errp, "can't create backend with size 0"); + return; + } + + if (host_memory_backend_mr_inited(backend)) { + return; + } + + backend->force_prealloc =3D mem_prealloc; + fd =3D qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD, backend->size, + m->hugetlb, m->hugetlbsize, m->seal ? + F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL : 0, + errp); + if (fd =3D=3D -1) { + return; + } + + name =3D object_get_canonical_path(OBJECT(backend)); + memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), + name, backend->size, true, fd, errp); + g_free(name); +} + +static bool +memfd_backend_get_hugetlb(Object *o, Error **errp) +{ + return MEMORY_BACKEND_MEMFD(o)->hugetlb; +} + +static void +memfd_backend_set_hugetlb(Object *o, bool value, Error **errp) +{ + MEMORY_BACKEND_MEMFD(o)->hugetlb =3D value; +} + +static void +memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + HostMemoryBackendMemfd *m =3D MEMORY_BACKEND_MEMFD(obj); + Error *local_err =3D NULL; + uint64_t value; + + if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) { + error_setg(&local_err, "cannot change property value"); + goto out; + } + + visit_type_size(v, name, &value, &local_err); + if (local_err) { + goto out; + } + if (!value) { + error_setg(&local_err, "Property '%s.%s' doesn't take value '%" + PRIu64 "'", object_get_typename(obj), name, value); + goto out; + } + m->hugetlbsize =3D value; +out: + error_propagate(errp, local_err); +} + +static void +memfd_backend_get_hugetlbsize(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + HostMemoryBackendMemfd *m =3D MEMORY_BACKEND_MEMFD(obj); + uint64_t value =3D m->hugetlbsize; + + visit_type_size(v, name, &value, errp); +} + +static bool +memfd_backend_get_seal(Object *o, Error **errp) +{ + return MEMORY_BACKEND_MEMFD(o)->seal; +} + +static void +memfd_backend_set_seal(Object *o, bool value, Error **errp) +{ + MEMORY_BACKEND_MEMFD(o)->seal =3D value; +} + +static void +memfd_backend_instance_init(Object *obj) +{ + HostMemoryBackendMemfd *m =3D MEMORY_BACKEND_MEMFD(obj); + + /* default to sealed file */ + m->seal =3D true; +} + +static void +memfd_backend_class_init(ObjectClass *oc, void *data) +{ + HostMemoryBackendClass *bc =3D MEMORY_BACKEND_CLASS(oc); + + bc->alloc =3D memfd_backend_memory_alloc; + + 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); +} + +static const TypeInfo memfd_backend_info =3D { + .name =3D TYPE_MEMORY_BACKEND_MEMFD, + .parent =3D TYPE_MEMORY_BACKEND, + .instance_init =3D memfd_backend_instance_init, + .class_init =3D memfd_backend_class_init, + .instance_size =3D sizeof(HostMemoryBackendMemfd), +}; + +static void register_types(void) +{ + type_register_static(&memfd_backend_info); +} + +type_init(register_types); diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 0400799efd..67eeeba5fc 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -8,3 +8,5 @@ common-obj-$(CONFIG_LINUX) +=3D hostmem-file.o =20 common-obj-y +=3D cryptodev.o common-obj-y +=3D cryptodev-builtin.o + +common-obj-$(CONFIG_LINUX) +=3D hostmem-memfd.o diff --git a/qemu-options.hx b/qemu-options.hx index 8ce427da78..265cf9fa38 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4024,6 +4024,28 @@ Memory backend objects offer more control than the @= option{-m} option that is traditionally used to define guest RAM. Please refer to @option{memory-backend-file} for a description of the options. =20 +@item -object memory-backend-memfd,id=3D@var{id},merge=3D@var{on|off},dump= =3D@var{on|off},prealloc=3D@var{on|off},size=3D@var{size},host-nodes=3D@var= {host-nodes},policy=3D@var{default|preferred|bind|interleave},seal=3D@var{o= n|off},hugetlb=3D@var{on|off},hugetlbsize=3D@var{size} + +Creates an anonymous memory file backend object, which allows QEMU to +share the memory with an external process (e.g. when using +vhost-user). The memory is allocated with memfd and optional +sealing. (Linux only) + +The @option{seal} option creates a sealed-file, that will block +further resizing the memory ('on' by default). + +The @option{hugetlb} option specify the file to be created resides in +the hugetlbfs filesystem (since Linux 4.14). Used in conjunction with +the @option{hugetlb} option, the @option{hugetlbsize} option specify +the hugetlb page size on systems that support multiple hugetlb page +sizes (it must be a power of 2 value supported by the system). + +In some versions of Linux, the @option{hugetlb} option is incompatible +with the @option{seal} option (requires at least Linux 4.16). + +Please refer to @option{memory-backend-file} for a description of the +other options. + @item -object rng-random,id=3D@var{id},filename=3D@var{/dev/random} =20 Creates a random number generator backend which obtains entropy from --=20 2.16.0.rc1.1.gef27df75a1 From nobody Fri May 3 21:29:59 2024 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; 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 1517492012390820.0043880031924; Thu, 1 Feb 2018 05:33:32 -0800 (PST) Received: from localhost ([::1]:36628 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEzb-0000Bg-H4 for importer@patchew.org; Thu, 01 Feb 2018 08:33:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59726) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEuw-0004ub-AY for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEuv-0000Om-7Z for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:42 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59158) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEuu-0000OF-W0 for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:41 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3D10E10F3DA for ; Thu, 1 Feb 2018 13:28:40 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 04C0B5DA5B; Thu, 1 Feb 2018 13:28:36 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:55 +0100 Message-Id: <20180201132757.23063-6-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 01 Feb 2018 13:28:40 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 5/7] tests: keep compiling failing vhost-user tests 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: pbonzini@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Let's protect the failing tests under a QTEST_VHOST_USER_FIXME environment variable, so we keep compiling the tests and we can easily run them. Signed-off-by: Marc-Andr=C3=A9 Lureau --- tests/vhost-user-test.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index ec6ac9dc9e..713ff5c5c6 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -30,8 +30,6 @@ #include #include =20 -#define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */ - /* GLIB version compatibility flags */ #if !GLIB_CHECK_VERSION(2, 26, 0) #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000)) @@ -765,7 +763,7 @@ static void wait_for_rings_started(TestServer *s, size_= t count) g_mutex_unlock(&s->data_mutex); } =20 -#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TES= TS) +#if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS) static inline void test_server_connect(TestServer *server) { test_server_create_chr(server, ",reconnect=3D1"); @@ -956,16 +954,19 @@ int main(int argc, char **argv) qtest_add_func("/vhost-user/migrate", test_migrate); qtest_add_func("/vhost-user/multiqueue", test_multiqueue); =20 -#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TES= TS) - qtest_add_func("/vhost-user/reconnect/subprocess", - test_reconnect_subprocess); - qtest_add_func("/vhost-user/reconnect", test_reconnect); - qtest_add_func("/vhost-user/connect-fail/subprocess", - test_connect_fail_subprocess); - qtest_add_func("/vhost-user/connect-fail", test_connect_fail); - qtest_add_func("/vhost-user/flags-mismatch/subprocess", - test_flags_mismatch_subprocess); - qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch); +#if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS) + /* keeps failing on build-system since Aug 15 2017 */ + if (getenv("QTEST_VHOST_USER_FIXME")) { + qtest_add_func("/vhost-user/reconnect/subprocess", + test_reconnect_subprocess); + qtest_add_func("/vhost-user/reconnect", test_reconnect); + qtest_add_func("/vhost-user/connect-fail/subprocess", + test_connect_fail_subprocess); + qtest_add_func("/vhost-user/connect-fail", test_connect_fail); + qtest_add_func("/vhost-user/flags-mismatch/subprocess", + test_flags_mismatch_subprocess); + qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch); + } #endif =20 ret =3D g_test_run(); --=20 2.16.0.rc1.1.gef27df75a1 From nobody Fri May 3 21:29:59 2024 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; 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 1517492037739962.9268763204916; Thu, 1 Feb 2018 05:33:57 -0800 (PST) Received: from localhost ([::1]:36642 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehF00-0000Wa-PA for importer@patchew.org; Thu, 01 Feb 2018 08:33:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59795) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEv1-0004zp-BW for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEv0-0000R7-AY for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41888) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEv0-0000Qh-3I for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:46 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 635F6C0467C6 for ; Thu, 1 Feb 2018 13:28:45 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 873D460F8B; Thu, 1 Feb 2018 13:28:41 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:56 +0100 Message-Id: <20180201132757.23063-7-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 01 Feb 2018 13:28:45 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 6/7] vhost-user-test: make read-guest-mem setup its own qemu 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: pbonzini@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Marc-Andr=C3=A9 Lureau --- tests/vhost-user-test.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index 713ff5c5c6..a22075ff26 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -48,6 +48,14 @@ #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \ QEMU_CMD_NETDEV QEMU_CMD_NET =20 +#define GET_QEMU_CMD(s) \ + g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ + (s)->socket_path, "", (s)->chr_name) + +#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \ + g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ + (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_AR= GS__) + #define HUGETLBFS_MAGIC 0x958458f6 =20 /*********** FROM hw/virtio/vhost-user.c *********************************= ****/ @@ -159,6 +167,10 @@ typedef struct TestServer { QGuestAllocator *alloc; } TestServer; =20 +static TestServer *test_server_new(const gchar *name); +static void test_server_free(TestServer *server); +static void test_server_listen(TestServer *server); + static const char *tmpfs; static const char *root; =20 @@ -225,9 +237,8 @@ static void wait_for_fds(TestServer *s) g_mutex_unlock(&s->data_mutex); } =20 -static void read_guest_mem(const void *data) +static void read_guest_mem_server(TestServer *s) { - TestServer *s =3D (void *)data; uint32_t *guest_mem; int i, j; size_t size; @@ -492,14 +503,6 @@ static void test_server_listen(TestServer *server) test_server_create_chr(server, ",server,nowait"); } =20 -#define GET_QEMU_CMD(s) \ - g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ - (s)->socket_path, "", (s)->chr_name) - -#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \ - g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ - (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_AR= GS__) - static gboolean _test_server_free(TestServer *server) { int i; @@ -652,7 +655,7 @@ static void test_read_guest_mem(void) =20 init_virtio_dev(server, 1u << VIRTIO_NET_F_MAC); =20 - read_guest_mem(server); + read_guest_mem_server(server); =20 uninit_virtio_dev(server); =20 @@ -730,7 +733,7 @@ static void test_migrate(void) global_qtest =3D to; qmp_eventwait("RESUME"); =20 - read_guest_mem(dest); + read_guest_mem_server(dest); =20 uninit_virtio_dev(s); =20 --=20 2.16.0.rc1.1.gef27df75a1 From nobody Fri May 3 21:29:59 2024 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; 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 1517492115322884.5419037780314; Thu, 1 Feb 2018 05:35:15 -0800 (PST) Received: from localhost ([::1]:36734 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehF1G-0001ku-FA for importer@patchew.org; Thu, 01 Feb 2018 08:35:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59806) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ehEv3-00054X-Gp for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ehEv2-0000S7-4B for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:49 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37182) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ehEv1-0000Ri-Rr for qemu-devel@nongnu.org; Thu, 01 Feb 2018 08:28:48 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2E1038046E for ; Thu, 1 Feb 2018 13:28:47 +0000 (UTC) Received: from localhost (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA0DA60F8B; Thu, 1 Feb 2018 13:28:46 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 1 Feb 2018 14:27:57 +0100 Message-Id: <20180201132757.23063-8-marcandre.lureau@redhat.com> In-Reply-To: <20180201132757.23063-1-marcandre.lureau@redhat.com> References: <20180201132757.23063-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 01 Feb 2018 13:28:47 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] [PATCH v7 7/7] tests: use memfd in vhost-user-test 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: pbonzini@redhat.com, ehabkost@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This will exercise the memfd memory backend and should generally be better for testing than memory-backend-file (thanks to anonymous files and sealing). If memfd is available, it is preferred. However, in order to check that file & memfd backends both work correctly, the read-guest-mem test is checked explicitly for each. Signed-off-by: Marc-Andr=C3=A9 Lureau --- tests/vhost-user-test.c | 90 +++++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 64 insertions(+), 26 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index a22075ff26..95eb449cfc 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -17,6 +17,7 @@ #include "qemu/range.h" #include "qemu/sockets.h" #include "chardev/char-fe.h" +#include "qemu/memfd.h" #include "sysemu/sysemu.h" #include "libqos/libqos.h" #include "libqos/pci-pc.h" @@ -39,23 +40,14 @@ #define HAVE_MONOTONIC_TIME #endif =20 -#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=3Dmem,size= =3D%dM,"\ +#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=3Dmem,size= =3D%dM," \ "mem-path=3D%s,share=3Don -numa node,memdev=3Dmem" +#define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=3Dmem,size= =3D%dM," \ + " -numa node,memdev=3Dmem" #define QEMU_CMD_CHR " -chardev socket,id=3D%s,path=3D%s%s" #define QEMU_CMD_NETDEV " -netdev vhost-user,id=3Dnet0,chardev=3D%s,vhostf= orce" #define QEMU_CMD_NET " -device virtio-net-pci,netdev=3Dnet0" =20 -#define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \ - QEMU_CMD_NETDEV QEMU_CMD_NET - -#define GET_QEMU_CMD(s) \ - g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ - (s)->socket_path, "", (s)->chr_name) - -#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \ - g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ - (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_AR= GS__) - #define HUGETLBFS_MAGIC 0x958458f6 =20 /*********** FROM hw/virtio/vhost-user.c *********************************= ****/ @@ -174,6 +166,33 @@ static void test_server_listen(TestServer *server); static const char *tmpfs; static const char *root; =20 +enum test_memfd { + TEST_MEMFD_AUTO, + TEST_MEMFD_YES, + TEST_MEMFD_NO, +}; + +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()) { + memfd =3D TEST_MEMFD_YES; + } + + if (memfd =3D=3D TEST_MEMFD_YES) { + return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR + QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem, + s->chr_name, s->socket_path, + chr_opts, s->chr_name, extra); + } else { + return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR + QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem, + mem_path, s->chr_name, s->socket_path, + chr_opts, s->chr_name, extra); + } +} + static void init_virtio_dev(TestServer *s, uint32_t features_mask) { uint32_t features; @@ -639,8 +658,9 @@ GSourceFuncs test_migrate_source_funcs =3D { .check =3D test_migrate_source_check, }; =20 -static void test_read_guest_mem(void) +static void test_read_guest_mem(const void *arg) { + enum test_memfd memfd =3D GPOINTER_TO_INT(arg); TestServer *server =3D NULL; char *qemu_cmd =3D NULL; QTestState *s =3D NULL; @@ -648,7 +668,7 @@ static void test_read_guest_mem(void) server =3D test_server_new("test"); test_server_listen(server); =20 - qemu_cmd =3D GET_QEMU_CMD(server); + qemu_cmd =3D get_qemu_cmd(server, 512, memfd, root, "", ""); =20 s =3D qtest_start(qemu_cmd); g_free(qemu_cmd); @@ -670,7 +690,7 @@ static void test_migrate(void) char *uri =3D g_strdup_printf("%s%s", "unix:", dest->mig_path); QTestState *global =3D global_qtest, *from, *to; GSource *source; - gchar *cmd; + gchar *cmd, *tmp; QDict *rsp; guint8 *log; guint64 size; @@ -678,7 +698,7 @@ static void test_migrate(void) test_server_listen(s); test_server_listen(dest); =20 - cmd =3D GET_QEMU_CMDE(s, 2, "", ""); + cmd =3D get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, "", ""); from =3D qtest_start(cmd); g_free(cmd); =20 @@ -687,7 +707,9 @@ static void test_migrate(void) size =3D get_log_size(s); g_assert_cmpint(size, =3D=3D, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)= ); =20 - cmd =3D GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri); + tmp =3D g_strdup_printf(" -incoming %s", uri); + cmd =3D get_qemu_cmd(dest, 2, TEST_MEMFD_AUTO, root, "", tmp); + g_free(tmp); to =3D qtest_init(cmd); g_free(cmd); =20 @@ -800,7 +822,7 @@ static void test_reconnect_subprocess(void) char *cmd; =20 g_thread_new("connect", connect_thread, s); - cmd =3D GET_QEMU_CMDE(s, 2, ",server", ""); + cmd =3D get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", ""); qtest_start(cmd); g_free(cmd); =20 @@ -838,7 +860,7 @@ static void test_connect_fail_subprocess(void) =20 s->test_fail =3D true; g_thread_new("connect", connect_thread, s); - cmd =3D GET_QEMU_CMDE(s, 2, ",server", ""); + cmd =3D get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", ""); qtest_start(cmd); g_free(cmd); =20 @@ -868,7 +890,7 @@ static void test_flags_mismatch_subprocess(void) =20 s->test_flags =3D TEST_FLAGS_DISCONNECT; g_thread_new("connect", connect_thread, s); - cmd =3D GET_QEMU_CMDE(s, 2, ",server", ""); + cmd =3D get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", ""); qtest_start(cmd); g_free(cmd); =20 @@ -903,11 +925,21 @@ static void test_multiqueue(void) s->queues =3D 2; test_server_listen(s); =20 - cmd =3D g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",qu= eues=3D%d " - "-device virtio-net-pci,netdev=3Dnet0,mq=3Don,ve= ctors=3D%d", - 512, 512, root, s->chr_name, - s->socket_path, "", s->chr_name, - s->queues, s->queues * 2 + 2); + if (qemu_memfd_check()) { + 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", + 512, 512, s->chr_name, + s->socket_path, "", s->chr_name, + s->queues, s->queues * 2 + 2); + } else { + cmd =3D g_strdup_printf( + QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=3D%d " + "-device virtio-net-pci,netdev=3Dnet0,mq=3Don,vectors=3D%d", + 512, 512, root, s->chr_name, + s->socket_path, "", s->chr_name, + s->queues, s->queues * 2 + 2); + } qtest_start(cmd); g_free(cmd); =20 @@ -953,7 +985,13 @@ 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 - qtest_add_func("/vhost-user/read-guest-mem", test_read_guest_mem); + if (qemu_memfd_check()) { + qtest_add_data_func("/vhost-user/read-guest-mem/memfd", + GINT_TO_POINTER(TEST_MEMFD_YES), + test_read_guest_mem); + } + qtest_add_data_func("/vhost-user/read-guest-mem/memfile", + GINT_TO_POINTER(TEST_MEMFD_NO), test_read_guest_me= m); qtest_add_func("/vhost-user/migrate", test_migrate); qtest_add_func("/vhost-user/multiqueue", test_multiqueue); =20 --=20 2.16.0.rc1.1.gef27df75a1