Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp352350pxb; Thu, 5 Nov 2020 01:35:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvttwGnqDBUJjKTy9UmB5z30lbQJPO5sxbn516bk0Po/5Frlpx93ohYwhUOeMOZ4TK6RPm X-Received: by 2002:a17:906:e15:: with SMTP id l21mr1457904eji.509.1604568955360; Thu, 05 Nov 2020 01:35:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604568955; cv=none; d=google.com; s=arc-20160816; b=gl/Hw/y51hgAKRYFfVZjf7dwW/2YwoEnT0mmdcpplOTis5GMruLYRhvgbuqE3bF59O 5CYQuHmgfkDpcG7GAyP0msvaIMlQEQ2escrfE1vgZR0I6kmyS+G9MRY+klt6aqQEAskF 7ytFTQTaaUtm8NuvqBJU35gUphmWHy5+t+EJ+PLioy/3uQPPJVHREVEa+YHmOf7MGfPd JJfXd8lzz9SiGpwR8QN3pfZH6nQcf50YEBCVcrhFFdtzoHDJ2t/aGa+1yVPDSvHG0sNy Op3RmkSQOh2C0lXmCZwgl2BfX3gEnZoNW8atcLGyhY/1sYoe5iLwuLC8Is4maPWqU++D 5XYw== 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-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=dhezt058VYAXUzsmT6wFAloMyt6Frs+cE6TCkSAcHPs=; b=p4QGJwrypnZZo9ZnBke2iJvqBwUFh/benV7LAzWWzspiqrC3+tJe51KzrBEgWQK3D2 1jRHLgR2g79wyu8IpITrtLTeWY3yoHt8hXMILe/FvmKPGknnTSK1yDqDg31n9UYFaZZ8 cU9riroaEr7CVA+G3RtsqU9SkAkPFQJQKLPFTLYq5o4MRuNla1XQBSx+QwpuRSy008/z p6cWzekRcugnOJ9ievQuEXDinWDBRhwMWtr2aAYuFO38L003zjdLf7myt/AGsGIl4kp/ xinvNLuL6IjjEJxnKd0sL3jsZ/e65OMmRM5iL8nuy3ECRz9e3NNWvjLuh3BG9k+XGfhW KS2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=ST87g3bo; 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 f12si664234ejb.687.2020.11.05.01.35.32; Thu, 05 Nov 2020 01:35:55 -0800 (PST) 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=ST87g3bo; 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 S1731175AbgKEJeQ (ORCPT + 99 others); Thu, 5 Nov 2020 04:34:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730933AbgKEJeP (ORCPT ); Thu, 5 Nov 2020 04:34:15 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09C4EC0613D2 for ; Thu, 5 Nov 2020 01:34:14 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id h62so875711wme.3 for ; Thu, 05 Nov 2020 01:34:13 -0800 (PST) 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:in-reply-to; bh=dhezt058VYAXUzsmT6wFAloMyt6Frs+cE6TCkSAcHPs=; b=ST87g3boTQObR5JxsvDJGIfMAlcgsXvr+F3JKuft7VsNKm+GuZmFkRWYBz/N9m+nP5 gUOFOdjErnnQz3ZNReWuFokDdnl7uPc7ZMOj6kqNide21CEbt+Pus48kXLDRURvnNC2N 3RAKhoWa6Hje1zG3vURpcraCOdJW7Q+XZ+t28= 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 :in-reply-to; bh=dhezt058VYAXUzsmT6wFAloMyt6Frs+cE6TCkSAcHPs=; b=fFD2BRDCEyQE5nt/h5IfQSdw0P7hMq3q0pmCSDE6JfsP+EpRW2Se6lN5MUNy0bcA6g HcHIlhNWbd/162PKtQnpGSTP3BcrOHmxbQeqXxhfTyAxueoiIsQ3EieyIs/7LL3nXSoU cNeY1xroj+lKEg4w61J09R/xwYlq1ZGu7hU6Sa9NB/FFEz0k/2niAI4TbA6qQwXQf1zY c/B0g4urbeGZlsRF920cgc8+LcnFVcrBqY7jE+VCJbMjOwEL9N24cE0/2pK+/qoXFMns mqiaZkGKnHX4Ojh6ARlHbWximCBrJrZtw1NqvPTGaB23w26wx2jJEpLziX8ZOBvgPYN5 y92g== X-Gm-Message-State: AOAM5315IQWLFvtC9gpfSEaPPenV4y3zuim5vFBO5CloD6H82xSq2Tg+ D9NsBIpz8BdFSbTjxBuxTg1EGA== X-Received: by 2002:a1c:21c1:: with SMTP id h184mr1802586wmh.106.1604568852772; Thu, 05 Nov 2020 01:34:12 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id s202sm1648809wme.39.2020.11.05.01.34.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2020 01:34:11 -0800 (PST) Date: Thu, 5 Nov 2020 10:34:09 +0100 From: Daniel Vetter To: Dan Williams Cc: Bjorn Helgaas , Daniel Vetter , DRI Development , LKML , KVM list , Linux MM , Linux ARM , linux-samsung-soc , "Linux-media@vger.kernel.org" , Daniel Vetter , Jason Gunthorpe , Kees Cook , Andrew Morton , John Hubbard , =?iso-8859-1?B?Suly9G1l?= Glisse , Jan Kara , Bjorn Helgaas , Linux PCI Subject: Re: [PATCH v5 11/15] PCI: Obey iomem restrictions for procfs mmap Message-ID: <20201105093409.GR401619@phenom.ffwll.local> Mail-Followup-To: Dan Williams , Bjorn Helgaas , DRI Development , LKML , KVM list , Linux MM , Linux ARM , linux-samsung-soc , "Linux-media@vger.kernel.org" , Daniel Vetter , Jason Gunthorpe , Kees Cook , Andrew Morton , John Hubbard , =?iso-8859-1?B?Suly9G1l?= Glisse , Jan Kara , Bjorn Helgaas , Linux PCI References: <20201104165017.GA352206@bjorn-Precision-5520> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: Linux phenom 5.7.0-1-amd64 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 04, 2020 at 12:12:15PM -0800, Dan Williams wrote: > On Wed, Nov 4, 2020 at 8:50 AM Bjorn Helgaas wrote: > > > > On Wed, Nov 04, 2020 at 09:44:04AM +0100, Daniel Vetter wrote: > > > On Tue, Nov 3, 2020 at 11:09 PM Dan Williams wrote: > > > > On Tue, Nov 3, 2020 at 1:28 PM Bjorn Helgaas wrote: > > > > > On Fri, Oct 30, 2020 at 11:08:11AM +0100, Daniel Vetter wrote: > > > > > > There's three ways to access PCI BARs from userspace: /dev/mem, sysfs > > > > > > files, and the old proc interface. Two check against > > > > > > iomem_is_exclusive, proc never did. And with CONFIG_IO_STRICT_DEVMEM, > > > > > > this starts to matter, since we don't want random userspace having > > > > > > access to PCI BARs while a driver is loaded and using it. > > > > > > > > > > > > Fix this by adding the same iomem_is_exclusive() check we already have > > > > > > on the sysfs side in pci_mmap_resource(). > > > > > > > > > > > > References: 90a545e98126 ("restrict /dev/mem to idle io memory ranges") > > > > > > Signed-off-by: Daniel Vetter > > > > > > > > > > This is OK with me but it looks like IORESOURCE_EXCLUSIVE is currently > > > > > only used in a few places: > > > > > > > > > > e1000_probe() calls pci_request_selected_regions_exclusive(), > > > > > ne_pci_probe() calls pci_request_regions_exclusive(), > > > > > vmbus_allocate_mmio() calls request_mem_region_exclusive() > > > > > > > > > > which raises the question of whether it's worth keeping > > > > > IORESOURCE_EXCLUSIVE at all. I'm totally fine with removing it > > > > > completely. > > > > > > > > Now that CONFIG_IO_STRICT_DEVMEM upgrades IORESOURCE_BUSY to > > > > IORESOURCE_EXCLUSIVE semantics the latter has lost its meaning so I'd > > > > be in favor of removing it as well. > > > > > > Still has some value since it enforces exclusive access even if the > > > config isn't enabled, and iirc e1000 had some fun with userspace tools > > > clobbering the firmware and bricking the chip. > > > > There's *some* value; I'm just skeptical since only three drivers use > > it. > > > > IORESOURCE_EXCLUSIVE is from e8de1481fd71 ("resource: allow MMIO > > exclusivity for device drivers"), and the commit message says this is > > only active when CONFIG_STRICT_DEVMEM is set. I didn't check to see > > whether that's still true. > > > > That commit adds a bunch of wrappers and "__"-prefixed functions to > > pass the IORESOURCE_EXCLUSIVE flag around. That's a fair bit of > > uglification for three drivers. > > > > > Another thing I kinda wondered, since pci maintainer is here: At least > > > in drivers/gpu I see very few drivers explicitly requestion regions > > > (this might be a historical artifact due to the shadow attach stuff > > > before we had real modesetting drivers). And pci core doesn't do that > > > either, even when a driver is bound. Is this intentional, or > > > should/could we do better? Since drivers work happily without > > > reserving regions I don't think "the drivers need to remember to do > > > this" will ever really work out well. > > > > You're right, many drivers don't call pci_request_regions(). Maybe we > > could do better, but I haven't looked into that recently. There is a > > related note in Documentation/PCI/pci.rst that's been there for a long > > time (it refers to "pci_request_resources()", which has never existed > > AFAICT). I'm certainly open to proposals. > > It seems a bug that the kernel permits MMIO regions with side effects > to be ioremap()'ed without request_mem_region() on the resource. I > wonder how much log spam would happen if ioremap() reported whenever a > non-IORESOURE_BUSY range was passed to it? The current state of > affairs to trust *remap users to have claimed their remap target seems > too ingrained to unwind now. Yeah I think that's hopeless. I think the only feasible approach is if bus drivers claim resources by default when a driver is bound (it should nest, so if the driver claims again, I think that should all keep working), just using the driver name. Probably with some special casing for legacy io (vgaarb.c should claim these I guess). Still probably tons of fallout. Once that's rolled out to all bus drivers we could perhaps add the ioremap check without drowning in log spam. Still a multi-year project I think :-/ -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch