From nobody Mon Feb 9 19:37:38 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.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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505841380075671.8130818276145; Tue, 19 Sep 2017 10:16:20 -0700 (PDT) Received: from localhost ([::1]:44224 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duM8B-0000aR-5H for importer@patchew.org; Tue, 19 Sep 2017 13:16:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42438) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duLhE-0001CZ-R8 for qemu-devel@nongnu.org; Tue, 19 Sep 2017 12:48:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1duLhC-0006OX-3j for qemu-devel@nongnu.org; Tue, 19 Sep 2017 12:48:28 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:46474) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1duLhB-0006NA-QG for qemu-devel@nongnu.org; Tue, 19 Sep 2017 12:48:26 -0400 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20170919164824euoutp02e559e21c19e8b6f0a95a8702a463959a~l0Tua0SdX2834328343euoutp02U; Tue, 19 Sep 2017 16:48:24 +0000 (GMT) Received: from eusmges2.samsung.com (unknown [203.254.199.241]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170919164823eucas1p2690fe1cf6ec5099f7a79750731b33b7d~l0TtxLeXi0226002260eucas1p2b; Tue, 19 Sep 2017 16:48:23 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2.samsung.com (EUCPMTA) with SMTP id 59.6F.12907.75A41C95; Tue, 19 Sep 2017 17:48:23 +0100 (BST) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170919164822eucas1p27957a05191b242b4982f62fab15a4539~l0TtD-o1w2115421154eucas1p2i; Tue, 19 Sep 2017 16:48:22 +0000 (GMT) Received: from eusync1.samsung.com ( [203.254.199.211]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 44.6C.18832.65A41C95; Tue, 19 Sep 2017 17:48:22 +0100 (BST) Received: from aperevalov-ubuntu.rnd.samsung.ru ([106.109.129.199]) by eusync1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OWJ003OHDC8XU40@eusync1.samsung.com>; Tue, 19 Sep 2017 17:48:22 +0100 (BST) X-AuditID: cbfec7f1-f793a6d00000326b-37-59c14a5720c5 From: Alexey Perevalov To: qemu-devel@nongnu.org Date: Tue, 19 Sep 2017 19:47:58 +0300 Message-id: <1505839684-10046-5-git-send-email-a.perevalov@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1505839684-10046-1-git-send-email-a.perevalov@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrNIsWRmVeSWpSXmKPExsWy7djP87rhXgcjDQ7NlLCYe/c8i0Xvtnvs FhPfrme1mPb5NrvFlfaf7BZb9n9jtzjeu4PF4s6WPiYHDo8n1zYzebzfd5XNo2/LKsYA5igu m5TUnMyy1CJ9uwSujDXtZ1gLJmpUbJn9h62BsUmui5GTQ0LARGJB81NmCFtM4sK99WxdjFwc QgJLGSV2H/zGCuF8ZpR48P8jC0zHm+/TmCESyxgl+hfMZYFwupkkpp7dztTFyMHBJmAgse+e LUiDiICkxO+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KM3BoiTO27tndaSQQHpiSWp2 ampBahFMlomDU6qBkXfmU3Xvp/mnv8kXMetOdQ5OyahznnbhpGJAUdb8Nwnp5jM/5LxZyLZd xzSa5923a3mmcis9hKya1x07et1X3MWa+5jjyisHnGd4LOKvf3xr8t21h9fnC0ZM9kpUdLj9 MKbKzj9wos05rykHjosc8t65LPj80e/pVz1WT2GY12o6o3FtE9PhnUosxRmJhlrMRcWJABRy wHkiAgAA X-CMS-MailID: 20170919164822eucas1p27957a05191b242b4982f62fab15a4539 X-Msg-Generator: CA X-Sender-IP: 182.198.249.179 X-Local-Sender: =?UTF-8?B?QWxleGV5IFBlcmV2YWxvdhtTUlItVmlydHVhbGl6YXRpb24g?= =?UTF-8?B?TGFiG+yCvOyEseyghOyekBtTZW5pb3IgRW5naW5lZXI=?= X-Global-Sender: =?UTF-8?B?QWxleGV5IFBlcmV2YWxvdhtTUlItVmlydHVhbGl6YXRpb24g?= =?UTF-8?B?TGFiG1NhbXN1bmcgRWxlY3Ryb25pY3MbU2VuaW9yIEVuZ2luZWVy?= X-Sender-Code: =?UTF-8?B?QzEwG0NJU0hRG0MxMEdEMDFHRDAxMDE1NA==?= CMS-TYPE: 201P X-CMS-RootMailID: 20170919164822eucas1p27957a05191b242b4982f62fab15a4539 X-RootMTR: 20170919164822eucas1p27957a05191b242b4982f62fab15a4539 References: <1505839684-10046-1-git-send-email-a.perevalov@samsung.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 210.118.77.12 Subject: [Qemu-devel] [PATCH v10 04/10] migration: split ufd_version_check onto receive/request features part 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: heetae82.ahn@samsung.com, quintela@redhat.com, Alexey Perevalov , peterx@redhat.com, dgilbert@redhat.com, i.maximets@samsung.com 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" This modification is necessary for userfault fd features which are required to be requested from userspace. UFFD_FEATURE_THREAD_ID is a one of such "on demand" feature, which will be introduced in the next patch. QEMU have to use separate userfault file descriptor, due to userfault context has internal state, and after first call of ioctl UFFD_API it changes its state to UFFD_STATE_RUNNING (in case of success), but kernel while handling ioctl UFFD_API expects UFFD_STATE_WAIT_= API. So only one ioctl with UFFD_API is possible per ufd. Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Alexey Perevalov --- migration/postcopy-ram.c | 94 ++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 88 insertions(+), 6 deletions(-) diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index a0e74db..bec6c2c 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -61,16 +61,67 @@ struct PostcopyDiscardState { #include #include =20 -static bool ufd_version_check(int ufd, MigrationIncomingState *mis) + +/** + * receive_ufd_features: check userfault fd features, to request only supp= orted + * features in the future. + * + * Returns: true on success + * + * __NR_userfaultfd - should be checked before + * @features: out parameter will contain uffdio_api.features provided by = kernel + * in case of success + */ +static bool receive_ufd_features(uint64_t *features) { - struct uffdio_api api_struct; - uint64_t ioctl_mask; + struct uffdio_api api_struct =3D {0}; + int ufd; + bool ret =3D true; + + /* if we are here __NR_userfaultfd should exists */ + ufd =3D syscall(__NR_userfaultfd, O_CLOEXEC); + if (ufd =3D=3D -1) { + error_report("%s: syscall __NR_userfaultfd failed: %s", __func__, + strerror(errno)); + return false; + } =20 + /* ask features */ api_struct.api =3D UFFD_API; api_struct.features =3D 0; if (ioctl(ufd, UFFDIO_API, &api_struct)) { error_report("%s: UFFDIO_API failed: %s", __func__, strerror(errno)); + ret =3D false; + goto release_ufd; + } + + *features =3D api_struct.features; + +release_ufd: + close(ufd); + return ret; +} + +/** + * request_ufd_features: this function should be called only once on a new= ly + * opened ufd, subsequent calls will lead to error. + * + * Returns: true on succes + * + * @ufd: fd obtained from userfaultfd syscall + * @features: bit mask see UFFD_API_FEATURES + */ +static bool request_ufd_features(int ufd, uint64_t features) +{ + struct uffdio_api api_struct =3D {0}; + uint64_t ioctl_mask; + + api_struct.api =3D UFFD_API; + api_struct.features =3D features; + if (ioctl(ufd, UFFDIO_API, &api_struct)) { + error_report("%s failed: UFFDIO_API failed: %s", __func__, + strerror(errno)); return false; } =20 @@ -82,11 +133,42 @@ static bool ufd_version_check(int ufd, MigrationIncomi= ngState *mis) return false; } =20 + return true; +} + +static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis) +{ + uint64_t asked_features =3D 0; + static uint64_t supported_features; + + /* + * it's not possible to + * request UFFD_API twice per one fd + * userfault fd features is persistent + */ + if (!supported_features) { + if (!receive_ufd_features(&supported_features)) { + error_report("%s failed", __func__); + return false; + } + } + + /* + * request features, even if asked_features is 0, due to + * kernel expects UFFD_API before UFFDIO_REGISTER, per + * userfault file descriptor + */ + if (!request_ufd_features(ufd, asked_features)) { + error_report("%s failed: features %" PRIu64, __func__, + asked_features); + return false; + } + if (getpagesize() !=3D ram_pagesize_summary()) { bool have_hp =3D false; /* We've got a huge page */ #ifdef UFFD_FEATURE_MISSING_HUGETLBFS - have_hp =3D api_struct.features & UFFD_FEATURE_MISSING_HUGETLBFS; + have_hp =3D supported_features & UFFD_FEATURE_MISSING_HUGETLBFS; #endif if (!have_hp) { error_report("Userfault on this host does not support huge pag= es"); @@ -147,7 +229,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingSt= ate *mis) } =20 /* Version and features check */ - if (!ufd_version_check(ufd, mis)) { + if (!ufd_check_and_apply(ufd, mis)) { goto out; } =20 @@ -523,7 +605,7 @@ int postcopy_ram_enable_notify(MigrationIncomingState *= mis) * Although the host check already tested the API, we need to * do the check again as an ABI handshake on the new fd. */ - if (!ufd_version_check(mis->userfault_fd, mis)) { + if (!ufd_check_and_apply(mis->userfault_fd, mis)) { return -1; } =20 --=20 1.9.1