Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp131997rdh; Tue, 13 Feb 2024 11:32:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVqtObw0t9CldpU26jpyDXVcG06piCwIsY608Pzyh9S/f7SSvdIQcTbP9wZh+OKxHWKs+6O5BTGjGGbr9nQ2FHeikhmFF2EIZ+lkP2oyg== X-Google-Smtp-Source: AGHT+IF7Wl8vkYzQz7TIMePbfBKgBZH7o2sDdWyPWrIWcnBGujtzB0b7ZMRYHSdq6VQlYR//6iWB X-Received: by 2002:a05:6a21:394b:b0:19e:5fc7:b3e8 with SMTP id ac11-20020a056a21394b00b0019e5fc7b3e8mr525723pzc.61.1707852764795; Tue, 13 Feb 2024 11:32:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707852764; cv=pass; d=google.com; s=arc-20160816; b=0ewIvWHTY8P8rHhd29zssxzWWTGmdtKbtf69vx/jjJKLkmb1g1wmfs2MNgHDbx5j5g rDTQfHBH6wNIxz2u7e3h1gwDgDZIRJTTocv6SJqhlqnrx22F+OkvaP/Rxsmsc4m4ERX4 wJMgjpyvfLFGNALrchUGBgNuRQg9b+pw6RbyiYTX9fv0vbaydx9zpnVRoB5OMeGmYG/T uHsbTPZfLpc39BxiNpGueT5l0xGvoOYi4eCJUn7oC+V1h+Vwx2HcB4Wj+QsBeEfHa8Xa GKGI4But7YNQaUMfdXy3NNEBWqoRZ+l5KK17l62p5BeEm5fi+dqvqv9yTvk+XTWQ+jQJ 3wSQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=rymEGgxm+55ne6xbo9ccaE33zwysrx5gpeBPKrQKGdo=; fh=HsfSYcoNNJzQqYXQedAz0Nm3FyUXoLBi9QlibqrMdFw=; b=Of0Ns06sD98a0nlQ4QwkswfevsUpObInzIq9REeVWYGUgJmBX1f6F/Yp8s6vDK5NNn 90kBPxT1FrkUCjjQTCBHmN8kidd5o0QXqCIMP05L0WSZlc/5FSoMaUfsWuQmdcB2+OMQ 0yyuF2f/hO3C6XXBW+oEd3b0mSwP711FFZ96J8q0LArbjBBeiuRWKONHYxX/IocG0Z// js/EXH13qtol3j4mfZ02E3v2ug5YLqO/dzrhWRnS8pCa5MbwkYA+ejW9eXGYWfhlax9W dtLkBEOxV2+rjjsGEQPtDUjxyW1vbdVBZ+8PTNZU2ZMZJf41yHZ3FABkJ3P8n1OT48Ra eJdA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Jon8VBr0; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64167-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64167-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVsTLKhSiJH3i6kyE18KJ09GhnmFkmP5qOJtoCX9ApNxNXQytVldZYKjcucV0HbmOvVaMQAlvgQTRzTghGt0CT1j4AAZ7ggg2d2dZv1aw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h28-20020a056a00001c00b006e06bff73d5si7179463pfk.104.2024.02.13.11.32.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 11:32:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64167-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Jon8VBr0; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64167-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64167-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 716E928D62C for ; Tue, 13 Feb 2024 19:32:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D7B960878; Tue, 13 Feb 2024 19:32:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Jon8VBr0" Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) (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 25F081CAA3 for ; Tue, 13 Feb 2024 19:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707852753; cv=none; b=dwtyjl81Tx1hMoQSP6frTbW6IA0faygWax+BY3sifrScYAB+KeZOe29KGH+d0Y1zm86q7r53aeRm4eqTADsGNg/BOnzbDVeu5PDqA1l+GY/+UaEXXGHnNmwiQZELQ7f1nltF12VcS1nfgfM3euN0OnrO6eI0HooE3UYCYHqaZVw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707852753; c=relaxed/simple; bh=t3pGuIMAeZfDG+Nm+EbDi25ksSmaX2ngcVn9XDu9vc4=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Eh2xR/Leb7gwul8yBW5PEr/UqRGQ7pexfQlthkfRpuR+ZWOq4CZCGBasKX6l8d9DIpIoKFNQS65Tg9OXvY0lgM7rkpFYFSfHYtPpesa8BjhTV78ZufsN7oNDysbz6XFvTHq8CEdhOq2No28v3VAGrU0lXFDdPipw71vg5ZVF3JU= 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=Jon8VBr0; arc=none smtp.client-ip=209.85.161.45 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 Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-59d11e0b9e1so2823452eaf.1 for ; Tue, 13 Feb 2024 11:32:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707852750; x=1708457550; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=rymEGgxm+55ne6xbo9ccaE33zwysrx5gpeBPKrQKGdo=; b=Jon8VBr0D6ju45gTv6XMepJF/4uvhaXJFN4QH7qdemSPI8KmcMK5IuCRTLNyays/tJ 9AFTAXQT1qGBPccP66akBXjjgBeKl0UeU5pdl0nletZLRQ0ei6QAWs1eryZ0hNef5ePy NKh8ZQCrCoMJyT6A1LUohs6AQo6jkfv15hRovL/RxaZhYFBN3g9gOcrD0cwJUvXmRxR6 M8/Xm3EFH5wCqA7ZEOk54ny0WoErixLYQjFeLnUFepTHEOrKm5r6IAIr+CmA41OE2pRl Z2h3Md9XQyHCZ6IBMw9BY+XtLZEK4Mjt2VaesN9JEB9obK2yI4sKSaIkNSdn2/q8hl0x 9nfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707852750; x=1708457550; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rymEGgxm+55ne6xbo9ccaE33zwysrx5gpeBPKrQKGdo=; b=TosXsTuVWO7TAahdyoa1IDudChJkMgNxO/hcLlhE7BI/WD1lldlaWOpr5/apE0WzzW 746nmQkptzBVv/KzrPTj9Zwy3PjX7YdmWNMN0tKP3JTsV/j7/kbJ64GxKYWTZmiQmzzM aSCTcjg+D0aHywlh6wh1/iI3nRJsXfe5Eb/ZwnKn0gsgDiixUugkPORW1McpWp2+/RzJ ZaiCmoiPFf4OpO0Ccmbt0yGwiq/BX0BCGD2kbvjHZMqE1sqR/5Mpy8BChTdyKvE7dyt4 ZlznVR7gdDkwDa/h7Vc/stQ1mpN+9zR5IVPSmzvAUeTD5RzMDA2PoxvpVDY124/7r/4K MKDA== X-Forwarded-Encrypted: i=1; AJvYcCUtKBkrauDYbsGvimpFAva3s8hjZqNIcPJQ+kNlHopFdwQv4GHnewSeNwkneZR8nRdrF80DsfnZ3Q+sGDbYqVvtRICP9N+DMcNWOReu X-Gm-Message-State: AOJu0YxFwiyaBNxfWEdfycxFGO4ccjQvN9zJ06Kblgq7WVOXxwGdF+62 3AU9wFzavYtPVXobMBwTNEtCBXJxynA1DmX9sMoN2AXQrtcOJ1Ymv8AflCfS0TV8K4nZF6mxtes EE1AjvPO/uMD/bKFk+zPDF+xC3Cmj4pbqjldz X-Received: by 2002:a4a:e615:0:b0:59d:d416:3372 with SMTP id f21-20020a4ae615000000b0059dd4163372mr582997oot.0.1707852750038; Tue, 13 Feb 2024 11:32:30 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240206153405.489531-1-avagin@google.com> In-Reply-To: From: Andrei Vagin Date: Tue, 13 Feb 2024 11:32:18 -0800 Message-ID: Subject: Re: [PATCH v2] kvm/x86: allocate the write-tracking metadata on-demand To: Sean Christopherson Cc: Paolo Bonzini , linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, Zhenyu Wang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 13, 2024 at 9:13=E2=80=AFAM Sean Christopherson wrote: > > On Tue, Feb 06, 2024, Andrei Vagin wrote: > > The write-track is used externally only by the gpu/drm/i915 driver. > > Currently, it is always enabled, if a kernel has been compiled with thi= s > > driver. > > > > Enabling the write-track mechanism adds a two-byte overhead per page ac= ross > > all memory slots. It isn't significant for regular VMs. However in gVis= or, > > where the entire process virtual address space is mapped into the VM, e= ven > > with a 39-bit address space, the overhead amounts to 256MB. > > > > This change rework the write-tracking mechanism to enable it on-demand > > in kvm_page_track_register_notifier. > > Don't use "this change", "this patch", or any other variant of "this blah= " that > you come up with. :-) Simply phrase the changelog as a command. ok:) > > > Here is Sean's comment about the locking scheme: > > > > The only potential hiccup would be if taking slots_arch_lock would > > deadlock, but it should be impossible for slots_arch_lock to be taken i= n > > any other path that involves VFIO and/or KVMGT *and* can be coincident. > > Except for kvm_arch_destroy_vm() (which deletes KVM's internal > > memslots), slots_arch_lock is taken only through KVM ioctls(), and the > > caller of kvm_page_track_register_notifier() *must* hold a reference to > > the VM. > > > > Cc: Paolo Bonzini > > Cc: Sean Christopherson > > Cc: Zhenyu Wang > > Suggested-by: Sean Christopherson > > Signed-off-by: Andrei Vagin > > --- > > v1: https://lore.kernel.org/lkml/ZcErs9rPqT09nNge@google.com/T/ > > v2: allocate the write-tracking metadata on-demand > > > > arch/x86/include/asm/kvm_host.h | 2 + > > arch/x86/kvm/mmu/mmu.c | 24 +++++------ > > arch/x86/kvm/mmu/page_track.c | 74 ++++++++++++++++++++++++++++----- > > arch/x86/kvm/mmu/page_track.h | 3 +- > > 4 files changed, 78 insertions(+), 25 deletions(-) > > > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm= _host.h > > index d271ba20a0b2..c35641add93c 100644 > > --- a/arch/x86/include/asm/kvm_host.h > > +++ b/arch/x86/include/asm/kvm_host.h > > @@ -1503,6 +1503,8 @@ struct kvm_arch { > > */ > > #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) > > struct kvm_mmu_memory_cache split_desc_cache; > > + > > + bool page_write_tracking_enabled; > > Rather than a generic page_write_tracking_enabled, I think it makes sense= to > explicitly track if there are *external* write tracking users. One could= argue > it makes the total tracking *too* fine grained, but I think it would be h= elpful > for readers to when KVM itself is using write tracking (shadow paging) ve= rsus > when KVM has write tracking enabled, but has not allocated rmaps (externa= l write > tracking user). > > That way, kernels with CONFIG_KVM_EXTERNAL_WRITE_TRACKING=3Dn don't need = to check > the bool (though they'll still check kvm_shadow_root_allocated()). And a= s a > bonus, the diff is quite a bit smaller. > Your patch looks good to me. I ran kvm and gvisor tests and didn't find any issues. I sent it as v3: https://lkml.org/lkml/2024/2/13/1181 I didn't do any changes, so feel free to change the author. Thanks for the help.