From patchwork Fri Dec 2 17:43:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063040 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 651B8C4332F for ; Fri, 2 Dec 2022 17:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233878AbiLBRoc (ORCPT ); Fri, 2 Dec 2022 12:44:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233873AbiLBRo0 (ORCPT ); Fri, 2 Dec 2022 12:44:26 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C0F9DEA48 for ; Fri, 2 Dec 2022 09:44:23 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id g14-20020adfa48e000000b00241f94bcd54so1256590wrb.23 for ; Fri, 02 Dec 2022 09:44:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yhyawS7kJhQQSVgVJnQgPFltY+BVOYcIZhbJ8N9jvFw=; b=gtsqf+Tqym3zq8mDcVkCeMWUB8q2lSdEtTktCFzYsBVqLm2De7d4IHGD8TsO5nFgm8 nQK38S/+XCyRIweXVhFDhaNgLD4UOUEBbtWpFcFJ7wsD3XVVeB4yUUHEBeJFEWdcPrLZ WeDF698pPg6ny61H+Vks0oJOz7cQM0KL7qN3AcICIdS7bGsPjI/PvSx+TwQgd6JBiBRG 44TMojD+pefKthphiaGbuO/H5G+hqPtzuH4midb7Os0brCQEc2xHzsE8WGYtvZZfUVPX xlE/P7waif9w2bwOMp+AAmZCYtI+F++/YZGRLaCTmJwGPcaAE+ZzoBLNQfTJegYj/5nt Uvjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yhyawS7kJhQQSVgVJnQgPFltY+BVOYcIZhbJ8N9jvFw=; b=d5msfIPbes7BAYPj2woYtklAzue2LlJZknp/3vGlCzZmeOm4qe8pHMCKsGFEwU/Y4l iPD1cdFe56W0pYcWteASQK62LM7KXIvDC3yWrDkvzqAOwZAu4Yig7VZijz6Z0beOAQoA F1Ku9d/JGf073wuBQvG4UfjfRefNIHPo5VAddLY53sRzVXFt8MidU8RPsDvKarIPzSrb 2PcQ9xlesQWYmkEWmhEFe6aru0+pBfXCWPJy/zAF96BTNTxmT68MzHoq0yNRVphIsHSE bZHU1cupiqqavkEsPGyTGpb46MpyKGNLkGpFIoK6KfwmmL0aFmvY+hJ7A0fZDXejp5rI A8Ww== X-Gm-Message-State: ANoB5pm/uiHgUfRsTgja0PqcKG3DO4e5DupQUna0TPVJzbLIOcHuDtt6 u63QTTVvH8ht+FeJABkBCfXbXf64W6I1CsTho0DtbaaAOZHsABTu+XEN4RTSJcsrrGeQK56ma49 leUOIxjr3zdd6Zh3aODZzM/+vWsWyX3JXnfjX6kabdGZuqiMTIfB06QA= X-Google-Smtp-Source: AA0mqf74e+cbmoJWL9Zi0NnL0rlr6BCAZZWZ2/NEBmLz2sjz91ObQB1kMw5ko7n/L/fnEyxUMcIzKAKVXQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:3514:b0:3cf:a985:7692 with SMTP id h20-20020a05600c351400b003cfa9857692mr45930356wmq.104.1670003061592; Fri, 02 Dec 2022 09:44:21 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:46 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-2-tabba@google.com> Subject: [RFC PATCH kvmtool v1 01/32] Initialize the return value in kvm__for_each_mem_bank() From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org If none of the bank types match, the function would return an uninitialized value. Signed-off-by: Fuad Tabba --- kvm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kvm.c b/kvm.c index 42b8812..765dc71 100644 --- a/kvm.c +++ b/kvm.c @@ -381,14 +381,15 @@ u64 host_to_guest_flat(struct kvm *kvm, void *ptr) * their type. * * If one call to @fun returns a non-zero value, stop iterating and return the - * value. Otherwise, return zero. + * value. If none of the bank types match, return -ENODEV. Otherwise, return + * zero. */ int kvm__for_each_mem_bank(struct kvm *kvm, enum kvm_mem_type type, int (*fun)(struct kvm *kvm, struct kvm_mem_bank *bank, void *data), void *data) { - int ret; struct kvm_mem_bank *bank; + int ret = -ENODEV; list_for_each_entry(bank, &kvm->mem_banks, list) { if (type != KVM_MEM_TYPE_ALL && !(bank->type & type)) From patchwork Fri Dec 2 17:43:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063041 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E475FC4332F for ; Fri, 2 Dec 2022 17:44:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233901AbiLBRog (ORCPT ); Fri, 2 Dec 2022 12:44:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233888AbiLBRo0 (ORCPT ); Fri, 2 Dec 2022 12:44:26 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8C0DDEA58 for ; Fri, 2 Dec 2022 09:44:25 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id e7-20020adf9bc7000000b00242121eebe2so1225179wrc.3 for ; Fri, 02 Dec 2022 09:44:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=30AptmEOrxEptsR4xup5ZnAEVV6MngChT14ZxzqU+kw=; b=Rg/bRb7xUsh3ybr2HBnoYLk5EM9nkIyydg1oLPtBI4hpGSaYU5EdeC5v0n1ihyaI2U VfWifEPUHMCxGNzVYv+/aMXRvp35lV1KM+iTTrxs4bidFxXOK9YYv4sgEFgvJ15ioYiO 04HDdY11JQjh35v5d8WRsbTRCu9AP28uuwMm8uAr+QuHybxSmW2c/G9IOK5fTgu54A1D CIBkZZIqj1xzn12w0Rz+tv7GVzMo+HDiRPJbr05M7xyDha23LQjyo7D4sU6AnseWKvom h9BtNqDtWwt9bciRE4jaXN9kV/WZZye/LENS4sbiwgjIJIU0HeWnIEHimzkwkrj200dr K+Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=30AptmEOrxEptsR4xup5ZnAEVV6MngChT14ZxzqU+kw=; b=rAc2T4NcApPrvhwg65nNL7IbS+8ywIzYtGdehfSRrxG7AfwQwmDH/VKXOuEoYZUIYk fQHQ6lOm2kPPDjlv9KbQ3kCpTIwrKsxe0Kwj89+Sf7jO90FNkcJugxh6BfJrPCpO+bj6 Tg1la0k76LPPhtKTGyRBIXPmHoYJ7I8K/4u5MU0Z5LH1jvog889tb9Q96SXKHOYNCPj6 pF7mzEe8L9zFKgk61YbmqqruN4Ky69arQovZcs/r0WlO5/zIdURlYFfriEuf8atLsrml Wb2i7GNcIm/GBpMFrsjDBJhn/1gDjEm94Wo12hgwnipylCkjCW4nimib2mH04nNW/sG5 GdrQ== X-Gm-Message-State: ANoB5pnrD+Nrnia2Hfvwkvivc+9WQ4zV48SstPx3/OQqoWgEI+Ic0asL cFDfETt0oesbttZOH1KOfkpLUs/tnHMZk11ipRX0Dh2Bbuu9vxa8fTkkYI4wsbCbJaSYVgy98z4 rIEIQxldzb1raeT1ZGWm1b2OIA1iGB9+qCLLqVeYTnzBu4D0lTUfZhQU= X-Google-Smtp-Source: AA0mqf4yVfa+tq3OK2llj3VIeUfB6d5/ddbIgqyenU673ogOKPz25MnpjYoMoERdQjQwfZkoMhFs3QCuGg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:3c91:b0:3cf:6f23:a3e3 with SMTP id bg17-20020a05600c3c9100b003cf6f23a3e3mr3618369wmb.1.1670003063959; Fri, 02 Dec 2022 09:44:23 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:47 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-3-tabba@google.com> Subject: [RFC PATCH kvmtool v1 02/32] Remove newline from end of die() aborts From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org die() already adds the newline at the end of the string. Suggested-by: Alexandru Elisei Signed-off-by: Fuad Tabba --- util/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/util/util.c b/util/util.c index 1877105..786dfc5 100644 --- a/util/util.c +++ b/util/util.c @@ -90,14 +90,14 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) unsigned long blk_size; if (statfs(htlbfs_path, &sfs) < 0) - die("Can't stat %s\n", htlbfs_path); + die("Can't stat %s", htlbfs_path); if ((unsigned int)sfs.f_type != HUGETLBFS_MAGIC) - die("%s is not hugetlbfs!\n", htlbfs_path); + die("%s is not hugetlbfs!", htlbfs_path); blk_size = (unsigned long)sfs.f_bsize; if (sfs.f_bsize == 0 || blk_size > size) { - die("Can't use hugetlbfs pagesize %ld for mem size %lld\n", + die("Can't use hugetlbfs pagesize %ld for mem size %lld", blk_size, (unsigned long long)size); } @@ -106,10 +106,10 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", htlbfs_path); fd = mkstemp(mpath); if (fd < 0) - die("Can't open %s for hugetlbfs map\n", mpath); + die("Can't open %s for hugetlbfs map", mpath); unlink(mpath); if (ftruncate(fd, size) < 0) - die("Can't ftruncate for mem mapping size %lld\n", + die("Can't ftruncate for mem mapping size %lld", (unsigned long long)size); addr = mmap(NULL, size, PROT_RW, MAP_PRIVATE, fd, 0); close(fd); From patchwork Fri Dec 2 17:43:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063042 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 862F2C47088 for ; Fri, 2 Dec 2022 17:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234036AbiLBRoh (ORCPT ); Fri, 2 Dec 2022 12:44:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233853AbiLBRo3 (ORCPT ); Fri, 2 Dec 2022 12:44:29 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BCF9DEA59 for ; Fri, 2 Dec 2022 09:44:28 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id d6-20020adfa346000000b0024211c0f988so1229214wrb.9 for ; Fri, 02 Dec 2022 09:44:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=O/131CUlG84aGb1aI35xcOB3PeBVwvnfM42yAaxe+WA=; b=SAB9ajEXUe02LaBAKG56vtO6rDrE0TzThndnZnsqxzi36+mNw+sAI55MV7Xvmr2JT8 bzScXzi91GMWaRM24Cm6bk6fTvNiry75vXvWTJKFijkugwB1jQPsWq0vjjdKz4iN4v9h jItGzdjhnRW7yxQZbG6BTc/fnndwyoAb/33jvW4tP4ZliXzFvlZTrSzx9Ky8mWDhA1Bb +7PnFjry/6h45JgwPeNX/tf++J2Sfg7yPp+biUQ3xEawLSlJHMutk35I29Gx9zfIlcEf XqrPhAVyy4j8/jwtv95RFWyKPjdtHSgn6re6wUE6OaLKcqojLDHWcufjYiWv2aCwzumd oujQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O/131CUlG84aGb1aI35xcOB3PeBVwvnfM42yAaxe+WA=; b=pl6HyMceWTarKqBRyEy18c6/33Dm703CauuEt/ni4T4bteLcYsP6+8BUEH+/VRT9um uUl+YxKuZUPLsGm4/cfngP6+LxZ2bODtleosqhprGiDAREj15Ky3q4URnnKEJTYk53Jt Gs/AjgvgoaA8EI8/WQtMERgFkoi/yNI1B6ls3j++4V8HMBz4kTP/SepGbF7DSxtq6cHu XDU+zoQuTJxqKIHJV5tHhVb/Wjp5RCXh2pVJ50PUnNE1Ntl2YjTJlEAVzyjYxWHPRmIe ZUl7vfDOkkQO9LC2s8jikYADG/H0B7IPa6w3M6wuKjpV9iBS6RlOqCJNGxUFzWnhXCuH 4Acg== X-Gm-Message-State: ANoB5pkkWLB6i6eNZ7/0NsUYOHBFVeFdRXxMT50GR4bbjLL1MRCRvmBw gA9/hw73VNcJDaZChl0SfsKbAwAHP9qPXdTRvGRmhSxFibo/LNZVA6q14bF92JozNSGePMIpQ8A fBnBMsJQX8KMQhT9yWgu00ZkpyIuxO1auTzvy2ugu4ikQ6HMIbKU6vKI= X-Google-Smtp-Source: AA0mqf6NI75EakafPWa3HDPXx0eLOkXYU/p5JuJom23fGd1R14vQNondI3f/LN9P9mkk48r+C5YUxgm+ew== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:4fcd:b0:3cf:7514:a80d with SMTP id o13-20020a05600c4fcd00b003cf7514a80dmr476111wmq.0.1670003066239; Fri, 02 Dec 2022 09:44:26 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:48 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-4-tabba@google.com> Subject: [RFC PATCH kvmtool v1 03/32] Make mmap_hugetlbfs() static From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This function isn't used outside of util.c. No functional change intended. Signed-off-by: Fuad Tabba --- include/kvm/util.h | 1 - util/util.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/include/kvm/util.h b/include/kvm/util.h index b494548..b0c3684 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -140,7 +140,6 @@ static inline int pow2_size(unsigned long x) } struct kvm; -void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size); void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size); #endif /* KVM__UTIL_H */ diff --git a/util/util.c b/util/util.c index 786dfc5..7a3fd62 100644 --- a/util/util.c +++ b/util/util.c @@ -81,7 +81,7 @@ void die_perror(const char *s) exit(1); } -void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) +static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) { char mpath[PATH_MAX]; int fd; From patchwork Fri Dec 2 17:43:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063043 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96208C4332F for ; Fri, 2 Dec 2022 17:44:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233944AbiLBRoi (ORCPT ); Fri, 2 Dec 2022 12:44:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233905AbiLBRob (ORCPT ); Fri, 2 Dec 2022 12:44:31 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46F72DEA48 for ; Fri, 2 Dec 2022 09:44:30 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id e19-20020adfa453000000b0024209415034so1242132wra.18 for ; Fri, 02 Dec 2022 09:44:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4ageV4gcuMu78Wx+6GrTJMvvsI3Mnk9DOcmBuuESKpA=; b=V9KWsUEGAe5MVZZlQ+w5H05acUS9wcWLm1VK2fNULxwiEzXtZgC6VBZhCJhR3pdYEW I7vO5WkDVMJCjKCHoVmKxhrmTlUh+jkEFdL5oRmg9MgLEcVxSFpDAnUcb5fZig2lLCLC 0tPtNbLdhgsJbsHc0xig7Z6Ar4mETWX+GuyX82VFG/wVO3P0ccNcKTx7lOdP5p3YAiT4 +pZ8eIhxqSxQUTHArcCoNd+8cQA+JR5b0XWPcXW60D6UBQPahQzru9vqIa42q/eHsLk9 O2lrN0r25XgpuJUa2LpRxFg/bLrYymu9tGbtqbzTrlGh298yLEcWzjXLaQBQKqZZiX9J QbFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4ageV4gcuMu78Wx+6GrTJMvvsI3Mnk9DOcmBuuESKpA=; b=xehFmlei8TuhJU/Lxr7WCjph+urGKdgGMtRBhk74QWu468VuVOPi5DB1nJcqR1vjW0 faiDDGcikLr6I+U7IxxlmK756Y3jtF0q+rTSOi8KVCBC3wFgSzIQoeok/HbK8u/4tQTY UwGccSQCjUF1CvbWdU4F9j7LRhhH1+0+UIgj75AFI1fXkuFH2OHHEv8hawC4Og4odH5K QN3BRNlOuA/Sv+nGzVEiC8a4hVH68r8q4iecI32l+NUsOFnswZF5kLxEntqEfaZq95Fy jvikDFZxzngTsZ8JC9J2mUYCrUyyBUlwKTdL4NAJlaR6NWiahMP0ZUllN2wcNiiJ5lNB yBqQ== X-Gm-Message-State: ANoB5pllyKKms15sQB4TnqYRT/PaBWY6hJzHhGXKu2edgNVV5hYtZbEK 9hsH4kclTHD9YAAht2Ngw9qZEXmGL3W+s34xCJEh/IHI575pL6GHguEAYBRoln0bAUxS4KKsluF Q8FO4f1c3JJaG8hgNCovBgB5BqhKCCBC8i+Wlv7r1b+N9bjbT2ZtqVhY= X-Google-Smtp-Source: AA0mqf5SAkRxyTG1Zcv6Zsrl0pU201UkEIDGKk1CdHEDWwh++q7zIxAxIK/jXtSXJ1BV8Ee1xZU6Z1Alfg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:4f83:b0:3cf:8b32:a52 with SMTP id n3-20020a05600c4f8300b003cf8b320a52mr57650596wmq.72.1670003068911; Fri, 02 Dec 2022 09:44:28 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:49 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-5-tabba@google.com> Subject: [RFC PATCH kvmtool v1 04/32] Rename parameter in mmap_anon_or_hugetlbfs() From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org For consistency with other similar functions in the same file. No functional change intended. Signed-off-by: Fuad Tabba --- util/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/util/util.c b/util/util.c index 7a3fd62..9f9642e 100644 --- a/util/util.c +++ b/util/util.c @@ -81,7 +81,7 @@ void die_perror(const char *s) exit(1); } -static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) +static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) { char mpath[PATH_MAX]; int fd; @@ -89,11 +89,11 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) void *addr; unsigned long blk_size; - if (statfs(htlbfs_path, &sfs) < 0) - die("Can't stat %s", htlbfs_path); + if (statfs(hugetlbfs_path, &sfs) < 0) + die("Can't stat %s", hugetlbfs_path); if ((unsigned int)sfs.f_type != HUGETLBFS_MAGIC) - die("%s is not hugetlbfs!", htlbfs_path); + die("%s is not hugetlbfs!", hugetlbfs_path); blk_size = (unsigned long)sfs.f_bsize; if (sfs.f_bsize == 0 || blk_size > size) { @@ -103,7 +103,7 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size) kvm->ram_pagesize = blk_size; - snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", htlbfs_path); + snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", hugetlbfs_path); fd = mkstemp(mpath); if (fd < 0) die("Can't open %s for hugetlbfs map", mpath); From patchwork Fri Dec 2 17:43:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063044 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BD82C4332F for ; Fri, 2 Dec 2022 17:44:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233974AbiLBRok (ORCPT ); Fri, 2 Dec 2022 12:44:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233989AbiLBRoe (ORCPT ); Fri, 2 Dec 2022 12:44:34 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97FF5DEA5F for ; Fri, 2 Dec 2022 09:44:32 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id i8-20020a1c3b08000000b003d0683389daso4439287wma.6 for ; Fri, 02 Dec 2022 09:44:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0buStrw79eK8dz6iyzCgpE2euqPvOQoxG6AVJwAz10Q=; b=Q+2RUqTAWTpNrDLho4QlBMCquPZbDqRk6wmZmtgjRRko9fyS3HKcRSNc1sFlTNahNa +12vO52zSgeEA6yRSi8at29VGEKOBxhtz38m7QAdqwVxAYb46W3/9fXQ+RZKXx6RLMCu 3kKsFJdt3di4tC8h0hkXjxfKM751WGxzRbx1nvHZWXgUNmpJKL+5Tcs2vbw+xgfAEFhR wUl1J1TNRCtmxqXNriaQalL5VofDoe3TG+RtdQ+8UyjZ2qKEw0nOp9XGe+QvOfJ4yLrh zeJrDa/rYvkDGjfKygah+jTKBEGS1+gtFCKj+9uTvrkpP8fwp+IejPN/zqdivb9wgZmN 0Rsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0buStrw79eK8dz6iyzCgpE2euqPvOQoxG6AVJwAz10Q=; b=iboIUx3SSzzszy88wkAQ63ijHkA6/enc6boyHt3eNlJfonl+bv/xEGuS68s5J6XMy7 BdqNKmzJ8VfEyFgGofr2M2/5zo6zh35T2jwKhatZY/F0+grPCiTU5fvdiWGFiyKKaO6a 11R2/GmpZ+kar7A8yfBkOTP3WNKwi0GkZrB9dJ4twZOh02MJv8FRXvKzEwvUf4kDwb8n 9sU0oZ+SKzJ7QS8Ci66/X6ivzrvlFKgR8BmsPT1IWTMELgZc+S5yeVxpxC4O4P1S4+EW 9rAViK5RbrGQG8OOej4LGv1D5+Nw28asHmhgTijHe4HS4er7PdrR/bbwleOEFP+lnJ6m 5hyQ== X-Gm-Message-State: ANoB5pnPiM71Pw84g8A6HRTh+QvdDCM4zuLfXsLNUN95IDWaMfIDeQ2J +chJ443SYo7BhJi5CcBhvQ91A8zLYBJMnypMeeBOX9shREo8XuhkQjdTMT4kqUxmMbrz73BIpjl pekBCwazUItiPlPxtou8D3uRJh2UDSspxrSZGDqTA5xvLofYkURsHcdo= X-Google-Smtp-Source: AA0mqf5dgBuxpRqATope9i45YD8sA9sJxm/AGxkgi4zxY4fEY/gir2vDl3YBREoYbW2mMrX9IrkBL3/LJA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:adf:ef04:0:b0:242:4b9b:5201 with SMTP id e4-20020adfef04000000b002424b9b5201mr1201515wro.97.1670003071113; Fri, 02 Dec 2022 09:44:31 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:50 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-6-tabba@google.com> Subject: [RFC PATCH kvmtool v1 05/32] Add hostmem va to debug print From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Useful when debugging. Signed-off-by: Fuad Tabba --- arm/kvm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arm/kvm.c b/arm/kvm.c index d51cc15..c84983e 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -66,8 +66,8 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.memory_guest_start = phys_start; - pr_debug("RAM created at 0x%llx - 0x%llx", - phys_start, phys_start + phys_size - 1); + pr_debug("RAM created at 0x%llx - 0x%llx (host_mem 0x%llx)", + phys_start, phys_start + phys_size - 1, (u64)host_mem); } void kvm__arch_delete_ram(struct kvm *kvm) From patchwork Fri Dec 2 17:43:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063045 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20192C47088 for ; Fri, 2 Dec 2022 17:44:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233902AbiLBRol (ORCPT ); Fri, 2 Dec 2022 12:44:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234029AbiLBRog (ORCPT ); Fri, 2 Dec 2022 12:44:36 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1622DEA4E for ; Fri, 2 Dec 2022 09:44:34 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id u9-20020a05600c00c900b003cfb12839d6so2194056wmm.5 for ; Fri, 02 Dec 2022 09:44:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=n9DP7lADh4c+E7x+otRAyP30/DemjSDCvHJYlgZT93I=; b=J7ua18QZPVZaLUzWblK6o+MbcbETRh+/PeykvEOiyVDv/S5qQxPk4h7b/FwUoMkenU bw6l4A7MYoWwHGbn8YjiENVAcq3p9vz/PgsBGjaFretVVVcsEvblKBDsssiNBNJuvayq yHUh2xqDZ/SVQ4ufK5EAmtlr/c6mFfQ8I9LH/F4FsX2NCq+FMScYzpsQow1M/LU01a0u NpO/zcGuuKAOfMyWs0avbn5lKLibWQ/txAaX1+UnSd3ObMOd5RWBgPWw2bfdw93iB/fc 9QP+NMYBtp44z2aBxVTHIQmbm1k7FgooU9AqYhZGgBoCUo0c2Wh877Gi7y0w1nTwZB8x iZaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=n9DP7lADh4c+E7x+otRAyP30/DemjSDCvHJYlgZT93I=; b=m0DdC4M4siQ4hfB0Ke/cWqYKWzB0WA7DGLVfk309JivOXZuMbCQykc3O5lFLxWX+yT rcnm/zoNZZGp4m0vguAR28Gt3h/wwlGADx3zEeaZkraGeaRyyY7r+nUUREi2ChStU8ru orPKfXU9ARNTvKD9TjXvkr3yIG+zMOVz7Q+UGF3KGPNy+25roszZXgYIlAw/eu0uzxwF UW5wpkYucfz6eTypvQ8QxHebilw5h0M6K1vKl0lWIxNM7Aj6na6r7oPzs97dHXfr7FIn obO3HXR0sSkuUp16QC2BOnZ8c2lee0/mqfynN1RUwST9Z2EqdPBMbx1rjQUkcFmtIlFl Xt2w== X-Gm-Message-State: ANoB5plytq6zezx91/4WAN5bsg8E6OI4gLd/tedATnL3VXD0MhsH2Sed pSt1y9C/iDXDj9E5XXyNnbL+Ng03YoeyRnIiygC8ccPce8oevD3ibYEglEuZ3tJJtTVPNuNVD3X yrRR4mXA7qtVIeAk83BxvyPyegCGkWTrLupTXsypCKYAfj1S3t+TnG+w= X-Google-Smtp-Source: AA0mqf5LpF6NWYMYBw1TBS14fejpiDfduxi9uGhOiD7AXLwIjfWP68XRX39l2O5+aWEvYNd0nIDLp8epbg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a7b:c456:0:b0:3a5:f600:502e with SMTP id l22-20020a7bc456000000b003a5f600502emr44127495wmi.39.1670003073443; Fri, 02 Dec 2022 09:44:33 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:51 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-7-tabba@google.com> Subject: [RFC PATCH kvmtool v1 06/32] Factor out getting the hugetlb block size From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This functionality will be needed separately in a future patch. No functional change intended. Signed-off-by: Fuad Tabba --- util/util.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/util/util.c b/util/util.c index 9f9642e..2c6fcc5 100644 --- a/util/util.c +++ b/util/util.c @@ -81,13 +81,9 @@ void die_perror(const char *s) exit(1); } -static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) +static u64 get_hugepage_blk_size(const char *hugetlbfs_path) { - char mpath[PATH_MAX]; - int fd; struct statfs sfs; - void *addr; - unsigned long blk_size; if (statfs(hugetlbfs_path, &sfs) < 0) die("Can't stat %s", hugetlbfs_path); @@ -95,10 +91,20 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz if ((unsigned int)sfs.f_type != HUGETLBFS_MAGIC) die("%s is not hugetlbfs!", hugetlbfs_path); - blk_size = (unsigned long)sfs.f_bsize; - if (sfs.f_bsize == 0 || blk_size > size) { - die("Can't use hugetlbfs pagesize %ld for mem size %lld", - blk_size, (unsigned long long)size); + return sfs.f_bsize; +} + +static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) +{ + char mpath[PATH_MAX]; + int fd; + void *addr; + u64 blk_size; + + blk_size = get_hugepage_blk_size(hugetlbfs_path); + if (blk_size == 0 || blk_size > size) { + die("Can't use hugetlbfs pagesize %lld for mem size %lld", + (unsigned long long)blk_size, (unsigned long long)size); } kvm->ram_pagesize = blk_size; From patchwork Fri Dec 2 17:43:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063047 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0D11C4321E for ; Fri, 2 Dec 2022 17:44:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234073AbiLBRoo (ORCPT ); Fri, 2 Dec 2022 12:44:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233853AbiLBRoh (ORCPT ); Fri, 2 Dec 2022 12:44:37 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEA6EDEA48 for ; Fri, 2 Dec 2022 09:44:36 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id h10-20020adfaa8a000000b0024208cf285eso1258976wrc.22 for ; Fri, 02 Dec 2022 09:44:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=u2mYeRLqrkrCVPKSHkXjmzqbL6Z26iJ6Ebt+DZZD59Y=; b=SsqqvxTAAq+beCl42UIw2OmG7mWy2DMJEV/OH/CErcjJjvORQcgatScBmUAG7aqE7w ZBAQtkeUwXBgrAiQfnaHTxqX0sFcTrqJEgmAod5cHYeeQ/0JQpjfHTUf/yXvKZkqgCAG VxgXpt2IuO5+M5gbgUWC0xXmfWhbaQUofpZcfPyEDOz1eUjDBn6X9n2Xo07pS2XXRZLp 9MmWDgnvI1fl4wuqo7lRY1p7N7swHvemnrgRjOpWwb0P/6XWXaMG0vK9/1GO5AvFtnY1 H2FPvSrNCVLWIL5fHh1EaSLqYMmJ7h5qsqWN54YcevrFBDJ3D6tyiy/wQObAVb5OHVUR lDGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u2mYeRLqrkrCVPKSHkXjmzqbL6Z26iJ6Ebt+DZZD59Y=; b=uN8PNa7SE4MEPz7ZPbONgte08KT3viabNCjHxSkxvc1ubflkmX96RtrZ8dJHZqtALZ GgW8BJFe3scTt4Tqe9gVn5fL5T2KzlkAjb18SQ85UTPadv3hx/tdRUtoteSg9kYblnSf j12AhrogKVkQMHftbdzzXTG2IajWl0maElHKDhbZIQY2X+RrUY0idhUaVqNKU9Sk3pxm VgKOOy+VgJ4RbPJfOI5of6GFlOfckPi+1CX23Mvt9lFEUGukMuZRiygtl+dT1tDyJrBb h30KNBPXkykdgdL8qzfzQStUxtQIUP8jnLW4uSn3BfXHlM/vFJwfOksqXahmTEZ/V9l9 k2oA== X-Gm-Message-State: ANoB5pm+YEqU0WaEoPnGrpo7Wosnb0PEIWIPM8LuyvAxQQa+UZCmBcH4 3izEcaWcFwojEhAoOOEPFvmB1i8RMbKpRRe6DA0f0tlBjXomV1vtNVUX1hpmgkKTWetdnwcpwRh jl1MHvdIel9uY16/3M7/2knA+fJ0Lvsm+ExjEzgKmwhR2w72ZLJ1oA9s= X-Google-Smtp-Source: AA0mqf6PE6ZAuEeIt+AcQisNNpuvL7G8vZZcAAF24cMc5fkIhr/2XntJdXwwqo+o3mYjluFb4OgY64C9Qg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a7b:cd8a:0:b0:3cf:e0f0:9e8 with SMTP id y10-20020a7bcd8a000000b003cfe0f009e8mr45753705wmj.156.1670003075380; Fri, 02 Dec 2022 09:44:35 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:52 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-8-tabba@google.com> Subject: [RFC PATCH kvmtool v1 07/32] Use memfd for hugetlbfs when allocating guest ram From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This removes the need of using a temporary file for the fd, and is the first step towards using memfd_create() across this series. Signed-off-by: Fuad Tabba --- util/util.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/util/util.c b/util/util.c index 2c6fcc5..b347fa3 100644 --- a/util/util.c +++ b/util/util.c @@ -6,6 +6,7 @@ #include #include /* For HUGETLBFS_MAGIC */ +#include #include #include #include @@ -96,7 +97,8 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path) static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) { - char mpath[PATH_MAX]; + const char *name = "kvmtool"; + unsigned int flags = 0; int fd; void *addr; u64 blk_size; @@ -107,13 +109,17 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz (unsigned long long)blk_size, (unsigned long long)size); } + if (!is_power_of_two(blk_size)) + die("Hugepage size must be a power of 2"); + + flags |= MFD_HUGETLB; + flags |= blk_size << MFD_HUGE_SHIFT; + kvm->ram_pagesize = blk_size; - snprintf(mpath, PATH_MAX, "%s/kvmtoolXXXXXX", hugetlbfs_path); - fd = mkstemp(mpath); + fd = memfd_create(name, flags); if (fd < 0) - die("Can't open %s for hugetlbfs map", mpath); - unlink(mpath); + die_perror("Can't memfd_create for hugetlbfs map"); if (ftruncate(fd, size) < 0) die("Can't ftruncate for mem mapping size %lld", (unsigned long long)size); From patchwork Fri Dec 2 17:43:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063046 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90EB1C4332F for ; Fri, 2 Dec 2022 17:44:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234000AbiLBRon (ORCPT ); Fri, 2 Dec 2022 12:44:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232550AbiLBRok (ORCPT ); Fri, 2 Dec 2022 12:44:40 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF700DEA5B for ; Fri, 2 Dec 2022 09:44:38 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id c187-20020a1c35c4000000b003cfee3c91cdso2814635wma.6 for ; Fri, 02 Dec 2022 09:44:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EtUwd18qcwkJhEJATUqJ5o4gXc9dWFfkzT8XmPE9Hrs=; b=Hl4biVTHwFhm5UBcmOLpRL0wGKh1S//eqRaddMd9fcqIYpIKpX8UlH1qgBUkCfvCIN WHuoMUtoigkD2E1SkEyRxn0W0gnMwrXZInySIpTCYK+OomiOoNw4QoyCBdhvtKwsBHwK 1ODFaFUVK6an+Yu772MCNvSqaXEZNVCbiOsyKW+UjirbA/nPV7MD0uxNSlS53tS3ARbA gAkX3WZWsQoV1jrJFMh6BqsbXzN7N2Yas8OzorLR75ebnsiob3LamlpnsA0H2yBqSX3K 7gwjld/miRAgonXzSepcaanOnL6KZKSAUyqijyJsHGGMLJyG5TMf4oeWpKiSTkuTlajp rbIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EtUwd18qcwkJhEJATUqJ5o4gXc9dWFfkzT8XmPE9Hrs=; b=uYQVSjR5z7i5s+s5j6z0Htnc6Vpo1cANNxtSjal8+4+Ig6J6UUpcdOZoY1ine+zWg8 Dqq6DWEpptpY+kUuxr0oFrvfyUfhvTkRlAP1KqODDIHEdSsy/mSWYoWnZXfx/RgyOC5T Ge3Q5mi7NQ+sWqOz8uabjR7/4f4SnZv6U7I849lAk3Jg2R7Ob+9PHwhs89vICzFB3w78 Dit8ei+AoBuFI2AXpnMSDTOT2JXRFvETd2/7TJM7M3szPNP8amVybR6/pJeIHQdJxZL5 MXWd2FOWZAzrP3uTqTjTmcj08mmACk5gM6yFOxG6tBjw4crzswyvameu3vdJ6yV69qWn xRiQ== X-Gm-Message-State: ANoB5pkNgNLtavIaYPvk8jERgyaqxBv2hwXC72miVq9l/jeoZLubpZTt nBSyguU8glvKQK3EIL9Agw8Ne0K86oXzxfVwBDQ1gAJ7Lopxfo53CZYVFwQxseOC8hhWmZ3/VTY mbxB8NX8Kxq33+9lHiAe7gCYMaYb04MOMSeor8+G5z8iWs/vefbwQrgE= X-Google-Smtp-Source: AA0mqf6tizpquIiG1q3fA9ZtGVu+coyn9gZNDl8lFNyzDFaKd/1g1zPx+Jm4zNUp9OR+9vKYNN6acWmq/g== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6000:508:b0:242:34c1:1fd1 with SMTP id a8-20020a056000050800b0024234c11fd1mr6064844wrf.218.1670003077423; Fri, 02 Dec 2022 09:44:37 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:53 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-9-tabba@google.com> Subject: [RFC PATCH kvmtool v1 08/32] Make blk_size a parameter and pass it to mmap_hugetlbfs() From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This is the first step of making this function more generic. The main purpose of this patch is to make it easier to review the next one. No functional change intended. Signed-off-by: Fuad Tabba --- util/util.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/util/util.c b/util/util.c index b347fa3..93f895a 100644 --- a/util/util.c +++ b/util/util.c @@ -95,19 +95,12 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path) return sfs.f_bsize; } -static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) +static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size, u64 blk_size) { const char *name = "kvmtool"; unsigned int flags = 0; int fd; void *addr; - u64 blk_size; - - blk_size = get_hugepage_blk_size(hugetlbfs_path); - if (blk_size == 0 || blk_size > size) { - die("Can't use hugetlbfs pagesize %lld for mem size %lld", - (unsigned long long)blk_size, (unsigned long long)size); - } if (!is_power_of_two(blk_size)) die("Hugepage size must be a power of 2"); @@ -115,8 +108,6 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz flags |= MFD_HUGETLB; flags |= blk_size << MFD_HUGE_SHIFT; - kvm->ram_pagesize = blk_size; - fd = memfd_create(name, flags); if (fd < 0) die_perror("Can't memfd_create for hugetlbfs map"); @@ -132,13 +123,23 @@ static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 siz /* This function wraps the decision between hugetlbfs map (if requested) or normal mmap */ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) { - if (hugetlbfs_path) - /* - * We don't /need/ to map guest RAM from hugetlbfs, but we do so - * if the user specifies a hugetlbfs path. - */ - return mmap_hugetlbfs(kvm, hugetlbfs_path, size); - else { + u64 blk_size = 0; + + /* + * We don't /need/ to map guest RAM from hugetlbfs, but we do so + * if the user specifies a hugetlbfs path. + */ + if (hugetlbfs_path) { + blk_size = get_hugepage_blk_size(hugetlbfs_path); + + if (blk_size == 0 || blk_size > size) { + die("Can't use hugetlbfs pagesize %lld for mem size %lld\n", + (unsigned long long)blk_size, (unsigned long long)size); + } + + kvm->ram_pagesize = blk_size; + return mmap_hugetlbfs(kvm, hugetlbfs_path, size, blk_size); + } else { kvm->ram_pagesize = getpagesize(); return mmap(NULL, size, PROT_RW, MAP_ANON_NORESERVE, -1, 0); } From patchwork Fri Dec 2 17:43:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063048 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9806C47088 for ; Fri, 2 Dec 2022 17:44:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234025AbiLBRoq (ORCPT ); Fri, 2 Dec 2022 12:44:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234034AbiLBRom (ORCPT ); Fri, 2 Dec 2022 12:44:42 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E20ADEA5B for ; Fri, 2 Dec 2022 09:44:41 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id h4-20020a1c2104000000b003d01b66fe65so4456192wmh.2 for ; Fri, 02 Dec 2022 09:44:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7mo9Hvguu8h16c/AUECuaAHaIG4PTEnHfuKBpU6xwF0=; b=PCIOZi62Y0UTBF/YESMprw6PGKgiXgx9zgBz156PqwP5jTuq+bAJ3dwHTkB0J9krqS Sxdyxq8AELHX4pL1+6ZQVKcfl/TWFgJUwiPDFZ+Cja5VVaQZ1+i6LGsaVDNU1Wc96YRF 5CxKb7ft2b3oczw6lKgf9/owM2z2g459NM+gCiDBh67y57fVgwA/lYeXIQBPp9BWDJQH AuphDwz9Xr3gWkaCmF+wMoBpkgXttcxGTqyE4lb6t4S5vmJ8QG6CNCz95az1mnxz0VWZ ekbxUy6s28nBw3jVVeR+nIveN7bu6e1EndOx9c9P5p1PGc2janMUI0hHIxBq7JTfRkZ0 PPMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7mo9Hvguu8h16c/AUECuaAHaIG4PTEnHfuKBpU6xwF0=; b=gIDuR2MwUZKVbqoil0IvjvgHLobX13yFpH6XqA1Xw7pJ3dSA4HNFY2BpzMRcS6YYB/ /i05+NSZR43CEagDeCyHZ6UdGelfVgaXSh2GL3b0OQd026yiq/Cr5TbF838dRs4mANHI mcm9Z7a/zDmf4o02ByA5HB403loNGn4fOiDrNoVhwmB867bpSrwEkLa3f9dRoGNMfw+n v1RYwo6lhuWcvVPr0Mtsmk5nxs0dTkA3Z7cvjf+4pD805By+cN8Gkn0sSY7MAOSEOuIx 2itR7wN0uFZWsD1AueUQ6NGQxvEeTQNb6vPq0aTKlPED34nzp9I8gdKCFZ7t/DUMwQK6 YvYA== X-Gm-Message-State: ANoB5pnXuuixrCYqGnMCmXDL/0bqYc0TMz3L32AEXgMeMLsQMn12MP2w v/pYBTl08D29GZ7dqh+uaaFBvPH716NLnFzJ+AfZKYd5SVM/9zyE3t+f+l7A0xtR1mWKKB21PCt yN6Vs/AF66WAZW2rCPwczMizXZ68DtaECQ+L8OVTqmnrZ+8UEzFytZCs= X-Google-Smtp-Source: AA0mqf4/IWzVyPS1V+YdpUkdDFQsK5YpaG/BIzsYKCGydfH8dp/mdYUXHsSFfTvFClrIdoxvlmC39aNuRg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:3c91:b0:3cf:6f23:a3e3 with SMTP id bg17-20020a05600c3c9100b003cf6f23a3e3mr3618433wmb.1.1670003079610; Fri, 02 Dec 2022 09:44:39 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:54 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-10-tabba@google.com> Subject: [RFC PATCH kvmtool v1 09/32] Use memfd for all guest ram allocations From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Allocate all guest ram backed by memfd/ftruncate instead of anonymous mmap. This will make it easier to use kvm with fd-based kvm guest memory proposals [*]. It also would make it easier to use ipc memory sharing should that be needed in the future. Signed-off-by: Fuad Tabba [*] https://lore.kernel.org/all/20221202061347.1070246-1-chao.p.peng@linux.intel.com/ --- include/kvm/kvm.h | 1 + include/kvm/util.h | 3 +++ kvm.c | 4 ++++ util/util.c | 30 ++++++++++++++++++------------ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 3872dc6..d0d519b 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -87,6 +87,7 @@ struct kvm { struct kvm_config cfg; int sys_fd; /* For system ioctls(), i.e. /dev/kvm */ int vm_fd; /* For VM ioctls() */ + int ram_fd; /* For guest memory. */ timer_t timerid; /* Posix timer for interrupts */ int nrcpus; /* Number of cpus to run */ diff --git a/include/kvm/util.h b/include/kvm/util.h index b0c3684..74713d9 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -140,6 +140,9 @@ static inline int pow2_size(unsigned long x) } struct kvm; +int memfd_alloc(u64 size, bool hugetlb, u64 blk_size); +void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, + u64 size, u64 align); void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size); #endif /* KVM__UTIL_H */ diff --git a/kvm.c b/kvm.c index 765dc71..84a8675 100644 --- a/kvm.c +++ b/kvm.c @@ -160,6 +160,7 @@ struct kvm *kvm__new(void) mutex_init(&kvm->mem_banks_lock); kvm->sys_fd = -1; kvm->vm_fd = -1; + kvm->ram_fd = -1; #ifdef KVM_BRLOCK_DEBUG kvm->brlock_sem = (pthread_rwlock_t) PTHREAD_RWLOCK_INITIALIZER; @@ -174,6 +175,9 @@ int kvm__exit(struct kvm *kvm) kvm__arch_delete_ram(kvm); + if (kvm->ram_fd >= 0) + close(kvm->ram_fd); + list_for_each_entry_safe(bank, tmp, &kvm->mem_banks, list) { list_del(&bank->list); free(bank); diff --git a/util/util.c b/util/util.c index 93f895a..54c2b26 100644 --- a/util/util.c +++ b/util/util.c @@ -95,35 +95,36 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path) return sfs.f_bsize; } -static void *mmap_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size, u64 blk_size) +int memfd_alloc(u64 size, bool hugetlb, u64 blk_size) { const char *name = "kvmtool"; unsigned int flags = 0; int fd; - void *addr; - if (!is_power_of_two(blk_size)) - die("Hugepage size must be a power of 2"); + if (hugetlb) { + if (!is_power_of_two(blk_size)) + die("Hugepage size must be a power of 2"); - flags |= MFD_HUGETLB; - flags |= blk_size << MFD_HUGE_SHIFT; + flags |= MFD_HUGETLB; + flags |= blk_size << MFD_HUGE_SHIFT; + } fd = memfd_create(name, flags); if (fd < 0) - die_perror("Can't memfd_create for hugetlbfs map"); + die_perror("Can't memfd_create for memory map"); + if (ftruncate(fd, size) < 0) die("Can't ftruncate for mem mapping size %lld", (unsigned long long)size); - addr = mmap(NULL, size, PROT_RW, MAP_PRIVATE, fd, 0); - close(fd); - return addr; + return fd; } /* This function wraps the decision between hugetlbfs map (if requested) or normal mmap */ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) { u64 blk_size = 0; + int fd; /* * We don't /need/ to map guest RAM from hugetlbfs, but we do so @@ -138,9 +139,14 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 si } kvm->ram_pagesize = blk_size; - return mmap_hugetlbfs(kvm, hugetlbfs_path, size, blk_size); } else { kvm->ram_pagesize = getpagesize(); - return mmap(NULL, size, PROT_RW, MAP_ANON_NORESERVE, -1, 0); } + + fd = memfd_alloc(size, hugetlbfs_path, blk_size); + if (fd < 0) + return MAP_FAILED; + + kvm->ram_fd = fd; + return mmap(NULL, size, PROT_RW, MAP_PRIVATE, kvm->ram_fd, 0); } From patchwork Fri Dec 2 17:43:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09B2BC4332F for ; Fri, 2 Dec 2022 17:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234124AbiLBRoq (ORCPT ); Fri, 2 Dec 2022 12:44:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234113AbiLBRoo (ORCPT ); Fri, 2 Dec 2022 12:44:44 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4A90DEA6F for ; Fri, 2 Dec 2022 09:44:43 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id g14-20020adfa48e000000b00241f94bcd54so1256721wrb.23 for ; Fri, 02 Dec 2022 09:44:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zPOx4m/3fYB8V48egMLSD29AXOysInYZsQD39byc48o=; b=FQIh4Qudy3vz3A+da2b6+80WNpXxA3iSS9zKi8944agRqf6/quOaELHt0b4hFvidUM 5DkPj/6Z9N+JlLHv9priyjLmU4ZmYBr4keAHGsheukiO3SwDqoBJ0HLOwzFvlP+L/k6V XDdJQkawhk1FZ5ZE8M+Pe9BcAxNzkpTcqpKRMYTa67MuDf6Ztedm9sfeJFbvaz3TR/zH O2xTsWSVIxREWdHQAByQ6ST6P0+8i+PVX2BAUkZZKNrG6JAn0wymuHILCIYE5qDl9JdG gfflFlVm2wS+k9kLwaludYpPROhHMbmMSVppxFKHUp6MU+jGVKCZuh0ydfX1l2nSQRz0 n/aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zPOx4m/3fYB8V48egMLSD29AXOysInYZsQD39byc48o=; b=telnzvefQ9oYE/SoCAJP+DC10Z85yjvIct3kiT1tFqZuJPIeopiACWKXPdw9TD9WXV aB4EIlVd7o5+nBGfd2WEGZSALaNefJ4lLmbLi59Z1jLJ4tiT9yRhx56Q4ZqLvx8Jlxrl yRpwqBsZCqUJU+fEckcdj2bKDSCDAZy2lFFwkbgObrarrkdEx+s2fE97Vi2ZIC6XT3/B NPqP0soeI5krRNbT7oPEv7rUb0p5zYmBSy0o8C/9j9iXxhCg1CEZHNcB7GVB8hnfplVN sc6ywtSyZ/9o4DtuZHiueCC7FhdmldKZEvT2X3e2IdFFylDmnSiSs5RD/bRr2Q41arDv ZWQA== X-Gm-Message-State: ANoB5plo0o6HGxDJA+zTUDwU41UwTD9DIkGkKhykaL5AXuH0FN5UuTIO Y8TkIZHai9MIyiK7Ro4Qq4fECteUSwYQTsQmXQaBpLFpVwbPaQzufEqGKh4DQIP5d6h0L0oHLFI CegRGtHhJ5V0j4tK4uyjwIpFkre3Gc1ngnLp1SfB1ymWgfT9m1w/n8hE= X-Google-Smtp-Source: AA0mqf7wuzE0gdsOQl9HBIaUjlnvXhJQNLa7uVCrgdG9rkOi7CkkSTdaAqUTvQTHHkoflTdMZwhWzdeW9w== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:1c9f:b0:3b4:cd96:1748 with SMTP id k31-20020a05600c1c9f00b003b4cd961748mr204639wms.2.1670003081656; Fri, 02 Dec 2022 09:44:41 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:55 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-11-tabba@google.com> Subject: [RFC PATCH kvmtool v1 10/32] Allocate pvtime memory with memfd From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Ensure that all guest memory is fd-based. Signed-off-by: Fuad Tabba --- arm/aarch64/pvtime.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/arm/aarch64/pvtime.c b/arm/aarch64/pvtime.c index 2933ac7..a7ba03e 100644 --- a/arm/aarch64/pvtime.c +++ b/arm/aarch64/pvtime.c @@ -8,25 +8,35 @@ #define ARM_PVTIME_STRUCT_SIZE (64) static void *usr_mem; +int user_mem_fd = -1; static int pvtime__alloc_region(struct kvm *kvm) { char *mem; + int mem_fd; int ret = 0; - mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, - MAP_ANON_NORESERVE, -1, 0); - if (mem == MAP_FAILED) + mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0); + if (mem_fd < 0) return -errno; + mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0); + if (mem == MAP_FAILED) { + ret = -errno; + close(mem_fd); + return ret; + } + ret = kvm__register_ram(kvm, ARM_PVTIME_BASE, ARM_PVTIME_SIZE, mem); if (ret) { munmap(mem, ARM_PVTIME_SIZE); + close(mem_fd); return ret; } usr_mem = mem; + user_mem_fd = mem_fd; return ret; } @@ -38,7 +48,9 @@ static int pvtime__teardown_region(struct kvm *kvm) kvm__destroy_mem(kvm, ARM_PVTIME_BASE, ARM_PVTIME_SIZE, usr_mem); munmap(usr_mem, ARM_PVTIME_SIZE); + close(user_mem_fd); usr_mem = NULL; + user_mem_fd = -1; return 0; } From patchwork Fri Dec 2 17:43:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063050 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 151A5C4332F for ; Fri, 2 Dec 2022 17:44:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234131AbiLBRot (ORCPT ); Fri, 2 Dec 2022 12:44:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234123AbiLBRoq (ORCPT ); Fri, 2 Dec 2022 12:44:46 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4554FDEA6C for ; Fri, 2 Dec 2022 09:44:45 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id d8-20020adf9b88000000b0024207f09827so1250375wrc.20 for ; Fri, 02 Dec 2022 09:44:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hOL+sVrDcqcOMr65I7XMCUXkitiCQrNrGUrpvFh3Rl0=; b=VmxuGMHFTrgQgIJffg2d7KaBa3cmR0fXgpru0DE6RDl+yUCaWoGanpZSketZGTFfWo MEY2XbRdT5QtoqGWFOG88NY8mGlsE4fl+SNrtsaslqq+UehZqR28FX259ralC2j9uBpo hli9X+qShSrhWZFQwXn/pBjxsRctWCqKj7PdrqdAeEd+IKZJ9wBu4KXW4Pst6bh2A9MJ WAsgrAmJd/AvmBGcS3ACL8658rvLanngiUg6u8yfWCEsofe6xEXxQPeqoCuQAWQ2+zEQ aeQnok7iEb3L0fEPJafeuN2s+uN3NwGRfzR/4mYF2RzVrTU1KIJzoruie8Tg7mDfI20G ij2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hOL+sVrDcqcOMr65I7XMCUXkitiCQrNrGUrpvFh3Rl0=; b=KFdXDx9P110fUenWs+qif0Z+DMV9utxMcgYuX/AQd6RGxHA7Blq8ww3+N4T9TCJNAE ceSTgvQoBEX5fer1gqDwdYdaHnBxkRUuXzF5tMMC/yeIVPogHGcl3GJUMkPArgMslxbp RM4f25PV3/1wmohKq8yxEa7bMO/Tbmc8AUIbEtQBHPbsyXnEMBuscb4OBSoByfBaGZT2 mvj3wXBXsaUZ0u6AqmMqXelvWKDSw337m9/0ExjWw55UX0Kk/u3hr7macQZ3aFWEqMjc Vivq3QfMD5Oh5Us0vTleGmVihaabqylq5cdkJHFi7Pn/UWyxj/jplScysA9w+zjHAaet qhVw== X-Gm-Message-State: ANoB5plAG7fWpRGcFQ61Mqhb0NvgVbbenFzzf/wPFf/QujBeT+euQ416 h4N48trqnc9U2P6SPEz12N7236l7zUmWsqziC+sA2qBFwvFryatHveI9kBYCh5Pdl8bPPq7htTA bJVwTJ2rDlE+PHY4e/v1PA8qySjDxbohbN2ke1yMSpv5vcTSGKUM0kKk= X-Google-Smtp-Source: AA0mqf6dNQFvzbiFwI8bMWh6TgeL+CWpXRQOd7+AroRr+2f6cmSKwan9BbKD0674MDInnMaMxER7ugaAwg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6000:5c5:b0:22f:6a99:19f3 with SMTP id bh5-20020a05600005c500b0022f6a9919f3mr43925972wrb.165.1670003083907; Fri, 02 Dec 2022 09:44:43 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:56 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-12-tabba@google.com> Subject: [RFC PATCH kvmtool v1 11/32] Allocate vesa memory with memfd From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Ensure that all guest memory is fd-based. Signed-off-by: Fuad Tabba --- framebuffer.c | 2 ++ hw/vesa.c | 15 +++++++++++++-- include/kvm/framebuffer.h | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/framebuffer.c b/framebuffer.c index fb8f51d..a025293 100644 --- a/framebuffer.c +++ b/framebuffer.c @@ -73,6 +73,8 @@ int fb__exit(struct kvm *kvm) fb->targets[i]->stop(fb); munmap(fb->mem, fb->mem_size); + if (fb->mem_fd >= 0) + close(fb->mem_fd); } return 0; diff --git a/hw/vesa.c b/hw/vesa.c index 7f82cdb..522ffa3 100644 --- a/hw/vesa.c +++ b/hw/vesa.c @@ -41,6 +41,7 @@ static struct framebuffer vesafb = { .depth = VESA_BPP, .mem_addr = VESA_MEM_ADDR, .mem_size = VESA_MEM_SIZE, + .mem_fd = -1, }; static void vesa_pci_io(struct kvm_cpu *vcpu, u64 addr, u8 *data, u32 len, @@ -66,6 +67,7 @@ struct framebuffer *vesa__init(struct kvm *kvm) { u16 vesa_base_addr; char *mem; + int mem_fd; int r; BUILD_BUG_ON(!is_power_of_two(VESA_MEM_SIZE)); @@ -88,22 +90,31 @@ struct framebuffer *vesa__init(struct kvm *kvm) if (r < 0) goto unregister_ioport; - mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_ANON_NORESERVE, -1, 0); - if (mem == MAP_FAILED) { + mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0); + if (mem_fd < 0) { r = -errno; goto unregister_device; } + mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0); + if (mem == MAP_FAILED) { + r = -errno; + goto close_memfd; + } + r = kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem); if (r < 0) goto unmap_dev; vesafb.mem = mem; + vesafb.mem_fd = mem_fd; vesafb.kvm = kvm; return fb__register(&vesafb); unmap_dev: munmap(mem, VESA_MEM_SIZE); +close_memfd: + close(mem_fd); unregister_device: device__unregister(&vesa_device); unregister_ioport: diff --git a/include/kvm/framebuffer.h b/include/kvm/framebuffer.h index e3200e5..c340273 100644 --- a/include/kvm/framebuffer.h +++ b/include/kvm/framebuffer.h @@ -22,6 +22,7 @@ struct framebuffer { char *mem; u64 mem_addr; u64 mem_size; + int mem_fd; struct kvm *kvm; unsigned long nr_targets; From patchwork Fri Dec 2 17:43:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063051 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E89A3C4332F for ; Fri, 2 Dec 2022 17:44:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234147AbiLBRow (ORCPT ); Fri, 2 Dec 2022 12:44:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234079AbiLBRov (ORCPT ); Fri, 2 Dec 2022 12:44:51 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68CF2DEA64 for ; Fri, 2 Dec 2022 09:44:47 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id bg25-20020a05600c3c9900b003cf3ed7e27bso2822944wmb.4 for ; Fri, 02 Dec 2022 09:44:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zQKiDjoP71FNxadoa0X576EGf59j0KZM9aNu/7L6Fz4=; b=rWAiEzlM8ILw3AV5CTC1SdN6uxvD5Nvbi+Oh5Znh+kPd6y2m/vPoazMEuVESVlihCb f3s/6DImPMlJAQFuf1BiX7S4MYZVL4cCVRJQw5HIfqJRnjHu8Oq4i9KQPcd0bHYqftnt 9PBaMVrrebaYG03g1Kz+eIn08PilsiX+1DvbHevW+wKXwWJX/VQX3f7SsAl2ESxAwmJI g0YdH6bZj2PYJONcdZ5QIQELl2MKrKhRtV3YmCfy+B32QiCAOrk1BajXoHZQDLKLPBPH Hpazd/nC69Z6zPoRuku4z+QqYjtWJT8RvYDyRn2cbHQSa7eOkwkRtJ8vwth+WJ641sf8 i7Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zQKiDjoP71FNxadoa0X576EGf59j0KZM9aNu/7L6Fz4=; b=KUpMfc+XqJaq2v7+Iz8JGZKn8mhyNEoF5R6D3/0t8tpTr/fW8oVWabsLxpqOg2YV9Y kd22p6RCOTwON6TleyiKn/L87QMbQeL68z4/CqgCRyXhxS38KdZKzT5gOK7xGhjaWJ7I NEMTL+hvRMxFX+AIDaWNzogxiNIWOhP+qSNFFAk+PSmLhJzAu5tw9gDQK+sK18tO3jJ9 oyaH56xy6k08vwHGtm2ucnLku29KZnvoEUXC6izs9Fxnf53BMYc4lEeSXp0Lm8Bl5vYW cfEFANrDmdTKv6f+CGrm8bbna0EEZIDhbjMMWzVltlNF6U+jPDmtWciTPJMyGbTIJLXm dXcg== X-Gm-Message-State: ANoB5pm5f3B8J2xkhiDBJd1NvdE4XU3gOPLAc2ZDrigbWIWGehBi64cV 5YPBotMuvfbp5NJrkre8w6/W1Uhlsixar9H1bjSpw9VgjA7F/X5f63n4v+XNfm7RLaXN/KFQXtI mL3GC/wnBjm/FhZ0cRIqnnRlLwjwh1rgYiV6C0yuT2vfctccx8rwpEK0= X-Google-Smtp-Source: AA0mqf7CQ6hw5ZWdpHIXPIXksD0oKnoM4dVHvD661QNWM+YV2Lo1nBdwPFcVzckXOzhEnwQyjp4uGFjv0Q== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:1c04:b0:3cf:ecdb:bcb7 with SMTP id j4-20020a05600c1c0400b003cfecdbbcb7mr50768653wms.180.1670003086111; Fri, 02 Dec 2022 09:44:46 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:57 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-13-tabba@google.com> Subject: [RFC PATCH kvmtool v1 12/32] Add a function that allocates aligned memory if specified From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a variant of mmap_anon_or_hugetlbfs() that allocates memory aligned as specified. This function doesn't map or allocate more memory than the requested amount. Signed-off-by: Fuad Tabba --- util/util.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/util/util.c b/util/util.c index 54c2b26..9f83d70 100644 --- a/util/util.c +++ b/util/util.c @@ -120,10 +120,17 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size) return fd; } -/* This function wraps the decision between hugetlbfs map (if requested) or normal mmap */ -void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) +/* + * This function allocates memory aligned to align_sz. + * It also wraps the decision between hugetlbfs (if requested) or normal mmap. + */ +void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, + u64 size, u64 align_sz) { u64 blk_size = 0; + u64 total_map = size + align_sz; + u64 start_off, end_off; + void *addr_map, *addr_align; int fd; /* @@ -143,10 +150,38 @@ void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 si kvm->ram_pagesize = getpagesize(); } + /* Create a mapping with room for alignment without allocating. */ + addr_map = mmap(NULL, total_map, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + if (addr_map == MAP_FAILED) + return MAP_FAILED; + fd = memfd_alloc(size, hugetlbfs_path, blk_size); if (fd < 0) return MAP_FAILED; + /* Map the allocated memory in the fd to the specified alignment. */ + addr_align = (void *)ALIGN((u64)addr_map, align_sz); + if (mmap(addr_align, size, PROT_RW, MAP_PRIVATE | MAP_FIXED, fd, 0) == + MAP_FAILED) { + close(fd); + return MAP_FAILED; + } + + /* Remove the mapping for unused address ranges. */ + start_off = addr_align - addr_map; + if (start_off) + munmap(addr_map, start_off); + + end_off = align_sz - start_off; + if (end_off) + munmap((void *)((u64)addr_align + size), end_off); + kvm->ram_fd = fd; - return mmap(NULL, size, PROT_RW, MAP_PRIVATE, kvm->ram_fd, 0); + return addr_align; +} + +void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size) +{ + return mmap_anon_or_hugetlbfs_align(kvm, hugetlbfs_path, size, 0); } From patchwork Fri Dec 2 17:43:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063052 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F285C4332F for ; Fri, 2 Dec 2022 17:44:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234123AbiLBRo4 (ORCPT ); Fri, 2 Dec 2022 12:44:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234028AbiLBRoz (ORCPT ); Fri, 2 Dec 2022 12:44:55 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F74EDEA6F for ; Fri, 2 Dec 2022 09:44:49 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id d4-20020adfa404000000b002421ca8cb07so1235719wra.2 for ; Fri, 02 Dec 2022 09:44:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=D+6nwI+wYM9c9YZVdbndQ6TxTN9W/+nNFqEhLqsjkJc=; b=mi/f7ytkyjY0Am7KUqq6EOBz7u2kq0MYJvgHN5n/7RzdPsO9rRpOMmvvxYgDdb+xml 5GcRwdnaTOLu3RiCjEQcUkdTMRVKb6pHSMbZg5KosIVzYSSrQM1zo89mx5M20okx6NPK vZHHr9q4Vj6Ui4hKHelMnjUr16KmQRxCgW9sc6PXk8CBSu5GlrtK2iyTdyB/j/m3ewRQ TVYsW/FoGSsPDHkqz4Z9OoAFAk3KGrofrjrC/eLYyFF/wTj63CBFVt8U+fsJy/reSlp5 W53bM+dBq6acCq6haW7jzugTtwzvpcBNACbA0ttPkeJDkgOPK2kY6fmf+QIZ/nlo3f6n 9Rfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D+6nwI+wYM9c9YZVdbndQ6TxTN9W/+nNFqEhLqsjkJc=; b=vcs8WxHEpFBDYcDXpsuC/rxLaX4x1WLmQNpLEMXeqfs9xtc7AyGgbYNZ78bWlFbl1+ M0OM/DvPMHwX7a7m2UsaDMkJBmL9jE78dtRspuWAmynfp8CGSImNQqmeNrSrSauK5iDC sT+fP+pEOAkT78TKpMC5uilC2KMHNa42dJWIegM4kDQtMdXn+RkMQGYc2z3BRoOFkLAh fPMaIkptMJlFwhOTB5xtKMqnGD6zKwiyhawtx80clXYWq4N8VTuw3sUB40hwaQ+/CZTP r+asPilIgFoTWlBGEHZxn3rcRL7dNh+mmcm44601LGZS7wyfATv57ZKAljmIEo0GQ2vk bcuA== X-Gm-Message-State: ANoB5pmfULDzPUxYGBoiGDTNxO6S2zQqQuy1JDjIW1uIqnFiTzkNCpNF mWXEWprggObI+NqVkKyogv4WwTzoR45CGtDcuiXbKJUXmYSRyf8oW385kw8uxuRdWdO6Wz0hIcp t3q5raIuKqtaA8/ZdOxiUWPSWp9a8lxFCBjXA/wr/g1JldMXpSvfV3Kw= X-Google-Smtp-Source: AA0mqf625dyZl+co1cKdjTkPOEIOLFPXesGpLXi0oNCmuwnM7mLiiuLNZWbXGoKwhYrw5BvWSQfn2j2ohw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:adf:f6ca:0:b0:241:dc52:5556 with SMTP id y10-20020adff6ca000000b00241dc525556mr35846345wrp.291.1670003088151; Fri, 02 Dec 2022 09:44:48 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:58 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-14-tabba@google.com> Subject: [RFC PATCH kvmtool v1 13/32] Use new function to align memory From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Use the new mmap_anon_or_hugetlbfs_align() to allocate memory aligned as needed instead of doing it at the caller while allocating and mapping more than needed. Signed-off-by: Fuad Tabba --- arm/kvm.c | 10 +++++----- riscv/kvm.c | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/arm/kvm.c b/arm/kvm.c index c84983e..0e5bfad 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -37,17 +37,17 @@ void kvm__init_ram(struct kvm *kvm) * 2M trumps 64K, so let's go with that. */ kvm->ram_size = kvm->cfg.ram_size; - kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, + kvm->arch.ram_alloc_size = kvm->ram_size; + kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm, kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + kvm->arch.ram_alloc_size, + SZ_2M); if (kvm->arch.ram_alloc_start == MAP_FAILED) die("Failed to map %lld bytes for guest memory (%d)", kvm->arch.ram_alloc_size, errno); - kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start, - SZ_2M); + kvm->ram_start = kvm->arch.ram_alloc_start; madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, MADV_MERGEABLE); diff --git a/riscv/kvm.c b/riscv/kvm.c index 4d6f5cb..e26b4f0 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -70,17 +70,17 @@ void kvm__arch_init(struct kvm *kvm) * 2M trumps 64K, so let's go with that. */ kvm->ram_size = min(kvm->cfg.ram_size, (u64)RISCV_MAX_MEMORY(kvm)); - kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, + kvm->arch.ram_alloc_size = kvm->ram_size; + kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm, kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); + kvm->arch.ram_alloc_size, + SZ_2M); if (kvm->arch.ram_alloc_start == MAP_FAILED) die("Failed to map %lld bytes for guest memory (%d)", kvm->arch.ram_alloc_size, errno); - kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start, - SZ_2M); + kvm->ram_start = kvm->arch.ram_alloc_start; madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, MADV_MERGEABLE); From patchwork Fri Dec 2 17:43:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4AE33C47088 for ; Fri, 2 Dec 2022 17:45:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234143AbiLBRo7 (ORCPT ); Fri, 2 Dec 2022 12:44:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234028AbiLBRo6 (ORCPT ); Fri, 2 Dec 2022 12:44:58 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA06EDEA63 for ; Fri, 2 Dec 2022 09:44:51 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id b47-20020a05600c4aaf00b003d031aeb1b6so4436826wmp.9 for ; Fri, 02 Dec 2022 09:44:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tb2/i5z2SM49ghSm0/D7B5+7IVHEWxAtSa+JkItmtE8=; b=dxd4OPFsvb86d3Iq1zAP3skHvxX32mkUw3Sv9ToO46aFddL6Q2j8UHOVVqs2DLyLO8 vvSsQsrnmGNfLvOJtC8J/yAEizF/JR/IYli/HV22CrpASLqdwlNf6DZKd4iWonuRCKbj k9B6UACXZ5iwdIV+JcISFA33XvpVQm/hUyWU6xwVx6vJROwi0DTSitCb+COxFp+y+DjE +6ycxKjG8Xd1qIXFFy6wByjPo3H4A+5AoflIIWxdaJhTZtAbxoAd7mwbnUiiduoxz2bL kHKjL2f6VWHgVNMEA+BnFxhZZ9H0ZWpcoGRqRXDg3Dg6WmQWBgDps0ZktzQbB6i8byos neiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tb2/i5z2SM49ghSm0/D7B5+7IVHEWxAtSa+JkItmtE8=; b=xq+0r7+yd5w+QQkTALlwUJTwt2NsA2QPcbaC8VMQmtPGhObXrsVJdOZNTXVLtNsW8t JmRaMwGTamQ3eXpxgEDjUey+H2doUEYq3RyIRQguXOxUdGJq5MvSysdtyq0xEVbIL5sT I0ad65xXhSEWP6fsRkYwV7TJU+VGK7bRwgl8rSbC5RsbLSPHaOYmQxeMsP8hmZzwY60G 9DdU5VB6u/pHw76WH6WB2nZH+2rOHmfQiSamHR8ocurnVs8xnGX9JgF9vOXw8y0OJHqn 7E4K7gwbHcsFrzUu5+vVaUis6i5F76y5DCKesAzIuO0les40TnaZawDRp80SSSjLKY3G jCGA== X-Gm-Message-State: ANoB5plTuJiVieEDZuTayRGCezoPTWz3fypywLGlGYKto3hmPdP9NO38 kOoIR2DOiVqrHo6RP2/UcgSrPeHwejCTNO6Z9HxzHAViF15WwfZjDq6e7JqbJNXIk6vjH5ZNn4N /4SpkCsGdjLqWZCVzT7xRxxb+tCNIMBe5XYvaQIvx+KLc1US8y1sVQpM= X-Google-Smtp-Source: AA0mqf6FrWavDFFYV1M8keBaJIE2TsVugeLr/0Dq4N98C4cBZrsGtGfVj1uM7Num2k2KgPdqZqsIjy1K6g== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:1f1b:b0:3cf:35c8:289f with SMTP id bd27-20020a05600c1f1b00b003cf35c8289fmr45006220wmb.153.1670003090215; Fri, 02 Dec 2022 09:44:50 -0800 (PST) Date: Fri, 2 Dec 2022 17:43:59 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-15-tabba@google.com> Subject: [RFC PATCH kvmtool v1 14/32] Remove struct fields and code used for alignment From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Now that the allocator allocates aligned memory, remove arch-specific code and struct fields used for alignment. No functional change intended. Signed-off-by: Fuad Tabba --- arm/include/arm-common/kvm-arch.h | 7 ------- arm/kvm.c | 31 +++++++++++-------------------- riscv/include/kvm/kvm-arch.h | 7 ------- riscv/kvm.c | 21 +++++++-------------- 4 files changed, 18 insertions(+), 48 deletions(-) diff --git a/arm/include/arm-common/kvm-arch.h b/arm/include/arm-common/kvm-arch.h index b2ae373..654abc9 100644 --- a/arm/include/arm-common/kvm-arch.h +++ b/arm/include/arm-common/kvm-arch.h @@ -96,13 +96,6 @@ static inline bool arm_addr_in_ioport_region(u64 phys_addr) } struct kvm_arch { - /* - * We may have to align the guest memory for virtio, so keep the - * original pointers here for munmap. - */ - void *ram_alloc_start; - u64 ram_alloc_size; - /* * Guest addresses for memory layout. */ diff --git a/arm/kvm.c b/arm/kvm.c index 0e5bfad..770075e 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -27,7 +27,6 @@ bool kvm__arch_cpu_supports_vm(void) void kvm__init_ram(struct kvm *kvm) { u64 phys_start, phys_size; - void *host_mem; int err; /* @@ -37,42 +36,34 @@ void kvm__init_ram(struct kvm *kvm) * 2M trumps 64K, so let's go with that. */ kvm->ram_size = kvm->cfg.ram_size; - kvm->arch.ram_alloc_size = kvm->ram_size; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size, - SZ_2M); + kvm->ram_start = mmap_anon_or_hugetlbfs_align(kvm, + kvm->cfg.hugetlbfs_path, + kvm->ram_size, SZ_2M); - if (kvm->arch.ram_alloc_start == MAP_FAILED) + if (kvm->ram_start == MAP_FAILED) die("Failed to map %lld bytes for guest memory (%d)", - kvm->arch.ram_alloc_size, errno); + kvm->ram_size, errno); - kvm->ram_start = kvm->arch.ram_alloc_start; - - madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, - MADV_MERGEABLE); - - madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, - MADV_HUGEPAGE); + madvise(kvm->ram_start, kvm->ram_size, MADV_MERGEABLE); + madvise(kvm->ram_start, kvm->ram_size, MADV_HUGEPAGE); phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; - host_mem = kvm->ram_start; - err = kvm__register_ram(kvm, phys_start, phys_size, host_mem); + err = kvm__register_ram(kvm, phys_start, phys_size, kvm->ram_start); if (err) die("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]", phys_size, phys_start, err); kvm->arch.memory_guest_start = phys_start; - pr_debug("RAM created at 0x%llx - 0x%llx (host_mem 0x%llx)", - phys_start, phys_start + phys_size - 1, (u64)host_mem); + pr_debug("RAM created at 0x%llx - 0x%llx (host ram_start 0x%llx)", + phys_start, phys_start + phys_size - 1, (u64)kvm->ram_start); } void kvm__arch_delete_ram(struct kvm *kvm) { - munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size); + munmap(kvm->ram_start, kvm->ram_size); } void kvm__arch_read_term(struct kvm *kvm) diff --git a/riscv/include/kvm/kvm-arch.h b/riscv/include/kvm/kvm-arch.h index 1e130f5..5bb7eee 100644 --- a/riscv/include/kvm/kvm-arch.h +++ b/riscv/include/kvm/kvm-arch.h @@ -56,13 +56,6 @@ struct kvm; struct kvm_arch { - /* - * We may have to align the guest memory for virtio, so keep the - * original pointers here for munmap. - */ - void *ram_alloc_start; - u64 ram_alloc_size; - /* * Guest addresses for memory layout. */ diff --git a/riscv/kvm.c b/riscv/kvm.c index e26b4f0..d05b8e4 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -48,7 +48,7 @@ void kvm__init_ram(struct kvm *kvm) void kvm__arch_delete_ram(struct kvm *kvm) { - munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size); + munmap(kvm->ram_start, kvm->ram_size); } void kvm__arch_read_term(struct kvm *kvm) @@ -70,23 +70,16 @@ void kvm__arch_init(struct kvm *kvm) * 2M trumps 64K, so let's go with that. */ kvm->ram_size = min(kvm->cfg.ram_size, (u64)RISCV_MAX_MEMORY(kvm)); - kvm->arch.ram_alloc_size = kvm->ram_size; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs_align(kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size, - SZ_2M); + kvm->ram_start = mmap_anon_or_hugetlbfs_align(kvm, + kvm->cfg.hugetlbfs_path, + kvm->ram_size, SZ_2M); - if (kvm->arch.ram_alloc_start == MAP_FAILED) + if (kvm->ram_start == MAP_FAILED) die("Failed to map %lld bytes for guest memory (%d)", kvm->arch.ram_alloc_size, errno); - kvm->ram_start = kvm->arch.ram_alloc_start; - - madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, - MADV_MERGEABLE); - - madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, - MADV_HUGEPAGE); + madvise(kvm->ram_start, kvm->ram_size, MADV_MERGEABLE); + madvise(kvm->ram_start, kvm->ram_size, MADV_HUGEPAGE); } #define FDT_ALIGN SZ_4M From patchwork Fri Dec 2 17:44:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35A00C4332F for ; Fri, 2 Dec 2022 17:45:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234243AbiLBRpJ (ORCPT ); Fri, 2 Dec 2022 12:45:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234155AbiLBRpD (ORCPT ); Fri, 2 Dec 2022 12:45:03 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9ABDEDD65 for ; Fri, 2 Dec 2022 09:44:53 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id c187-20020a1c35c4000000b003cfee3c91cdso2814986wma.6 for ; Fri, 02 Dec 2022 09:44:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JIsIuRXiWXnXCKwBJDitiOnCfkciTH1HHiRQDJHgesk=; b=lY3D0DG7OYlu/yLytmFiO8bmtzr8j9RfeLmUddAzokAVSmv+7ynAuyoKHx7GcG8t/F 55ZnmvJMFoCE0O0DAA8Axw8QMGxURWTXZsAb7YDYjVjKkZXhnxCe4Fk8ik8VyKqnCvRL L6BNV1DD2lvnBStXHefVhTW4vwgGHseRN4XPWWc2TCRVgZ/AKW+nUTUVKzF0gqdUOIpl GgsiQUH2slfO3tUMNZSJrY31AmOGGLD9oie3FPW1g9QRDikHVR0cBa3AgsL6Ipkr0VdL rwnWmr2s+oCVC8OmEfGr81javC5h99KB6ZQuYwZaFWYZmsxXUhIJ2CznxwRmcL4gczNc hhNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JIsIuRXiWXnXCKwBJDitiOnCfkciTH1HHiRQDJHgesk=; b=IsDH+61WsfwIm4djGEB+6BBAoj881/eRc5sVdlciWHCTlImj6mgl+1a+niARQBdqEb B0uN3e1ULuEmCvEfOIpF/AshZ+rvODtdub0wkiu/5NGM1G1i7YU9dif5ge56pZFwsocM IkCj9bmbdq2ByySBIBkjTzDLZB8X+NwZSVt9cY0xDoUF3RtINt7OshkCWMc6kmdOLE9q ebk1wBWGkh2c34rCHVjFriZq7WVGfREHd/7WOeaqabQ8R/a04exxkr+fl0NQ3pT9Gfxb ijMSG5T2j3In3av5fPzYH90oMOKRhK1cRkCcmHhvhxYzvS7TQUS/Jp8DKsVjuMuzs8y8 pbTw== X-Gm-Message-State: ANoB5pmLl5KDmPSjBj/HG+XlougSH8WhvyZ5/cSm48iEepkglyBGVU9T F4BSiCDmup7/AFxlVUFQWanQvcRmgUKEgSnLUGKwlDxPh7ykReTxPc/QlbsKodQhfc2Ho9nV5wx b55EZfKvIf+ydEjKFP+AzLZlpNPMvt4Ye/Zck8cah1NRtqDNXXrHZxCQ= X-Google-Smtp-Source: AA0mqf43ij/55PqTAKj6ipU15xRnsATW4/+aFXEeVudl+Q4NKkeJLuJLubxlzu1acaQojbKVMfCbZr/kLQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:2145:b0:3d0:920f:f7b7 with SMTP id v5-20020a05600c214500b003d0920ff7b7mr515672wml.22.1670003092186; Fri, 02 Dec 2022 09:44:52 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:00 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-16-tabba@google.com> Subject: [RFC PATCH kvmtool v1 15/32] Replace kvm__arch_delete_ram() with kvm__delete_ram() From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Now that deleting ram is the same across all architectures, no need for arch-specific deletion of ram. No functional change intended. Signed-off-by: Fuad Tabba --- arm/kvm.c | 5 ----- include/kvm/kvm.h | 1 - kvm.c | 13 +++++++++---- mips/kvm.c | 5 ----- powerpc/kvm.c | 5 ----- riscv/kvm.c | 5 ----- x86/kvm.c | 5 ----- 7 files changed, 9 insertions(+), 30 deletions(-) diff --git a/arm/kvm.c b/arm/kvm.c index 770075e..5cceef8 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -61,11 +61,6 @@ void kvm__init_ram(struct kvm *kvm) phys_start, phys_start + phys_size - 1, (u64)kvm->ram_start); } -void kvm__arch_delete_ram(struct kvm *kvm) -{ - munmap(kvm->ram_start, kvm->ram_size); -} - void kvm__arch_read_term(struct kvm *kvm) { serial8250__update_consoles(kvm); diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index d0d519b..f0be524 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -198,7 +198,6 @@ void kvm__arch_validate_cfg(struct kvm *kvm); void kvm__arch_set_cmdline(char *cmdline, bool video); void kvm__arch_init(struct kvm *kvm); u64 kvm__arch_default_ram_address(void); -void kvm__arch_delete_ram(struct kvm *kvm); int kvm__arch_setup_firmware(struct kvm *kvm); int kvm__arch_free_firmware(struct kvm *kvm); bool kvm__arch_cpu_supports_vm(void); diff --git a/kvm.c b/kvm.c index 84a8675..3a3383a 100644 --- a/kvm.c +++ b/kvm.c @@ -169,14 +169,19 @@ struct kvm *kvm__new(void) return kvm; } -int kvm__exit(struct kvm *kvm) +static void kvm__delete_ram(struct kvm *kvm) { - struct kvm_mem_bank *bank, *tmp; - - kvm__arch_delete_ram(kvm); + munmap(kvm->ram_start, kvm->ram_size); if (kvm->ram_fd >= 0) close(kvm->ram_fd); +} + +int kvm__exit(struct kvm *kvm) +{ + struct kvm_mem_bank *bank, *tmp; + + kvm__delete_ram(kvm); list_for_each_entry_safe(bank, tmp, &kvm->mem_banks, list) { list_del(&bank->list); diff --git a/mips/kvm.c b/mips/kvm.c index 0faa03a..0a0d025 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -56,11 +56,6 @@ void kvm__init_ram(struct kvm *kvm) } } -void kvm__arch_delete_ram(struct kvm *kvm) -{ - munmap(kvm->ram_start, kvm->ram_size); -} - void kvm__arch_set_cmdline(char *cmdline, bool video) { diff --git a/powerpc/kvm.c b/powerpc/kvm.c index 7b0d066..8d467e9 100644 --- a/powerpc/kvm.c +++ b/powerpc/kvm.c @@ -148,11 +148,6 @@ void kvm__arch_init(struct kvm *kvm) SPAPR_PCI_IO_WIN_SIZE); } -void kvm__arch_delete_ram(struct kvm *kvm) -{ - munmap(kvm->ram_start, kvm->ram_size); -} - void kvm__irq_trigger(struct kvm *kvm, int irq) { kvm__irq_line(kvm, irq, 1); diff --git a/riscv/kvm.c b/riscv/kvm.c index d05b8e4..4a2a3df 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -46,11 +46,6 @@ void kvm__init_ram(struct kvm *kvm) kvm->arch.memory_guest_start = phys_start; } -void kvm__arch_delete_ram(struct kvm *kvm) -{ - munmap(kvm->ram_start, kvm->ram_size); -} - void kvm__arch_read_term(struct kvm *kvm) { serial8250__update_consoles(kvm); diff --git a/x86/kvm.c b/x86/kvm.c index 328fa75..8d29904 100644 --- a/x86/kvm.c +++ b/x86/kvm.c @@ -177,11 +177,6 @@ void kvm__arch_init(struct kvm *kvm) die_perror("KVM_CREATE_IRQCHIP ioctl"); } -void kvm__arch_delete_ram(struct kvm *kvm) -{ - munmap(kvm->ram_start, kvm->ram_size); -} - void kvm__irq_line(struct kvm *kvm, int irq, int level) { struct kvm_irq_level irq_level; From patchwork Fri Dec 2 17:44:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B4DAC4332F for ; Fri, 2 Dec 2022 17:45:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234178AbiLBRpV (ORCPT ); Fri, 2 Dec 2022 12:45:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234212AbiLBRpJ (ORCPT ); Fri, 2 Dec 2022 12:45:09 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 826D1DEA75 for ; Fri, 2 Dec 2022 09:44:55 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id o10-20020adfa10a000000b00241f603af8dso1254252wro.11 for ; Fri, 02 Dec 2022 09:44:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ru8qbdvAloJLwy+YGAn5IzE203RPNqWWa5fSGCZqBCo=; b=jKvyUlwdpl0YKXAJUrLmjMPBIDPj/ChNCuoXhRm98+hESGUiyCtw4pigFVZXMyU1ju HYuzZJAoDIRGeZfBObvuM/nptO+ml36NAm8a0lVA3cDhWsvAEsvbuxMNIREEo3aAk0Un tJnhvpHGila41E/L1t0JdzzCDBOIdgBJXQxpm9tzXjWIGKpM8wpMkJsvZAR3MxNpF2Jz Hp49Px4I8eFbQEUi78E6n6bin4JIU4HszFBQTQ86xwNjw0UWooJsQy3HdLTHZ+WpyVDi TUemRzDJzso8O6K6mRfxjWURbFo2Th4C2rWliFavcaLWQbDu+Vpovy0cFRzQynpdSKmv qoug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ru8qbdvAloJLwy+YGAn5IzE203RPNqWWa5fSGCZqBCo=; b=AIL7+haUaQHPrVy/gwMsajqJ1NOVRaiG8LdlSYbHLdKoNmjXlcjtsnTCdiGVDJz8r1 a0a6iRb/S0ZefzNfkw4wrupBJf40N3h5JzRODApBz1x6kgjigvK8OQN6Jo109/RmnMag hDSJJo7y/THXZxMfs1RqwXcI2oGCpodGtJYc3m2BamaQjDcuUi/uo/6XmUKxbJhmDLrj 05S6+UC/f6OM8CCGpTIXzM1MKwwq5mQUFESnURUHwaeRjfRqmVl4rvu/PVqBFbwcqXmA 4nq5dZtez2yEGng4IZscCOqRbndYha720UjqaZN+s/jnJVP567U309jQA5+OwJMCJT1v CSHg== X-Gm-Message-State: ANoB5pkaq81Pvdh2SZDcfgq+fVpoAPa15aXLzUqpFMb2lzDYWFay1iV2 xC/9v2V3bDRsdLUqx3luCGOiepsnnLvQH5HOMmnhFFmqFfWq6j9zFNo1Oke0bNlT4ef/su80GNR aO5RbeBFLZzcSb+qkui/w51s7dssLUdhxS5OE5sTFUcjoQEMUNop0GLU= X-Google-Smtp-Source: AA0mqf6PTb77JiquoUva7B0ZaY1i+B88dlD/YT6FogNOrGUtp+XZqAHOL51r4orhcL94vmp0VCXVWC2oNg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6000:1e0c:b0:242:3fa4:820d with SMTP id bj12-20020a0560001e0c00b002423fa4820dmr3875680wrb.564.1670003094138; Fri, 02 Dec 2022 09:44:54 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:01 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-17-tabba@google.com> Subject: [RFC PATCH kvmtool v1 16/32] Remove no-longer used macro From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Not needed anymore since we're not doing anonymous allocation. No functional change intended. Signed-off-by: Fuad Tabba --- include/kvm/util.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/kvm/util.h b/include/kvm/util.h index 74713d9..79275ed 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -35,7 +35,6 @@ extern bool do_debug_print; #define PROT_RW (PROT_READ|PROT_WRITE) -#define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE) extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); extern void die_perror(const char *s) NORETURN; From patchwork Fri Dec 2 17:44:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063056 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C61DC4332F for ; Fri, 2 Dec 2022 17:45:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234313AbiLBRph (ORCPT ); Fri, 2 Dec 2022 12:45:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234256AbiLBRpY (ORCPT ); Fri, 2 Dec 2022 12:45:24 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFEDEEDD67 for ; Fri, 2 Dec 2022 09:45:07 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id o8-20020adfba08000000b00241e80f08e0so1242658wrg.12 for ; Fri, 02 Dec 2022 09:45:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+AbD6eW9Ewpp9J8IQbS9YAmB+Rhm6ksZAMygNcR9t8c=; b=EqAA7GvZjA3UQUsiM1sFAaY3+Lc9qdPmYo9pQT/hppuVBWNJBekOUoeh48RG1U/2r7 ISk0VDTjQs+yss+qj/Jn8rwoOKVbZk1gFucEoh8GvnI+SqWzwsCJcjCY6rq/l3xzw5MA VxgU41jzXvPvvqXgqxJDGH5Ja99YnKB/iunHg/oo05W1nHb2+tSBd1fAFQQIuAGbjyTE qy0Xvp4rwnqkB8WU3YBTa+jcrZjOI0lYd6WiHZ6ljZqJEm+wvc+6FfjXBrlPZFhyrJNR ytMun4VrlAGN3/AFA9tTHwk4w4rB8z4QbYa65kxiqua0RkmQCSbGxU03STJXIcK+1GQ9 6/dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+AbD6eW9Ewpp9J8IQbS9YAmB+Rhm6ksZAMygNcR9t8c=; b=fWGd1LLpsETBOXGKsJHDNpl8bPb2QsrcnkfimdgGBkH+qmrMlaCaK7ew3az+ruP6kQ 20TzZQB/Q1p5HK6DXNApiagbGdZRyLdLkMfPAvndHzCNFuZODQmyupSlzb37lQaTl59m DjNNT7pNuggKFqQz2AGphpdKh4mN2hDTCvDZZmL2gMrgHdlh/Ik4X0iV5RNIPv1IJjnB WhDCZzencIMGvmy3bI8HvYWbUkmJhDw+crC5Gr3Ks3abhdHtM7sJ1ULhx05tz646U+NC bv8gd/lQDNfMS8vWcK1/ZR6rJe8/i4PibTnTRfROny4VF8PUIp68VMlhIvh+/P1YDM0h fWnw== X-Gm-Message-State: ANoB5pkoTy7kZL4/eoc5X5MjfwNk/u8P+MXWQmnPDydNPemlHRZhyx9k pwxVFlWBbrvuNVbE/KjiNP/OhrGitr5m2SnwowoNTTFUZKYeinv4GmVuS/aqERqNhDLg9ntrxz8 MHKQl9+urgY6GkXMm3IdPUzGBJeCg28UvfS7AD+EEvqnfcgqBLsDxlas= X-Google-Smtp-Source: AA0mqf7R2VP/tC7C+nsn7l94H4EkFZm2cjbL5Ywl8fXgv8dPRI1bKyYJ/EH7XGxzZBSHGt2EhIYJ7C3tRg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6000:1e0c:b0:242:3fa4:820d with SMTP id bj12-20020a0560001e0c00b002423fa4820dmr3875733wrb.564.1670003096056; Fri, 02 Dec 2022 09:44:56 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:02 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-18-tabba@google.com> Subject: [RFC PATCH kvmtool v1 17/32] Factor out set_user_memory_region code From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This is common code, and will be reused in the future when setting memory regions using file descriptors. No functional change intended. Signed-off-by: Fuad Tabba --- kvm.c | 53 ++++++++++++++++++++++++++++------------------------- util/util.c | 2 +- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/kvm.c b/kvm.c index 3a3383a..c71646f 100644 --- a/kvm.c +++ b/kvm.c @@ -193,10 +193,30 @@ int kvm__exit(struct kvm *kvm) } core_exit(kvm__exit); + +static int set_user_memory_region(int vm_fd, u32 slot, u32 flags, + u64 guest_phys, u64 size, + u64 userspace_addr) +{ + int ret = 0; + struct kvm_userspace_memory_region mem = { + .slot = slot, + .flags = flags, + .guest_phys_addr = guest_phys, + .memory_size = size, + .userspace_addr = (unsigned long)userspace_addr, + }; + + ret = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, &mem); + if (ret < 0) + ret = -errno; + + return ret; +} + int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr) { - struct kvm_userspace_memory_region mem; struct kvm_mem_bank *bank; int ret; @@ -220,18 +240,10 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - mem = (struct kvm_userspace_memory_region) { - .slot = bank->slot, - .guest_phys_addr = guest_phys, - .memory_size = 0, - .userspace_addr = (unsigned long)userspace_addr, - }; - - ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem); - if (ret < 0) { - ret = -errno; + ret = set_user_memory_region(kvm->vm_fd, bank->slot, 0, guest_phys, 0, + (u64) userspace_addr); + if (ret < 0) goto out; - } list_del(&bank->list); free(bank); @@ -246,7 +258,6 @@ out: int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr, enum kvm_mem_type type) { - struct kvm_userspace_memory_region mem; struct kvm_mem_bank *merged = NULL; struct kvm_mem_bank *bank; struct list_head *prev_entry; @@ -327,19 +338,11 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |= KVM_MEM_READONLY; if (type != KVM_MEM_TYPE_RESERVED) { - mem = (struct kvm_userspace_memory_region) { - .slot = slot, - .flags = flags, - .guest_phys_addr = guest_phys, - .memory_size = size, - .userspace_addr = (unsigned long)userspace_addr, - }; - - ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem); - if (ret < 0) { - ret = -errno; + ret = set_user_memory_region(kvm->vm_fd, slot, flags, + guest_phys, size, + (u64) userspace_addr); + if (ret < 0) goto out; - } } list_add(&bank->list, prev_entry); diff --git a/util/util.c b/util/util.c index 9f83d70..1f2e1a6 100644 --- a/util/util.c +++ b/util/util.c @@ -141,7 +141,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, blk_size = get_hugepage_blk_size(hugetlbfs_path); if (blk_size == 0 || blk_size > size) { - die("Can't use hugetlbfs pagesize %lld for mem size %lld\n", + die("Can't use hugetlbfs pagesize %lld for mem size %lld", (unsigned long long)blk_size, (unsigned long long)size); } From patchwork Fri Dec 2 17:44:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063057 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED9D0C47088 for ; Fri, 2 Dec 2022 17:45:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234334AbiLBRpj (ORCPT ); Fri, 2 Dec 2022 12:45:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234265AbiLBRp1 (ORCPT ); Fri, 2 Dec 2022 12:45:27 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B8EBEFD20 for ; Fri, 2 Dec 2022 09:45:10 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id ay40-20020a05600c1e2800b003cf8aa16377so2817073wmb.7 for ; Fri, 02 Dec 2022 09:45:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JuW98NK3szawI9EWp4s7ai+ga43lAX+Pov5xEYz6c0U=; b=XNIkLOcJfFB16149vMnzWKOCFBWvKDgknVxHjgKpbHllb1p94gyN2NwKGeCOfyBpMC bcIboAVxJQHjL9bz29cTC5lLHkf3zp9QnSvY5T/9g9uZUak4hV/JJco2No/ZD1RzrfXY Nh1a+/ecv15WLLMP+vYcUO9ryI0feSvCZ2GjjL7fdNAu+5WmrTnnq8rikHlXW+HtyxBu Snx7cLsbvrkLtDuKqIZzVZaCs3JokSaCScvziwqOtxChRjSiPw9M2jtyk66osBXyhoV7 RnkNBwJY+2ZuLz5nndkxkZeH+pXgJDYKRE2tpl5Y85vCJEU0hkY+ji9VhlRvggf2dN1S VeWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JuW98NK3szawI9EWp4s7ai+ga43lAX+Pov5xEYz6c0U=; b=TAA7rT8X/bU7yF/XaNfp8uDNLe4F7cYy2jiqixmlNjx50IPDjdaOIzNP9MHWsLfMI/ SF6byJdAEMwV1Gpykr9Y2YPkCVcOoGMxeFqmwXHA7QmhqE6X3mlSwSNuDsTj7MqiHb1K epqyZ8kYmr/E+7hbS9jTA46jU783i3wYsyAfKo6A2NucGZwlk48uW1LsEY9B/T2Rtgj8 hEq86Fs9xxW6lTx9DUz6TjP33p2enUNsZNoUu+0UzHYnapVcL89M8/VyYNu7nwVfzTLQ p3/IHuW8jRICHRbTxsJKiUBS5q4OKgK4ll+3AO+aDdGnH+Wl42M/AiZAusD49vrErowA Kumw== X-Gm-Message-State: ANoB5pnnD8FNW/qq+cv1XKRwws/txImhozp7STSeGmn42u2MOqEzv3pI MvHVuZKKPXqvIAn0dvTqfmeM3GEvRjdNO3GvHbnVUMMcEWSyVnJaOeIJ8pFVOb2QRvV4rIUwKW4 Rl8DqoW+IfzZlRzaIFLyueDsbWOxEiOkrlPZglOP6Yh+Wswa5qTxLo40= X-Google-Smtp-Source: AA0mqf7Im89Ehl+ovCJZqxkqbqa06pTnHR9DJ2DgfayflqV0O+zF/70eu4SECFxNVoJZZP3YYFrb9xtgVw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:adf:f7c5:0:b0:242:9e8:84b8 with SMTP id a5-20020adff7c5000000b0024209e884b8mr20798438wrq.25.1670003097943; Fri, 02 Dec 2022 09:44:57 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:03 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-19-tabba@google.com> Subject: [RFC PATCH kvmtool v1 18/32] Pass the memory file descriptor and offset when registering ram From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Since the memory file descriptor is the canonical reference to guest memory, pass that and the offset when registering guest memory. Future fd-based kvm proposals might even not require a userspace address [*]. No functional change intended. Signed-off-by: Fuad Tabba [*] https://lore.kernel.org/all/20221202061347.1070246-1-chao.p.peng@linux.intel.com/ --- arm/aarch64/pvtime.c | 2 +- arm/kvm.c | 3 ++- hw/cfi_flash.c | 4 +++- hw/vesa.c | 2 +- include/kvm/kvm.h | 17 +++++++++-------- kvm.c | 3 ++- mips/kvm.c | 6 +++--- powerpc/kvm.c | 2 +- riscv/kvm.c | 2 +- vfio/core.c | 3 ++- x86/kvm.c | 6 +++--- 11 files changed, 28 insertions(+), 22 deletions(-) diff --git a/arm/aarch64/pvtime.c b/arm/aarch64/pvtime.c index a7ba03e..9b06ee4 100644 --- a/arm/aarch64/pvtime.c +++ b/arm/aarch64/pvtime.c @@ -28,7 +28,7 @@ static int pvtime__alloc_region(struct kvm *kvm) } ret = kvm__register_ram(kvm, ARM_PVTIME_BASE, - ARM_PVTIME_SIZE, mem); + ARM_PVTIME_SIZE, mem, mem_fd, 0); if (ret) { munmap(mem, ARM_PVTIME_SIZE); close(mem_fd); diff --git a/arm/kvm.c b/arm/kvm.c index 5cceef8..8772a55 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -50,7 +50,8 @@ void kvm__init_ram(struct kvm *kvm) phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; - err = kvm__register_ram(kvm, phys_start, phys_size, kvm->ram_start); + err = kvm__register_ram(kvm, phys_start, phys_size, kvm->ram_start, + kvm->ram_fd, 0); if (err) die("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]", phys_size, phys_start, err); diff --git a/hw/cfi_flash.c b/hw/cfi_flash.c index 7faecdf..92a6567 100644 --- a/hw/cfi_flash.c +++ b/hw/cfi_flash.c @@ -131,6 +131,7 @@ struct cfi_flash_device { u32 size; void *flash_memory; + int flash_fd; u8 program_buffer[PROGRAM_BUFF_SIZE]; unsigned long *lock_bm; u64 block_address; @@ -451,7 +452,7 @@ static int map_flash_memory(struct kvm *kvm, struct cfi_flash_device *sfdev) int ret; ret = kvm__register_mem(kvm, sfdev->base_addr, sfdev->size, - sfdev->flash_memory, + sfdev->flash_memory, sfdev->flash_fd, 0, KVM_MEM_TYPE_RAM | KVM_MEM_TYPE_READONLY); if (!ret) sfdev->is_mapped = true; @@ -583,6 +584,7 @@ static struct cfi_flash_device *create_flash_device_file(struct kvm *kvm, ret = -errno; goto out_free; } + sfdev->flash_fd = fd; sfdev->base_addr = KVM_FLASH_MMIO_BASE; sfdev->state = READY; sfdev->read_mode = READ_ARRAY; diff --git a/hw/vesa.c b/hw/vesa.c index 522ffa3..277d638 100644 --- a/hw/vesa.c +++ b/hw/vesa.c @@ -102,7 +102,7 @@ struct framebuffer *vesa__init(struct kvm *kvm) goto close_memfd; } - r = kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem); + r = kvm__register_dev_mem(kvm, VESA_MEM_ADDR, VESA_MEM_SIZE, mem, mem_fd, 0); if (r < 0) goto unmap_dev; diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index f0be524..33cae9d 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -135,24 +135,25 @@ bool kvm__emulate_io(struct kvm_cpu *vcpu, u16 port, void *data, int direction, bool kvm__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 *data, u32 len, u8 is_write); int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr); int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr, - enum kvm_mem_type type); + int memfd, u64 offset, enum kvm_mem_type type); static inline int kvm__register_ram(struct kvm *kvm, u64 guest_phys, u64 size, - void *userspace_addr) + void *userspace_addr, int memfd, u64 offset) { - return kvm__register_mem(kvm, guest_phys, size, userspace_addr, - KVM_MEM_TYPE_RAM); + return kvm__register_mem(kvm, guest_phys, size, userspace_addr, memfd, + offset, KVM_MEM_TYPE_RAM); } static inline int kvm__register_dev_mem(struct kvm *kvm, u64 guest_phys, - u64 size, void *userspace_addr) + u64 size, void *userspace_addr, + int memfd, u64 offset) { - return kvm__register_mem(kvm, guest_phys, size, userspace_addr, - KVM_MEM_TYPE_DEVICE); + return kvm__register_mem(kvm, guest_phys, size, userspace_addr, memfd, + offset, KVM_MEM_TYPE_DEVICE); } static inline int kvm__reserve_mem(struct kvm *kvm, u64 guest_phys, u64 size) { - return kvm__register_mem(kvm, guest_phys, size, NULL, + return kvm__register_mem(kvm, guest_phys, size, NULL, -1, 0, KVM_MEM_TYPE_RESERVED); } diff --git a/kvm.c b/kvm.c index c71646f..fc0bfc4 100644 --- a/kvm.c +++ b/kvm.c @@ -256,7 +256,8 @@ out: } int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, - void *userspace_addr, enum kvm_mem_type type) + void *userspace_addr, int memfd, u64 offset, + enum kvm_mem_type type) { struct kvm_mem_bank *merged = NULL; struct kvm_mem_bank *bank; diff --git a/mips/kvm.c b/mips/kvm.c index 0a0d025..ebb2b19 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -38,21 +38,21 @@ void kvm__init_ram(struct kvm *kvm) phys_size = kvm->ram_size; host_mem = kvm->ram_start; - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); } else { /* one region for memory that fits below MMIO range */ phys_start = 0; phys_size = KVM_MMIO_START; host_mem = kvm->ram_start; - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); /* one region for rest of memory */ phys_start = KVM_MMIO_START + KVM_MMIO_SIZE; phys_size = kvm->ram_size - KVM_MMIO_START; host_mem = kvm->ram_start + KVM_MMIO_START; - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); } } diff --git a/powerpc/kvm.c b/powerpc/kvm.c index 8d467e9..c36c497 100644 --- a/powerpc/kvm.c +++ b/powerpc/kvm.c @@ -88,7 +88,7 @@ void kvm__init_ram(struct kvm *kvm) "overlaps MMIO!\n", phys_size); - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); } void kvm__arch_set_cmdline(char *cmdline, bool video) diff --git a/riscv/kvm.c b/riscv/kvm.c index 4a2a3df..bb79c5d 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -38,7 +38,7 @@ void kvm__init_ram(struct kvm *kvm) phys_size = kvm->ram_size; host_mem = kvm->ram_start; - err = kvm__register_ram(kvm, phys_start, phys_size, host_mem); + err = kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); if (err) die("Failed to register %lld bytes of memory at physical " "address 0x%llx [err %d]", phys_size, phys_start, err); diff --git a/vfio/core.c b/vfio/core.c index 3ff2c0b..ea189a0 100644 --- a/vfio/core.c +++ b/vfio/core.c @@ -255,7 +255,8 @@ int vfio_map_region(struct kvm *kvm, struct vfio_device *vdev, region->host_addr = base; ret = kvm__register_dev_mem(kvm, region->guest_phys_addr, map_size, - region->host_addr); + region->host_addr, vdev->fd, + region->info.offset); if (ret) { vfio_dev_err(vdev, "failed to register region with KVM"); return ret; diff --git a/x86/kvm.c b/x86/kvm.c index 8d29904..cee82d3 100644 --- a/x86/kvm.c +++ b/x86/kvm.c @@ -107,7 +107,7 @@ void kvm__init_ram(struct kvm *kvm) phys_size = kvm->ram_size; host_mem = kvm->ram_start; - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); } else { /* First RAM range from zero to the PCI gap: */ @@ -115,7 +115,7 @@ void kvm__init_ram(struct kvm *kvm) phys_size = KVM_32BIT_GAP_START; host_mem = kvm->ram_start; - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); /* Second RAM range from 4GB to the end of RAM: */ @@ -123,7 +123,7 @@ void kvm__init_ram(struct kvm *kvm) phys_size = kvm->ram_size - phys_start; host_mem = kvm->ram_start + phys_start; - kvm__register_ram(kvm, phys_start, phys_size, host_mem); + kvm__register_ram(kvm, phys_start, phys_size, host_mem, kvm->ram_fd, 0); } } From patchwork Fri Dec 2 17:44:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063058 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FD43C4332F for ; Fri, 2 Dec 2022 17:45:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234195AbiLBRpl (ORCPT ); Fri, 2 Dec 2022 12:45:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234266AbiLBRp1 (ORCPT ); Fri, 2 Dec 2022 12:45:27 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 923B6EFD2A for ; Fri, 2 Dec 2022 09:45:11 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id c26-20020a05600c0ada00b003d07fd2473bso1955411wmr.9 for ; Fri, 02 Dec 2022 09:45:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4+qWJurAOH4Q75h2TLkoqQeRdLRNZJQrceNDqoL1TZ8=; b=BXEnd9RLsRVLBMzRUYnEpE1fU3R++U4Nu+XsV23SMEn55IEZroaqj0pjxMGzdvE8pp 9QdMSuHureSfZPU+UAEluTeFHQO+1lQ0uV8HfeU4z8f0NbraVIUOBbmdRU6+1tbmU4Y7 ZzGqbWFTZzwxvkF+iUz6OQvAVu3fu4vcIG2sbbwekDFHB1AA3eePrA56nRN6Znk9oshF lImYGS/YTagTmjxgb/yfWNEX1OsODL7ue1bQh8jFPqIUVjky66+YoDZ2pHL/mQYUa6jV wLAjB8yWXLRFOK2VDv2GqQNoyXtE3IVPcDVwE6hqOLlGs/nXGDO+F1csgbBsDT6VZedO ORmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4+qWJurAOH4Q75h2TLkoqQeRdLRNZJQrceNDqoL1TZ8=; b=Wi6CqRJnW+bmwVULiemMKRgbEeUcGeaZTfkF7W5U+QF6LpzN1Lx+eG4KE9G4aIqO6s F57C0xoIKcSTmBl0WbPul3QtR3e+ehCx5a0vRx7jr2F9q0aIuQq8LlX24czEWBJHlquy 22frYvZijjmb4A2B099UHAW23UYJobS17sLlWJoa1FWPsACAaE8LOIG+Nmq/eRm3st6T J5fTdn1ij4fGQYdRkSFaEfdkfhrQct+LNR/+qPfSqAAHOx7rE7dLDqWgUrWRsH+aVK7O Ri+BaIl0vrxTVOxiJMscsobDedslBKyz4BtcouwNrEZSQTVseNC622a119UrA/W1yfEA a2fw== X-Gm-Message-State: ANoB5pmRHBzfWWNLNPI1iV+atwnrx2ip29nAVSZnvjH6CsRpO1g5SDS6 hQisKhQEb9x4vsiXTB+u7BVFIULJSv4k+LWElSj/kJPDw9PUusYEa/fAiRbKZxFOF9habPpChMK 2uFmdfuXrnwtRLqlSeErvQXlJgI6cVzfuslvaxtSoZpZxgteozDvWqCg= X-Google-Smtp-Source: AA0mqf50EftSpucIJunkyF9cz3Sg2D2WWnpg9pQZK8pDqLbmuGpyZ3B7yOCEVMCIHt6L4HzCnyQGn3qvsA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:1c9f:b0:3b4:cd96:1748 with SMTP id k31-20020a05600c1c9f00b003b4cd961748mr204711wms.2.1670003100209; Fri, 02 Dec 2022 09:45:00 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:04 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-20-tabba@google.com> Subject: [RFC PATCH kvmtool v1 19/32] Add memfd_restricted system call From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This enables building kvmtool on a host that doesn't have the kernel with the new extensions yet. No functional change intended. Signed-off-by: Fuad Tabba --- util/util.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/util/util.c b/util/util.c index 1f2e1a6..1424815 100644 --- a/util/util.c +++ b/util/util.c @@ -10,6 +10,17 @@ #include #include #include +#include + +#ifndef __NR_memfd_restricted +#define __NR_memfd_restricted 451 +__SYSCALL(__NR_memfd_restricted, sys_memfd_restricted) +#endif + +static inline int memfd_restricted(unsigned int flags) +{ + return syscall(__NR_memfd_restricted, flags); +} static void report(const char *prefix, const char *err, va_list params) { From patchwork Fri Dec 2 17:44:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063059 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59AA3C4332F for ; Fri, 2 Dec 2022 17:45:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234291AbiLBRpn (ORCPT ); Fri, 2 Dec 2022 12:45:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234276AbiLBRp2 (ORCPT ); Fri, 2 Dec 2022 12:45:28 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E2FCEFD30 for ; Fri, 2 Dec 2022 09:45:13 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-3dddef6adb6so41118627b3.11 for ; Fri, 02 Dec 2022 09:45:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=beB34PxrwokOKRwQ8xX4T+xnA3Uimq2Xl8QpEqhqffA=; b=o/OjwIVyALbfqTrJjPTPr8HceThCvm7SjfCFEzPLNyJJCJPsa3QcZ2DImnmBkrZCW4 7XDL+CaXI6VgRa5YAh+f29lE2McM4H3vX3I+4ynv8jjgrY+D7dtMiJ1aNs70pC6Ab1RT JBBegRulixaGpkMAsIV1FTj9RoYrttZHyBUOlY4wuhQnjmb7fCLR7gK9jjw3y/qsnZ2Y VKRTrnZfONqx2StzOk/PMG2BNqOy9OjLXqBrlcqFDuuMGgaqLfx6axGGB0D58vil6aeD evg7fowe0LVlDotyWmoajqy7gwJUikq8/nCGQFbGVxhl9nLuZdvasALvONSB30WlZXvC sBLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=beB34PxrwokOKRwQ8xX4T+xnA3Uimq2Xl8QpEqhqffA=; b=71ddygpIxAttG0rexOtlxz3wo8I2TAakUSmrK4GQhX5UsMWCNf+dghrD0eFZMo5DlB SDz31tGXTPoKCMizPas+5OGIqPYTtyFZI+TZcjjEIW9BuKWqIPjuu5Dh+fzvzxAb7nYu 8hV1ofzvJwKC+yfpd6nuHUNomfpk5t4eVBonypG/D1X1Gfs+dlvNxX+r+YVGReAqILiM Gb12+UvXYws/mA1MCLO+YOb3yOkCmEXb7TrtuzSf+wdbWt90oYYJP6k6S0uVL16WIo8e rJdtYW2nZJZDA6kjUd8WGEpwgj2mkbW+YLNBLzitJgvEv+E9IQtUz+EPCL/lpUa/tkxU FsFw== X-Gm-Message-State: ANoB5pkDN8MVxsCrI+aVO4n5x6dCx91tzK34XT+A0hEHuQlchlKEZdjF euTPdN82fYPds9GIl5O58qFqjVDmj4/dZMh9DeS0m0ZEhlB+9sLEMK1jG687pUC/CKextBIDg3D eLR66bCAwA9hREdIU54ChWMT7643FbyudWnms6MOKbfx17WTnjhm1Y3w= X-Google-Smtp-Source: AA0mqf7i54GXu9DUwt9r49UbWUKugAzuHriqgSQUqIFDvtyFnVoQlgcnPMdFw+wTcw5d7m8LJcUrWKeyBw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a81:715:0:b0:369:edd9:6c8b with SMTP id 21-20020a810715000000b00369edd96c8bmr576329ywh.452.1670003102340; Fri, 02 Dec 2022 09:45:02 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:05 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-21-tabba@google.com> Subject: [RFC PATCH kvmtool v1 20/32] Add kvm linux headers and structure extensions for restricted_fd From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This enables building kvmtool on a host that doesn't have the kernel with the new extensions yet. No functional change intended. Signed-off-by: Fuad Tabba --- include/linux/kvm.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/linux/kvm.h b/include/linux/kvm.h index 0d5d441..1d35153 100644 --- a/include/linux/kvm.h +++ b/include/linux/kvm.h @@ -103,6 +103,14 @@ struct kvm_userspace_memory_region { __u64 userspace_addr; /* start of the userspace allocated memory */ }; +struct kvm_userspace_memory_region_ext { + struct kvm_userspace_memory_region region; + __u64 restricted_offset; + __u32 restricted_fd; + __u32 pad1; + __u64 pad2[14]; +}; + /* * The bit 0 ~ bit 15 of kvm_memory_region::flags are visible for userspace, * other bits are reserved for kvm internal use which are defined in @@ -110,6 +118,7 @@ struct kvm_userspace_memory_region { */ #define KVM_MEM_LOG_DIRTY_PAGES (1UL << 0) #define KVM_MEM_READONLY (1UL << 1) +#define KVM_MEM_PRIVATE (1UL << 2) /* for KVM_IRQ_LINE */ struct kvm_irq_level { @@ -272,6 +281,7 @@ struct kvm_xen_exit { #define KVM_EXIT_RISCV_SBI 35 #define KVM_EXIT_RISCV_CSR 36 #define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_MEMORY_FAULT 38 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -510,6 +520,14 @@ struct kvm_run { #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) __u32 flags; } notify; + /* KVM_EXIT_MEMORY_FAULT */ + struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1 << 0) + __u32 flags; + __u32 padding; + __u64 gpa; + __u64 size; + } memory; /* Fix the size of the union. */ char padding[256]; }; @@ -1178,6 +1196,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_ZPCI_OP 221 #define KVM_CAP_S390_CPU_TOPOLOGY 222 #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223 +#define KVM_CAP_PRIVATE_MEM 224 #ifdef KVM_CAP_IRQ_ROUTING From patchwork Fri Dec 2 17:44:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063060 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72FAEC4332F for ; Fri, 2 Dec 2022 17:45:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234372AbiLBRpq (ORCPT ); Fri, 2 Dec 2022 12:45:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234298AbiLBRp2 (ORCPT ); Fri, 2 Dec 2022 12:45:28 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C59DEFD3A for ; Fri, 2 Dec 2022 09:45:14 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id s24-20020a7bc398000000b003d087485a9aso950352wmj.1 for ; Fri, 02 Dec 2022 09:45:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1JhQFmKC0trfMj3OYJRD3fE4sfKhFU5srepZrR6R1LA=; b=OGIdvxnj2RZt51XNqSbWS7t5fAHAqWtMKWXV/YtKDXlYfLuRJEFdiJnABW2Gso5oOS 7yRInVbZ8Uv2PW+z3iSaYxlGA6rYbxH3VLrdHRDwL6JOGWCiQGXTfJtcYgfViVlGK60r 7oe3RZG0wFfvuCWvdxG7V34m3+pt3ka95Zr/XB+ySGymC0/seh7oyusMl4j0XCwgb+JF iEzV+GQF9Vwh9VAC6KGcvOJjRFn0i8ADPPjtonhPrjwK7zc2irTsEKKPZFEc/CM9GDJq 4K00ICeazPb+0ByTSgXEwr9Qtm6bIznfvWtD+DlggttmKekRUofEhM367P5Pal3V630P RSWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1JhQFmKC0trfMj3OYJRD3fE4sfKhFU5srepZrR6R1LA=; b=fAw/hhfLj9TfLyqclFF7icPSRPZKlPsTuhi0ZV5MAX30EyXt68pOoWip42uLuFYZhL zO4kwtW26fJ/Xd3KSYJVipdTMcxk6VihINQpR1bfd/90gMMljsVvlqDWHWJpV24Wcn1b iF+kAcYDVNuqWX8/FkTlZaGorXbkiQ9okTsFoa5xd7mwn9GIOq+wR5QHZe3D95htNM+I PRqpYjVf/eeaD6fM4MGL57QfohtC3LzwL7svTW8xK18HI6vZxneMYPlawGEJbGWpKeH5 Sz4aaQyEKJSfATHqzCgTKQtsRfb0VAw7ml52rp7ivlvpNQ8f9cwDKGYPJCwlxt0h7Q3B Hh3g== X-Gm-Message-State: ANoB5pm3Rx7Sq/oIh+Ce9lEMTJ/ZDzdaIZ0l1XN3yeUu2tsfOGs+GzAA 5tvteJ0VrW/ZQbsZy6JEASp9DhlebpCB5mijiSBT0ca0BV2RcV2ckUz2TCgs1Ex5pLxV5h6fbiY zsdHE4dYATGLKCIPW0pMBcqgfP+WUkdtYaEua3c88WK7/RFZCLACTxsc= X-Google-Smtp-Source: AA0mqf6SdCgGxNMSUKjDcZ3Pyx0Rvw6lzLFadPcCmIoTZD3xODT7kWVEvwNp99Eq+mP8hnzO0geg4Xa7MQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6000:4a:b0:242:2db9:df6e with SMTP id k10-20020a056000004a00b002422db9df6emr8279492wrx.480.1670003104425; Fri, 02 Dec 2022 09:45:04 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:06 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-22-tabba@google.com> Subject: [RFC PATCH kvmtool v1 21/32] Add option for enabling restricted memory for guests From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Currently this way for testing only. When the option restricted_mem is set, the guest will use the new restricted memory extensions. This is done this way for now to enable testing and debugging. In the future, pKVM will require that all its guest use restricted memory, so instead of a flag, the intention is for the final version of this patch series to rely on KVM_CAP_PRIVATE_MEM and fail if that capability isn't supported. Signed-off-by: Fuad Tabba --- arm/kvm.c | 5 +++++ builtin-run.c | 2 ++ include/kvm/kvm-config.h | 1 + 3 files changed, 8 insertions(+) diff --git a/arm/kvm.c b/arm/kvm.c index 8772a55..094fbe4 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -74,6 +74,11 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) void kvm__arch_init(struct kvm *kvm) { + if (kvm->cfg.restricted_mem && + !kvm__supports_extension(kvm, KVM_CAP_PRIVATE_MEM)) { + die("Guest restricted memory capability not supported."); + } + /* Create the virtual GIC. */ if (gic__create(kvm, kvm->cfg.arch.irqchip)) die("Failed to create virtual GIC"); diff --git a/builtin-run.c b/builtin-run.c index bb7e6e8..4642bc4 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -202,6 +202,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) "Hugetlbfs path"), \ OPT_BOOLEAN('\0', "virtio-legacy", &(cfg)->virtio_legacy, \ "Use legacy virtio transport"), \ + OPT_BOOLEAN('\0', "restricted_mem", &(cfg)->restricted_mem, \ + "Use restricted memory for guests"), \ \ OPT_GROUP("Kernel options:"), \ OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel", \ diff --git a/include/kvm/kvm-config.h b/include/kvm/kvm-config.h index 368e6c7..ea5f3ea 100644 --- a/include/kvm/kvm-config.h +++ b/include/kvm/kvm-config.h @@ -65,6 +65,7 @@ struct kvm_config { bool ioport_debug; bool mmio_debug; bool virtio_legacy; + bool restricted_mem; }; #endif From patchwork Fri Dec 2 17:44:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063062 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25BADC4332F for ; Fri, 2 Dec 2022 17:45:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234374AbiLBRpw (ORCPT ); Fri, 2 Dec 2022 12:45:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234174AbiLBRpc (ORCPT ); Fri, 2 Dec 2022 12:45:32 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 465DCE1187 for ; Fri, 2 Dec 2022 09:45:19 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id 6-20020a1c0206000000b003d082ecf13cso1472547wmc.3 for ; Fri, 02 Dec 2022 09:45:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QdoCj1rXjeYFBHU/71+LGjI2Pw5KPr0Z0Mp1yJyEH1U=; b=nw+A+Zto29NK4MH+ZRrCcHGW0bmlkqhVbApKNOsLf5Wxd1R16e4ooLsAOljrvyq/Xv NzB54iGsvo8dHUrOj+UZEYm/XemLehZIp52YjOnOnSqvZeWxJnXMqZc1Bo6DLbtRx0Ix IYowBkYWo0vc4W6+HGxtDHEkwaoDY9XM9h9ZAETzuNFGjbPhO7M/VPqfMtULbsTe29WM lqsts+Ok4Tq5ooI3RTwTwA5qsWx/VENMMQRvTCAqr8Nm8hGcdcDNCofo5vbg4Ao7NG/8 c3CEK/XMeQWbv9zLK212XOL9sSoqHL6s466ih0U9DQVjhZ6P/SVl+jDUpaeEZbBvlsYy mj0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QdoCj1rXjeYFBHU/71+LGjI2Pw5KPr0Z0Mp1yJyEH1U=; b=xtmGus3y2NB5EJXDq5LIaxLtix+9NlO6TVQWR7iHo7CyMpdn2WSo+63UkNjOmj1DNZ 2Cs+iX0yqrHAhLTbqM5vYdQxA70A6yU/9rGSyp0FFOlNjzWdT+2QH8ao41ilmm2hTmco eaj5M6g8ujz1qbphAY44HZIORCnnLyTE22IVShu0632RWeRsONxM3FHh43xJSkEJLimI hK9TD0QqHtzocSIlT6CzWaQehOYund+xKldk0S1ZfohaiLSJ/CjI1mfspIj3EwRWR4jR V5uw+0O2tiwL4a8QwzDnuXGhJ1u+WLlRx5324f7dVyjBuUn44HXLyKus5WEQG2cImMJg b8Ug== X-Gm-Message-State: ANoB5pkQF9GX0WY/h0Xv5rnB4oYorMjZddXQEw/IuZEcA7M35BCN8ffp izqFsUhwVXbjURY4QlwML2EOjzFAAntEk2Smvxl0pVZZakjWa2phoeQxY1WbvbpupbJwVP0GebD dM0haXEQgkCngCXlcwL61SJaqzc13Up4JrffcmaqrhlAPu4guGY+opDU= X-Google-Smtp-Source: AA0mqf4vIE2Zt55ftcwKznK3RCYnIs6pMePm7ZMmo22ZArU9tSH4UfvFg3QjK82403NACEneGHuunVvCrQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:4e13:b0:3cf:86b8:71f7 with SMTP id b19-20020a05600c4e1300b003cf86b871f7mr58403563wmq.76.1670003106698; Fri, 02 Dec 2022 09:45:06 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:07 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-23-tabba@google.com> Subject: [RFC PATCH kvmtool v1 22/32] Change guest ram mapping from private to shared From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Private mappings don't work with restricted memory since it might be COWed. Signed-off-by: Fuad Tabba --- util/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/util.c b/util/util.c index 1424815..107f34d 100644 --- a/util/util.c +++ b/util/util.c @@ -173,7 +173,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, /* Map the allocated memory in the fd to the specified alignment. */ addr_align = (void *)ALIGN((u64)addr_map, align_sz); - if (mmap(addr_align, size, PROT_RW, MAP_PRIVATE | MAP_FIXED, fd, 0) == + if (mmap(addr_align, size, PROT_RW, MAP_SHARED | MAP_FIXED, fd, 0) == MAP_FAILED) { close(fd); return MAP_FAILED; From patchwork Fri Dec 2 17:44:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063061 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18C92C4332F for ; Fri, 2 Dec 2022 17:45:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234306AbiLBRpu (ORCPT ); Fri, 2 Dec 2022 12:45:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234293AbiLBRpc (ORCPT ); Fri, 2 Dec 2022 12:45:32 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CB12E118B for ; Fri, 2 Dec 2022 09:45:19 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id bg25-20020a05600c3c9900b003cf3ed7e27bso2823404wmb.4 for ; Fri, 02 Dec 2022 09:45:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RcceNNV74mWRvfNnRdRHxXqpLA5rMluXWpmCinfrAO4=; b=dB2jQjNAjKHWVg6i2fnn5DYvoUYPTOPe6g6D5NDlYPmNLCbbdXurDG7l3tBGmRnQZJ MmSpeH1WV+l4j0pXh9UNhpAAjNFD1HCx2tFJt2KGZRr/hVn37Kn3Hn5M1w67mQcUrI7s oNtH20bjZXFBXWxM8V0eisKyqzF8PB3azf32mCa72QZHBqxGsinAgKfqD92GAy0fnjTg pBCDck7PnknriEPt7tg9S7PJfZGMrUnYTe7pyMHZKDub5OtyZCH/6USI5JilBofzXBZP DV2VuCjK84UrMNyyNNI2Sr0IvJZ9zDkmEZHodOsaU4XlqikU2k+7ZnkTC0KpEdJRNVTs Fhkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RcceNNV74mWRvfNnRdRHxXqpLA5rMluXWpmCinfrAO4=; b=5HrQna3JHMf/U0XJSGwueJYfwzeAz6XPS/BBhsYBZDrm55hIqzTIP2tXsTa7d+pbn0 eWdEO6ItRpkNd82ExersYIxu3mn43lfRDdhajvgqyGDUnIDuS8CdMQ2Gbbunnr3cxL9w tBchFUwtjNgHTFlcd1POFjiFrdj+PNzLwpMbFs7JnqZISfyJlHX6weUfPXuALRl3mJgk Pax/hTgo6gYyv280y5uu4myF5bVrYnkui4LcrTaofsQiHYAe/J1GTOECoB325LNRjjdk TiVN1zD721D2IJZopYMYVPORVX+oCEDP71ldc8CPixC048uaQ1rLaDtdvFbV25Tzjx5m HMIg== X-Gm-Message-State: ANoB5pmfzGSVeZvDzpsu5SiB70BJydlfmu7uR6wdwj7CVOvwKRYCo3Mu crOzqupgDCHDD3YBxa/1bM2VOr7ycU0e0Qxt7JS7BnKvxyEfIpWreWe/7aiSaADXuiETyEOCP7f DOh5cpTRIlL9x0tm2yB6U9BLeAEKXEr4bKBxCCQRDWqqkIdE+qnWEXg4= X-Google-Smtp-Source: AA0mqf5FGBvWE56xTYlHasc5VW7hiCKk/l7OhpAKHcUOs/2SBAsfGGs5U5hXFyAYei7anzRyTSsOzDM5Ug== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5d:5251:0:b0:242:39bc:497 with SMTP id k17-20020a5d5251000000b0024239bc0497mr5369114wrc.411.1670003108672; Fri, 02 Dec 2022 09:45:08 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:08 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-24-tabba@google.com> Subject: [RFC PATCH kvmtool v1 23/32] Change pvtime mapping from private to shared From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Private mappings don't work with restricted memory since it might be COWed. Signed-off-by: Fuad Tabba --- arm/aarch64/pvtime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arm/aarch64/pvtime.c b/arm/aarch64/pvtime.c index 9b06ee4..a452938 100644 --- a/arm/aarch64/pvtime.c +++ b/arm/aarch64/pvtime.c @@ -20,7 +20,7 @@ static int pvtime__alloc_region(struct kvm *kvm) if (mem_fd < 0) return -errno; - mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0); + mem = mmap(NULL, ARM_PVTIME_SIZE, PROT_RW, MAP_SHARED, mem_fd, 0); if (mem == MAP_FAILED) { ret = -errno; close(mem_fd); From patchwork Fri Dec 2 17:44:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063063 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2187C4321E for ; Fri, 2 Dec 2022 17:45:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234382AbiLBRp4 (ORCPT ); Fri, 2 Dec 2022 12:45:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234302AbiLBRpd (ORCPT ); Fri, 2 Dec 2022 12:45:33 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4110DDEA73 for ; Fri, 2 Dec 2022 09:45:23 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id j2-20020a05600c1c0200b003cf7397fc9bso2821595wms.5 for ; Fri, 02 Dec 2022 09:45:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qdGLApNIp2m2lMTsqjYlEA803gC0XNxs7/9Tj6tqLSA=; b=bjnibou905nzLPV3mq8fu2cP7YKBPUaCztouvWXecO2/JSqysCL2OXyhgbuYq4XBtn JPR+FJtGZ9/17ShTLfNiVf/UnAW7nWAjG2Euq4Cv92cKuL/MzDmBtAj0pAiVbsxbSgOQ CVjIpY90Od5gqO5nLD3PV6EHhAuuW427ZVf5/HS3eyndN/bAEhzzZYk10wyt8RfOkSzi qsC5Bea1LcFixTQYIqlfjuGDCFBIdvuBVz3lckFvuCRg4ypcfMoAjlVQUMHyQZ9NUo9W bs5sznFeHkl3CSZzr8Cge8ipjugfjTZA29TWpi7JBi8sXN4AbaHr/DXtaCKw3AoJ0W+N umPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qdGLApNIp2m2lMTsqjYlEA803gC0XNxs7/9Tj6tqLSA=; b=0kbS7aGA/GQcZWBbN3wVYoO+SFjR5ztHBA+LMuuFcMufg+gHMoOBoZAHupJPlBzzqR FDjbNqhQAAg8yEbwPufn+/JJwxwRw9OFLaJpYwdp9MQVNS9QbBEkErpLOU2aM1DQ1w6i pUPRdOKa0oPB7KMWBGFZ/XC6ZT/ylY/yGkTFriKuMs4fH36o2qM/65acKjz/x3Rr887v tu4b3rwk2G7Ny+LQULku2pvYyX3NpOkTf/6DQxjTku+rpJ3nrxPpxELNLauxEgK8LKSs J6OxWxkH3TR5Msgx2EUefqinhpoFoaWVmok/ZxnolMUa8TK5Nr2XtmS4OVgLxFrOrsOg M3IA== X-Gm-Message-State: ANoB5pk/uWfD3dQgdxLbvHdKfoEhVI705rXxl7Y/FnXRDq9rOcPTg9kO 44MytczrlQTV4KwuP5MQAL1HM1bg658ZD3p1Z5MNl/6EsLjo1e+2heAzhppi2uPbXjMv63g4qAx ZI7HBPENLhbnb+TBOOLrX8gcpzwluBZhfI9Zx7IZ08xdlL3i933t9kE4= X-Google-Smtp-Source: AA0mqf5h2J9oFoH6OwPRKwdUMH9YrATOyswrARRBpdBZ2Q0ivPxSMzGHGfcBLerWqa/pSKgv1IwtB/BIPg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:384d:b0:3cf:7217:d5fa with SMTP id s13-20020a05600c384d00b003cf7217d5famr42457601wmr.191.1670003110613; Fri, 02 Dec 2022 09:45:10 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:09 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-25-tabba@google.com> Subject: [RFC PATCH kvmtool v1 24/32] Change vesa mapping from private to shared From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Private mappings don't work with restricted memory since it might be COWed. Signed-off-by: Fuad Tabba --- hw/vesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/vesa.c b/hw/vesa.c index 277d638..3233794 100644 --- a/hw/vesa.c +++ b/hw/vesa.c @@ -96,7 +96,7 @@ struct framebuffer *vesa__init(struct kvm *kvm) goto unregister_device; } - mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_PRIVATE, mem_fd, 0); + mem = mmap(NULL, VESA_MEM_SIZE, PROT_RW, MAP_SHARED, mem_fd, 0); if (mem == MAP_FAILED) { r = -errno; goto close_memfd; From patchwork Fri Dec 2 17:44:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063064 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DED67C4708D for ; Fri, 2 Dec 2022 17:45:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234389AbiLBRp5 (ORCPT ); Fri, 2 Dec 2022 12:45:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234220AbiLBRpd (ORCPT ); Fri, 2 Dec 2022 12:45:33 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54FD2E0740 for ; Fri, 2 Dec 2022 09:45:23 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id v188-20020a1cacc5000000b003cf76c4ae66so4440515wme.7 for ; Fri, 02 Dec 2022 09:45:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tfiJ2mCVxWsxcWxP4lFGuce8U8ci+ACFUdji+1DMoMo=; b=dVmyIp1ooDfs+sq2MVmdV6sxUyN6x3Pc3lnTq80AlOv62NL7B7sM0KTJ7QCcgaqoQO jX9UVbi5QEuPYbRuzb3ObdfCcJSnx0dmKya4GPLtLNzedWGpM2ReWdTtD5xpImZ5Zk5P Rb8IsgC1auAJQmmSQysKmQhhhwOVIeobIusXA52rc43M19YGNNsJrqzh6Qb3XBHKmtFg vH85Ov692x55iCJo1FH1+BGEIk/dXFB5MU5W/nzz2i9E16VJLndxGbh/NZqDM8EU6P16 FFwFtBpBALPQFylFgLRQrDAx5cfeB5GhCC0gM2enbwH6hqFhLzRgK5eDbBezgAK4vNJu 5Q7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tfiJ2mCVxWsxcWxP4lFGuce8U8ci+ACFUdji+1DMoMo=; b=OQULs6SahYza36+JBFO4XBxfJ/nyr/jTii4QKxHoWgJWXkr5BwgenDLqkS0xU8OE7F p1W2hi4+OJpGnYeXQcw8B1TZbBAeCimCFyDhuafAGyJF+mY2e1VWXgQ7NU4hUbrNhhL4 3bRKYkJMJpDozXALWLkHMpjAxF6Zlu6CkUk1apbZdc2eDx8szkR9PI58OllJLGt7D5R0 7ygsKJiRcFiHqtStJwboUjETtk6hmCrvfr8zfh60Yp/2fQjIBd0spGiHBTnNmWorBVhm Q/9n4dt7eSEZE4V/LCpmGZIhIPirnNk+baavzMuT52pmKWl31KXsiVvAyXb4JCRnJ/Ce Km7Q== X-Gm-Message-State: ANoB5pmuzYQYE0AdsYJ7oxwVIhNJziImoobJ9bhnMtAomPtqo4KIRYbE N2YeTjwNgCpEM0gyQ5Fl6G46/kDGD1v8MMzJxxnIyBtrNuOihrxgs/X44pAZ58DUDtzaz55Yvz1 5pQ50p6MhL6ALdfcZbS+2bJYFj8zAEcm2BsRLF77r/JkGqoQGJGv0Atg= X-Google-Smtp-Source: AA0mqf6vYZf7PTbisFcht2A4L79+L4Lho4V6MeOta9c5Pfs53CZcGoaaQTDWkjciRgh105QRhv8usiYkxg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:35d4:b0:3cf:84e9:e722 with SMTP id r20-20020a05600c35d400b003cf84e9e722mr51385974wmq.8.1670003112844; Fri, 02 Dec 2022 09:45:12 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:10 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-26-tabba@google.com> Subject: [RFC PATCH kvmtool v1 25/32] Allocate guest memory as restricted if needed From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org If specified by the option and supported by KVM, allocate guest memory as restricted with the new system call. Signed-off-by: Fuad Tabba --- arm/aarch64/pvtime.c | 2 +- hw/vesa.c | 2 +- include/kvm/util.h | 2 +- util/util.c | 12 ++++++++---- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/arm/aarch64/pvtime.c b/arm/aarch64/pvtime.c index a452938..8247c52 100644 --- a/arm/aarch64/pvtime.c +++ b/arm/aarch64/pvtime.c @@ -16,7 +16,7 @@ static int pvtime__alloc_region(struct kvm *kvm) int mem_fd; int ret = 0; - mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0); + mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0); if (mem_fd < 0) return -errno; diff --git a/hw/vesa.c b/hw/vesa.c index 3233794..6c5287a 100644 --- a/hw/vesa.c +++ b/hw/vesa.c @@ -90,7 +90,7 @@ struct framebuffer *vesa__init(struct kvm *kvm) if (r < 0) goto unregister_ioport; - mem_fd = memfd_alloc(ARM_PVTIME_SIZE, false, 0, 0); + mem_fd = memfd_alloc(kvm, ARM_PVTIME_SIZE, false, 0, 0); if (mem_fd < 0) { r = -errno; goto unregister_device; diff --git a/include/kvm/util.h b/include/kvm/util.h index 79275ed..5a98d4a 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -139,7 +139,7 @@ static inline int pow2_size(unsigned long x) } struct kvm; -int memfd_alloc(u64 size, bool hugetlb, u64 blk_size); +int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 hugepage_size); void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, u64 size, u64 align); void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size); diff --git a/util/util.c b/util/util.c index 107f34d..13b3e82 100644 --- a/util/util.c +++ b/util/util.c @@ -17,7 +17,7 @@ __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted) #endif -static inline int memfd_restricted(unsigned int flags) +static int memfd_restricted(unsigned int flags) { return syscall(__NR_memfd_restricted, flags); } @@ -106,7 +106,7 @@ static u64 get_hugepage_blk_size(const char *hugetlbfs_path) return sfs.f_bsize; } -int memfd_alloc(u64 size, bool hugetlb, u64 blk_size) +int memfd_alloc(struct kvm *kvm, size_t size, bool hugetlb, u64 blk_size) { const char *name = "kvmtool"; unsigned int flags = 0; @@ -120,7 +120,11 @@ int memfd_alloc(u64 size, bool hugetlb, u64 blk_size) flags |= blk_size << MFD_HUGE_SHIFT; } - fd = memfd_create(name, flags); + if (kvm->cfg.restricted_mem) + fd = memfd_restricted(flags); + else + fd = memfd_create(name, flags); + if (fd < 0) die_perror("Can't memfd_create for memory map"); @@ -167,7 +171,7 @@ void *mmap_anon_or_hugetlbfs_align(struct kvm *kvm, const char *hugetlbfs_path, if (addr_map == MAP_FAILED) return MAP_FAILED; - fd = memfd_alloc(size, hugetlbfs_path, blk_size); + fd = memfd_alloc(kvm, size, hugetlbfs_path, blk_size); if (fd < 0) return MAP_FAILED; From patchwork Fri Dec 2 17:44:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063065 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69F1CC4332F for ; Fri, 2 Dec 2022 17:46:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234256AbiLBRp7 (ORCPT ); Fri, 2 Dec 2022 12:45:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234250AbiLBRpe (ORCPT ); Fri, 2 Dec 2022 12:45:34 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1312CE1193 for ; Fri, 2 Dec 2022 09:45:24 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id e8-20020a05600c218800b003cf634f5280so2192654wme.8 for ; Fri, 02 Dec 2022 09:45:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IDYXn6TvCy+Qfe8MDP2oOQsNsQcYdi8tVtAq7gLMUyI=; b=CnL3sC9ZmTnlMZxSSixGhu19a5LsbZ6biR1xoPDlNT35BwgU04HngvN7WJ+wQik+at o3MDTMU4R0msFLhP3BbTweU38E6RFl8P/jtvOtVGUgInH0PYINV9K6ColCkur4MjnAfY hfP31jsxTqymhtl1WWw499KdxkWj/Sqt997buq7hm/MK1MGO9hq5xIhDXdowngwEo6Jx eS1qp/gxwChmF4nb+PTOUiY2XfT33HiSpaxWNvDqewZOb5oqW+WSk8Mt2KsCWxaiZlBe 795ldkAQVB9BbU4iBql5AerlP/Y4XoWCDOItIdz6/Q/tAlmVKk5K7zIQCxjZ5SMVsXR5 QP+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IDYXn6TvCy+Qfe8MDP2oOQsNsQcYdi8tVtAq7gLMUyI=; b=mH3Qe2QGj8e2A3RnV7BtwgwF1Ot58/ESws6IvlnShrTd9dnC81Ev2C06PhkH3le2C2 nDZxUA+ViC9xr0OSCSP7YYJNAHmVuwakQE/O0/zBn01DTKVI8Ai++lPXkDkKz5EgXYY2 jgSJmd8vdjmAubxcuzPlG4BBVNe+1J9oDR3l3sYVBrOAD5kkGKuY+G8YZxbnLpcqrEY0 ndI8/ktCV8v8o2ICPetCu9OSM/3E8gkPQF+vjHyqUz0eRcxSNQQ49/U2JhiK79s7SrCs ey2mBVAK3CchaPYnpuWM4HmF9bzg2VoXHRQuk6bMIOU/NqpvDWc6F4Dpb7N4p4Hd5c4Y jRwA== X-Gm-Message-State: ANoB5pmlb8rjNmlU94oFLigUKd16ubat6aa+ybiD7VS6ha6N6P3xsM6S FeTXYSnICqXWYDXJQhSiEYupdOGguNZvsViNTioIACMpa8UkS0Utjx/Edpz7raQFBZH0T1VP7Ow yXFf98sP0nOtvyERDsD+Pm7r+oOTIqi2k2zpFF53IRSv8SCSB0lRYzIs= X-Google-Smtp-Source: AA0mqf6E0qTTu0wHO5cJHBdjbupuRhEbWTzk2fj19cl67v6+/R0Gl4kveBNeC3HAZzJxP/9NzGA6V3aLzA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5d:53cd:0:b0:242:47b9:7ad6 with SMTP id a13-20020a5d53cd000000b0024247b97ad6mr1956636wrw.93.1670003114911; Fri, 02 Dec 2022 09:45:14 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:11 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-27-tabba@google.com> Subject: [RFC PATCH kvmtool v1 26/32] Use the new fd-based extended memory region From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org If restricted guest memory is enabled, use the new extended guest memory region interface to pass the guest memory to kvm using the restricted file descriptor. In the future, pKVM will require this, and the successor of patch will instead force the use of restricted guest memory for pKVM (and other potential environments) that require restricted memory. Signed-off-by: Fuad Tabba --- kvm.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/kvm.c b/kvm.c index fc0bfc4..bde6708 100644 --- a/kvm.c +++ b/kvm.c @@ -214,6 +214,41 @@ static int set_user_memory_region(int vm_fd, u32 slot, u32 flags, return ret; } +static int set_user_memory_region_ext(int vm_fd, u32 slot, u32 flags, + u64 guest_phys, u64 size, + u64 userspace_addr, u32 fd, u64 offset) +{ + int ret = 0; + struct kvm_enc_region range; + struct kvm_userspace_memory_region_ext mem = { + .region = { + .slot = slot, + .flags = flags, + .guest_phys_addr = guest_phys, + .memory_size = size, + .userspace_addr = 0, + }, + .restricted_fd = fd, + .restricted_offset = offset, + }; + + ret = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, &mem); + if (ret < 0) { + ret = -errno; + goto out; + } + + /* Inform KVM that the region is protected. */ + range.addr = guest_phys; + range.size = size; + ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_REG_REGION, &range); + if (ret) + ret = -errno; + +out: + return ret; +} + int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr) { @@ -240,8 +275,13 @@ int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, goto out; } - ret = set_user_memory_region(kvm->vm_fd, bank->slot, 0, guest_phys, 0, - (u64) userspace_addr); + if (kvm->cfg.restricted_mem) + ret = set_user_memory_region_ext(kvm->vm_fd, bank->slot, + KVM_MEM_PRIVATE, guest_phys, 0, (u64) userspace_addr, + 0, 0); + else + ret = set_user_memory_region(kvm->vm_fd, bank->slot, 0, + guest_phys, 0, (u64) userspace_addr); if (ret < 0) goto out; @@ -339,9 +379,13 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, flags |= KVM_MEM_READONLY; if (type != KVM_MEM_TYPE_RESERVED) { - ret = set_user_memory_region(kvm->vm_fd, slot, flags, - guest_phys, size, - (u64) userspace_addr); + if (kvm->cfg.restricted_mem) + ret = set_user_memory_region_ext(kvm->vm_fd, slot, + flags | KVM_MEM_PRIVATE, guest_phys, size, + (u64) userspace_addr, memfd, offset); + else + ret = set_user_memory_region(kvm->vm_fd, slot, flags, + guest_phys, size, (u64) userspace_addr); if (ret < 0) goto out; } From patchwork Fri Dec 2 17:44:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063066 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 616D2C4321E for ; Fri, 2 Dec 2022 17:46:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234405AbiLBRqB (ORCPT ); Fri, 2 Dec 2022 12:46:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234254AbiLBRpf (ORCPT ); Fri, 2 Dec 2022 12:45:35 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A31DE1196 for ; Fri, 2 Dec 2022 09:45:24 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id c187-20020a1c35c4000000b003cfee3c91cdso2815493wma.6 for ; Fri, 02 Dec 2022 09:45:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2ThRYodwzO4HlBh5q1CYryWQzrraRJD2u7VnaCxXbdY=; b=A+2T7WRHYTGIS3MKZCWzunj489VwfelhrmYewv6d8sJJ2/jP3QiFtG1YqS4Uwnm/by xQ9MG3TSBQwfCmXSU5oB3EYF7C6SwI+0G7xQ8H6P56AKuyebYZdyF/rucRBXmxbER9vD m3bGgYj7iSMjxiAOFF86uyAbETmwOezIduHzn3j7aXKbJtq0rTm+txsBfFMBrCTNKQ4w 62Dp1Vl6X5F6o2eMOU0WMaTga++AbCWfAvfLGAyZ6OeP18IQmlzeI2/oXeK9kzEZ3JsZ Hl0k+5bpXRYGpFdQMj70/mp0kTTwQpKcLrGejNbNLFuHUUHWgSgZ3RXZssM7CTLE5Te+ AXNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2ThRYodwzO4HlBh5q1CYryWQzrraRJD2u7VnaCxXbdY=; b=Hh1KXxIM04POp0Xtxr49KiSuvBQ7Br78MSinva5ziUYibiz36sdj/qkusSQbOYMZIA Kv16T67/kHXJAHw8qVsVDD3L0efc9VZh3Z408RVjJxdesV21utukvw5Dq+80jVGD/r5w HpgIb7eQTdLSBI8aWcgRbhVuDca3x9U/hRy/Akwgi3qQQ3OkkRQnHnG9kEBfdksPMaOW L1UNUVOwncMn058o71KbhnpW15uKxwvKbwXUZSacMN1/AuD85249MWj7S7DE33FGV0Oo QTSiVEwAVhQUlUX3E2HBpJulswzYEH7j45+rTxr7KHCzgh82mpEDjrqr6frbUXrEKROI 8i5A== X-Gm-Message-State: ANoB5pkjM+e6DQc35aqPQARdj24WjBj5fl5KbgOvsF4H50TbBSoRjKS1 BVkFiOxyPEWDAJEPcJb6wEj2YlEFFoyYr80xVBiFJeivmIH1CPAz0xdGriQHmL7NyxAML86pHda Httk8qb+k1IkFg2LtvrY8s5dVZkoqdDbMKRteb5gz7bekJ5a+EllwBAs= X-Google-Smtp-Source: AA0mqf5P1OxcbmXmwUL2lbMOwW30hpJxCjWwQ5bMVjlZ0i22PpPkwbPtviFKfRwzzOGQ+FECnmrnGqcFfA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:3421:b0:3cf:ac8a:d43e with SMTP id y33-20020a05600c342100b003cfac8ad43emr41493887wmp.65.1670003116803; Fri, 02 Dec 2022 09:45:16 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:12 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-28-tabba@google.com> Subject: [RFC PATCH kvmtool v1 27/32] Track the memfd in the bank From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Guest memory is canonically represented by the fd, so use it to refer to guest memory in the kvmtool guest memory banks. Signed-off-by: Fuad Tabba --- include/kvm/kvm.h | 2 ++ kvm.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 33cae9d..6192f6c 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -80,6 +80,8 @@ struct kvm_mem_bank { u64 size; enum kvm_mem_type type; u32 slot; + int memfd; + u64 memfd_offset; }; struct kvm { diff --git a/kvm.c b/kvm.c index bde6708..a0bddf4 100644 --- a/kvm.c +++ b/kvm.c @@ -374,6 +374,8 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, bank->size = size; bank->type = type; bank->slot = slot; + bank->memfd = memfd; + bank->memfd_offset = offset; if (type & KVM_MEM_TYPE_READONLY) flags |= KVM_MEM_READONLY; From patchwork Fri Dec 2 17:44:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063069 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E609C4321E for ; Fri, 2 Dec 2022 17:46:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234359AbiLBRqG (ORCPT ); Fri, 2 Dec 2022 12:46:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234324AbiLBRpi (ORCPT ); Fri, 2 Dec 2022 12:45:38 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 720E4E11A0 for ; Fri, 2 Dec 2022 09:45:28 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id e8-20020a05600c218800b003cf634f5280so2192716wme.8 for ; Fri, 02 Dec 2022 09:45:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ojAIDneqly7urLUtSI7yC5BejOF4LmcYgz67ETGcXNg=; b=RmDaW5xV98br0A+I4X8nKQxFUMHcQ4sapYf//UkOuibrJ4ua7j1fQCu2bYFjRdA8U/ wNSnbd7QE+2N/tk/XGm6BivQSSt4NUWaPByzCCo3ka8PquxmrOYB0UuQGfU6L2SdyEz+ w5DF8XEzwE/qgKZXXOFHgIhChTWZi+9Q05N5ls991gR2B32YT0w85kosnI03mSyeb8Rh fbL7o4lbelQ619nvM4FymGDHa09fN9gLj6eIzVHUBQdh8HYiyCu6/gGkrgoFAEGNDEqf 9fzelRXfVj3WS86nxjk0IeJBBD/EwWOIn6Q8yAZs+eiQYuxcOKRnU0XZhJGuBDNoIcDo MFrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ojAIDneqly7urLUtSI7yC5BejOF4LmcYgz67ETGcXNg=; b=5TieTwZN0q4Tv0Nvyf3BK8RwQFlcfDyCqIwuOldIowfxjFeX7C5lsW3ysFHdQ/rixH Yaf6Z4CxnnAvXJDWHwrLCNBPkYE5eobYJ74FcIqruoZkv8RwULOEX8VxNVttqc+VfW5d pqeIloqglB7zRlqplxHWLpoWJCyw+qzL5TjW4BeaWrYMhCik9mUw6IMxso3eCTZIsm6X B07/5eBuzy1CG/e/2wABUVNWrsmOHwIsbWwBufE7ZAYtfqJSs0lLGmpGdaJ9Ox6fRH8Z W417LuAbBwd9YnXrfoMIqIAC6g6yDoouIlJp//LyxzLoT8V6AcoFEDpHpLSyaALYx89U ZY3w== X-Gm-Message-State: ANoB5pnLKKHFyH2kBRF7oLQyckOISqdKyB94wP35luFVsHRlajgeLHVt dz9hq6fZxhHh7fN/IqtrqJlpPqqIR72QL88aouvI55myeor0VNYuNVugcREUuFQyPotirw2EpSm aJsGBpRbdFbP2FpUtnO1NLDz/RQPZpdhZkPRUot6ttCF7cwLhlm8ZJO8= X-Google-Smtp-Source: AA0mqf67b6YUTuw8IVvtFaKeWF+h7bmEauUlGhv/RG/zZjeABDv6PvbgtnybGDhOcsvkYqOIgpKrPhG2jQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:6888:b0:3d0:7513:d149 with SMTP id fn8-20020a05600c688800b003d07513d149mr9595758wmb.156.1670003118948; Fri, 02 Dec 2022 09:45:18 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:13 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-29-tabba@google.com> Subject: [RFC PATCH kvmtool v1 28/32] Add functions for mapping/unmapping guest memory From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Host shouldn't map guest memory not shared with it. These functions allow kvmtool in a future patch to toggle the mapping depending on the sharing status, even if it changes. Signed-off-by: Fuad Tabba --- include/kvm/kvm.h | 4 + kvm.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 6192f6c..66b5664 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -220,6 +220,10 @@ static inline bool kvm__arch_has_cfg_ram_address(void) void *guest_flat_to_host(struct kvm *kvm, u64 offset); u64 host_to_guest_flat(struct kvm *kvm, void *ptr); +void map_guest_range(struct kvm *kvm, u64 gpa, u64 size); +void unmap_guest_range(struct kvm *kvm, u64 gpa, u64 size); +void map_guest(struct kvm *kvm); +void unmap_guest(struct kvm *kvm); bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline); diff --git a/kvm.c b/kvm.c index a0bddf4..66e6a8e 100644 --- a/kvm.c +++ b/kvm.c @@ -462,6 +462,189 @@ int kvm__for_each_mem_bank(struct kvm *kvm, enum kvm_mem_type type, return ret; } +static void *_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) +{ + return mmap(addr, len, prot, flags | MAP_FIXED, fd, offset); +} + +static int _munmap(void *addr, size_t len) +{ + if (mmap(addr, len, PROT_NONE, MAP_SHARED | MAP_FIXED | MAP_ANON, -1, 0) != MAP_FAILED) + return 0; + + return -EFAULT; +} + +struct bank_range { + u64 gpa; + u64 size; +}; + +static bool is_bank_range(struct kvm_mem_bank *bank, struct bank_range *range) +{ + u64 bank_start = bank->guest_phys_addr; + u64 bank_end = bank_start + bank->size; + u64 gpa_end = range->gpa + range->size; + + if (range->gpa < bank_start || range->gpa >= bank_end) + return false; + + if (gpa_end > bank_end || gpa_end <= bank_start) { + pr_warning("%s invalid guest range", __func__); + return false; + } + + return true; +} + +static int map_bank_range(struct kvm *kvm, struct kvm_mem_bank *bank, void *data) +{ + struct bank_range *range = data; + u64 gpa_offset; + u64 map_offset; + u64 hva; + void *mapping; + + if (!is_bank_range(bank, range)) + return 0; + + gpa_offset = range->gpa - bank->guest_phys_addr; + map_offset = bank->memfd_offset + gpa_offset; + hva = (u64) bank->host_addr + gpa_offset; + + BUG_ON(map_offset > bank->memfd_offset + bank->size); + BUG_ON(map_offset < bank->memfd_offset); + BUG_ON(hva < (u64)bank->host_addr); + BUG_ON(!bank->memfd); + + mapping = _mmap((void *)hva, range->size, PROT_RW, MAP_SHARED, bank->memfd, map_offset); + if (mapping == MAP_FAILED || mapping != (void *)hva) + pr_warning("%s gpa 0x%llx (size: %llu) at hva 0x%llx failed with mapping 0x%llx", + __func__, + (unsigned long long)range->gpa, + (unsigned long long)range->size, + (unsigned long long)hva, + (unsigned long long)mapping); + + /* Do not proceed to trying to map the other banks. */ + return 1; +} + +static int unmap_bank_range(struct kvm *kvm, struct kvm_mem_bank *bank, void *data) +{ + struct bank_range *range = data; + u64 gpa_offset; + u64 hva; + int ret; + + if (!is_bank_range(bank, range)) + return 0; + + gpa_offset = range->gpa - bank->guest_phys_addr; + hva = (u64)bank->host_addr + gpa_offset; + + BUG_ON(hva < (u64)bank->host_addr); + BUG_ON(!bank->memfd); + + ret = _munmap((void *)hva, range->size); + if (ret) + pr_warning("%s gpa 0x%llx (size: %llu) at hva 0x%llx failed with error %d", + __func__, + (unsigned long long)range->gpa, + (unsigned long long)range->size, + (unsigned long long)hva, + ret); + + /* Do not proceed to trying to unmap the other banks. */ + return 1; +} + +static int map_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data) +{ + void *mapping; + + BUG_ON(!bank->memfd); + + pr_debug("%s hva 0x%llx (size: %llu) of memfd %d (offset %llu)", + __func__, + (unsigned long long)bank->host_addr, + (unsigned long long)bank->size, + bank->memfd, + (unsigned long long)bank->memfd_offset); + + mapping = _mmap(bank->host_addr, bank->size, PROT_RW, MAP_SHARED, bank->memfd, bank->memfd_offset); + if (!mapping || mapping != bank->host_addr) + pr_warning("%s hva 0x%llx (size: %llu) failed with return 0x%llx", + __func__, + (unsigned long long)bank->host_addr, + (unsigned long long)bank->size, + (unsigned long long)mapping); + return 0; +} + +static int unmap_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void *data) +{ + int ret; + + pr_debug("%s hva 0x%llx (size: %llu)", + __func__, + (unsigned long long)bank->host_addr, + (unsigned long long)bank->size); + + ret = _munmap(bank->host_addr, bank->size); + if (ret) + pr_warning("%s hva 0x%llx (size: %llu) failed with error %d", + __func__, + (unsigned long long)bank->host_addr, + (unsigned long long)bank->size, + ret); + return 0; +} + +void map_guest_range(struct kvm *kvm, u64 gpa, u64 size) +{ + struct bank_range range = { .gpa = gpa, .size = size }; + int ret; + + ret = kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM|KVM_MEM_TYPE_DEVICE, + map_bank_range, &range); + + if (!ret) + pr_warning("%s gpa 0x%llx (size: %llu) found no matches", + __func__, + (unsigned long long)gpa, + (unsigned long long)size); +} + +void unmap_guest_range(struct kvm *kvm, u64 gpa, u64 size) +{ + struct bank_range range = { .gpa = gpa, .size = size }; + int ret; + + ret = kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM|KVM_MEM_TYPE_DEVICE, + unmap_bank_range, &range); + + if (!ret) + pr_warning("%s gpa 0x%llx (size: %llu) found no matches", + __func__, + (unsigned long long)gpa, + (unsigned long long)size); + + return; +} + +void map_guest(struct kvm *kvm) +{ + kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM|KVM_MEM_TYPE_DEVICE, + map_bank, NULL); +} + +void unmap_guest(struct kvm *kvm) +{ + kvm__for_each_mem_bank(kvm, KVM_MEM_TYPE_RAM|KVM_MEM_TYPE_DEVICE, + unmap_bank, NULL); +} + int kvm__recommended_cpus(struct kvm *kvm) { int ret; From patchwork Fri Dec 2 17:44:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063067 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7B9CC47088 for ; Fri, 2 Dec 2022 17:46:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234344AbiLBRqC (ORCPT ); Fri, 2 Dec 2022 12:46:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234149AbiLBRph (ORCPT ); Fri, 2 Dec 2022 12:45:37 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90D5DE11A2 for ; Fri, 2 Dec 2022 09:45:28 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id j6-20020a05690212c600b006fc7f6e6955so5003009ybu.12 for ; Fri, 02 Dec 2022 09:45:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dXMwf5objvW3u6ccglKiojiodstPXhl+xSwcan5VDRU=; b=ZPgK5NQWkr0CXkRYqMCgUI/C4GoMsg8aMCqYvByQ/aXs15J98f8nqD3uBJQ0qnvuDa uwkTf0mC9yssNCXWIzIIKVslBkw65hrViaXtVkmOx/y7kUTr0jlZEEMXzQn6vvqTwwad UeJif98U8hmBbQNYYNuj+4AYmBL9PKPCJtpHLsKFUNB/vHLWy5nnVsXcMOGkG8DaoHa6 ATctdP5PSik0cOhwczfgaojaACqO1H5u10nZZvVOxw1OJNGY4UgJEGqGavxB9Astj6WK OrJ8lQWQKJxv3jytV+xd9Cv4KsojHjg77uWoF/xUbtcHAaRfjmIwgZo7TcZeaZY0P8LR grGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dXMwf5objvW3u6ccglKiojiodstPXhl+xSwcan5VDRU=; b=BSXGm+lo7m9QVp3c5jFT5uLdN4kebX65L02sp8lc51fOPsQfi61V4LQF6rrLmW4ZgR m2Xu3pbpRhuHF5pRzmT4qhGz9v1Dz4vXHbOUeDjGjcVArrhtpnaINRSPEmdlmO29yCBe KczZyx3hvLb4EobdS++C2RQ08RwQgcCjbWJthJsmlmBxksyGr/bRNPc9LMBlB/DoLJmr rPOb7/TtK+DOQqkfng2Bh0l8IOCN5nsT9j/FctBoEcFIt2m5wCVQAGGrIjOKZZNDp1r8 TNLuGvPCsZPaGjQZdBwADBeHnTAzXY4+4URiAj5Hv3MxwkQ6c2YsqXwb2TrVl3mXBRsG vNsA== X-Gm-Message-State: ANoB5pnYo/Xy8RrdL0mgfULRl0ymx9X1l6QEl6xmQfoHUHSuDsJVkCIK vkgO5BeW7fbZKw5BCLWZ3GXUJVFPK2kQNvUp6Ci19RiKIdqqoA3nknsns4neDW626yuc6l9iX8x Im54rQ+Zk/AaZcJR3w5MDuvAd0uL8MqsqkmP6v5SE1BIutkkbf795FXk= X-Google-Smtp-Source: AA0mqf6rHNMirunmaZE2JJDlCsgHJepjQN08CpR4XnoKViXcj/YzhRg8yOKieWyVhuqDC4mgnCL0clQMHw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a0d:ea91:0:b0:3d4:8f32:89d6 with SMTP id t139-20020a0dea91000000b003d48f3289d6mr13794377ywe.35.1670003120853; Fri, 02 Dec 2022 09:45:20 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:14 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-30-tabba@google.com> Subject: [RFC PATCH kvmtool v1 29/32] pkvm: Add option to spawn a protected vm in pkvm From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Will Deacon For Testing Even when pkvm is enabled, guests are not protected by default. This allows the creation of protected guests with kvmtool. This is based on the current pKVM proposal [1, 2], and is likely to be different in the final version. [1] https://lore.kernel.org/kvmarm/20220519134204.5379-1-will@kernel.org/ [2] https://lore.kernel.org/all/20221110190259.26861-1-will@kernel.org/ Signed-off-by: Will Deacon Signed-off-by: Fuad Tabba --- arm/aarch64/kvm.c | 3 +++ arm/fdt.c | 18 ++++++++++++++++++ arm/include/arm-common/fdt-arch.h | 2 +- arm/pci.c | 3 +++ builtin-run.c | 2 ++ include/kvm/kvm-config.h | 1 + virtio/pci-modern.c | 3 +++ 7 files changed, 31 insertions(+), 1 deletion(-) diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index 54200c9..f65c9c1 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -131,6 +131,9 @@ int kvm__get_vm_type(struct kvm *kvm) if (ipa_bits > max_ipa_bits) die("Memory too large for this system (needs %d bits, %d available)", ipa_bits, max_ipa_bits); + if (kvm->cfg.pkvm) + return KVM_VM_TYPE_ARM_IPA_SIZE(ipa_bits) | (1U << 8); + return KVM_VM_TYPE_ARM_IPA_SIZE(ipa_bits); } diff --git a/arm/fdt.c b/arm/fdt.c index 286ccad..0049bef 100644 --- a/arm/fdt.c +++ b/arm/fdt.c @@ -116,6 +116,7 @@ static int setup_fdt(struct kvm *kvm) void (*)(void *, u8, enum irq_type)); void (*generate_cpu_peripheral_fdt_nodes)(void *, struct kvm *) = kvm->cpus[0]->generate_fdt_nodes; + u64 resv_mem_prop; /* Create new tree without a reserve map */ _FDT(fdt_create(fdt, FDT_MAX_SIZE)); @@ -163,6 +164,23 @@ static int setup_fdt(struct kvm *kvm) _FDT(fdt_property(fdt, "reg", mem_reg_prop, sizeof(mem_reg_prop))); _FDT(fdt_end_node(fdt)); + if (kvm->cfg.pkvm) { + /* Reserved memory (restricted DMA) */ + _FDT(fdt_begin_node(fdt, "reserved-memory")); + _FDT(fdt_property_cell(fdt, "#address-cells", 0x2)); + _FDT(fdt_property_cell(fdt, "#size-cells", 0x2)); + _FDT(fdt_property(fdt, "ranges", NULL, 0)); + + _FDT(fdt_begin_node(fdt, "restricted_dma_reserved")); + _FDT(fdt_property_string(fdt, "compatible", "restricted-dma-pool")); + resv_mem_prop = cpu_to_fdt64(SZ_8M); + _FDT(fdt_property(fdt, "size", &resv_mem_prop, sizeof(resv_mem_prop))); + _FDT(fdt_property_cell(fdt, "phandle", PHANDLE_DMA)); + _FDT(fdt_end_node(fdt)); + + _FDT(fdt_end_node(fdt)); + } + /* CPU and peripherals (interrupt controller, timers, etc) */ generate_cpu_nodes(fdt, kvm); if (generate_cpu_peripheral_fdt_nodes) diff --git a/arm/include/arm-common/fdt-arch.h b/arm/include/arm-common/fdt-arch.h index 60c2d40..81df744 100644 --- a/arm/include/arm-common/fdt-arch.h +++ b/arm/include/arm-common/fdt-arch.h @@ -1,6 +1,6 @@ #ifndef ARM__FDT_H #define ARM__FDT_H -enum phandles {PHANDLE_RESERVED = 0, PHANDLE_GIC, PHANDLE_MSI, PHANDLES_MAX}; +enum phandles {PHANDLE_RESERVED = 0, PHANDLE_GIC, PHANDLE_MSI, PHANDLE_DMA, PHANDLES_MAX}; #endif /* ARM__FDT_H */ diff --git a/arm/pci.c b/arm/pci.c index 5bd82d4..d183177 100644 --- a/arm/pci.c +++ b/arm/pci.c @@ -74,6 +74,9 @@ void pci__generate_fdt_nodes(void *fdt, struct kvm *kvm) if (irqchip == IRQCHIP_GICV2M || irqchip == IRQCHIP_GICV3_ITS) _FDT(fdt_property_cell(fdt, "msi-parent", PHANDLE_MSI)); + if (kvm->cfg.pkvm) + _FDT(fdt_property_cell(fdt, "memory-region", PHANDLE_DMA)); + /* Generate the interrupt map ... */ dev_hdr = device__first_dev(DEVICE_BUS_PCI); while (dev_hdr && nentries < ARRAY_SIZE(irq_map)) { diff --git a/builtin-run.c b/builtin-run.c index 4642bc4..9ec5701 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -204,6 +204,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) "Use legacy virtio transport"), \ OPT_BOOLEAN('\0', "restricted_mem", &(cfg)->restricted_mem, \ "Use restricted memory for guests"), \ + OPT_BOOLEAN('\0', "pkvm", &(cfg)->pkvm, \ + "Spawn a protected VM (pkvm)"), \ \ OPT_GROUP("Kernel options:"), \ OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel", \ diff --git a/include/kvm/kvm-config.h b/include/kvm/kvm-config.h index ea5f3ea..a18b8a3 100644 --- a/include/kvm/kvm-config.h +++ b/include/kvm/kvm-config.h @@ -66,6 +66,7 @@ struct kvm_config { bool mmio_debug; bool virtio_legacy; bool restricted_mem; + bool pkvm; }; #endif diff --git a/virtio/pci-modern.c b/virtio/pci-modern.c index c5b4bc5..84af042 100644 --- a/virtio/pci-modern.c +++ b/virtio/pci-modern.c @@ -150,6 +150,9 @@ static bool virtio_pci__common_read(struct virtio_device *vdev, struct virtio_pci *vpci = vdev->virtio; u64 features = 1ULL << VIRTIO_F_VERSION_1; + if (vpci->kvm->cfg.pkvm) + features |= 1ULL << VIRTIO_F_ACCESS_PLATFORM; + switch (offset - VPCI_CFG_COMMON_START) { case VIRTIO_PCI_COMMON_DFSELECT: val = vpci->device_features_sel; From patchwork Fri Dec 2 17:44:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063068 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B70E2C4332F for ; Fri, 2 Dec 2022 17:46:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234408AbiLBRqE (ORCPT ); Fri, 2 Dec 2022 12:46:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234322AbiLBRpi (ORCPT ); Fri, 2 Dec 2022 12:45:38 -0500 Received: from mail-wm1-x349.google.com (mail-wm1-x349.google.com [IPv6:2a00:1450:4864:20::349]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F87EE11A8 for ; Fri, 2 Dec 2022 09:45:29 -0800 (PST) Received: by mail-wm1-x349.google.com with SMTP id m17-20020a05600c3b1100b003cf9cc47da5so2814633wms.9 for ; Fri, 02 Dec 2022 09:45:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=E8eIZIOTFSHtVkJmIkwiyFF0y+C+tlZXEXKV5fvsuno=; b=e9IDouXfpTSkQYjuCbbtIpfmVCcJv2mfFjBsCaTFMY2rJ1DQ2hi2x0fIqQ2elNTiyB pUlHDS+JKUSi/Vm0DFiAxYXG3fsenCKqexBcelDVa30jqI9+qj8OKQU2yHhMiidMkHqm gYCoRL529tXldEogvFsy1B0noHa+7QuRTcAWJQoSvoxSw0E8uv50c1a5Do8JVeSD71Ol FuI1dsFPEAuxQ0kfhyyA8UdbAuB3mhIMZuByaRg4s7gmjcMIp5IFCLargQE5zgSWUnPx pQ98756rH/drzw94itOfVVKS5+mTkjH/7RhC6XYCJt8FcxK/EQ5NB6KZgfKEOncItEAb DZyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=E8eIZIOTFSHtVkJmIkwiyFF0y+C+tlZXEXKV5fvsuno=; b=fWAZv1O+Yqv12UUxaiIe/THAuwUPoGz9anFClH6EryBp+kmDw4FaDWoqe+FyUHnNnW K24072pXVgwm69R9T6CTHJSv2Kflli8pADkjOJVeYeH5E/PjJY5e2o49jV01I7sRfDDE 8EfponI9canVKMHWP9dlP+6tKZUgPOwoZfN9zkXQpd5Iv9zg8gVr0x1sYDQHUWP+A3rs +00xdEikkdItoyvG6ne+Kt1bpHc+7fzarrU0Gw4q+bkVJ9PnLtwBzddTKj0UgQGQ8vVC e18ww9CmMTOLOVcJAGtMvW3SQPvVHQFSwKY+UweGeebKnlvnuyDe4OsAK1chUfzm/4vR BiJQ== X-Gm-Message-State: ANoB5plwYADhXkBjWgf8D8fF40hoWuxr4tIPOX2I0/zm21s8q6BxbVyP te+AWav5wQdf3iRkdUMA/EqP9w34r7oLAorlFvkSBO28qiaR/juE8SI0aqPYpamqzDDjY04jTyn 8Ql3TO3VSt++2vVttTH/0y8kkv7yBuC+QLDYKhXqDhzXp3T01Yt9QTR0= X-Google-Smtp-Source: AA0mqf6EfJKf1MWAukgvlsb+btHTzUv18ftmQpzYMrFJoz6A8kUp8+8aFS5J6TlwV1nafBlAQitb0lfaDQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:4d07:b0:3cf:8108:de64 with SMTP id u7-20020a05600c4d0700b003cf8108de64mr39606262wmp.139.1670003122986; Fri, 02 Dec 2022 09:45:22 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:15 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-31-tabba@google.com> Subject: [RFC PATCH kvmtool v1 30/32] pkvm: Enable exit hypercall capability if supported From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This hypercall allows the guest to communicate with the host via the new exit type. It will be used in future patches to communicate guest-triggered change of memory sharing status with the host/vmm (kvmtool). Signed-off-by: Fuad Tabba --- arm/aarch32/include/kvm/kvm-arch.h | 1 + arm/aarch64/include/kvm/kvm-arch.h | 1 + arm/aarch64/kvm.c | 23 +++++++++++++++++++++++ arm/kvm-cpu.c | 10 ++++++++++ arm/kvm.c | 1 + 5 files changed, 36 insertions(+) diff --git a/arm/aarch32/include/kvm/kvm-arch.h b/arm/aarch32/include/kvm/kvm-arch.h index 467fb09..5666f2f 100644 --- a/arm/aarch32/include/kvm/kvm-arch.h +++ b/arm/aarch32/include/kvm/kvm-arch.h @@ -7,6 +7,7 @@ struct kvm; static inline void kvm__arch_enable_mte(struct kvm *kvm) {} +static inline void kvm__arch_enable_exit_hypcall(struct kvm *kvm) {} #define MAX_PAGE_SIZE SZ_4K diff --git a/arm/aarch64/include/kvm/kvm-arch.h b/arm/aarch64/include/kvm/kvm-arch.h index 02d09a4..9af002b 100644 --- a/arm/aarch64/include/kvm/kvm-arch.h +++ b/arm/aarch64/include/kvm/kvm-arch.h @@ -7,6 +7,7 @@ struct kvm; unsigned long long kvm__arch_get_kern_offset(struct kvm *kvm, int fd); int kvm__arch_get_ipa_limit(struct kvm *kvm); void kvm__arch_enable_mte(struct kvm *kvm); +void kvm__arch_enable_exit_hypcall(struct kvm *kvm); #define MAX_PAGE_SIZE SZ_64K diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index f65c9c1..604a5e8 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -163,3 +163,26 @@ void kvm__arch_enable_mte(struct kvm *kvm) pr_debug("MTE capability enabled"); } + +void kvm__arch_enable_exit_hypcall(struct kvm *kvm) +{ + struct kvm_enable_cap cap = { + .cap = KVM_CAP_EXIT_HYPERCALL, + .args[0] = KVM_EXIT_HYPERCALL_VALID_MASK, + }; + + if (kvm->cfg.arch.aarch32_guest) { + pr_debug("EXIT HYPERCALL is incompatible with AArch32"); + return; + } + + if (!kvm__supports_extension(kvm, KVM_CAP_EXIT_HYPERCALL)) { + pr_debug("EXIT HYPERCALL capability not available"); + return; + } + + if (ioctl(kvm->vm_fd, KVM_ENABLE_CAP, &cap)) + die_perror("KVM_ENABLE_CAP(KVM_CAP_EXIT_HYPERCALL)"); + + pr_debug("EXIT capability enabled"); +} diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index 98bc5fd..cb5a92a 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -146,6 +146,16 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu) bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) { + switch (vcpu->kvm_run->exit_reason) { + case KVM_EXIT_HYPERCALL: + pr_warning("Unhandled exit hypercall: 0x%llx, 0x%llx, 0x%llx, 0x%llx", + vcpu->kvm_run->hypercall.nr, + vcpu->kvm_run->hypercall.ret, + vcpu->kvm_run->hypercall.args[0], + vcpu->kvm_run->hypercall.args[1]); + return true; + } + return false; } diff --git a/arm/kvm.c b/arm/kvm.c index 094fbe4..f4b0247 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -84,6 +84,7 @@ void kvm__arch_init(struct kvm *kvm) die("Failed to create virtual GIC"); kvm__arch_enable_mte(kvm); + kvm__arch_enable_exit_hypcall(kvm); } #define FDT_ALIGN SZ_2M From patchwork Fri Dec 2 17:44:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063070 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3591DC4332F for ; Fri, 2 Dec 2022 17:46:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234285AbiLBRqI (ORCPT ); Fri, 2 Dec 2022 12:46:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234270AbiLBRpj (ORCPT ); Fri, 2 Dec 2022 12:45:39 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71802E11A9 for ; Fri, 2 Dec 2022 09:45:29 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id y5-20020adfc7c5000000b002423fada7deso955991wrg.7 for ; Fri, 02 Dec 2022 09:45:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=60bGsGvOuzsAih8loS2U41EM+pg36WcOg61x1u2LkyQ=; b=Ovbm1Hb3f/ASfmn+WumjbavCzzHTtBiZofkBBePxPlJxJyyxIlfAq+Mzai0/UNbX98 AnC7prbH5HtdL42CqBpJjPAGcxD2LI5i9kAImE/EWvW1GbKVXHossexjArNDMAdpN8Ro /pWXbmXVfGK21oCa55ulT70CsJ9vPSFEDAkk5IC+fDfGLt2ZvjW7BlzoP+WzWCW7QKw3 RqcJc5xaupsH/cyBm/ZOGZ7TxInLkYNgP0EonJPh0LCGndBLcjcejap55MA6fRXEgzoe OvjxnPUSnPGMTPpPGzPb+nOXUPupa6LPPRvvetLqYqlIEpuT+37sLlwXR2nRvWtAYlu1 gjLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=60bGsGvOuzsAih8loS2U41EM+pg36WcOg61x1u2LkyQ=; b=npJajxbuMITdOixJq/h8H0Csyve90m0mlcUd0Pd2MGFY8Lqxf472X9tcURCTrsyNBH a7GGN3qHft0S2oO6HJ6/lGbgpyB8bZTE55djr8YZS66FqT851+uv8JlOUdgtmMcwniUK CH8gt3LMOWS/sRHGGzYGGvYe1lmrLyi2a7CL5r132SjA1jb0ZxzmMsSxl9AsaD7mw72b PxNhcfgPovvj0I1pNSzuZpLRnW3H66t+sDsCSCwXORq5VRXr9SQiQHo8aj6bcQ1EYPfY cv8uIOGqbUrKuoRi3feFZcH1y2VRhbMluwbv+QbshM91phXKxR41sIPL98yhQWhcKTAE onTg== X-Gm-Message-State: ANoB5pmeWYdXVhEz53Iw5ZoH2uN645NtViAc1cE2VdBFbpkEUFp4k4YB JAdtJVxcuNH0IJYvF8j1BYEcS0Wgc/7KiKB1dITG4/wS5nxcLOerS0OCuO8xl7T4CYKIq1iP9gW FERwi2PSSD0UBkekXXLvmjsThAqwCkhzTGoSB5LZPLdKmvuKzcG4Pbzk= X-Google-Smtp-Source: AA0mqf5CaEwzOsRkM2CW3GffNOmQ6eWQhWvm6e5oD6mwACVptAW1KgmpZ2jE6v0xc6Hwqs8d+2isyxuHEg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:a4a:b0:3cf:e138:cd80 with SMTP id c10-20020a05600c0a4a00b003cfe138cd80mr41264459wmq.78.1670003125248; Fri, 02 Dec 2022 09:45:25 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:16 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-32-tabba@google.com> Subject: [RFC PATCH kvmtool v1 31/32] pkvm: Handle (un)share hypercalls coming from the guest From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org (Un)map guest memory if it's sharing status changes. Signed-off-by: Fuad Tabba --- arm/aarch64/include/asm/kvm.h | 7 +++++ arm/kvm-cpu.c | 58 ++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/arm/aarch64/include/asm/kvm.h b/arm/aarch64/include/asm/kvm.h index 316917b..662c178 100644 --- a/arm/aarch64/include/asm/kvm.h +++ b/arm/aarch64/include/asm/kvm.h @@ -467,6 +467,13 @@ enum { /* run->fail_entry.hardware_entry_failure_reason codes. */ #define KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED (1ULL << 0) +#define ARM_SMCCC_KVM_FUNC_MEM_SHARE 3 +#define ARM_SMCCC_KVM_FUNC_MEM_UNSHARE 4 + +#define KVM_EXIT_HYPERCALL_VALID_MASK ((1ULL << ARM_SMCCC_KVM_FUNC_MEM_SHARE) | \ + (1ULL << ARM_SMCCC_KVM_FUNC_MEM_UNSHARE)) + + #endif #endif /* __ARM_KVM_H__ */ diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index cb5a92a..fcefec8 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -144,16 +144,64 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu) free(vcpu); } -bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) +static bool handle_mem_share(struct kvm_cpu *vcpu) { - switch (vcpu->kvm_run->exit_reason) { - case KVM_EXIT_HYPERCALL: - pr_warning("Unhandled exit hypercall: 0x%llx, 0x%llx, 0x%llx, 0x%llx", + u64 gpa = vcpu->kvm_run->hypercall.args[0]; + + if (!vcpu->kvm->cfg.pkvm) { + pr_warning("%s: non-protected guest memshare request for gpa 0x%llx", + __func__, gpa); + return true; + } + + map_guest_range(vcpu->kvm, gpa, PAGE_SIZE); + + return true; +} + +static bool handle_mem_unshare(struct kvm_cpu *vcpu) +{ + u64 gpa = vcpu->kvm_run->hypercall.args[0]; + + if (!vcpu->kvm->cfg.pkvm) { + pr_warning("%s: non-protected guest memunshare request for gpa 0x%llx", + __func__, gpa); + return true; + } + + unmap_guest_range(vcpu->kvm, gpa, PAGE_SIZE); + + return true; +} + +static bool handle_hypercall(struct kvm_cpu *vcpu) +{ + u64 call_nr = vcpu->kvm_run->hypercall.nr; + + switch (call_nr) + { + case ARM_SMCCC_KVM_FUNC_MEM_SHARE: + return handle_mem_share(vcpu); + case ARM_SMCCC_KVM_FUNC_MEM_UNSHARE: + return handle_mem_unshare(vcpu); + default: + pr_warning("%s: Unhandled exit hypercall: 0x%llx, 0x%llx, 0x%llx, 0x%llx", + __func__, vcpu->kvm_run->hypercall.nr, vcpu->kvm_run->hypercall.ret, vcpu->kvm_run->hypercall.args[0], vcpu->kvm_run->hypercall.args[1]); - return true; + break; + } + + return true; +} + +bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) +{ + switch (vcpu->kvm_run->exit_reason) { + case KVM_EXIT_HYPERCALL: + return handle_hypercall(vcpu); } return false; From patchwork Fri Dec 2 17:44:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13063071 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20509C4321E for ; Fri, 2 Dec 2022 17:46:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234227AbiLBRqJ (ORCPT ); Fri, 2 Dec 2022 12:46:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234272AbiLBRpj (ORCPT ); Fri, 2 Dec 2022 12:45:39 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAD32E11AA for ; Fri, 2 Dec 2022 09:45:29 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id e8-20020a05600c218800b003cf634f5280so2192863wme.8 for ; Fri, 02 Dec 2022 09:45:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MnK0SbAbrFxX8lABSVfT8kY69zxWWvrnpuk1/1B8fuk=; b=kSuBJdt+hS5x3ZHEtRSrDlEuNlbOEXIMnxwRPZ1SivIweURft1uP22Tg/yGUhCatxW 3G+9hc+K3EzL0ZAgvmTbOVgPvmGyonkqK/VvQrN7MqspXdSmMO3wCuonF/qZ8TZTn7JG TeWtmuqzBEBdUJ7jEci2npqJ1xVMHlySE9Mgs9m8E/K5jVYGmBslrxfUgIqJ2k1D7L3n ur1evyor+vBw78mHpgLnRE7tXpw/slvUBFibv+k1FijXJk9BzVfONSwJ9sWxP6lm8vre o1WaRs1Z8uI3glhb0g7EUQJltwLqoyFA5qKqk/CuwOcAlPMFHGwAURXoig2/ub4cxkh6 HnNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MnK0SbAbrFxX8lABSVfT8kY69zxWWvrnpuk1/1B8fuk=; b=hCm9wwBgu0FJEW95UNgpVNekwKlPU2vkbQElT4qRFMGdMA5fZ7eXb/jPqoLbTcaLf7 YoPYcZX25ePl3KZ/K6gJEu8TNbvq5sFHlVI6AZcRsHOORTr9dID9Ej2WyFLakUrSJZOt 1r6hs27zxoPNNK55psI1M2QuqIFqFksvLRq3klpAxZMFt/EQLeqL7vjrnlOxDB+LGk2S vM2/PSG+b22r3wZlEBziAqLGfwkdZywXOu2fRQfafvRQhq3u6AgqCQf6tCuf7Nlbmnkw HsOgfBUMkZduDgXBjwjW+yGeHbP++wOT/D2UO77fcO3MadvtKwMu4zqynN3/4fwN/vIy efpQ== X-Gm-Message-State: ANoB5pljl4DF+erej4Phmzb4SF4DD5RH/9hWV1o/l3z/15f344ZibHYV Eoh+Mzb9Te4iuF3ZoqngTzIl4RURspV3QV1TlK+eU9a8s/oVOMvE/9ZMnWUOpktvQr3DXEoVzQY /Te939W8K1KNTovdJ8WzgTs6n4fQZ57zcCk1/qoLohXHX/AwWa494T6c= X-Google-Smtp-Source: AA0mqf5nPWg/qdRIXxY5gq6kDrNPAC3zIzmRYFmoWPe7h3o/QDE4od2BgTGULPcpbtgkL4ehwyHFzh09aA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:3d08:b0:3cf:e84d:6010 with SMTP id bh8-20020a05600c3d0800b003cfe84d6010mr43895963wmb.197.1670003127164; Fri, 02 Dec 2022 09:45:27 -0800 (PST) Date: Fri, 2 Dec 2022 17:44:17 +0000 In-Reply-To: <20221202174417.1310826-1-tabba@google.com> Mime-Version: 1.0 References: <20221202174417.1310826-1-tabba@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202174417.1310826-33-tabba@google.com> Subject: [RFC PATCH kvmtool v1 32/32] pkvm: Unmap all guest memory after initialization From: Fuad Tabba To: kvm@vger.kernel.org Cc: julien.thierry.kdev@gmail.com, andre.przywara@arm.com, alexandru.elisei@arm.com, alex.bennee@linaro.org, will@kernel.org, tabba@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In pkvm, assume that all guest memory is private and unmap it by default. If it's shared and needed, the host can map it later. Signed-off-by: Fuad Tabba --- builtin-run.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builtin-run.c b/builtin-run.c index 9ec5701..23a4f8b 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -780,6 +780,9 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) if (init_list__init(kvm) < 0) die ("Initialisation failed"); + if (kvm->cfg.pkvm) + unmap_guest(kvm); + return kvm; }