From patchwork Wed Jun 7 09:46:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Perevalov X-Patchwork-Id: 9771161 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 D125C60393 for ; Wed, 7 Jun 2017 09:54:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB66728459 for ; Wed, 7 Jun 2017 09:54:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9FE952847B; Wed, 7 Jun 2017 09:54:23 +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 1246A28469 for ; Wed, 7 Jun 2017 09:54:23 +0000 (UTC) Received: from localhost ([::1]:42338 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIXfS-0007S9-6r for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Jun 2017 05:54:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58085) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIXYv-0001B1-7P for qemu-devel@nongnu.org; Wed, 07 Jun 2017 05:47:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dIXYr-0000Fu-85 for qemu-devel@nongnu.org; Wed, 07 Jun 2017 05:47:37 -0400 Received: from mailout4.w1.samsung.com ([210.118.77.14]:26023) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dIXYr-0000Ex-22 for qemu-devel@nongnu.org; Wed, 07 Jun 2017 05:47:33 -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 <0OR6006LS8J58O00@mailout4.w1.samsung.com> for qemu-devel@nongnu.org; Wed, 07 Jun 2017 10:47:29 +0100 (BST) Received: from eusmges2.samsung.com (unknown [203.254.199.241]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170607094728eucas1p25e167bb198565ee32ec0b3f97d92a7b8~FzehSTsgq1381413814eucas1p2I; Wed, 7 Jun 2017 09:47:28 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges2.samsung.com (EUCPMTA) with SMTP id D2.5E.04459.0BBC7395; Wed, 7 Jun 2017 10:47:28 +0100 (BST) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170607094728eucas1p1984b365dd09f3222b758075e651a5b5d~FzegncEY50264102641eucas1p1E; Wed, 7 Jun 2017 09:47:28 +0000 (GMT) X-AuditID: cbfec7f1-f796e6d00000116b-69-5937cbb065cc Received: from eusync1.samsung.com ( [203.254.199.211]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 6E.22.20206.FABC7395; Wed, 7 Jun 2017 10:47:27 +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 <0OR600I3B8ILMHB0@eusync1.samsung.com>; Wed, 07 Jun 2017 10:47:27 +0100 (BST) From: Alexey Perevalov To: qemu-devel@nongnu.org Date: Wed, 07 Jun 2017 12:46:31 +0300 Message-id: <1496828798-27548-5-git-send-email-a.perevalov@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1496828798-27548-1-git-send-email-a.perevalov@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrGIsWRmVeSWpSXmKPExsWy7djPc7obTptHGvSeUrWYe/c8i0Xvtnvs Flfaf7JbbNn/jd3ieO8OFgdWjyfXNjN5vN93lc2jb8sqxgDmKC6blNSczLLUIn27BK6M9/tq Cq6pV/ztW8PUwHhOtouRk0NCwESibf95ZghbTOLCvfVsXYxcHEICSxklpj/rY4dwPjNKzL27 lx2mo3XCBkaIxDJGiQUnF7JAON1MEp3z5wFlODjYBAwk9t2zBWkQEZCU+N11GmwFs0CBxMfW lSwgtrBAosSblyeZQGwWAVWJrrVnGEFsXgF3iQ1njrJBLJOTOHlsMiuIzSngIfFj6m1mkF0S AtfZJHZ2vmIF2SUhICux6QDUCy4SF5YtYIWwhSVeHd8CdbSMxOXJ3SwQve2MEt07O1khnAmM Emem/4Wqspc4dfMqE8SlfBKTtk1nhljAK9HRJgRR4iHx9tlSRgjbUWLmiafQkJjNKLH+2Ey2 CYwyCxgZVjGKpJYW56anFhvpFSfmFpfmpesl5+duYgTG5ul/xz/uYHx/wuoQowAHoxIPr8Au s0gh1sSy4srcQ4wSHMxKIrwTj5pHCvGmJFZWpRblxxeV5qQWH2KU5mBREuflOnUtQkggPbEk NTs1tSC1CCbLxMEp1cBo/+lm3+yiMuUlk2YaCmSK88cwP1hafEuER3x/66W4yac8Gr8lXHg2 tV0/7OXSRrtv52dsXdfnuWJW9vt91/x4z03xDJP58itNdi5H40l+k+jn2eX+c7XPd27Y+H7t 4SemD+3eW+/bUyXey8t346DeTCWz3Y/nOO2otZ1c29F8b+5ssZg5whuilViKMxINtZiLihMB yCkuUMkCAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrCLMWRmVeSWpSXmKPExsVy+t/xy7rrT5tHGuyZam4x9+55FovebffY La60/2S32LL/G7vF8d4dLA6sHk+ubWbyeL/vKptH35ZVjAHMUW42GamJKalFCql5yfkpmXnp tkqhIW66FkoKeYm5qbZKEbq+IUFKCmWJOaVAnpEBGnBwDnAPVtK3S3DLeL+vpuCaesXfvjVM DYznZLsYOTkkBEwkWidsYISwxSQu3FvP1sXIxSEksIRRYvXxh0wQTi+TxOOGTvYuRg4ONgED iX33bEEaRAQkJX53nWYGsZkFCiTOTZ7CAmILCyRKvHl5kgnEZhFQlehaewZsAa+Au8SGM0fZ IJbJSZw8NpkVxOYU8JD4MfU22BwhoJrbd84wTWDkXcDIsIpRJLW0ODc9t9hIrzgxt7g0L10v OT93EyMwRLcd+7llB2PXu+BDjAIcjEo8vBl7zCKFWBPLiitzDzFKcDArifBOPGoeKcSbklhZ lVqUH19UmpNafIjRFOioicxSosn5wPjJK4k3NDE0tzQ0MrawMDcyUhLnnfrhSriQQHpiSWp2 ampBahFMHxMHp1QDo0OVlke1PW9iziJJMRGWmP6SMLcDtqotVkefqhpu8JVXl3nrXXB79rMH L/Kf/JrpftG7oq6+xVtGvCA05lSyZ+qp3kXPoxic/8S+1c3oDPmU8Gxnfl/tkTcqFhP55pt4 NC2TbGnj2tN+c7q6s56ju/7DVC7ja5JL3C4k1d66cCpby8DiOr8SS3FGoqEWc1FxIgCFWOsH ZwIAAA== X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170607094728eucas1p1984b365dd09f3222b758075e651a5b5d 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: 20170607094728eucas1p1984b365dd09f3222b758075e651a5b5d X-RootMTR: 20170607094728eucas1p1984b365dd09f3222b758075e651a5b5d References: <1496828798-27548-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 v8 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+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 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 Reviewed-by: Dr. David Alan Gilbert --- 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 8838901..cbe8f9f 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -63,16 +63,67 @@ struct PostcopyDiscardState { #include #include -static bool ufd_version_check(int ufd, MigrationIncomingState *mis) + +/** + * receive_ufd_features: check userfault fd features, to request only supported + * 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 = {0}; + int ufd; + bool ret = true; + + /* if we are here __NR_userfaultfd should exists */ + ufd = syscall(__NR_userfaultfd, O_CLOEXEC); + if (ufd == -1) { + error_report("%s: syscall __NR_userfaultfd failed: %s", __func__, + strerror(errno)); + 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__, strerror(errno)); + ret = false; + goto release_ufd; + } + + *features = api_struct.features; + +release_ufd: + close(ufd); + return ret; +} + +/** + * request_ufd_features: this function should be called only once on a newly + * 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 = {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; } @@ -84,11 +135,42 @@ static bool ufd_version_check(int ufd, MigrationIncomingState *mis) return false; } + return true; +} + +static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis) +{ + uint64_t asked_features = 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() != 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"); @@ -149,7 +231,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; } @@ -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; }