Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp5331395pxu; Wed, 21 Oct 2020 22:09:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpZO5rhzRXwbiav5SVJkC37xcwYpsMJdFj9z4jesWSEJEhkgnfz0DYka4wtNW1MBp4+BDi X-Received: by 2002:a05:6402:1615:: with SMTP id f21mr689656edv.257.1603343398565; Wed, 21 Oct 2020 22:09:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603343398; cv=none; d=google.com; s=arc-20160816; b=jqbOpSL9nJcfuvAmzG2uIZPIprekWJp9suheodDq7qzj205RVWK5FsuzhLTbQO55A2 iez4NBkZidn8LBODxyD4arwk1k2pNKqQKu4+gPz5ueUzxmEiOPUDe/zrXT7AFbm6CC9V cTuPufRd/yQfv3uPEfUqQK6lWcOgJYNSEM5lMNwih5ilPIgFJMg/WQ5wjEHMsu0oIy05 D80+GofLdXHK1cNdEX+LKpl50zWCsLOhVVoJJM3HUUFCWzU14nL2sj9nrtuG4m64HY83 o1bCxE28XjYpdd3RYbty17/0UkmZEQAhfBtaCs8eYXcw1y//HLU4z27KVGjhnXNoFjvW qLGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=C6DKYnDfudq4L3jq8cgaqwhb0+5UPYI4hKd8ZXy4r2g=; b=ea9mRtQdmROFy63xNqVe4ZkKP8U/xLhdpa+lKGV2I81OmHpP74OKSEL0I6aJNbVeEp SYKg35wfXjIkx6ANDPFG4ztJvnG/W2V7SoKjPfV2ASz4eSSWiZlAAW+Hk+Dl5UKbpi9t KXBN/tdQd7Cc7/OAsONoHGaYDAEyNRUZvU3VyXGTOUbo54j9RLbz6AmoeSCLEPI2XeyZ 00AaK055UolgLwU7pQuhOjZ8i9xdgQi1+UkagbD//tvsobWnc/If1XZNjqqiKs0PDA/X lF++HxOG/F44O3abtm3lwVgRZA7ghUql4AY8oQXYx2cepsEBk02H32i/XlNG3/B0a9G+ tWRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=bOKxdvhj; 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 y1si306446edv.123.2020.10.21.22.09.36; Wed, 21 Oct 2020 22:09:58 -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=bOKxdvhj; 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 S2444217AbgJUPzJ (ORCPT + 99 others); Wed, 21 Oct 2020 11:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2444214AbgJUPzI (ORCPT ); Wed, 21 Oct 2020 11:55:08 -0400 Received: from mail-oi1-x243.google.com (mail-oi1-x243.google.com [IPv6:2607:f8b0:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42141C0613D6 for ; Wed, 21 Oct 2020 08:55:06 -0700 (PDT) Received: by mail-oi1-x243.google.com with SMTP id n3so2610682oie.1 for ; Wed, 21 Oct 2020 08:55:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=C6DKYnDfudq4L3jq8cgaqwhb0+5UPYI4hKd8ZXy4r2g=; b=bOKxdvhjgEKOtLPpVYj6Ijwh2mKSXVoQ+GQkd6y9om8AOiAaZ5BVOKtRbAIB6TOef6 JViom2WHSbHDxfwbjTICNMKIpnUHoAO8RMBp0R1dGY04OeAqtrO7mA9xr7W57bhgZWkI m7xoz6/cv19qNKpiDTjcgK90UI0cWYA6F2WWM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=C6DKYnDfudq4L3jq8cgaqwhb0+5UPYI4hKd8ZXy4r2g=; b=AM8wDPfGP8gmEpPjie5jzaULPUFIY8sj5VOgJxhGzkFMdIYioeKZnUucCZzOg3HArC 8/KwpC08fJ/wV8eqOqm/I/SKaaGFBkGFklfrzp7NXdcM/e4FjATaorG13hNRLGqhc4Z5 QPcmlX4NCP2FeAOrSwtvZ7uQAOtd1QMA4EhOLKUXGK1l5cYcjsHqNe8v/J8og+8UZmFX dgEFRFBat+mrULrzDx1U9XAPRluwSH94C6oZ0XzxOtvtzVYxGkSMLQqS/P49WakD/a8K YojuYHw9UeS5wIS38CpIs2yYDs54DYR4lq3ruiehjZf9S0FOkrNhVsinjNR8H1w9UGuw UTtA== X-Gm-Message-State: AOAM530xI/ZUzgCzYHegYasxvHJS4uwymRwkry78nDvDhata5/pWWG/3 CMWeAAIoYITjYDkZMjLSibfB8S5XhQ3V6/qLC9Z7SWzgJ0zsltrW X-Received: by 2002:aca:52c4:: with SMTP id g187mr2521031oib.101.1603295705466; Wed, 21 Oct 2020 08:55:05 -0700 (PDT) MIME-Version: 1.0 References: <20201021085655.1192025-1-daniel.vetter@ffwll.ch> <20201021085655.1192025-13-daniel.vetter@ffwll.ch> <20201021125030.GK36674@ziepe.ca> <20201021151352.GL36674@ziepe.ca> In-Reply-To: <20201021151352.GL36674@ziepe.ca> From: Daniel Vetter Date: Wed, 21 Oct 2020 17:54:54 +0200 Message-ID: Subject: Re: [PATCH v3 12/16] PCI: Obey iomem restrictions for procfs mmap To: Jason Gunthorpe Cc: DRI Development , LKML , KVM list , Linux MM , Linux ARM , linux-samsung-soc , "open list:DMA BUFFER SHARING FRAMEWORK" , linux-s390 , Daniel Vetter , Kees Cook , Dan Williams , Andrew Morton , John Hubbard , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Jan Kara , Bjorn Helgaas , Linux PCI , Daniel Vetter Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 21, 2020 at 5:13 PM Jason Gunthorpe wrote: > > On Wed, Oct 21, 2020 at 04:42:11PM +0200, Daniel Vetter wrote: > > > Uh yes. In drivers/gpu this isn't a problem because we only install > > ptes from the vm_ops->fault handler. So no races. And I don't think > > you can fix this otherwise through holding locks: mmap_sem we can't > > hold because before vma_link we don't even know which mm_struct is > > involved, so can't solve the race. Plus this would be worse that > > mm_take_all_locks used by mmu notifier. And the address_space > > i_mmap_lock is also no good since it's not held during the ->mmap > > callback, when we write the ptes. And the resource locks is even less > > useful, since we're not going to hold that at vma_link() time for > > sure. > > > > Hence delaying the pte writes after the vma_link, which means ->fault > > time, looks like the only way to close this gap. > > > Trouble is I have no idea how to do this cleanly ... > > How about add a vm_ops callback 'install_pages'/'prefault_pages' ? > > Call it after vm_link() - basically just move the remap_pfn, under > some other lock, into there. Yeah, I think that would be useful. This might also be useful for something entirely different: For legacy fbdev emulation on top of drm kernel modesetting drivers we need to track dirty pages of VM_IO mmaps. Right now that's a gross hack, and essentially we just pay the price for entirely separate storage and an additional memcpy when this is needed to emulate fbdev mmap on top of drm. But if we have install_ptes callback or similar we could just wrap the native vm_ops and add a mkwrite callback on top for that dirty tracking. For that the hook would need to be after vm_set_page_prot so that we write-protect the ptes by default, since that's where we compute vma_wants_writenotify(). That's also after vma_link, so one hook for two use-cases. The trouble is that io_remap_pfn adjust vma->pgoff, so we'd need to split that. So ideally ->mmap would never set up any ptes. I guess one option would be if remap_pfn_range would steal the vma->vm_ops pointer for itself, then it could set up the correct ->install_ptes hook. But there's tons of callers for that, so not sure that's a bright idea. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch