Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp199032lql; Fri, 12 Apr 2024 07:55:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVAozbvjjnfrfUzXHlyBKWdnl7d7cxGRdTljfif6wc1r5aXytHGSYJtGDOcz0bUGZN0/wCLBwf93B4u4cZ2o+73a5Rpi3VGzSaNA7fYrw== X-Google-Smtp-Source: AGHT+IGLE9Y+IWdGjfpjB0E1kR7AqDMRmxeT/2L8uUoH4J8tfLS+srbxCoMszIMGVaL1WFmx6mG7 X-Received: by 2002:a17:90a:eb17:b0:2a2:399f:60e7 with SMTP id j23-20020a17090aeb1700b002a2399f60e7mr2851703pjz.23.1712933704090; Fri, 12 Apr 2024 07:55:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712933704; cv=pass; d=google.com; s=arc-20160816; b=chOY7O9Ta7r1SHpEDNy8z2ZT2YWM+m0ImdZSPEzxaqHtJn+LSIvV8ilCTA3BfRpVUZ uw8Nfvafbb5z08vZIJH1bk3nM5TkR/h2+l8PeNlB1k4PEz2kDGddtIuWRKHBFRVktMOG tVOO3H1d+gAy/e4tGkEZOBoad6edRkTHd3gsiF2WNTHB4Q0r/KD/CFHED3tZ17YtuqxN brkkn7vEiaW4URbK3eFclgcG5wq1uFERs5OsYqMvy56uDMilWzLLdKFYCFDVlrvjBAy9 oWxSr/Gqq5agSoNoFBAUiBk1PlapPjlAgbPpQ/HmO1FLvlz32rZ16+hRi8FqKpbX59ud nTnQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=ZrmdCrVWtwb7lFW6AyHWbgOgdloa5ymd2fy0iQuLqTo=; fh=bWv1ijUqIK6jgNrGn22CWFmuZ+4tR5jPW24NDuQ6Z5w=; b=neTuyg20aDVsVKmUCHlcIIwE10y/bsC9HzagAq7MZWcZ8zJPXQpV3P/SbmM02kvqxy PDhRCp/9+Vfl08q80SxP6HCuJgsWZrVhWTMJ3sO6fhR0Kuhe+zqh2udtT7JStooi9ecd 9/BmV1XAJvdtNhUTc7NFYDQbC+pxbvsKLtO3Y7lkDAlucPogSV45SYTsbzohauyDt+wZ m9ghqoBpfiGRGqMHhSS4Vnnf0Y7//AmRniyt3ro0l2JEDsAfrc+kLTta7U+/gHWXYOX6 KqJ73XgLpbFaMXNK1u5RA6MHBhIFXf1lTtZVIhpIu8GVi2ciloO6SnQmJ8lFxDaSeLJV 5PWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jZMOLO9M; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-142883-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142883-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id rm8-20020a17090b3ec800b002a26a2d0472si3633061pjb.165.2024.04.12.07.55.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 07:55:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142883-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jZMOLO9M; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-142883-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142883-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 C06D528488F for ; Fri, 12 Apr 2024 14:55:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C2077146A88; Fri, 12 Apr 2024 14:54:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jZMOLO9M" Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 6C18E147C9C for ; Fri, 12 Apr 2024 14:54:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712933666; cv=none; b=ZPOtBwBt9AmSv/TGPkt94mCX9/GOnXlyN9LtYhA9IVzGuUFHYWYa8Ie5iBBY+JSUim4UeZak2DM+E1HFuwnuc0JEKn6CV4XBnyh2AOEU8h8rWsc1Y0p9gfKHJJG69WDsqJmieiCII8IKbPf1jMNJoWfdodTQyZZVkLpisvY6PFc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712933666; c=relaxed/simple; bh=ZtUMxIcPii7SYptK1bsvjnMTv2XY+8o/C+H99TV4mko=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HIXOA/KRT2CZIZmBrbTz/+pjgNy6r3I0elDpFHaq9urgmbQT+n9lyckIhuIYmTEhESe/viuczcQAUMz+PDLfCn+KhzD77rOooVQmbPk/XjyOgl6ePE8qajIytEORDaG8wAli6h6y04X+5N6HciwlnEZqpgi3dTf/zG1+p01xlR8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jZMOLO9M; arc=none smtp.client-ip=209.85.210.201 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=flex--seanjc.bounces.google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6ed33476d82so978133b3a.0 for ; Fri, 12 Apr 2024 07:54:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712933664; x=1713538464; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZrmdCrVWtwb7lFW6AyHWbgOgdloa5ymd2fy0iQuLqTo=; b=jZMOLO9Mr0xDTGxrxV8mIoSXOfsy0NmwYQW0zI/ASTTgNcMfwZ1UQ+4+EWeTJclvO0 Taso9FlZRd68oNAXtsEXqF+Zh6esim3EnEM79tEAtREazLVZQ6C9RcZr2bB6YXUecyGS i7mCDGukKBi1YWkZxqotQaPbtcMhvIO0ZTKV6N8znDXDGxWCjC5OU7bIOqzfxP3fuG1U E2JP1zqWTjEE923qoZFUYQEtzonrgr20LU+ra6Rlt0Z1LsoznWEvbR4GBs+l5h7rbR0e cIJ7OWbCRDSO7twJVl7NouedyjTGtc284+QIYkl4d3Zq7NLXu8Bt0ccYTnihc37Pwxpl yNYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712933664; x=1713538464; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZrmdCrVWtwb7lFW6AyHWbgOgdloa5ymd2fy0iQuLqTo=; b=de939af+32QF5/yX1PNMPdUAoFGycKAPYHQsk27kxeubknU+PyudTxL8lgGwYMvnnt rxLIoXrZAZEAFCFfjEYqd5jjdqGJ9gm39tydXVrqxiS88KCuduSN31B29MyAci7FWVoD J5hpiz75eLLy+OPSSYfAmaIi8EdEBRVHD8jc9fcbzlHU8S9LyDv8oXdYTs6H1BbWcdxD LkKj0+MvbMLJyVlNfSiKKEPVqyIhXMAbLGKmzISXywyia3ZJbhT1+G54j4+Uxil+CA1e FOki7c7zfaZ1G18Fti8nbVQWNIa4H9O3gVZcCWSf4NxYmvPcNabveAlG1Mzf8npGebwT QaeA== X-Forwarded-Encrypted: i=1; AJvYcCUWCMfMT7nJ9czeAUsGM6zYeRdcv60LDGWYYUr46+jLSBXr9H1jfgA0GJ/yH7BEm0L14CR3jz5A+lGEHI7XWTi/xVTkxsi2qOOTrTTk X-Gm-Message-State: AOJu0Yx+A8dtseQDg7olg5LmSfa0gnLPZ6zi9YQhnBeklSxk6MmMfCDn pKidktvbzXaSxEDYvTeGLG8frCZoi/JFvxedzDttHbGLe7anMzi0xBKEuX8A0U0AekP6mWSLsM9 AJw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2d94:b0:6ed:95ce:3417 with SMTP id fb20-20020a056a002d9400b006ed95ce3417mr191844pfb.5.1712933663717; Fri, 12 Apr 2024 07:54:23 -0700 (PDT) Date: Fri, 12 Apr 2024 07:54:22 -0700 In-Reply-To: <86jzl2sovz.wl-maz@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240405115815.3226315-1-pbonzini@redhat.com> <20240405115815.3226315-2-pbonzini@redhat.com> <20240412104408.GA27645@willie-the-truck> <86jzl2sovz.wl-maz@kernel.org> Message-ID: Subject: Re: [PATCH 1/4] KVM: delete .change_pte MMU notifier callback From: Sean Christopherson To: Marc Zyngier Cc: Will Deacon , Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Oliver Upton , Tianrui Zhao , Bibo Mao , Thomas Bogendoerfer , Nicholas Piggin , Anup Patel , Atish Patra , Andrew Morton , David Hildenbrand , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Fri, Apr 12, 2024, Marc Zyngier wrote: > On Fri, 12 Apr 2024 11:44:09 +0100, Will Deacon wrote: > > On Fri, Apr 05, 2024 at 07:58:12AM -0400, Paolo Bonzini wrote: > > Also, if you're in the business of hacking the MMU notifier code, it > > would be really great to change the .clear_flush_young() callback so > > that the architecture could handle the TLB invalidation. At the moment, > > the core KVM code invalidates the whole VMID courtesy of 'flush_on_ret' > > being set by kvm_handle_hva_range(), whereas we could do a much > > lighter-weight and targetted TLBI in the architecture page-table code > > when we actually update the ptes for small ranges. > > Indeed, and I was looking at this earlier this week as it has a pretty > devastating effect with NV (it blows the shadow S2 for that VMID, with > costly consequences). > > In general, it feels like the TLB invalidation should stay with the > code that deals with the page tables, as it has a pretty good idea of > what needs to be invalidated and how -- specially on architectures > that have a HW-broadcast facility like arm64. Would this be roughly on par with an in-line flush on arm64? The simpler, more straightforward solution would be to let architectures override flush_on_ret, but I would prefer something like the below as x86 can also utilize a range-based flush when running as a nested hypervisor. diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ff0a20565f90..b65116294efe 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -601,6 +601,7 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, struct kvm_gfn_range gfn_range; struct kvm_memory_slot *slot; struct kvm_memslots *slots; + bool need_flush = false; int i, idx; if (WARN_ON_ONCE(range->end <= range->start)) @@ -653,10 +654,22 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, break; } r.ret |= range->handler(kvm, &gfn_range); + + /* + * Use a precise gfn-based TLB flush when possible, as + * most mmu_notifier events affect a small-ish range. + * Fall back to a full TLB flush if the gfn-based flush + * fails, and don't bother trying the gfn-based flush + * if a full flush is already pending. + */ + if (range->flush_on_ret && !need_flush && r.ret && + kvm_arch_flush_remote_tlbs_range(kvm, gfn_range.start + gfn_range.end - gfn_range.start + 1)) + need_flush = true; } } - if (range->flush_on_ret && r.ret) + if (need_flush) kvm_flush_remote_tlbs(kvm); if (r.found_memslot)