From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 11F5AC77B7A for ; Wed, 24 May 2023 19:10:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jSoWhiKZX1Oq2hPJxIU98RSoslDD/w4QN2I6MHg5sNw=; b=w1vbrE62GkK4eD p8k3DEqGrdd7yKdvghtzjmaYmrid55SG4mCLbn+njPLlNiNkLjqNKjXG/gc2uaY6pU10F4/pGv80v ehgQ++FoViAt/HoOHr0OJqSdAEl8KvFDg1Wfr4pa2K4NGzRx2agdd4ow9XKUL/LiiRwpAW+QVWIYb 0JN/hYHQY0whiRt6bcips0xVPJDRd3OsuCQTtpM7OJndLmTeSQ+7uXQLbaZkzjNkgCGpnnFhNiaR8 EceVSRgwtE9MPxrWI6nsiuh6oZv6Teu9Ps/FEn5udf5l2VDOEDRdNNMccgEBSPHz5ytETyJPbNjDm fjSDurYXxuo5GolEF4gw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q1trh-00ERuM-22; Wed, 24 May 2023 19:09:41 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q1trf-00ERtv-0N for linux-arm-kernel@lists.infradead.org; Wed, 24 May 2023 19:09:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684955377; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RLt1jUkMRuYsgAKC2Cdank6FFEsvFTMJq5xzMGDDBh0=; b=LnuK/8EldpE9l5cbJq7oPJxrkWqGF+DLCUinVrt6ZwFTRwfzz560Lb2MI+KhSC0Ydk4Heb rvjMapF86MkxWLbWBRNFuMG65FJrAzZ/Y85PGk3tU7fM1fYJSC8S/Sr1X/4Gj7oy33o6cf Sy6txeoSy6MaJaqqT3ZfQg2FU+UKI4U= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-458-vd7NVNNROHCsDV_gJ4iu9g-1; Wed, 24 May 2023 15:09:36 -0400 X-MC-Unique: vd7NVNNROHCsDV_gJ4iu9g-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3f7d72d552fso453041cf.0 for ; Wed, 24 May 2023 12:09:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684955375; x=1687547375; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=RLt1jUkMRuYsgAKC2Cdank6FFEsvFTMJq5xzMGDDBh0=; b=BqfwEV6y55JqhYzmzsnBGeQqcXAmcbxcQdRZMjZDnqsRk0VkXgB/148A2P8wnXIRdJ eWOq4xF67oKFrC94GElm+4KcRShOZ00bCRJCkJhejTOoJ6+oicjTQeLI4KiXL6OBng/E HibBO59clKf3jjOyM4Elu+b7yoW505GYkAMmgMPU+JQaMvvE/oH05LRxLMlfTqdw3qGx r1dclfX8aUgKCk9vS7PFkxAyrh0P5Yab5yUBldVAUQokGA8xsJcOfz4R1RxaW+o58HJx jW6vFIkq0HjfXAWdomr1GG5zXJKTBEGxlbD1mUMgGYRt1AnNhkIHA2bZVOq7cktUHPTg pLyg== X-Gm-Message-State: AC+VfDyP7mk37D+CcWVYZ+C3if4Eg1BIVBdUq07KM5suqCa37UXaOW0r EF9pbAXwwD2Z/niUFJn/F3wsd33XGnQwTmyRsM+QboxSpSjzpblSbM8LurwBCtGEuwfmBPuyGws tbKER+VMBdFekjsQDtjPO6Qi4RzyH9uiS/cc= X-Received: by 2002:a05:622a:1a91:b0:3f4:fdaa:8e14 with SMTP id s17-20020a05622a1a9100b003f4fdaa8e14mr4565647qtc.2.1684955375206; Wed, 24 May 2023 12:09:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7BwuqNh4b6tgKt0w1lmfeAbL8sRjfw3morvt4PO25eB8mOzI2e/Sm85Z9xwtVdn4gN42cjgA== X-Received: by 2002:a05:622a:1a91:b0:3f4:fdaa:8e14 with SMTP id s17-20020a05622a1a9100b003f4fdaa8e14mr4565606qtc.2.1684955374809; Wed, 24 May 2023 12:09:34 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-62-70-24-86-62.dsl.bell.ca. [70.24.86.62]) by smtp.gmail.com with ESMTPSA id r11-20020ac85e8b000000b003f6be76a5c1sm1315507qtx.6.2023.05.24.12.09.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 12:09:34 -0700 (PDT) Date: Wed, 24 May 2023 15:09:33 -0400 From: Peter Xu To: Sean Christopherson 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 Subject: Re: [PATCH v6 1/4] KVM: mmu: introduce new gfn_to_pfn_noref functions Message-ID: References: <20230330085802.2414466-1-stevensd@google.com> <20230330085802.2414466-2-stevensd@google.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230524_120939_372957_333F38F5 X-CRM114-Status: GOOD ( 39.69 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, May 24, 2023 at 11:29:45AM -0700, Sean Christopherson wrote: > 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. I'm not familiar enough with kmalloc, but what I think is kmalloc for large chunks will be the same as alloc_pages, and I thought it should also be a compound page already. If that needs to be mmap()ed to userapp then I assume it mostly should be kmalloc_large(). kmalloc -> kmalloc_large -> __kmalloc_large_node: flags |= __GFP_COMP; Then when the new page allocated and being prepared (prep_new_page): if (order && (gfp_flags & __GFP_COMP)) prep_compound_page(page, order); I assume prep_compound_page() will make PageCompound return true for those pages returned. So I know I still miss something, but not sure where.. because IIRC we're at least talking about !PageCompound pages. > > > > 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. I see. I think you mentioned that we can use one special bit in the shadow pte to mark such special pages. Does it mean that your above patch will still cover what you wanted to protect even if we use the trick? Because then kvm_is_ad_tracked_page() should only be called when we're sure the special bit is not set. IOW, we can still rule out these pages already and page_count()==0 check here can still be helpful to track kvm bugs? -- Peter Xu _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel