From patchwork Wed Sep 16 17:34:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 11780269 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9215A14F6 for ; Wed, 16 Sep 2020 17:36:26 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5EE4520708 for ; Wed, 16 Sep 2020 17:36:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="uZOtRVpo"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="HNZo35gC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5EE4520708 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=RYlZBH8XnSHhHgTuXGlV4tfM+xwnaJbV7kxUJcxqCkk=; b=uZOtRVpoAXdlYnAc1yiBQ4MYg CdJM+mXuI25+ysGlRa6pL8N7faQPZu15rZIrO0jgWarJ7Sl6D8dKfShgKHdZdPbFWdvORA+W0SaQG 6rf0v0cITotLlc/VOXG5l9qTQmap8DqQff6RwxIPj+BFTzEtppwMphdkjCew4zROnbrW8kFQGOr7U cIdrJ7LstSo6ALtHvo8VSAh6phz6vGbBLXVRAgIxpyA9RqLYQjb9gP0JKJkRDuESv5YRg4mUjCsFF wZJTmmGKE2e0is95oj5UmUqjCwqJJos8cwRF68iZhFroCMGzwqgz0rVFgm7+/waKZVdYhB4rGALf3 1lmtZKwaw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kIbLn-0001K0-Vd; Wed, 16 Sep 2020 17:36:12 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kIbKi-0000vr-3m for linux-arm-kernel@lists.infradead.org; Wed, 16 Sep 2020 17:35:05 +0000 Received: by mail-wm1-x343.google.com with SMTP id a9so3902062wmm.2 for ; Wed, 16 Sep 2020 10:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3ZouLpoLHZGhnRn3IE3lAdYTtqQtOaDPWcW0BXT72MM=; b=HNZo35gC+J7olQM/J4RAmLlZS6QxVf8SG/2BDEI9P1bohZ+P5Q96wTZVwc7II0n9BL cgE7q3s2HuB8SyeYAwP76jfvS9ilgnaSdWf3BN5CpaP3PpCWpHo7OyCzXoLczzUjYIh+ TAvf72HVFbYYjUfwXRs9KQWyCZzjv5SmQjRTwHXgeAoUK3/2zXTwbrvIkPx4Um1P5RvS TU1N+Ppw0DKArVS8ynQ4bf+jV171VTr1y4ZU53me1jkwW1cniiMaCioHU1u/AMChZdp6 1BnnWaIQvyESEBhMsuSyugrLCEkTdp5crIYFCA9FneqGow+l1YuEeHkCEHhBbzEX3vON E0iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3ZouLpoLHZGhnRn3IE3lAdYTtqQtOaDPWcW0BXT72MM=; b=eLTFncN2Sw+AMkTwhKpSwv8upD4RgpXGBtBN04m07Q4D2hyXuFZTX9DaEU54x2hEgo j2Xex7dBhLn8YL+sVjY5cfob++DVoAIoZpWEcbcnVypnw1hRzoO0d1Rq4Q/l8wpB+cTl fOolesoIf9K+5tPOaak/2wHr7nkno4iQrVKqeIjBRPMKOwxgLAVKF43Mdd6BXJLRC+nw oSRsL1h4kEyWQV7ADn2iQ+FZ2Zwvsg7fyNCUPMUxBgvV1QphGsRxOZ/OYSkWoraVPoH8 N8943Zv9SOiA5vvnRw4KgrMQIB7Ryrk38aAyIUFk6TARO0FnnAqDJuskrpaNHvhERZHf X50A== X-Gm-Message-State: AOAM533v5IxGKHp0vUNJrP6UUIVJOrEsl8MfVZNtyYBApk1XFta1ZUgK Pt1c84n2cDQ8x+e4Ut6+J52raEAfSi7zFU31 X-Google-Smtp-Source: ABdhPJyjAeHVuIWwfCgiltujOqCdZJciMMvPMbdeAr38UjlrUERhWuN+mDjxXhXo26gYVThTzJRNbg== X-Received: by 2002:a1c:2e08:: with SMTP id u8mr6233481wmu.156.1600277702955; Wed, 16 Sep 2020 10:35:02 -0700 (PDT) Received: from localhost ([2a01:4b00:8523:2d03:e49d:f6be:d31b:ad3c]) by smtp.gmail.com with ESMTPSA id p1sm8808360wma.0.2020.09.16.10.35.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Sep 2020 10:35:01 -0700 (PDT) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v3 09/11] kvm: arm64: Mark hyp stack pages reserved Date: Wed, 16 Sep 2020 18:34:37 +0100 Message-Id: <20200916173439.32265-10-dbrazdil@google.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200916173439.32265-1-dbrazdil@google.com> References: <20200916173439.32265-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200916_133504_204589_A7C06652 X-CRM114-Status: GOOD ( 18.67 ) X-Spam-Score: -15.7 (---------------) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-15.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:343 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -7.5 USER_IN_DEF_SPF_WL From: address is in the default SPF white-list -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.5 ENV_AND_HDR_SPF_MATCH Env and Hdr From used in default SPF WL Match -0.0 DKIMWL_WL_MED DKIMwl.org - Medium sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kernel-team@android.com, Suzuki K Poulose , Catalin Marinas , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Marc Zyngier , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org In preparation for unmapping hyp pages from host stage-2, allocate/free hyp stack using new helpers which automatically mark the pages reserved. Signed-off-by: David Brazdil --- arch/arm64/kvm/arm.c | 49 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 7af9809fa193..58d7d614519b 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1456,13 +1456,58 @@ static int init_subsystems(void) return err; } +/* + * Alloc pages and mark them reserved so the kernel never tries to + * take them away from the hypervisor. + */ +static unsigned long alloc_hyp_pages(gfp_t flags, unsigned int order) +{ + struct page *page; + unsigned long i; + + page = alloc_pages(flags, order); + if (!page) + return 0; + + for (i = 0; i < (1ul << order); ++i) + mark_page_reserved(page + i); + + return (unsigned long)page_address(page); +} + +static unsigned long alloc_hyp_page(gfp_t flags) +{ + return alloc_hyp_pages(flags, 0); +} + +/* + * Free pages which were previously marked reserved for the hypervisor. + */ +static void free_hyp_pages(unsigned long addr, unsigned int order) +{ + unsigned long i; + struct page *page; + + if (!addr) + return; + + page = virt_to_page(addr); + for (i = 0; i < (1ul << order); ++i) + free_reserved_page(page + i); +} + +static void free_hyp_page(unsigned long addr) +{ + return free_hyp_pages(addr, 0); +} + static void teardown_hyp_mode(void) { int cpu; free_hyp_pgds(); for_each_possible_cpu(cpu) - free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); + free_hyp_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); } /** @@ -1486,7 +1531,7 @@ static int init_hyp_mode(void) for_each_possible_cpu(cpu) { unsigned long stack_page; - stack_page = __get_free_page(GFP_KERNEL); + stack_page = alloc_hyp_page(GFP_KERNEL); if (!stack_page) { err = -ENOMEM; goto out_err;