Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6175758rwd; Wed, 24 May 2023 11:50:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Ya4L1QB2BevHiL935L46KY+abxXXD6NLZz3PZqbCd+dcu16JsfteeSvdnfcc4C1LhVSSd X-Received: by 2002:a17:902:d04a:b0:1ac:5b6b:df4c with SMTP id l10-20020a170902d04a00b001ac5b6bdf4cmr14777353pll.69.1684954205682; Wed, 24 May 2023 11:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684954205; cv=none; d=google.com; s=arc-20160816; b=OEt64vh4HcSIOKkKCUYJFZg6eKs5DkZUl1bhpn4yrSvUhFKt0ic4BRQAA2Q6M3/jjx Fy3l6lDkprQB3naoekqTFhzoJ0rnnjYmiv6MuyLRFEC4tt1P/JKYz15c6mFzwXHNdAyj SZb2dM5G+xDNXX+NeGRATyUBln5RwpLrRXbX0i4MW3tnacemXKe549O5T31izBfTaGmb pG3mbuJDtCxPHZENZ+HHgfpcTOBuLIz25KDQmgNY6vwVtjJTF5MoAjlDDzCXJRaKPH8S EC5tWC8/WUuLgepFet5uvJpckQiH1irRgSjireI32NCZHLBrdSK++ILahRsGkQnIGpGu Tmyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=zhCLzNiRBNlROtfKp+ujTOcVOVXlrI8zrVzKw44qRAY=; b=RJXo5dERks8IFjhwUaY3wtI8ED8x18OmmyjtNeGEyzIHreI8E/vNeOqYao+RcOwAhJ vuL2zBGnYTmQxEno5Lbnjpz63y16fNdPzrkktmNIXRod2HROa/+0VJsTHebGrhGMxca5 2buSfZZopXCY1ZmiPaDqP+SGevWl6G0SnM/1l5yua/bhvyWXc3tQmLDznzawxFznJ22P 3aNcz+9IinW9E3KmZgld0e+4HU2p42cxaX4CZTqi2Lvmxw0KfQtU4Q37qabmq7iq5yWS 0yYXJucQopy40vGbzRhX+QVQBToYxzH4/pNNBcYNJ86O7oMT6n1QU524+c1AltvWOi5t E8AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=gk9VY1Fi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d13-20020a170902aa8d00b001afaae8a0a8si1598869plr.8.2023.05.24.11.49.50; Wed, 24 May 2023 11:50:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=gk9VY1Fi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237368AbjEXS35 (ORCPT + 99 others); Wed, 24 May 2023 14:29:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237415AbjEXS3t (ORCPT ); Wed, 24 May 2023 14:29:49 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC01CB6 for ; Wed, 24 May 2023 11:29:47 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-babb7aaa605so2573573276.3 for ; Wed, 24 May 2023 11:29:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684952987; x=1687544987; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zhCLzNiRBNlROtfKp+ujTOcVOVXlrI8zrVzKw44qRAY=; b=gk9VY1Fi8rAEgoEQhDDDcAlyZtuvVYH+tDqstE9RgYMCMx44Q39XjyhilHUndn79Jf DsYr1NeHSyJUP6Xnpups0LK8LE/e6MaJSo5KgXEbph/nbdV6f20iBAAvJDcDAb8yWxqa PwXxf/osa258qdIt5lOohF/nQXbGcInom6MWSqv5xu2G/FDMvyF17aGJfKTyj0ZxdkU+ 1YwX6JquS35m12/IprqBATiSQnRhymKxch+P3H7efEhJZWSGzCXGn81+zzZVAaeP//kP +akCm4mHwxaHiE3+Y6UMz/ktFwEkssQ3cCRXbiLWobVua0yuYn9XCHMU6TN87sPyU0Lr hBbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684952987; x=1687544987; 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=zhCLzNiRBNlROtfKp+ujTOcVOVXlrI8zrVzKw44qRAY=; b=cpUqTnbpXva8M8zQ3EHVTLAslFoGuW7Hmy5xFymBxGTcK/ieCF63CDAsLF0TmSdTwq jeNIev9fJYjM5bsArcXcCss2QCT3+7QlYbB02vJE9yqA0nK9ICikJLuGRoKmjXOwHY7t JK6NeGItqI93OrcrxChz3gbTbdwz/EldZGwJ1Ei2HFLBColh9YmwgBradRFJyUB7vfEH I8JYQuzjG9FQLfq1GFLGsdG0N0IUp51j51nKhc4wHwfbluauQjnoCF2sYEtXbFMErDKq AF6AlMqmjBLFo1lYNbudmWnEPSHEgrnteGXjOx00uC4rvxOZwLpTzioPdfrYRcgNE/7L DzJg== X-Gm-Message-State: AC+VfDwbHEE8/REMflbLsv9toFME8C6UT46QF+rj61ivsxLMXidv1WC6 aMNWhjICp5xW/NjyVSqjw68EV5R7RME= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1024:b0:ba8:6422:7fc with SMTP id x4-20020a056902102400b00ba8642207fcmr398046ybt.7.1684952987216; Wed, 24 May 2023 11:29:47 -0700 (PDT) Date: Wed, 24 May 2023 11:29:45 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230330085802.2414466-1-stevensd@google.com> <20230330085802.2414466-2-stevensd@google.com> Message-ID: Subject: Re: [PATCH v6 1/4] KVM: mmu: introduce new gfn_to_pfn_noref functions From: Sean Christopherson To: Peter Xu Cc: David Stevens , Marc Zyngier , Oliver Upton , Paolo Bonzini , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 24, 2023, Peter Xu wrote: > On Wed, May 24, 2023 at 09:46:13AM -0700, Sean Christopherson wrote: > > If we hack kvm_pfn_to_refcounted_page(), then all of those protections are lost > > because KVM would drop its assertions and also skip dirtying pages, i.e. would > > effectively suppress the latent detection by check_new_page_bad(). > > So it's probably that I totally have no idea what are the attributes for > those special pages so I don't understand enough on why we need to handle > those pages differently from e.g. PFNMAP pages, and also the benefits. > > I think what I can tell is that they're pages that doesn't have > PageCompound bits set on either head or tails, however it's still a > multi-2-order large page. Is there an example on how these pages are used > and allocated? Why would we need those pages, and whether these pages need > to be set dirty/accessed after all? The use case David is interested in is where an AMD GPU driver kmallocs() a chunk of memory, let's it be mmap()'d by userspace, and userspace then maps it into the guest for a virtual (passthrough?) GPU. For all intents and purposes, it's normal memory, just not refcounted. > > static bool kvm_is_ad_tracked_page(struct page *page) > > { > > + /* > > + * Assert that KVM isn't attempting to mark a freed page as Accessed or > > + * Dirty, i.e. that KVM's MMU doesn't have a use-after-free bug. KVM > > + * (typically) doesn't pin pages that are mapped in KVM's MMU, and > > + * instead relies on mmu_notifiers to know when a mapping needs to be > > + * zapped/invalidated. Unmapping from KVM's MMU must happen _before_ > > + * KVM returns from its mmu_notifier, i.e. the page should have an > > + * elevated refcount at this point even though KVM doesn't hold a > > + * reference of its own. > > + */ > > + if (WARN_ON_ONCE(!page_count(page))) > > + return false; > > + > > /* > > * Per page-flags.h, pages tagged PG_reserved "should in general not be > > * touched (e.g. set dirty) except by its owner". > > > > This looks like a good thing to have, indeed. But again it doesn't seem > like anything special to the pages we're discussing here, say, !Compound && > refcount==0 ones. The problem is that if KVM ignores refcount==0 pages, then KVM can't distinguish between the legitimate[*] refcount==0 AMD GPU case and a buggy refcount==0 use-after-free scenario. I don't want to make that sacrifice as the legimiate !refcounted use case is a very specific use case, whereas consuming refcounted memory is ubiquituous (outside of maybe AWS). [*] Consuming !refcounted pages is safe only for flows that are tied into the mmu_notifiers. The current proposal/plan is to add an off-by-default module param that let's userspace opt-in to kmap() use of !refcounted memory, e.g. this case and PFNMAP memory.