Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4167921pxf; Tue, 23 Mar 2021 04:36:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyASZXhBoLi/t3TEryfgERI3KDhr4RaG8lSi9qtW18WEG7t2O/SyJp2mbO+6iN+p+I//k5n X-Received: by 2002:a17:906:1a44:: with SMTP id j4mr4517026ejf.401.1616499388433; Tue, 23 Mar 2021 04:36:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616499388; cv=none; d=google.com; s=arc-20160816; b=AaFygxL41Zfe2mgHBazIES/akS4dcBnaTWqu+FjCmJoZlZVMYP2upnbVe+ZtI1YyWO mgso8PifgnbSYfEuvDtQB/ELlBzA4OMGC2G9sBcGYOzR2k6NLLRgcbwr2zH3lYipd+mc umi1PfWIC/kWzmIcmSxWXKQ4C5uT+dZ0/NKKbKxDqdL5rtvZDyUAilAhJWus0GiHYi+J Y5PVcX7vs/RihRaF56ZZuXjg+duL5yDEiiMjya6fe+IARGqih3Qdduq33zitzhuXDuWS Fp2DUiQXltoQ3W3Mu2OXXbUXAtCkNY0/hV0cv1pXZtNzbX0I1OO7CvRXB/4AzWT7bQsW LckQ== 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:mail-followup-to :message-id:subject:cc:to:from:date:dkim-signature; bh=kJG6PyZc23JI+PUGii9zTxJBB3El6qdcUXm7Ja4hn5w=; b=EmKuFG/CFD/lz70xMEgn7qZuOK/1ZbSKS0yLzbdE8XujQW25o4YuhPLaGX6OJ9LdkU S5HRoQ+n6nvTX5tWoUazqL6TMciy46VdureUzQAm9s/uoTZ7CFdMYtNlVHvD2zz95QI1 /v8mwAC8dAWz5pZ9dT11LOgqszWUh/LNDZ7m89rvou/GlF348y+LgogRqiZdBDtk9+rF 0uYeiCWZpDY20kqSh6AskTKm2Nq7Z8avj1STCaQRwUmUlfpKaznJ3V3aY2Xxv/yBwMic dkSjrEh7SeGqRsKortyitflSN/I6X7WCDauzGyi9gAYnMehfEiuyA5iKYePmYJuDnfgg +KpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=WH+kDMCf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o21si14015775ejc.724.2021.03.23.04.36.05; Tue, 23 Mar 2021 04:36:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=WH+kDMCf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230080AbhCWLfI (ORCPT + 99 others); Tue, 23 Mar 2021 07:35:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230079AbhCWLen (ORCPT ); Tue, 23 Mar 2021 07:34:43 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 414BEC061574 for ; Tue, 23 Mar 2021 04:34:43 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 12so10847960wmf.5 for ; Tue, 23 Mar 2021 04:34:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=kJG6PyZc23JI+PUGii9zTxJBB3El6qdcUXm7Ja4hn5w=; b=WH+kDMCfYBdW7/F2qQa1invWtJWWKHsmhMnK7Wa6ORo5PhDr8OH1NtstjlLVKkIYNJ zE+MVu0P11LlLzWbcm8J/yUSsIPb2gXI7FPN5EedkchW8r7aT+8ayoOl8KiNNR1p2XVk bToehT/I1pqXS+twn7B1+B76Tu0Ojh7JAtVUc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :content-transfer-encoding:in-reply-to; bh=kJG6PyZc23JI+PUGii9zTxJBB3El6qdcUXm7Ja4hn5w=; b=SNS9Gkxqb07ZsvdXfOUuWCQzQs9kmIAkQO3Ln0kIty5koHo4psmMGkLhMKo0e31U0e 1pKxp5JouBwzqZo486Wf5PO74p8VzVSmTTwg113B8FBcpaZJyYMOE4nBNld4fgCX3+of xgPRgtVDLYx184PU5kxdw6qL3c+Lvo/kN3ccgKIHQjhQD3vaUQNLPqylPdDKPrViU3// ZIvSrsGN7Zz1evRqrfT+OShg+p/klks4oIyboL8HceVusjKmzYIFIdGlFV3i83Mnt0b1 M8BqGeqYfEMERxcE8ympB9XeMeROM5dI/LQaieJYDL7zBxuJx8jjOGHLhh3XbGezBlXX iHng== X-Gm-Message-State: AOAM531dNXY4PK96rA/MEiWgIfLDngDBaM1e1ZOfvRxBeeucmzxTiDSk XoGKr4utG9Jnaqvc6f0HdC5zvQ== X-Received: by 2002:a1c:498b:: with SMTP id w133mr2995826wma.134.1616499281914; Tue, 23 Mar 2021 04:34:41 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id z2sm26287859wrm.0.2021.03.23.04.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 04:34:41 -0700 (PDT) Date: Tue, 23 Mar 2021 12:34:39 +0100 From: Daniel Vetter To: Thomas =?iso-8859-1?Q?Hellstr=F6m_=28Intel=29?= Cc: dri-devel@lists.freedesktop.org, Christian Koenig , David Airlie , Daniel Vetter , Andrew Morton , Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH 1/2] mm,drm/ttm: Block fast GUP to TTM huge pages Message-ID: Mail-Followup-To: Thomas =?iso-8859-1?Q?Hellstr=F6m_=28Intel=29?= , dri-devel@lists.freedesktop.org, Christian Koenig , David Airlie , Andrew Morton , Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20210321184529.59006-1-thomas_os@shipmail.org> <20210321184529.59006-2-thomas_os@shipmail.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210321184529.59006-2-thomas_os@shipmail.org> X-Operating-System: Linux phenom 5.7.0-1-amd64 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Mar 21, 2021 at 07:45:28PM +0100, Thomas Hellstr?m (Intel) wrote: > TTM sets up huge page-table-entries both to system- and device memory, > and we don't want gup to assume there are always valid backing struct > pages for these. For PTEs this is handled by setting the pte_special bit, > but for the huge PUDs and PMDs, we have neither pmd_special nor > pud_special. Normally, huge TTM entries are identified by looking at > vma_is_special_huge(), but fast gup can't do that, so as an alternative > define _devmap entries for which there are no backing dev_pagemap as > special, update documentation and make huge TTM entries _devmap, after > verifying that there is no backing dev_pagemap. > > One other alternative would be to block TTM huge page-table-entries > completely, and while currently only vmwgfx use them, they would be > beneficial to other graphis drivers moving forward as well. > > Cc: Christian Koenig > Cc: David Airlie > Cc: Daniel Vetter > Cc: Andrew Morton > Cc: Jason Gunthorpe > Cc: linux-mm@kvack.org > Cc: dri-devel@lists.freedesktop.org > Cc: linux-kernel@vger.kernel.org > Signed-off-by: Thomas Hellstr?m (Intel) > --- > drivers/gpu/drm/ttm/ttm_bo_vm.c | 17 ++++++++++++++++- > mm/gup.c | 21 +++++++++++---------- > mm/memremap.c | 5 +++++ > 3 files changed, 32 insertions(+), 11 deletions(-) > > diff --git a/drivers/gpu/drm/ttm/ttm_bo_vm.c b/drivers/gpu/drm/ttm/ttm_bo_vm.c > index 6dc96cf66744..1c34983480e5 100644 > --- a/drivers/gpu/drm/ttm/ttm_bo_vm.c > +++ b/drivers/gpu/drm/ttm/ttm_bo_vm.c > @@ -195,6 +195,7 @@ static vm_fault_t ttm_bo_vm_insert_huge(struct vm_fault *vmf, > pfn_t pfnt; > struct ttm_tt *ttm = bo->ttm; > bool write = vmf->flags & FAULT_FLAG_WRITE; > + struct dev_pagemap *pagemap; > > /* Fault should not cross bo boundary. */ > page_offset &= ~(fault_page_size - 1); > @@ -210,6 +211,20 @@ static vm_fault_t ttm_bo_vm_insert_huge(struct vm_fault *vmf, > if ((pfn & (fault_page_size - 1)) != 0) > goto out_fallback; > > + /* > + * Huge entries must be special, that is marking them as devmap > + * with no backing device map range. If there is a backing > + * range, Don't insert a huge entry. > + * If this check turns out to be too much of a performance hit, > + * we can instead have drivers indicate whether they may have > + * backing device map ranges and if not, skip this lookup. > + */ I think we can do this statically: - if it's system memory we know there's no devmap for it, and we do the trick to block gup_fast - if it's iomem, we know gup_fast wont work anyway if don't set PFN_DEV, so might as well not do that I think that would cover all cases without this check here? -Daniel > + pagemap = get_dev_pagemap(pfn, NULL); > + if (pagemap) { > + put_dev_pagemap(pagemap); > + goto out_fallback; > + } > + > /* Check that memory is contiguous. */ > if (!bo->mem.bus.is_iomem) { > for (i = 1; i < fault_page_size; ++i) { > @@ -223,7 +238,7 @@ static vm_fault_t ttm_bo_vm_insert_huge(struct vm_fault *vmf, > } > } > > - pfnt = __pfn_to_pfn_t(pfn, PFN_DEV); > + pfnt = __pfn_to_pfn_t(pfn, PFN_DEV | PFN_MAP); > if (fault_page_size == (HPAGE_PMD_SIZE >> PAGE_SHIFT)) > ret = vmf_insert_pfn_pmd_prot(vmf, pfnt, pgprot, write); > #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD > diff --git a/mm/gup.c b/mm/gup.c > index e40579624f10..1b6a127f0bdd 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -1993,6 +1993,17 @@ static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start, > } > > #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL > +/* > + * If we can't determine whether or not a pte is special, then fail immediately > + * for ptes. Note, we can still pin HugeTLB as it is guaranteed not to be > + * special. For THP, special huge entries are indicated by xxx_devmap() > + * returning true, but a corresponding call to get_dev_pagemap() will > + * return NULL. > + * > + * For a futex to be placed on a THP tail page, get_futex_key requires a > + * get_user_pages_fast_only implementation that can pin pages. Thus it's still > + * useful to have gup_huge_pmd even if we can't operate on ptes. > + */ > static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > unsigned int flags, struct page **pages, int *nr) > { > @@ -2069,16 +2080,6 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > return ret; > } > #else > - > -/* > - * If we can't determine whether or not a pte is special, then fail immediately > - * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not > - * to be special. > - * > - * For a futex to be placed on a THP tail page, get_futex_key requires a > - * get_user_pages_fast_only implementation that can pin pages. Thus it's still > - * useful to have gup_huge_pmd even if we can't operate on ptes. > - */ > static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > unsigned int flags, struct page **pages, int *nr) > { > diff --git a/mm/memremap.c b/mm/memremap.c > index 7aa7d6e80ee5..757551cd2a4d 100644 > --- a/mm/memremap.c > +++ b/mm/memremap.c > @@ -471,6 +471,11 @@ void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns) > * > * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap > * is non-NULL but does not cover @pfn the reference to it will be released. > + * > + * Return: A referenced pointer to a struct dev_pagemap containing @pfn, > + * or NULL if there was no such pagemap registered. For interpretion > + * of NULL returns for pfns extracted from valid huge page table entries, > + * please see gup_pte_range(). > */ > struct dev_pagemap *get_dev_pagemap(unsigned long pfn, > struct dev_pagemap *pgmap) > -- > 2.30.2 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch