From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB0DC46AA7C for ; Tue, 4 Aug 2026 15:23:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785856984; cv=none; b=jrnBUkV4H4sgu99MT7Bw+Zh7V1GZkAxKsx3aaE7AIK3/iDKu3wvWIQipzSiq8BPe6Q6ycub2wA+6c+xl+K6GPBqjUcGA7msl46qTLEgjMhvvaUSWiReRo3fpHZMEdkSZW5IDxAJ8pCv6k6cIPbw8Tzn51U7m1xMbqy3NXOK7j/k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785856984; c=relaxed/simple; bh=ARPOQ+c6UDtO7LhU3svWeAQohZ9g6PrIcJqv4Q8q4tM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Pu7ON4E8eB6PUazI8ZoEpHZcvEWKOgE5J4tUTiEdzt22LDUZgZAfFZoamO/fuPJOXXNvjCWRrjj889ILYemI5xAhnOMkWFstTTdTVGdwAimQN9CB6LBgz9/DjlfesG2Auv7A8jiiU7tV5WPk9rQd09HW4IARsNtIaUq0iAJITTE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fK0WEWXs; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fK0WEWXs" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-498012a61f6so60545e9.0 for ; Tue, 04 Aug 2026 08:22:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785856978; x=1786461778; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=57eVHha9yn05PRoBRgBz6lC7HiDLbzFyMEEXKt7qjx0=; b=fK0WEWXsRectEafZYgVdJtYbSZKZXlpLvwuZqFDXhwF15zrI2kiOW2Jh1nKQu5sX6u 5XcifA//2RS24k1V9gRybs6VGw0qmYhUpfJ4qP6NXEL0+oMhYbDfIo8ZR779uOK0MGh8 GfDOeyZ+z1VBMcy44qpZzGrtI5ieYXh1MbijUVOCfvxEbE0XBDJ7Y7KQmMOrKZPaiobN FiJAdpi7fP2DSjXl9aM4NDAdlnSVcQgWD8IIOg3r0+nnjEtFs0zfncYGguSSUiP3UlD3 hQJxohnqol7I6lvdhGm0VcL0TRxdRCt64WwGp+ywRYqNhsqk0kV0BmfnknKr2V65XC+R xarw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785856978; x=1786461778; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=57eVHha9yn05PRoBRgBz6lC7HiDLbzFyMEEXKt7qjx0=; b=rrf6x1KVnc8Rcvi57xgBn6J5I8EjLM3XNimoHxgdKwl7E8d5ob+aqizTz7dVsfdqGi UfmU2Po1ERQmjz3ZiQXxRKNoayOIKTmNycjKtgcmv0NWWlHtmsCfpaF5a/aSrqeS2ocD +CKKBEzhTgi585eHYhkCWsp9nwb1J9af0+/K0Bpv+LL0HMDG5iV7cM8h9rv/SdGCQMQc NmNG4AKuVe/cTbmPmp/R972+EeuiIqQ7Zap49Q+15rkFMA/JVOAze3vZKlF0f3ebWHs+ 1KCbQrdF7CG0utfF9cUn+KfNDrPr3RP4vHyfu7ZKPV6Qh4A36wHxP7LtKNjguz7HjGr0 gZzw== X-Forwarded-Encrypted: i=1; AHgh+RrtXpJJCZJaa+GvDnj76gN+GFacX5fEHBviU7iLizxamnxwLk0zxc++0fWrFzHG4ihH03wJqqZ35DM1OCw=@vger.kernel.org X-Gm-Message-State: AOJu0YyJ7Ek9lVUOuCBn9FozTSCKifX4SyVLB3638ST4m39fchpLSn+M erYMfDB9yy9t5SERBOPIopw1PCQCmJ4qq0hn059l3FrdegINwiXIzpI85LjQN5dNiA== X-Gm-Gg: AR+sD10FsOnxuyVaxyDCZ/7h7W3I3wlGgxphNGZgkENeIVpExQX4kVtc1jSggtErQie cT4wIsR8rcJzt10luZqHwgGR99KHa00dl94Qs8KfMDzB+U96er8RbRwkG0PtCjTBL8pOPgS1Izf Lr3ZF7VExQo83yf8255hqK9v0CDeKTOXh7u2KNKSO+iWR2JAXRrwdrA7lr0yZXXg6JptfKARqUS 2UOiocHnf276G1tjn+U63tAvBu1Q8AElkb3/IcoRt41M3z7GAWIH0mmxc5au8PzaPZGCcz7aE4b kovidag4VeauTehxzvxO1V+yer2cQRklcvKfU2VoKdMzo8lskK8NI8EbJWzLGiwU6WuqKR3GIU8 peiE/aT42mCVcGMkZRMPQ/IB5osENVfPVrVuwUQHIlJdGeLvGpV3CZ/Z4V4ASYozvm4T8Bglxf3 srVPuRN93lbuc94s8f2pNyuqJw3gwtFQi1S0CWz9JdNL9iQVliCSxaoctZOyaC69Y16/gKwFvxK dzwUFGV5q1c4vPVJLhjFJ9fdC1kLy/x X-Received: by 2002:a05:600c:4987:b0:495:51b1:3b7c with SMTP id 5b1f17b1804b1-4994a3818b8mr1298075e9.8.1785856977525; Tue, 04 Aug 2026 08:22:57 -0700 (PDT) Received: from google.com (63.235.189.35.bc.googleusercontent.com. [35.189.235.63]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49949fcb0c5sm87206685e9.4.2026.08.04.08.22.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2026 08:22:56 -0700 (PDT) Date: Tue, 4 Aug 2026 15:22:52 +0000 From: Sebastian Ene To: Fuad Tabba Cc: alexandru.elisei@arm.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, android-kvm@google.com, catalin.marinas@arm.com, dbrazdil@google.com, joey.gouly@arm.com, kees@kernel.org, mark.rutland@arm.com, maz@kernel.org, oupton@kernel.org, perlarsen@google.com, qperret@google.com, rananta@google.com, smostafa@google.com, suzuki.poulose@arm.com, tglx@kernel.org, vdonnefort@google.com, bgrzesik@google.com, will@kernel.org, yuzenghui@huawei.com Subject: Re: [PATCH 02/14] KVM: arm64: Track host-unmapped MMIO regions in a static array Message-ID: References: <20260310124933.830025-1-sebastianene@google.com> <20260310124933.830025-3-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Mar 12, 2026 at 07:05:16PM +0000, Fuad Tabba wrote: > Hi Sebastian, > > On Tue, 10 Mar 2026 at 12:49, Sebastian Ene wrote: > > > > Introduce a registry to track protected MMIO regions that are unmapped > > from the host stage-2 page tables. These regions are stored in a > > fixed-size array and their ownership is donated to the hypervisor during > > initialization to ensure host-exclusion and persistent tracking. > > > > Signed-off-by: Sebastian Ene > > --- > > arch/arm64/include/asm/kvm_pkvm.h | 10 ++++++++++ > > arch/arm64/kvm/hyp/nvhe/mem_protect.c | 3 +++ > > arch/arm64/kvm/hyp/nvhe/setup.c | 25 +++++++++++++++++++++++++ > > 3 files changed, 38 insertions(+) > > > > diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h > > index 757076ad4ec9..48ec7d519399 100644 > > --- a/arch/arm64/include/asm/kvm_pkvm.h > > +++ b/arch/arm64/include/asm/kvm_pkvm.h > > @@ -17,6 +17,16 @@ > > > > #define HYP_MEMBLOCK_REGIONS 128 > > > > +#define PKVM_PROTECTED_REGS_NUM 8 > > + > > +struct pkvm_protected_reg { > > + u64 start_pfn; > > + size_t num_pages; > > +}; > > + > > +extern struct pkvm_protected_reg kvm_nvhe_sym(pkvm_protected_regs)[]; > > +extern unsigned int kvm_nvhe_sym(num_protected_reg); > > + > > int pkvm_init_host_vm(struct kvm *kvm); > > int pkvm_create_hyp_vm(struct kvm *kvm); > > bool pkvm_hyp_vm_is_created(struct kvm *kvm); > > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > > index 0808367c52e5..7c125836b533 100644 > > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > > @@ -23,6 +23,9 @@ > > > > struct host_mmu host_mmu; > > > > +struct pkvm_protected_reg pkvm_protected_regs[PKVM_PROTECTED_REGS_NUM]; > > +unsigned int num_protected_reg; > > + > > static struct hyp_pool host_s2_pool; > > > > static DEFINE_PER_CPU(struct pkvm_hyp_vm *, __current_vm); > > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > > index 90bd014e952f..ad5b96085e1b 100644 > > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > > @@ -284,6 +284,27 @@ static int fix_hyp_pgtable_refcnt(void) > > &walker); > > } > > > > +static int unmap_protected_regions(void) > > I think the name of this function is confusing. It's not really > unmapping, it's donating the regions to hyp. Maybe > donate_protected_regions() or claim_protected_mmio_regions to reflect > that we are doing an ownership transfer? I renamed it to donate_protected_mmio_regions to make it more clear in regards to what it does. > > > +{ > > + struct pkvm_protected_reg *reg; > > + int i, ret, j = 0; > > Please don't interleave the iterators with ret. Moreover, you can > define ret inside the loop below. Also, you don't need to initialze j. > > > + > > + for (i = 0; i < num_protected_reg; i++) { > > + reg = &pkvm_protected_regs[i]; > > + for (j = 0; j < reg->num_pages; j++) { > > + ret = __pkvm_host_donate_hyp_mmio(reg->start_pfn + j); > > + if (ret) > > + goto err_setup; > > + } > > + } > > + > > + return 0; > > +err_setup: > > + for (j = j - 1; j >= 0; j--) > > + __pkvm_hyp_donate_host_mmio(reg->start_pfn + j); > > This rolls back the regions only for the latest `i` iteration, not all > the `i` iterations. > I re-wrote this bit since the last version of the __pkvm_host_donate_hyp_mmio accepts a size as well. > How about (not tested): > > + err_setup: > + while (j--) > + __pkvm_hyp_donate_host_mmio(reg->start_pfn + j); > + > + while (i--) { > + reg = &pkvm_protected_regs[i]; > + for (j = reg->num_pages - 1; j >= 0; j--) > + __pkvm_hyp_donate_host_mmio(reg->start_pfn + j); > + } > > Thanks, > /fuad > Cheers, Sebastian > > > + return ret; > > +} > > + > > void __noreturn __pkvm_init_finalise(void) > > { > > struct kvm_cpu_context *host_ctxt = host_data_ptr(host_ctxt); > > @@ -324,6 +345,10 @@ void __noreturn __pkvm_init_finalise(void) > > if (ret) > > goto out; > > > > + ret = unmap_protected_regions(); > > + if (ret) > > + goto out; > > + > > ret = hyp_ffa_init(ffa_proxy_pages); > > if (ret) > > goto out; > > -- > > 2.53.0.473.g4a7958ca14-goog > >