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 X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_RED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79BE6C433C1 for ; Tue, 23 Mar 2021 11:47:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1056A619C0 for ; Tue, 23 Mar 2021 11:47:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1056A619C0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ffwll.ch Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 99E196B016D; Tue, 23 Mar 2021 07:47:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 975526B016F; Tue, 23 Mar 2021 07:47:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C76C6B0170; Tue, 23 Mar 2021 07:47:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0115.hostedemail.com [216.40.44.115]) by kanga.kvack.org (Postfix) with ESMTP id 60D386B016D for ; Tue, 23 Mar 2021 07:47:29 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 219906D63 for ; Tue, 23 Mar 2021 11:47:29 +0000 (UTC) X-FDA: 77950963818.25.37C7F18 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by imf05.hostedemail.com (Postfix) with ESMTP id 0AC24E0001AF for ; Tue, 23 Mar 2021 11:47:27 +0000 (UTC) Received: by mail-wr1-f49.google.com with SMTP id e9so20438943wrw.10 for ; Tue, 23 Mar 2021 04:47:28 -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=riev4q0SocYg+t59VV4jeUGzLfgXPce8r3inSkdpgP8=; b=EuGZoQRp0Ar/FWrbti0VOzivbL8+K52z7tT1QolbfVKrztBeft/JghVd5GK47YMBcM 6H8OxStUnFCIUTORj+zKe5F4jfvbqIOPivAXAipd7SBEgfBqxF4a+w7fUzcWxzwPBmuN AKnebZ/VmCFFdHuIfHUaenqMo58v+a9dYAoZQ= 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=riev4q0SocYg+t59VV4jeUGzLfgXPce8r3inSkdpgP8=; b=liZG1k80WXByRtO/y64Gp/Jc8dnM44WTuyADSKAoPADccywgvJ6+BKPgZwIze0Nb4W 9thsngaudtmKvBGqew3zMiyQ5F90MF9xY75KbFenIM5ddcnnPBQrEum37BS50ZzmpNS/ j0vrtINrjngYEpzfKScUZ2sl7tLXR2OQGgKJWIiodLcpwXG+9U7gAVY9rR7UaTxxx9ww wkHOHxg5SSKoEzL5zbuAh8eZrQgINA4joGMPsgDrIEJ4Cylq6PBnU6EsfujdUJQbqjwW LqkUHxSDyRPnKxF4xDd8jHyQ148law+7urCwrr9RJA6kywczKwO0Ga9TaHdSnnwQ/fHG sYpQ== X-Gm-Message-State: AOAM532tKAEXKMW0DXGW7pVAZiQX763N+udEtkzEhwO8Hb9sN9Orrpr1 FCcxjOT34wywYSToeRnmesd//w== X-Google-Smtp-Source: ABdhPJxkKPcUm9KmorxPmIPOybYhrPJMk/cWM/v2xitGTE3ftHnFLeE0TR7EAx44csdliBNJeki4DA== X-Received: by 2002:a05:6000:1563:: with SMTP id 3mr3514940wrz.211.1616500047082; Tue, 23 Mar 2021 04:47:27 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id t23sm24019097wra.50.2021.03.23.04.47.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 04:47:26 -0700 (PDT) Date: Tue, 23 Mar 2021 12:47:24 +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 2/2] mm,drm/ttm: Use VM_PFNMAP for TTM vmas 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-3-thomas_os@shipmail.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <20210321184529.59006-3-thomas_os@shipmail.org> X-Operating-System: Linux phenom 5.7.0-1-amd64 X-Stat-Signature: ox8ctt45djtfn3kdjmgei8myqudthaxe X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 0AC24E0001AF Received-SPF: none (ffwll.ch>: No applicable sender policy available) receiver=imf05; identity=mailfrom; envelope-from=""; helo=mail-wr1-f49.google.com; client-ip=209.85.221.49 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616500047-695851 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Sun, Mar 21, 2021 at 07:45:29PM +0100, Thomas Hellstr=F6m (Intel) wrot= e: > To block fast gup we need to make sure TTM ptes are always special. > With MIXEDMAP we, on architectures that don't support pte_special, > insert normal ptes, but OTOH on those architectures, fast is not > supported. > At the same time, the function documentation to vm_normal_page() sugges= ts > that ptes pointing to system memory pages of MIXEDMAP vmas are always > normal, but that doesn't seem consistent with what's implemented in > vmf_insert_mixed(). I'm thus not entirely sure this patch is actually > needed. >=20 > But to make sure and to avoid also normal (non-fast) gup, make all > TTM vmas PFNMAP. With PFNMAP we can't allow COW mappings > anymore so make is_cow_mapping() available and use it to reject > COW mappigs at mmap time. >=20 > There was previously a comment in the code that WC mappings together > with x86 PAT + PFNMAP was bad for performance. However from looking at > vmf_insert_mixed() it looks like in the current code PFNMAP and MIXEDMA= P > are handled the same for architectures that support pte_special. This > means there should not be a performance difference anymore, but this > needs to be verified. >=20 > 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=F6m (Intel) > --- > drivers/gpu/drm/ttm/ttm_bo_vm.c | 22 ++++++++-------------- > include/linux/mm.h | 5 +++++ > mm/internal.h | 5 ----- > 3 files changed, 13 insertions(+), 19 deletions(-) >=20 > diff --git a/drivers/gpu/drm/ttm/ttm_bo_vm.c b/drivers/gpu/drm/ttm/ttm_= bo_vm.c > index 1c34983480e5..708c6fb9be81 100644 > --- a/drivers/gpu/drm/ttm/ttm_bo_vm.c > +++ b/drivers/gpu/drm/ttm/ttm_bo_vm.c > @@ -372,12 +372,7 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_faul= t *vmf, > * at arbitrary times while the data is mmap'ed. > * See vmf_insert_mixed_prot() for a discussion. > */ > - if (vma->vm_flags & VM_MIXEDMAP) > - ret =3D vmf_insert_mixed_prot(vma, address, > - __pfn_to_pfn_t(pfn, PFN_DEV), > - prot); > - else > - ret =3D vmf_insert_pfn_prot(vma, address, pfn, prot); > + ret =3D vmf_insert_pfn_prot(vma, address, pfn, prot); > =20 > /* Never error on prefaulted PTEs */ > if (unlikely((ret & VM_FAULT_ERROR))) { > @@ -555,18 +550,14 @@ static void ttm_bo_mmap_vma_setup(struct ttm_buff= er_object *bo, struct vm_area_s > * Note: We're transferring the bo reference to > * vma->vm_private_data here. > */ > - > vma->vm_private_data =3D bo; > =20 > /* > - * We'd like to use VM_PFNMAP on shared mappings, where > - * (vma->vm_flags & VM_SHARED) !=3D 0, for performance reasons, > - * but for some reason VM_PFNMAP + x86 PAT + write-combine is very > - * bad for performance. Until that has been sorted out, use > - * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719 > + * PFNMAP forces us to block COW mappings in mmap(), > + * and with MIXEDMAP we would incorrectly allow fast gup > + * on TTM memory on architectures that don't have pte_special. > */ > - vma->vm_flags |=3D VM_MIXEDMAP; > - vma->vm_flags |=3D VM_IO | VM_DONTEXPAND | VM_DONTDUMP; > + vma->vm_flags |=3D VM_PFNMAP | VM_IO | VM_DONTEXPAND | VM_DONTDUMP; > } > =20 > int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, > @@ -579,6 +570,9 @@ int ttm_bo_mmap(struct file *filp, struct vm_area_s= truct *vma, > if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET_START)) > return -EINVAL; > =20 > + if (unlikely(is_cow_mapping(vma->vm_flags))) > + return -EINVAL; > + > bo =3D ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma)); > if (unlikely(!bo)) > return -EINVAL; > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 77e64e3eac80..c6ebf7f9ddbb 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -686,6 +686,11 @@ static inline bool vma_is_accessible(struct vm_are= a_struct *vma) > return vma->vm_flags & VM_ACCESS_FLAGS; > } > =20 > +static inline bool is_cow_mapping(vm_flags_t flags) Bit a bikeshed, but I wonder whether the public interface shouldn't be vma_is_cow_mapping. Or whether this shouldn't be rejected somewhere else, since at least in drivers/gpu we have tons of cases that don't check for this and get it all kinds of wrong I think. remap_pfn_range handles this for many cases, but by far not for all. Anyway patch itself lgtm: Reviewed-by: Daniel Vetter I'll try and find some -mm folks to look at this too. -Daniel > +{ > + return (flags & (VM_SHARED | VM_MAYWRITE)) =3D=3D VM_MAYWRITE; > +} > + > #ifdef CONFIG_SHMEM > /* > * The vma_is_shmem is not inline because it is used only by slow > diff --git a/mm/internal.h b/mm/internal.h > index 9902648f2206..1432feec62df 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -296,11 +296,6 @@ static inline unsigned int buddy_order(struct page= *page) > */ > #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) > =20 > -static inline bool is_cow_mapping(vm_flags_t flags) > -{ > - return (flags & (VM_SHARED | VM_MAYWRITE)) =3D=3D VM_MAYWRITE; > -} > - > /* > * These three helpers classifies VMAs for virtual memory accounting. > */ > --=20 > 2.30.2 >=20 --=20 Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch