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=-3.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no 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 BFB50C56202 for ; Wed, 21 Oct 2020 15:55:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E5915222C8 for ; Wed, 21 Oct 2020 15:55:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=ffwll.ch header.i=@ffwll.ch header.b="bOKxdvhj" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E5915222C8 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 191806B005C; Wed, 21 Oct 2020 11:55:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 145496B005D; Wed, 21 Oct 2020 11:55:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 05A486B0068; Wed, 21 Oct 2020 11:55:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0085.hostedemail.com [216.40.44.85]) by kanga.kvack.org (Postfix) with ESMTP id CE5716B005C for ; Wed, 21 Oct 2020 11:55:07 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5EB14181AEF09 for ; Wed, 21 Oct 2020 15:55:07 +0000 (UTC) X-FDA: 77396381454.10.noise49_1308e6827249 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 3C80F16A0DE for ; Wed, 21 Oct 2020 15:55:07 +0000 (UTC) X-HE-Tag: noise49_1308e6827249 X-Filterd-Recvd-Size: 5653 Received: from mail-oi1-f193.google.com (mail-oi1-f193.google.com [209.85.167.193]) by imf38.hostedemail.com (Postfix) with ESMTP for ; Wed, 21 Oct 2020 15:55:06 +0000 (UTC) Received: by mail-oi1-f193.google.com with SMTP id m128so2578156oig.7 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=PVvUhtjAiA0DbGmH8SV6cAJWPtx0UwN5a7fJHJXwtySBuzccBmr7qQXkSBJ/pgndVm xPCR+hzyfiAi6MUlKSFankMcZJ0BqkbkH0BP87zbNpMHqRIkQH62MiCZZi/wYz/YKah7 ygAk7G1paP5bp44JHYAXM5zQMB2rSSGgiFjcYhIzaHh6+LZt/8dZOwWiQPtKp13eUDOz jAYKmtn1FrhWMfda9eVQGK6GU8kjdO9PPXtZZ66S0CnabRMqiqp6BQHPoU2S13pv0Erl RIMBiSvdeoFjIt9EImXEnZqFZnN9Pm7eCx7llI85Q4ITYOXDft7jrvfl7zE7AEbWwpRO cauQ== X-Gm-Message-State: AOAM531DiuX+zOBUnN8E32fb2fHZENGDhnlpzlTAPwOjwT4zBM6bMTjM zw/Z0fS6sF3j1Pw8gfNHJADxS/o/3c5J+eo46USr9Q== X-Google-Smtp-Source: ABdhPJzPCgyzodnSmRD9rGONXEuc3qJsREMQopHY6rQLHriRRIWOuQr4ZeQqsR42HntwRWBXJ/bxEHV/oXvYTFC/kGQ= 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" 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 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