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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id AF934C3ABC9 for ; Fri, 16 May 2025 13:00:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WrZr+9kvdLhQCEHYgExLyOxJH0JqtQdJZj1do8Bl26o=; b=z66gHUy2W2SKZLztj70ElQYv8/ ARstmsRZdW2/tuIyMkBF2MoQUqYytToj5RW/v058gHUM3E7WdzfdTFkoIO/H4i30lH1fA7AkmZ8Bl IP46aNIAuW8hXtr6Zi0WcSE2E6dBkbKNxEpdYP9/r2WYfYIJV+WHYYlaqw2YcgfOuBpsajeMJ9Io8 vigC0KZva8gJS1O6PAYRSkYvTYdXvA58IeH7b4g7nKMYZNdNYpgqP7J3YmULaXWCyv9aMMF4uBInX +JH5jqI/Xsdooseh08ZjLr+uHCGvqzGcyLeJtuQb+JXslvxt0zMHSizmd6Mn5FMFOpjvT3RKa14dc kM+ybBHw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uFufT-00000003SKm-1dL6; Fri, 16 May 2025 13:00:03 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uFudQ-00000003S1c-01DC for linux-arm-kernel@lists.infradead.org; Fri, 16 May 2025 12:57:56 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 304BD629F9; Fri, 16 May 2025 12:57:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2798C4CEE4; Fri, 16 May 2025 12:57:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747400274; bh=YZxxILgmfc1qF1rrNWG6/WtUXq2234pqpZt+RioE+Oc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=c3wJ9KUewGkliyK1GQc3uMF2StFwkDkd44yXnLyqtpOXcMZY+iFDdhz1CMgJvR1JS QnxWz3HQ7VsoXpbIiUeguYfzo+JHtEjj1Sp3o7PjYG4oOGzi2thZ8XU1Sg2c9wX10g pzPgsVc5yKRXhPo0LRSO+smBm9BVkKmLcZWORBURWZO4i7gHN+Nd05jXe8PbfFPdrL IB5xV8xKWoJ5Dl6qdc69cCSvEBJK6we9xOT2fCMDD+AP/O6gT7aBINVnCXv80Jl3RU E4BSWm+viNGiy5iex/O5cn6lomBwWInw/9XIA9yJe03a3bTxekkYEPg1LjmXgzJ2IO FukrHGfuxqiVw== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1uFudM-00FYgz-LR; Fri, 16 May 2025 13:57:52 +0100 Date: Fri, 16 May 2025 13:57:52 +0100 Message-ID: <86a57cg1bj.wl-maz@kernel.org> From: Marc Zyngier To: Vincent Donnefort Cc: oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, qperret@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v4 02/10] KVM: arm64: Introduce for_each_hyp_page In-Reply-To: <20250509131706.2336138-3-vdonnefort@google.com> References: <20250509131706.2336138-1-vdonnefort@google.com> <20250509131706.2336138-3-vdonnefort@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: vdonnefort@google.com, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, qperret@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 09 May 2025 14:16:58 +0100, Vincent Donnefort wrote: > > Add a helper to iterate over the hypervisor vmemmap. This will be > particularly handy with the introduction of huge mapping support > for the np-guest stage-2. > > Signed-off-by: Vincent Donnefort > > diff --git a/arch/arm64/kvm/hyp/include/nvhe/memory.h b/arch/arm64/kvm/hyp/include/nvhe/memory.h > index eb0c2ebd1743..676a0a13c741 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/memory.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/memory.h > @@ -96,24 +96,24 @@ static inline struct hyp_page *hyp_phys_to_page(phys_addr_t phys) > #define hyp_page_to_virt(page) __hyp_va(hyp_page_to_phys(page)) > #define hyp_page_to_pool(page) (((struct hyp_page *)page)->pool) > > -static inline enum pkvm_page_state get_host_state(phys_addr_t phys) > +static inline enum pkvm_page_state get_host_state(struct hyp_page *p) > { > - return (enum pkvm_page_state)hyp_phys_to_page(phys)->__host_state; > + return (enum pkvm_page_state)p->__host_state; I'm not quite sure why we have this cast the first place. If we are so keen on type consistency, why isn't __host_state an enum pkvm_page_state the first place? > } > > -static inline void set_host_state(phys_addr_t phys, enum pkvm_page_state state) > +static inline void set_host_state(struct hyp_page *p, enum pkvm_page_state state) > { > - hyp_phys_to_page(phys)->__host_state = state; > + p->__host_state = state; > } > > -static inline enum pkvm_page_state get_hyp_state(phys_addr_t phys) > +static inline enum pkvm_page_state get_hyp_state(struct hyp_page *p) > { > - return hyp_phys_to_page(phys)->__hyp_state_comp ^ PKVM_PAGE_STATE_MASK; > + return p->__hyp_state_comp ^ PKVM_PAGE_STATE_MASK; And we don't seem that bothered here. > } > > -static inline void set_hyp_state(phys_addr_t phys, enum pkvm_page_state state) > +static inline void set_hyp_state(struct hyp_page *p, enum pkvm_page_state state) > { > - hyp_phys_to_page(phys)->__hyp_state_comp = state ^ PKVM_PAGE_STATE_MASK; > + p->__hyp_state_comp = state ^ PKVM_PAGE_STATE_MASK; > } > > /* > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > index 23544928a637..4d269210dae0 100644 > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > @@ -60,6 +60,9 @@ static void hyp_unlock_component(void) > hyp_spin_unlock(&pkvm_pgd_lock); > } > > +#define for_each_hyp_page(start, size, page) \ > + for (page = hyp_phys_to_page(start); page < hyp_phys_to_page((start) + (size)); page++) > + Huh. This really should be something like: #define for_each_hyp_page(__p, __st, __sz) \ for (struct hyp_page *__p = hyp_phys_to_page(__st), \ *__e = __p + ((__sz) >> PAGE_SHIFT); \ __p < __e; __p++) so that: - the iterator variable comes first and looks like a normal for-loop - the iterator has a scope limited to the loop - we don't evaluate parameters multiple times - we don't evaluate the end of the loop multiple times - we try not to reuse common variable names > static void *host_s2_zalloc_pages_exact(size_t size) > { > void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size)); > @@ -481,7 +484,8 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range) > return -EAGAIN; > > if (pte) { > - WARN_ON(addr_is_memory(addr) && get_host_state(addr) != PKVM_NOPAGE); > + WARN_ON(addr_is_memory(addr) && > + get_host_state(hyp_phys_to_page(addr)) != PKVM_NOPAGE); > return -EPERM; > } > > @@ -507,10 +511,10 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size, > > static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_state state) > { > - phys_addr_t end = addr + size; > + struct hyp_page *page; and then these extra declarations can go. Thanks, M. -- Without deviation from the norm, progress is not possible.