From patchwork Fri May 12 11:31:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Perevalov X-Patchwork-Id: 9723977 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1AAC7600CB for ; Fri, 12 May 2017 11:43:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 14761287A5 for ; Fri, 12 May 2017 11:43:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 08F13287E0; Fri, 12 May 2017 11:43:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 288B1287A5 for ; Fri, 12 May 2017 11:43:41 +0000 (UTC) Received: from localhost ([::1]:53025 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d98yy-0000dq-9e for patchwork-qemu-devel@patchwork.kernel.org; Fri, 12 May 2017 07:43:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59477) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d98y7-0000cO-Cr for qemu-devel@nongnu.org; Fri, 12 May 2017 07:42:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d98y2-0000eM-Bn for qemu-devel@nongnu.org; Fri, 12 May 2017 07:42:47 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:14280) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d98y2-0000Ye-0k for qemu-devel@nongnu.org; Fri, 12 May 2017 07:42:42 -0400 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OPU00JUS81XF380@mailout1.w1.samsung.com> for qemu-devel@nongnu.org; Fri, 12 May 2017 12:32:21 +0100 (BST) Received: from eusmges2.samsung.com (unknown [203.254.199.241]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170512113221eucas1p2312c0a7db5b11eac1dbd1f3192dc56c0~92IqUKDnQ2905629056eucas1p2a; Fri, 12 May 2017 11:32:21 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges2.samsung.com (EUCPMTA) with SMTP id CB.9A.04459.44D95195; Fri, 12 May 2017 12:32:20 +0100 (BST) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170512113220eucas1p1eab5d2f9773a132e09393f41e6dfd251~92IptfQTS0876608766eucas1p11; Fri, 12 May 2017 11:32:20 +0000 (GMT) X-AuditID: cbfec7f1-f796e6d00000116b-64-59159d44bfa0 Received: from eusync2.samsung.com ( [203.254.199.212]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 87.A1.20206.46D95195; Fri, 12 May 2017 12:32:52 +0100 (BST) Received: from aperevalov-ubuntu.rnd.samsung.ru ([106.109.129.199]) by eusync2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OPU0053K81CCS30@eusync2.samsung.com>; Fri, 12 May 2017 12:32:20 +0100 (BST) From: Alexey Perevalov To: qemu-devel@nongnu.org Date: Fri, 12 May 2017 14:31:38 +0300 Message-id: <1494588703-9076-5-git-send-email-a.perevalov@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1494588703-9076-1-git-send-email-a.perevalov@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrCIsWRmVeSWpSXmKPExsWy7djPc7ouc0UjDY68NbSYe/c8i0Xvtnvs Flfaf7JbbNn/jd3ieO8OFgdWjyfXNjN5vN93lc2jb8sqxgDmKC6blNSczLLUIn27BK6Mn88W sBfMUK242f2AqYHxhnQXIyeHhICJxL7OXhYIW0ziwr31bF2MXBxCAksZJU6/WMQM4XxmlNj9 7yIjTMeqBzdYIRLLGCXufTgCVdXNJNF2eSJ7FyMHB5uAgcS+e7YgDSICkhK/u04zg9jMArES b69fYwOxhQXiJU70f2IGKWcRUJU49FQBJMwr4CbR9+oSO8QuOYmTxyazgticAu4SH9YsZwRZ JSFwnU2iddMUJpBeCQFZiU0HmCHqXSQ29R1ig7CFJV4d3wI1R0bi8uRuFojedkaJ7p2drBDO BEaJM9P/QlXZS5y6eZUJ4lA+iUnbpjNDLOCV6GgTgjA9JH5uKoAwHSXuvsyH+HwWo8SCqa+Z JzDKLGBkWMUoklpanJueWmykV5yYW1yal66XnJ+7iREYl6f/Hf+4g/H9CatDjAIcjEo8vBJV IpFCrIllxZW5hxglOJiVRHhXxItGCvGmJFZWpRblxxeV5qQWH2KU5mBREuflOnUtQkggPbEk NTs1tSC1CCbLxMEp1cB4aq7FkU7NBnmDCBvprb804nisVTaWOFWFnU5pLnj0aerXL1Xz9dsi b1zZPMetVdnD+eDByreSh2pjeq9krPM4Xfsp5Y2vT7oif9Hs3xdOaa1X/bg8aGOZabqtHa/8 DEt5/ReM5Ye7Vmc4rT68//WNxU0bGDjmKSj+fZZ661mblM85ZVf9J8uVWIozEg21mIuKEwHt 2RMixwIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrMLMWRmVeSWpSXmKPExsVy+t/xK7opc0UjDaZMF7KYe/c8i0Xvtnvs Flfaf7JbbNn/jd3ieO8OFgdWjyfXNjN5vN93lc2jb8sqxgDmKDebjNTElNQihdS85PyUzLx0 W6XQEDddCyWFvMTcVFulCF3fkCAlhbLEnFIgz8gADTg4B7gHK+nbJbhl/Hy2gL1ghmrFze4H TA2MN6S7GDk5JARMJFY9uMEKYYtJXLi3nq2LkYtDSGAJo8TKX49YIJxeJomdlyYzdjFycLAJ GEjsu2cL0iAiICnxu+s0M4jNLBArcWnCCzYQW1ggXuLG3ANMIOUsAqoSh54qgIR5Bdwk+l5d YofYJSdx8thksL2cAu4SH9YsB5suBFQz91/sBEbeBYwMqxhFUkuLc9Nzi430ihNzi0vz0vWS 83M3MQIDdNuxn1t2MHa9Cz7EKMDBqMTDK1ElEinEmlhWXJl7iFGCg1lJhHdFvGikEG9KYmVV alF+fFFpTmrxIUZToJMmMkuJJucDoyevJN7QxNDc0tDI2MLC3MhISZx36ocr4UIC6Yklqdmp qQWpRTB9TBycUg2MgazRXBdNL3f6tyuueKZtr9jMPufdS94Thfp9ak2t7CkWRZ8nx23p6Akt yo23/GjRKZEpuGhr7wtXxfo1PK/871kJ+y24w+evwPZb69GKA6cKPj328Juxfcvjvr8nLm+5 VSzM1u3B8Tdy18ZFm8J8XnnfDrZtCJm38PGeWZmPwt4901AtVXBVYinOSDTUYi4qTgQA4+q4 AmYCAAA= X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170512113220eucas1p1eab5d2f9773a132e09393f41e6dfd251 X-Msg-Generator: CA X-Sender-IP: 182.198.249.180 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: 20170512113220eucas1p1eab5d2f9773a132e09393f41e6dfd251 X-RootMTR: 20170512113220eucas1p1eab5d2f9773a132e09393f41e6dfd251 References: <1494588703-9076-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.11 Subject: [Qemu-devel] [PATCH V4 4/9] 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, dgilbert@redhat.com, peterx@redhat.com, a.perevalov@samsung.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 need 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 | 82 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index b6eccaf..a16cde8 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -59,32 +59,96 @@ struct PostcopyDiscardState { #include #include -static bool ufd_version_check(int ufd, MigrationIncomingState *mis) + +/* + * Check userfault fd features, to request only supported features in + * future. + * __NR_userfaultfd - should be checked before + * Return obtained features + */ +static bool receive_ufd_features(uint64_t *features) { - struct uffdio_api api_struct; - uint64_t ioctl_mask; + struct uffdio_api api_struct = {0}; + int ufd; + bool ret = true; + + /* if we are here __NR_userfaultfd should exists */ + ufd = syscall(__NR_userfaultfd, O_CLOEXEC); + if (ufd == -1) { + return false; + } + /* ask features */ api_struct.api = UFFD_API; api_struct.features = 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 = false; + goto release_ufd; + } + + *features = api_struct.features; + +release_ufd: + close(ufd); + return ret; +} + +static bool request_ufd_features(int ufd, uint64_t features) +{ + struct uffdio_api api_struct = {0}; + uint64_t ioctl_mask; + + api_struct.api = UFFD_API; + api_struct.features = features; + if (ioctl(ufd, UFFDIO_API, &api_struct)) { + error_report("%s failed: UFFDIO_API failed: %s", __func__, + strerror(errno)); return false; } - ioctl_mask = (__u64)1 << _UFFDIO_REGISTER | - (__u64)1 << _UFFDIO_UNREGISTER; + ioctl_mask = 1 << _UFFDIO_REGISTER | + 1 << _UFFDIO_UNREGISTER; if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { error_report("Missing userfault features: %" PRIx64, (uint64_t)(~api_struct.ioctls & ioctl_mask)); return false; } + return true; +} + +static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis) +{ + uint64_t asked_features = 0; + uint64_t supported_features; + + /* + * it's not possible to + * request UFFD_API twice per one fd + */ + 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() != ram_pagesize_summary()) { bool have_hp = false; /* We've got a huge page */ #ifdef UFFD_FEATURE_MISSING_HUGETLBFS - have_hp = api_struct.features & UFFD_FEATURE_MISSING_HUGETLBFS; + have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS; #endif if (!have_hp) { error_report("Userfault on this host does not support huge pages"); @@ -135,7 +199,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingState *mis) } /* Version and features check */ - if (!ufd_version_check(ufd, mis)) { + if (!ufd_check_and_apply(ufd, mis)) { goto out; } @@ -512,7 +576,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; }