From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06441C433F5 for ; Mon, 4 Oct 2021 10:07:31 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 C8E7F611C5 for ; Mon, 4 Oct 2021 10:07:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C8E7F611C5 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=+XABC4CCcVagLDKayjz608QS5Ysa0LveTvPHLDvCEBQ=; b=K7eYbbPnalWdgS pFDk/LR0h+9XJqylxyN5+mcVyyBXrV0Dx0c8tOZwMPCCV5mLmsXwwjxnd/U2NszYLl1I3eChWP/Tk H3IYtHKaULLQ+lueCzfO+wovPidufD3z1JK6dpNMXexWGMszotuSDp6lsW+xyG+7hinTanRApoTe5 LMJF4utwcjwX22mox7nJRogBQWNMplYOgdg5UZPsvInyk8BdGCwaiQcV+RaI64BP76fdDrrsNXsEC rD6xs3E4+8zxfQLX7Fi2yxzD0Znah2m+NJZFq0A+k0V37tFyRY0tYvQ6bcWBwcTtrje9EYWWxqEHk duJkLE92zXMWxgyzrBTg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mXKqb-005uZt-1o; Mon, 04 Oct 2021 10:05:25 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mXKqX-005uZA-EY for linux-arm-kernel@lists.infradead.org; Mon, 04 Oct 2021 10:05:22 +0000 Received: by mail-wm1-x32d.google.com with SMTP id o4-20020a05600c510400b0030d55d6449fso5936577wms.5 for ; Mon, 04 Oct 2021 03:05:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=vbaYN6Cj0naTpTKsC/ilkJz/q62BHSUwgRaYb1zy820=; b=LluNYf2Z05QVWW3ZMGQu7ViGnKRokxh8x9obFg78/LobCRNeHuSjcYtPJ8nsGZ7Y3a oSErIdBzV56H4zULb8mhOSjzbHshp2yY/bbB1Ckja475D5j27k9cpbv4SqaKqOrSR106 K5uwNEZobD09+yLG0U8fGSijBJLNmaiHW8pGKM0ZDc+Nc4NVFkhg9eVxEoimDsmjUAUW 5SnFBm7ycmllCe9LrxSMfJTbgawR/68sw41PAZa1BungeaKMjD2rC7cXR0GjePmdYpo1 hfxUumMppw/hJxPsrh8wIaAVleboSvxMSBPzKwoL1RZOcQrCxmUAnAe+CBFw6QufGT93 ncGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vbaYN6Cj0naTpTKsC/ilkJz/q62BHSUwgRaYb1zy820=; b=OG02kd1XZR+9AjR300NaN8lhm61afOJbOfn6st0GsmctTFjIpZRg8rYuRW6aDtm6uM yZOyw+Xv4LvqWOGMnHc9mLUkJC2UAhBAUtbEXd3/q35tEMlCaCfMIXRbBo0PTi0pK4Pc MBj9tpKAgNrgcNC+g8H1FAzplb+f0YiK+gJNODciqVK3769Z3ypvZ+/qQxr3//nvtH5l F5MaKjAZ60T7aqxuJ0/KargOirNhNa4alanZrai59KYezTCMzhkTNWg/dAh09uxp0pjA Ed5PshPfGwy+NPSHBKeI+/sQ1ncmccOfGqtUE+kZRxZKqP53PX9+Gm56YkIrGYnN5hoy RWOA== X-Gm-Message-State: AOAM5333CLOvB5lJdA627PWAr5YXw0nvAh7TberBteNiKmg31VHLLGLV lYMV116Ug5Ij+EAx3Qu5RCJqHw== X-Google-Smtp-Source: ABdhPJzctEI2RjiPKdjlUZAMqkTfZuBtvB97o4FGjBX56pMR2EnD7CcYgItKQi/sFeYGsYqR7QX98w== X-Received: by 2002:a1c:f70a:: with SMTP id v10mr18110666wmh.3.1633341916520; Mon, 04 Oct 2021 03:05:16 -0700 (PDT) Received: from google.com ([2a00:79e0:d:210:669b:5b16:60b7:a3d4]) by smtp.gmail.com with ESMTPSA id d2sm8169477wrs.73.2021.10.04.03.05.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Oct 2021 03:05:16 -0700 (PDT) Date: Mon, 4 Oct 2021 11:05:12 +0100 From: Quentin Perret To: Marc Zyngier Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Fuad Tabba , David Brazdil , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH 1/2] KVM: arm64: Fix host stage-2 PGD refcount Message-ID: References: <20211004090328.540941-1-qperret@google.com> <20211004090328.540941-2-qperret@google.com> <87bl45ru66.wl-maz@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <87bl45ru66.wl-maz@kernel.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211004_030521_537441_354FD7C0 X-CRM114-Status: GOOD ( 28.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hey Marc, On Monday 04 Oct 2021 at 10:55:13 (+0100), Marc Zyngier wrote: > Hi Quentin, > > On Mon, 04 Oct 2021 10:03:13 +0100, > Quentin Perret wrote: > > > > The KVM page-table library refcounts the pages of concatenated stage-2 > > PGDs individually. However, the host's stage-2 PGD is currently managed > > by EL2 as a single high-order compound page, which can cause the > > refcount of the tail pages to reach 0 when they really shouldn't, hence > > corrupting the page-table. > > nit: this comment only applies to the protected mode, right? As far as > I can tell, 'classic' KVM is just fine. Correct, this really only applies to the host stage-2, which implies we're in protected mode. I'll make that a bit more explicit. > > Fix this by introducing a new hyp_split_page() helper in the EL2 page > > allocator (matching EL1's split_page() function), and make use of it > > uber nit: split_page() is not an EL1 function. more of a standard > kernel function. Fair enough :) > > from host_s2_zalloc_page(). > > > > Fixes: 1025c8c0c6ac ("KVM: arm64: Wrap the host with a stage 2") > > Suggested-by: Will Deacon > > Signed-off-by: Quentin Perret > > --- > > arch/arm64/kvm/hyp/include/nvhe/gfp.h | 1 + > > arch/arm64/kvm/hyp/nvhe/mem_protect.c | 6 +++++- > > arch/arm64/kvm/hyp/nvhe/page_alloc.c | 14 ++++++++++++++ > > 3 files changed, 20 insertions(+), 1 deletion(-) > > > > diff --git a/arch/arm64/kvm/hyp/include/nvhe/gfp.h b/arch/arm64/kvm/hyp/include/nvhe/gfp.h > > index fb0f523d1492..0a048dc06a7d 100644 > > --- a/arch/arm64/kvm/hyp/include/nvhe/gfp.h > > +++ b/arch/arm64/kvm/hyp/include/nvhe/gfp.h > > @@ -24,6 +24,7 @@ struct hyp_pool { > > > > /* Allocation */ > > void *hyp_alloc_pages(struct hyp_pool *pool, unsigned short order); > > +void hyp_split_page(struct hyp_page *page); > > void hyp_get_page(struct hyp_pool *pool, void *addr); > > void hyp_put_page(struct hyp_pool *pool, void *addr); > > > > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > > index bacd493a4eac..93a79736c283 100644 > > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > > @@ -35,7 +35,11 @@ const u8 pkvm_hyp_id = 1; > > > > static void *host_s2_zalloc_pages_exact(size_t size) > > { > > - return hyp_alloc_pages(&host_s2_pool, get_order(size)); > > + void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size)); > > + > > + hyp_split_page(hyp_virt_to_page(addr)); > > The only reason this doesn't lead to a subsequent memory leak is that > concatenated page tables are always a power of two, right? Indeed, and also because the host stage-2 is _never_ freed, so that's not memory we're going to reclaim anyway -- we don't have an implementation of ->free_pages_exact() in the host stage-2 mm_ops. > If so, that deserves a comment, because I don't think this works in > the general case unless you actively free the pages that are between > size and (1 << order). Ack, that'll probably confuse me too in a few weeks, so a comment won't hurt. I'll re-spin shortly. Thanks, Quentin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel