From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 935062F9DAE for ; Tue, 23 Sep 2025 20:07:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758658053; cv=none; b=ejsm33BxJ44LKcrOyAMLFiiIiFbR9WhGITLhwnAr9JRazFkhb/G7c9pUdmcEgenX9MXEK4a2nYNNCkaafgU3KsYIJJ8JNm45FXMDbmICdYl2RGBWxegdbHK94niCd7bUW6tGkBczRnR/w1mso9fFWIEB9Rkbl7DywUyGc+hfvC8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758658053; c=relaxed/simple; bh=/cNMCmhs2+2GgzKjW2K7MV1lRjCXVMAS8z1dlxsrJ7Y=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LFHtCmunNjDPdRP/dgaiHu9fsbkOH4h7u3KWNnjXWwD98O4+lIf3AZt1IlIPi/bjLXTjsKHixKW6Zm03tLs6N5pkloAlvy3fftmHJTDJg8VTClVVfgsEJ2aaqnFqWJl3dGoY5vXM+GVqysDOdpzwQeFBOGx8Ob41iK0sRKojBBY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZLFqnU6d; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZLFqnU6d" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758658050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TkF/1TIVCdQ8I35UWInq/5XGtjT7LO6t3DfocUuGzJk=; b=ZLFqnU6d+6znjZm1pAwJ06tM7mu3sevqZlUrT8MjnxwlkXnkTcyAXiAA6TG2ugWnXotjoI TdSoPOFUBZUupSa72gNvmK6pOUe++iUCJ2LAaZQ+ZbW03xt+Z/3dlVMHZjvF6jguJV9+zY anHh/hhO8vGM/0FtG3uQIIO8IH4Ytj4= Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-_KnEq0x9Or-YIOCSP1MBIQ-1; Tue, 23 Sep 2025 16:07:29 -0400 X-MC-Unique: _KnEq0x9Or-YIOCSP1MBIQ-1 X-Mimecast-MFC-AGG-ID: _KnEq0x9Or-YIOCSP1MBIQ_1758658048 Received: by mail-io1-f71.google.com with SMTP id ca18e2360f4ac-8935214d60bso139783939f.1 for ; Tue, 23 Sep 2025 13:07:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758658048; x=1759262848; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TkF/1TIVCdQ8I35UWInq/5XGtjT7LO6t3DfocUuGzJk=; b=B9AH2N8h7yRmREpLvM/i6i78P8vspOYtm0QVTrBJvTdkvRzf9Ef+9kaL9B0l7XysZ3 Iy3BQWIFe21vVigZNjwRjJgf9OPBV6DbJC5DgoMnsz5wxpNolj6V8atzrtI3FFrtDgCH BctemTC21DghbsXtOhN5ShuGaX4uY3x/O8bW0uPq1NCpZNvzQK7CydvpXxvvpP++9YLa nboV7/33B/pfyplRvQ+CjZxSqbfDBDixQso7ElLJnwECE4DnQvPQ+Ani/wL4NsQE2yTC JjileudfS4asHftXyVGwM6yHCn7hxIvqUi4uXLIVXmOZhEmno6Rl/n41S9crSlNNQwDE ikig== X-Forwarded-Encrypted: i=1; AJvYcCWxen/5xrXKu3TsdN/oKylLMboNoVoChbdzViQp9i7AE1AzAV/RE09YJBfruyLJnhSvStNAJ0ienQY/36I=@vger.kernel.org X-Gm-Message-State: AOJu0YxYgj26Sq6xRWA8b2dkP6ZSzTCzNBj1VhkmnnhDqsYo1WH23ySN LbdD8b32sCa19YT1OEFHTpwhQn014plx/InYqpNU8tAF6OLZLaPF0Va2cvKdkIYkPPisR7InVyQ 6HFfgfGXUpNoTWwrqE4LEOQYiLeJkF8jxpyGXlPzAm6d8PBrnDMkdGFKt1jXCLTfFlw== X-Gm-Gg: ASbGnctVQfjumbE6AFuBdyn7YlcBGJw3gnMxur5PwwbjeEqqpKF7ZRcREj8/JWMAuE5 zUITvXy53ruS+SS0ueYxWB8VdmOTc2EW5CPxaMkJH1F4jEPqLqwb2Q/0u8DI3GpXdEBNaRd8Z7X T6dyCMBasYnxcpaDQO0ScRLlW58xIMwximA2bBSYI5Dx6Pm5hqVdZTtFUh9jC5xSpNI+oiTcOE4 z1VRLgcgvNaEij4HmEZ1wmWlWbVbmdcywOcy8VjbSLY0KP1R9NilNYn58n2+3w7K4Q9Bd/f6kRT Zy7cSO3nhjbO3x70ZjtDzwTUGwTeUITrqoc1oAxke5s= X-Received: by 2002:a05:6e02:b27:b0:423:fd07:d3fe with SMTP id e9e14a558f8ab-42581e0924amr21638635ab.2.1758658048125; Tue, 23 Sep 2025 13:07:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEmMxIqHNMDHwq6jPuldjwSxK84807XGPUliuEF5qCvF8SuPWmF0LqQLkrDELCWNHqAt6pi1g== X-Received: by 2002:a05:6e02:b27:b0:423:fd07:d3fe with SMTP id e9e14a558f8ab-42581e0924amr21638385ab.2.1758658047638; Tue, 23 Sep 2025 13:07:27 -0700 (PDT) Received: from redhat.com ([38.15.36.11]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-425813f3053sm15141865ab.21.2025.09.23.13.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Sep 2025 13:07:26 -0700 (PDT) Date: Tue, 23 Sep 2025 14:07:23 -0600 From: Alex Williamson To: Leon Romanovsky Cc: Jason Gunthorpe , Andrew Morton , Bjorn Helgaas , Christian =?UTF-8?B?S8O2bmln?= , dri-devel@lists.freedesktop.org, iommu@lists.linux.dev, Jens Axboe , Joerg Roedel , kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Logan Gunthorpe , Marek Szyprowski , Robin Murphy , Sumit Semwal , Vivek Kasireddy , Will Deacon Subject: Re: [PATCH v2 03/10] PCI/P2PDMA: Refactor to separate core P2P functionality from memory allocation Message-ID: <20250923140723.14c63741.alex.williamson@redhat.com> In-Reply-To: <20250923171228.GL10800@unreal> References: <1e2cb89ea76a92949d06a804e3ab97478e7cacbb.1757589589.git.leon@kernel.org> <20250922150032.3e3da410.alex.williamson@redhat.com> <20250923171228.GL10800@unreal> X-Mailer: Claws Mail 4.3.1 (GTK 3.24.43; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 23 Sep 2025 20:12:28 +0300 Leon Romanovsky wrote: > On Mon, Sep 22, 2025 at 03:00:32PM -0600, Alex Williamson wrote: > > On Thu, 11 Sep 2025 14:33:07 +0300 > > Leon Romanovsky wrote: > > > > > From: Leon Romanovsky > > > > > > Refactor the PCI P2PDMA subsystem to separate the core peer-to-peer DMA > > > functionality from the optional memory allocation layer. This creates a > > > two-tier architecture: > > > > > > The core layer provides P2P mapping functionality for physical addresses > > > based on PCI device MMIO BARs and integrates with the DMA API for > > > mapping operations. This layer is required for all P2PDMA users. > > > > > > The optional upper layer provides memory allocation capabilities > > > including gen_pool allocator, struct page support, and sysfs interface > > > for user space access. > > > > > > This separation allows subsystems like VFIO to use only the core P2P > > > mapping functionality without the overhead of memory allocation features > > > they don't need. The core functionality is now available through the > > > new pci_p2pdma_enable() function that returns a p2pdma_provider > > > structure. > > > > > > Signed-off-by: Leon Romanovsky > > > --- > > > drivers/pci/p2pdma.c | 129 +++++++++++++++++++++++++++---------- > > > include/linux/pci-p2pdma.h | 5 ++ > > > 2 files changed, 100 insertions(+), 34 deletions(-) > > <...> > > > > -static int pci_p2pdma_setup(struct pci_dev *pdev) > > > +/** > > > + * pcim_p2pdma_enable - Enable peer-to-peer DMA support for a PCI device > > > + * @pdev: The PCI device to enable P2PDMA for > > > + * @bar: BAR index to get provider > > > + * > > > + * This function initializes the peer-to-peer DMA infrastructure for a PCI > > > + * device. It allocates and sets up the necessary data structures to support > > > + * P2PDMA operations, including mapping type tracking. > > > + */ > > > +struct p2pdma_provider *pcim_p2pdma_enable(struct pci_dev *pdev, int bar) > > > { > > > - int error = -ENOMEM; > > > struct pci_p2pdma *p2p; > > > + int i, ret; > > > + > > > + p2p = rcu_dereference_protected(pdev->p2pdma, 1); > > > + if (p2p) > > > + /* PCI device was "rebound" to the driver */ > > > + return &p2p->mem[bar]; > > > > > > > This seems like two separate functions rolled into one, an 'initialize > > providers' and a 'get provider for BAR'. The comment above even makes > > it sound like only a driver re-probing a device would encounter this > > branch, but the use case later in vfio-pci shows it to be the common > > case to iterate BARs for a device. > > > > But then later in patch 8/ and again in 10/ why exactly do we cache > > the provider on the vfio_pci_core_device rather than ask for it on > > demand from the p2pdma? > > In addition to what Jason said about locking. The whole p2pdma.c is > written with assumption that "pdev->p2pdma" pointer is assigned only > once during PCI device lifetime. For example, see how sysfs files > are exposed and accessed in p2pdma.c. Except as Jason identifies in the other thread, the p2pdma is a devm object, so it's assigned once during the lifetime of the driver, not the device. It seems that to get the sysfs attributes exposed, a driver would need to call pci_p2pdma_add_resource() to setup a pool, but that pool setup is only done if pci_p2pdma_add_resource() itself calls pcim_p2pdma_enable(): p2pdma = rcu_dereference_protected(pdev->p2pdma, 1); if (!p2pdma) { mem = pcim_p2pdma_enable(pdev, bar); if (IS_ERR(mem)) return PTR_ERR(mem); error = pci_p2pdma_setup_pool(pdev); ... } else mem = &p2pdma->mem[bar]; Therefore as proposed here a device bound to vfio-pci would never have these sysfs attributes. > Once you initialize p2pdma, it is much easier to initialize all BARs at > the same time. I didn't phrase my question above well. We can setup all the providers on the p2pdma at once, that's fine. My comment is related to the awkward API we're creating and what seems to be gratuitously caching the providers on the vfio_pci_core_device when it seems much more logical to get the provider for a specific dmabuf and cache it on the vfio_pci_dma_buf object in the device feature ioctl. We could also validate the provider at that point rather than the ad-hoc, parallel checks for MMIO BARs. Thanks, Alex