Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp9756258rwl; Wed, 11 Jan 2023 09:31:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXu0acUqGS9cJoN74ULnXNssb2+tsMXnPCUl2C5Je+54yU9moKtS5wewX+qrBKdu40vnNMEd X-Received: by 2002:a05:6a21:1643:b0:b5:e639:2833 with SMTP id no3-20020a056a21164300b000b5e6392833mr4022631pzb.20.1673458279366; Wed, 11 Jan 2023 09:31:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673458279; cv=none; d=google.com; s=arc-20160816; b=TRN/OHHar/LSIbri6UM4UUPAlkYBij5EvYa0XC0jzEsY8yrt/0Up9lwxTX6aIR+NVB WoKW2W7INUtq3Jru/HUJ5JYaRHQYtWh1zmZ8DVrn3Qkd/4jrfFZEDn3lL1KQRn48+tOo RBhP3pWONuUffAOgVcCLcrqqdj74STg+pwi9sgGtLsHkkp9fiObbiFldio0zanshhaO4 9aR24/sk42YzzUvotMFmdRVqjVOiMV23RZCpD2uOHWKmyImfU7vvtgx6mUv3kZzrMvM7 uzeRoKzuW8W3DasghyHtDV/F/mvX8tvv5L7asB415hs9X7OILq4uDuzr6wwqadP+jmvJ djuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=oDP9iHBz114gTPVon7+29iOp7H+9gVmvkP1sBBtZVDA=; b=gu0usKnM1Sylg4iZ3obxqd4FH3d/iXoZltYuGFUxliGvnjYSyQ8rGsIdlhUXP+kSTJ IyHsFCumLKXPg6OUVw9ItlUTbIAuo+rX/BkZxeTaT/ecpX6OTpWv7UbCOVlpMwy3XSrS S799/pLMrsA9ljMvVePzzO9COZ2/6/4H3040u9Wud5HT15oa8HS//ztDcS5/kaXZob2n kr1HnowAgWSNCmJEBtYOm99NovaP0TjB06nTk4ksuyI29uWCQeTh0gmHq4DFj9XMFwCd iCuhpSH3mLnF6kfxDjJwxHYGlAG/wCUa5YbHP9h0fbA/I/bk4WuSjPqb8+fDmYrptfBQ RJ2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=fJF1iA21; 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 d11-20020a631d0b000000b0046040a8be4esi14548218pgd.754.2023.01.11.09.31.11; Wed, 11 Jan 2023 09:31:19 -0800 (PST) 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=20210112 header.b=fJF1iA21; 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 S239078AbjAKRHV (ORCPT + 51 others); Wed, 11 Jan 2023 12:07:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239152AbjAKRGr (ORCPT ); Wed, 11 Jan 2023 12:06:47 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA5DF32267 for ; Wed, 11 Jan 2023 09:05:10 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id v23so12515016plo.1 for ; Wed, 11 Jan 2023 09:05:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=oDP9iHBz114gTPVon7+29iOp7H+9gVmvkP1sBBtZVDA=; b=fJF1iA21Eh2Ps4YUG5BTSRAlKr8ewtW0a/S0d61RuFJnx5BLNsRr7So6UdDtgMe5iG 9WBXJelltOenGOc6m7LAPpTpasCUh002+Mfoi87hGTXHAlo79u5Y2689VNmCUoB+ZNEq nXnke2ST7CeYJdq2c1cv0FMX5oao34vkAePIjWt/a8zU5K2gUR1hauI8esZEUaq2mB0H U3VrTTIgxzQp3MLFL+Z2FLi0rgquIKmfcqQ/FT8djk+IryiB6xlR2CAQLi4A6IAMGPnF mSczaPgLDxyVlW0K0mBBzgVzeTlYMuLmooheNK8bJaqkobZ1NkpWcuu43dnrCB9TiyeE es6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding: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=oDP9iHBz114gTPVon7+29iOp7H+9gVmvkP1sBBtZVDA=; b=IpcHc3znSj5AvT33EM54dahzItroz4QBt5nLPgonexxveEQuXKqaw+cq6IrshLwhHc 9ToYsX5cQP5CiBEUyxlFOvXIv1Z65dcRcYPPMnudtbTqScKaxlipqVtp3ema0Fcqul2c jOWpW95E7vzZXZvw8RG9Ubc4NLTJd5ThqpM0EpL2SWy8NG9D9Z6nDm8rCOvlqmXAWBLZ rJuCePGfuYP/LTwDF5ggW5nuCaCQdRZ4v0TE7Hwv3O/Ai+c4c+3SZIq2iX3MrPhD1X8X Z0TaYQqjZSKALIYqhn8HN4KhzUpJaG97jnEHn4Nzlg8exfcf8JDAnkCc3FOUHeQCH9C5 u+NA== X-Gm-Message-State: AFqh2kqtRwnJDZIk/MbMXkkp6UTgv3J8tnL5j6VjwrHEg6PnhdLDZmiF 4ZkLwRVeGIF4niQza3lJfZTNbA== X-Received: by 2002:a17:90b:274b:b0:219:f970:5119 with SMTP id qi11-20020a17090b274b00b00219f9705119mr425092pjb.1.1673456710117; Wed, 11 Jan 2023 09:05:10 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id l17-20020a170903245100b001890cbd1ff1sm10482685pls.149.2023.01.11.09.05.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 09:05:09 -0800 (PST) Date: Wed, 11 Jan 2023 17:05:05 +0000 From: Sean Christopherson To: Christian =?iso-8859-1?Q?K=F6nig?= Cc: Dmitry Osipenko , David Airlie , Huang Rui , Daniel Vetter , Trigger Huang , Gert Wollny , Antonio Caggiano , Paolo Bonzini , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Dmitry Osipenko , kvm@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org Subject: Re: [PATCH v1] drm/ttm: Refcount allocated tail pages Message-ID: References: <8f749cd0-9a04-7c72-6a4f-a42d501e1489@amd.com> <5340d876-62b8-8a64-aa6d-7736c2c8710f@collabora.com> <594f1013-b925-3c75-be61-2d649f5ca54e@amd.com> <6893d5e9-4b60-0efb-2a87-698b1bcda63e@collabora.com> <73e5ed8d-0d25-7d44-8fa2-e1d61b1f5a04@amd.com> <6effcd33-8cc3-a4e0-3608-b9cef7a76da7@collabora.com> <48b5dd12-b0df-3cc6-a72d-f35156679844@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Thu, Aug 18, 2022, Christian K?nig wrote: > Am 18.08.22 um 01:13 schrieb Dmitry Osipenko: > > On 8/18/22 01:57, Dmitry Osipenko wrote: > > > On 8/15/22 18:54, Dmitry Osipenko wrote: > > > > On 8/15/22 17:57, Dmitry Osipenko wrote: > > > > > On 8/15/22 16:53, Christian K?nig wrote: > > > > > > Am 15.08.22 um 15:45 schrieb Dmitry Osipenko: > > > > > > > [SNIP] > > > > > > > > Well that comment sounds like KVM is doing the right thing, so I'm > > > > > > > > wondering what exactly is going on here. > > > > > > > KVM actually doesn't hold the page reference, it takes the temporal > > > > > > > reference during page fault and then drops the reference once page is > > > > > > > mapped, IIUC. Is it still illegal for TTM? Or there is a possibility for > > > > > > > a race condition here? > > > > > > > > > > > > > Well the question is why does KVM grab the page reference in the first > > > > > > place? > > > > > > > > > > > > If that is to prevent the mapping from changing then yes that's illegal > > > > > > and won't work. It can always happen that you grab the address, solve > > > > > > the fault and then immediately fault again because the address you just > > > > > > grabbed is invalidated. > > > > > > > > > > > > If it's for some other reason than we should probably investigate if we > > > > > > shouldn't stop doing this. ... > > > > If we need to bump the refcount only for VM_MIXEDMAP and not for > > > > VM_PFNMAP, then perhaps we could add a flag for that to the kvm_main > > > > code that will denote to kvm_release_page_clean whether it needs to put > > > > the page? > > > The other variant that kind of works is to mark TTM pages reserved using > > > SetPageReserved/ClearPageReserved, telling KVM not to mess with the page > > > struct. But the potential consequences of doing this are unclear to me. > > > > > > Christian, do you think we can do it? > > Although, no. It also doesn't work with KVM without additional changes > > to KVM. > > Well my fundamental problem is that I can't fit together why KVM is grabing > a page reference in the first place. It's to workaround a deficiency in KVM. > See the idea of the page reference is that you have one reference is that > you count the reference so that the memory is not reused while you access > it, e.g. for I/O or mapping it into different address spaces etc... > > But none of those use cases seem to apply to KVM. If I'm not totally > mistaken in KVM you want to make sure that the address space mapping, e.g. > the translation between virtual and physical address, don't change while you > handle it, but grabbing a page reference is the completely wrong approach > for that. TL;DR: 100% agree, and we're working on fixing this in KVM, but were still months away from a full solution. Yep. KVM uses mmu_notifiers to react to mapping changes, with a few caveats that we are (slowly) fixing, though those caveats are only tangentially related. The deficiency in KVM is that KVM's internal APIs to translate a virtual address to a physical address spit out only the resulting host PFN. The details of _how_ that PFN was acquired are not captured. Specifically, KVM loses track of whether or not a PFN was acquired via gup() or follow_pte() (KVM is very permissive when it comes to backing guest memory). Because gup() gifts the caller a reference, that means KVM also loses track of whether or not KVM holds a page refcount. To avoid pinning guest memory, KVM does quickly put the reference gifted by gup(), but because KVM doesn't _know_ if it holds a reference, KVM uses a heuristic, which is essentially "is the PFN associated with a 'normal' struct page?". /* * Returns a 'struct page' if the pfn is "valid" and backed by a refcounted * page, NULL otherwise. Note, the list of refcounted PG_reserved page types * is likely incomplete, it has been compiled purely through people wanting to * back guest with a certain type of memory and encountering issues. */ struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn) That heuristic also triggers if follow_pte() resolves to a PFN that is associated with a "struct page", and so to avoid putting a reference it doesn't own, KVM does the silly thing of manually getting a reference immediately after follow_pte(). And that in turn gets tripped up non-refcounted tail pages because KVM sees a normal, valid "struct page" and assumes it's refcounted. To fudge around that issue, KVM requires "struct page" memory to be refcounted. The long-term solution is to refactor KVM to precisely track whether or not KVM holds a reference. Patches have been prosposed to do exactly that[1], but they were put on hold due to the aforementioned caveats with mmu_notifiers. The caveats are that most flows where KVM plumbs a physical address into hardware structures aren't wired up to KVM's mmu_notifier. KVM could support non-refcounted struct page memory without first fixing the mmu_notifier issues, but I was (and still am) concerned that that would create an even larger hole in KVM until the mmu_notifier issues are sorted out[2]. [1] https://lore.kernel.org/all/20211129034317.2964790-1-stevensd@google.com [2] https://lore.kernel.org/all/Ydhq5aHW+JFo15UF@google.com