From patchwork Fri May 12 13:31:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Perevalov X-Patchwork-Id: 9724203 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 1C3B360382 for ; Fri, 12 May 2017 13:40:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1771128823 for ; Fri, 12 May 2017 13:40:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0A8E828822; Fri, 12 May 2017 13:40:27 +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 111552882B for ; Fri, 12 May 2017 13:40:25 +0000 (UTC) Received: from localhost ([::1]:53867 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d9Anw-00032G-6X for patchwork-qemu-devel@patchwork.kernel.org; Fri, 12 May 2017 09:40:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57676) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d9Afi-0005O0-Rz for qemu-devel@nongnu.org; Fri, 12 May 2017 09:32:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d9Afe-00072K-3e for qemu-devel@nongnu.org; Fri, 12 May 2017 09:31:54 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:20657) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d9Afd-00070o-Ts for qemu-devel@nongnu.org; Fri, 12 May 2017 09:31:50 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OPU00GMPDKPLB10@mailout1.w1.samsung.com> for qemu-devel@nongnu.org; Fri, 12 May 2017 14:31:47 +0100 (BST) Received: from eusmges5.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170512133146eucas1p15c30d46baefd49c6b6a6dbe51e479ff3~93w73KTrT1211312113eucas1p1t; Fri, 12 May 2017 13:31:46 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges5.samsung.com (EUCPMTA) with SMTP id 7D.51.25577.249B5195; Fri, 12 May 2017 14:31:46 +0100 (BST) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170512133146eucas1p17df48bb6b5fcefe3717e18cd9afd84b7~93w7Sw4Qy1208612086eucas1p1s; Fri, 12 May 2017 13:31:46 +0000 (GMT) X-AuditID: cbfec7f5-f792f6d0000063e9-db-5915b942393f Received: from eusync1.samsung.com ( [203.254.199.211]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id BF.71.20206.269B5195; Fri, 12 May 2017 14:32:18 +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 <0OPU00791DKM5L30@eusync1.samsung.com>; Fri, 12 May 2017 14:31:45 +0100 (BST) From: Alexey Perevalov To: qemu-devel@nongnu.org Date: Fri, 12 May 2017 16:31:21 +0300 Message-id: <1494595886-30912-5-git-send-email-a.perevalov@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1494595886-30912-1-git-send-email-a.perevalov@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrGIsWRmVeSWpSXmKPExsWy7djPc7pOO0UjDaZ2yVrMvXuexaJ32z12 iyvtP9kttuz/xm5xvHcHiwOrx5Nrm5k83u+7yubRt2UVYwBzFJdNSmpOZllqkb5dAlfG/5c3 GQumqVYsnTOTuYHxmnQXIyeHhICJxMMTm5kgbDGJC/fWs4HYQgJLGSXuvVfqYuQCsj8zSnR+ ns0G03D5yBV2iMQyRokzJ66xQjjdTBK/1nwBynBwsAkYSOy7ZwvSICIgKfG76zQziM0sECvx 9vo1sEHCAvESs+7uYgGxWQRUJTZd7AKr4RVwlzjR95IZYpmcxMljk1lBbE4BD4nHd2+B7ZIQ uM4msX3hS0aQXRICshKbDkDVu0g87V/DCmELS7w6voUdwpaRuDy5mwWit51RontnJ9SgCUAf TP8LVWUvcermVSaIS/kkJm2bzgyxgFeio00IosRDYturj1DljhLtyzcxQjw/m1Hi5P2JTBMY ZRYwMqxiFEktLc5NTy021StOzC0uzUvXS87P3cQIjM3T/45/3cG49JjVIUYBDkYlHl6FtaKR QqyJZcWVuYcYJTiYlUR4DbcAhXhTEiurUovy44tKc1KLDzFKc7AoifNynboWISSQnliSmp2a WpBaBJNl4uCUamD0t5+S+3/2ybNhzMkGa+b83nZzzXG+CtfuoONiu47I+yopvu25yxtt+IOj vi6rr/HdgutHGr9/ZD6wOfcAc8HeFQxhQk9Xzv2azjIv+qBdUF3Hug9l+fzd7r9SVU22qaot FNY+7Z4gfeHqh/vBH2bdr3kmtPl88iyH6Q//XNv+Y7YEz6RUodA6JZbijERDLeai4kQApCO8 qMkCAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrKLMWRmVeSWpSXmKPExsVy+t/xy7pJO0UjDf4eZLSYe/c8i0Xvtnvs Flfaf7JbbNn/jd3ieO8OFgdWjyfXNjN5vN93lc2jb8sqxgDmKDebjNTElNQihdS85PyUzLx0 W6XQEDddCyWFvMTcVFulCF3fkCAlhbLEnFIgz8gADTg4B7gHK+nbJbhl/H95k7FgmmrF0jkz mRsYr0l3MXJySAiYSFw+coUdwhaTuHBvPVsXIxeHkMASRomGH73MEE4vk8TrCRcYuxg5ONgE DCT23bMFaRARkJT43XWaGcRmFoiVuDThBRuILSwQL/G4bTUTiM0ioCqx6WIXWA2vgLvEib6X zBDL5CROHpvMCmJzCnhIPL57C8wWAqrZ1rCUbQIj7wJGhlWMIqmlxbnpucVGesWJucWleel6 yfm5mxiBQbrt2M8tOxi73gUfYhTgYFTi4a1YLxopxJpYVlyZe4hRgoNZSYTXcAtQiDclsbIq tSg/vqg0J7X4EKMp0FETmaVEk/OBEZRXEm9oYmhuaWhkbGFhbmSkJM479cOVcCGB9MSS1OzU 1ILUIpg+Jg5OqQbGpfvPLRab9cX3T2hzzcwfquXPeDIMtaYLbvrva+nQ+/qGcrPGfKcDi24+ Z7K+mHeevWz21c8nuqxV27+80F55rfrus8c87t1carY9zcVvbi9s8P887bTCuWlLg3+EG9md VmW8sXUFx71Dbkvdfk0PmvF6Tva/lOjJG/1+nRBpyTfi9VLxv3QlS4mlOCPRUIu5qDgRAF1A 5vloAgAA X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170512133146eucas1p17df48bb6b5fcefe3717e18cd9afd84b7 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: 20170512133146eucas1p17df48bb6b5fcefe3717e18cd9afd84b7 X-RootMTR: 20170512133146eucas1p17df48bb6b5fcefe3717e18cd9afd84b7 References: <1494595886-30912-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 V5 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 0f75700..c96d5f5 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -60,32 +60,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"); @@ -136,7 +200,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; } @@ -513,7 +577,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; }