From nobody Wed Nov 5 15:55:51 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.zoho.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 1496821373669497.60510191345907; Wed, 7 Jun 2017 00:42:53 -0700 (PDT) Received: from localhost ([::1]:41677 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIVcC-0000m8-A3 for importer@patchew.org; Wed, 07 Jun 2017 03:42:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54650) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIVVk-00049R-Oe for qemu-devel@nongnu.org; Wed, 07 Jun 2017 03:36:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dIVVh-0000uQ-Ab for qemu-devel@nongnu.org; Wed, 07 Jun 2017 03:36:12 -0400 Received: from mailout4.w1.samsung.com ([210.118.77.14]:17202) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dIVVh-0000tj-1o for qemu-devel@nongnu.org; Wed, 07 Jun 2017 03:36:09 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OR600KDU2G4G160@mailout4.w1.samsung.com> for qemu-devel@nongnu.org; Wed, 07 Jun 2017 08:36:04 +0100 (BST) Received: from eusmges5.samsung.com (unknown [203.254.199.245]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170607073604eucas1p26e424e1ecfca82bf14478399a5eac139~Fxrx_97F80637006370eucas1p2O; Wed, 7 Jun 2017 07:36:04 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges5.samsung.com (EUCPMTA) with SMTP id 7B.BF.25577.3ECA7395; Wed, 7 Jun 2017 08:36:03 +0100 (BST) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170607073603eucas1p192b908ff347cad65092412266fa0e2b0~FxrxP5kOz1839218392eucas1p1G; Wed, 7 Jun 2017 07:36:03 +0000 (GMT) Received: from eusync4.samsung.com ( [203.254.199.214]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 98.A5.17452.3ECA7395; Wed, 7 Jun 2017 08:36:03 +0100 (BST) Received: from aperevalov-ubuntu.rnd.samsung.ru ([106.109.129.199]) by eusync4.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OR600HQN2FH4N90@eusync4.samsung.com>; Wed, 07 Jun 2017 08:36:03 +0100 (BST) X-AuditID: cbfec7f5-f792f6d0000063e9-7e-5937ace36945 From: Alexey Perevalov To: qemu-devel@nongnu.org Date: Wed, 07 Jun 2017 10:35:24 +0300 Message-id: <1496820931-27416-5-git-send-email-a.perevalov@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1496820931-27416-1-git-send-email-a.perevalov@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrCIsWRmVeSWpSXmKPExsWy7djPc7qP15hHGiyZYmgx9+55FovebffY La60/2S32LL/G7vF8d4dLA6sHk+ubWbyeL/vKptH35ZVjAHMUVw2Kak5mWWpRfp2CVwZc5+e Yy+YoFHx+dhj9gbGf7JdjJwcEgImEjuOrWKCsMUkLtxbz9bFyMUhJLCUUWJLyx1mCOczo8S1 T7tZYDoebTrBCJFYxiixqWkRK4TTzSTx5dBXoCoODjYBA4l992xBGkQEJCV+d51mBr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WmoeKcSbklhZ lVqUH19UmpNafIjRFOimicxSosn5wOjJK4k3NDE0tzQ0MrawMDcyUhLnLflwJVxIID2xJDU7 NbUgtQimj4mDU6qBsdTOfuVBNqn5tT3Z/3hfO9WvLnc6s1GOo++TZ9fGfxeTFX4ZxbNLs4Xt 3XiL0fvomh+3jTLLyw0XO8iu/N81beXNvK0JUw7s38ah81zPq2Zt5buiSazfQiJzP6QXvQ55 bNMVEtI6lZtV6ebJkIopunctrFadLbQ7WZB7wSA8on3ngwsuntJKSizFGYmGWsxFxYkAxeMn KGYCAAA= X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170607073603eucas1p192b908ff347cad65092412266fa0e2b0 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-HopCount: 7 X-CMS-RootMailID: 20170607073603eucas1p192b908ff347cad65092412266fa0e2b0 X-RootMTR: 20170607073603eucas1p192b908ff347cad65092412266fa0e2b0 References: <1496820931-27416-1-git-send-email-a.perevalov@samsung.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 210.118.77.14 Subject: [Qemu-devel] [[PATCH V7] 04/11] 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: i.maximets@samsung.com, Alexey Perevalov , peterx@redhat.com, dgilbert@redhat.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. Signed-off-by: Alexey Perevalov --- migration/postcopy-ram.c | 96 ++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 89 insertions(+), 7 deletions(-) diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index 9963ce4..cbe8f9f 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -63,15 +63,66 @@ 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__ + 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; } @@ -84,11 +135,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"); @@ -149,7 +231,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 @@ -525,7 +607,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