From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=113.46.200.216 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796273835; b=DyPATqMqMyiLrJ3qys1gwXC4/31YVGIIqIyf2P8oGL/Did7dTWn6ee98ki0qWfjepxU+ XD740a02tIZHeIlNHWcP5aAn/r59mjpgnAbDarcxjFCrMMsbX0fGsXa1r2q94zDMX7JMc LBFWVoGIcszImy97puZdHjhDvIOzmyg86mfNzZu2peExUSttcvfYkUIVdQbPkFlgrjRcD QU3nU7jkIGo4FzwN0PCFTfGIncbUE76HH/0FYc47gG9DQNO3qhOy3D1L6JcJX9P06VJw9 Rpmxgf6GCTGtXcpCVbfD2TLwSHFU8s/fdbX2CKv9Lvs3HpCEFYETj6FLjXU68930epw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273835; h=DMARC-Filter:dkim-signature:Received:Received:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:CC:References:From: In-Reply-To:Content-Type:Content-Transfer-Encoding:X-Originating-IP: X-ClientProxiedBy; bh=pSs9AjqN7jP+RG/2D2s8sfE64bURdsZp/Yz1Yc/FWzA=; b=drM2/AEAQF9L6RB/j+pCHbvp3pmVPU0zW3zHobyN83ailM1xK5Cx6zjlgO3bIThI18Oc xFhH3gFq9bzkACyCEGx0E/+PMDHi2bmTyoUIHuMFVhKHrkg2UOmKoK86BB13ahqBg86B8 H7YfBNgfRApflZquK+NwGYzorojCfh+YmmiZuKFRKqhhK0t0Vbta17MS1Mj7gjJyCbUTn /UfFnF10FQFLsrAIvCzWZp7PFi1jiQ2PaQXocsPT9a8HhQGH9f3zt9Peal0AYMknyAp8E J3VAKtD0zFtjJL6nI6wXM3yMbXcbEH7fk9dkLUMwQExnY5myvM2J5/MhXlBIALlzLyA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=CJeBtLZK; arc=none smtp.remote-ip=113.46.200.216 Received: from canpmsgout01.his.huawei.com (canpmsgout01.his.huawei.com [113.46.200.216]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62B19606A322; Thu, 3 Dec 2026 04:57:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 62B19606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=CJeBtLZK dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=pSs9AjqN7jP+RG/2D2s8sfE64bURdsZp/Yz1Yc/FWzA=; b=CJeBtLZKT/jh7jJmdgTNJgMsYwHtHFfet+PE/emFqOFDfheZjLYJCsZ9ckrQ5vLxRjqBIud3J rk20moiSiU0Xj8FtPPy18yGVm6um8GmUSB1B8ZBV1Q2kzNaYFTX+BSP8l2Y1QojkBOiuynQfTcR KHHSJL1wy4m7c4DHsGrgy2M= Received: from mail.maildlp.com (unknown [172.19.163.104]) by canpmsgout01.his.huawei.com (SkyGuard) with ESMTPS id 4jg2Q56rM1z1T4Fl; Thu, 3 Dec 2026 12:55:57 +0800 (CST) Received: from kwepemr100010.china.huawei.com (unknown [7.202.195.125]) by mail.maildlp.com (Postfix) with ESMTPS id D8AC84056E; Thu, 3 Dec 2026 12:56:58 +0800 (CST) Received: from [10.67.120.103] (10.67.120.103) by kwepemr100010.china.huawei.com (7.202.195.125) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Thu, 3 Dec 2026 12:56:57 +0800 Message-ID: <0a0952f9-30b3-4799-b3fc-fb871a7f40d8@huawei.com> Date: Thu, 3 Dec 2026 12:56:57 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 13/15] KVM: arm64: Add HDBSS buffer size ioctl for dirty-bitmap mode To: Leonardo Bras CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , References: <20260929103655.85107-1-zhengtian10@huawei.com> <20260929103655.85107-14-zhengtian10@huawei.com> <5a096cca-713a-4449-83dc-c763dc06c085@huawei.com> From: Tian Zheng In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: kwepems100002.china.huawei.com (7.221.188.206) To kwepemr100010.china.huawei.com (7.202.195.125) On 11/6/2026 11:37 PM, Leonardo Bras wrote: > On Fri, Nov 06, 2026 at 06:27:27PM +0800, Tian Zheng wrote: >> >> >> On 11/2/2026 11:12 PM, Leonardo Bras wrote: >>> On Tue, Sep 29, 2026 at 06:36:53PM +0800, Tian Zheng wrote: >>>> In dirty-bitmap mode, a larger HDBSS buffer lets the guest dirty more >>>> pages between VM exits, at the cost of memory. The default of one page >>>> per vCPU is a reasonable starting point, so allow userspace to opt >>>> into a larger buffer via KVM_CAP_ARM_HDBSS_BUFFER_SIZE. >>>> >>>> The size is specified in bytes through KVM_ENABLE_CAP and must be a >>>> power of two in [PAGE_SIZE, SZ_2M]: the upper bound is the largest >>>> HDBSSBR_EL2.SZ encoding, and the power-of-two requirement matches the >>>> buddy allocator. It must be set before any vCPU is created: -EINVAL >>>> after vCPUs exist, -EBUSY if already set. >>>> >>>> The capability is rejected in dirty-ring mode, where the size is >>>> pinned by kvm_arch_dirty_ring_size_updated(). >>>> >>>> KVM_CHECK_EXTENSION returns the configured size for a VM, or SZ_2M >>>> when queried globally with a NULL kvm. >>>> >>>> Signed-off-by: Tian Zheng >>>> --- >>>> arch/arm64/kvm/arm.c | 34 ++++++++++++++++++++++++++++++++++ >>>> include/uapi/linux/kvm.h | 1 + >>>> 2 files changed, 35 insertions(+) >>>> >>>> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c >>>> index 5a7046acb62e..eadbf3a88d31 100644 >>>> --- a/arch/arm64/kvm/arm.c >>>> +++ b/arch/arm64/kvm/arm.c >>>> @@ -19,6 +19,7 @@ >>>> #include >>>> #include >>>> #include >>>> +#include >>>> #include >>>> #include >>>> #include >>>> @@ -203,6 +204,31 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, >>>> r = 0; >>>> set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags); >>>> break; >>>> + case KVM_CAP_ARM_HDBSS_BUFFER_SIZE: { >>>> + u64 size = cap->args[0]; >>>> + >>>> + if (!system_supports_hdbss()) >>>> + break; >>>> + if (kvm->dirty_ring_size) >>>> + break; >>>> + if (size < KVM_ARM_HDBSS_DEFAULT_SIZE || >>>> + size > KVM_ARM_HDBSS_MAX_SIZE) >>>> + break; >>>> + if (!is_power_of_2(size)) >>>> + break; >>>> + >>>> + mutex_lock(&kvm->lock); >>>> + if (kvm->created_vcpus) { >>>> + r = -EINVAL; >>>> + } else if (kvm->arch.hdbss_buffer_size) { >>>> + r = -EBUSY; >>>> + } else { >>>> + kvm->arch.hdbss_buffer_size = size; >>>> + r = 0; >>>> + } >>>> + mutex_unlock(&kvm->lock); >>>> + break; >>>> + } >>>> default: >>>> break; >>>> } >>>> @@ -500,6 +526,14 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) >>>> else >>>> r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT; >>>> break; >>>> + case KVM_CAP_ARM_HDBSS_BUFFER_SIZE: >>>> + if (!system_supports_hdbss()) >>>> + r = 0; >>>> + else if (kvm) >>>> + r = kvm->arch.hdbss_buffer_size ?: KVM_ARM_HDBSS_DEFAULT_SIZE; >>> >>> This whole thing of allowing hdbss_buf_size to be set only once makes >>> everything more complex. Why do we have this limitation again? >>> Hi Leo, I'll drop the -EBUSY gate on hdbss_buffer_size: ``` + } else if (kvm->arch.hdbss_buffer_size) { + r = -EBUSY; ``` On the ioctl design itself, I have two points: one on freeing the buffers, one on when the size can change. **when to free the buffers:** I will free at vCPU destroy only, which means the buffers live from the first enable until the vCPU is destroyed. The alternative is freeing at disable, but the RELOAD request we send when disabling only guarantees that every vCPU has left the guest; the flush runs after the request IPI is acknowledged, so freeing right after the request returns would race an in-flight drain. Moving the drain ahead of the request-ack point would mean draining with interrupts disabled, which is milliseconds on a full buffer. So vCPU destroy is the simplest correct point. >>> I mean, if we want to restrict size changes after the first hdbss buffer >>> gets allocated, it makes sense, other than that, I would like to understand >>> the reason for that. >>> Based on the above freeing design, the second point is about when userspace is allowed to use this ioctl to configure the size. We adopt lazy allocation to save memory, which means the buffers are allocated at the first dirty-logging enable and freed only at vCPU destroy, so a VM that never migrates never allocates them. Given that, the remaining question is **when the size can be changed** (there are three methods): - Allow changes only before the first vCPU is created. The size is a setup parameter: no extra state, no races. - Allow changes any time before the first HDBSS enable. This needs a flag recording that the first enable happened, and the ioctl has to take the memslot lock to serialize against a concurrent enable. - Allow changes any time HDBSS is off, even after an enable. This means freeing the old buffers from the ioctl: serialize against concurrent enables, drain in-flight exit-path drains, then free and let the next enable allocate at the new size. I think the first two are reasonable. I'd also like to hear your opinion on this, and I'd really appreciate a reply whenever you get a chance. >>> Thanks! >>> Leo >> >> Hi Leo, >> >> You're right, I re-examined the whole flow and the "set once" >> restriction isn't actually needed. I'll remove it. >> > > Awesome, thanks! > Leo Thanks, Tian