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.129.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 7B35F30594E for ; Wed, 10 Dec 2025 20:47:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765399662; cv=none; b=YIO5PRl/r6NwXrqvulv90KHlwizkD2P8hREEzJD4LMElp3Sptzpin+SNEjTISYv+XEK4C+R2Waob6KVgpg2bk1APzLz+idhFqIEezgppU/scy/abP3jjBojpLIN1xOWkhm2GBLzYIFm+0sYo945bLru3JvU32xdPVf4+4Tp8Ies= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765399662; c=relaxed/simple; bh=erN3LFsrewv9M2WhBDnOEPbfNdtL2RO7z/u4FW6ywvc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u6mdJHquwbu9F/04ZjvsZJ9/n6T6GlehOnlACT/7+e4uPSRAKF58oT69ldHutPn8NJ4PXY/vzQkkqHKI3ZwlIT6Et77Ujq14j21gk/+ej6Rv9+ZXBLzYzNcyxXsx+17wVaG8XRODS0kfipIFzBDWCvZXfNNQrrba4YMJMNREeKU= 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=e7vO1dHm; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=Zhr1JmDt; arc=none smtp.client-ip=170.10.129.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="e7vO1dHm"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zhr1JmDt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765399659; 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: in-reply-to:in-reply-to:references:references; bh=MdaxxiCV8H2eHhp9NcagGoogiR+1MMFcEmWlgLwt4lY=; b=e7vO1dHmt2AvhFEnSWxNyOPPEhEGq79V0cf10wQMoGCaD9q8WABl5IG83Exnoth4+SG7Ge v2c4NO8fye8BfzRi/zC60C0H8qSTvA3TRIHio6R3m3BuGatJsjH+2v7CMlLOoEJdPUXj1I nHv0hH7aDtQOozXXCZ7/tIQPnox6JmQ= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-595-i-45vccmOh2ZgpkvbprhLA-1; Wed, 10 Dec 2025 15:43:46 -0500 X-MC-Unique: i-45vccmOh2ZgpkvbprhLA-1 X-Mimecast-MFC-AGG-ID: i-45vccmOh2ZgpkvbprhLA_1765399426 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-8804823b757so29357566d6.0 for ; Wed, 10 Dec 2025 12:43:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1765399426; x=1766004226; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MdaxxiCV8H2eHhp9NcagGoogiR+1MMFcEmWlgLwt4lY=; b=Zhr1JmDtXXxa1wzkd9qt+4YquyhKEy79vPyO5KLiOS8c29a8IxNAYYaGgCvM65w/LB ukRBv+ACK/nfJq4CgB4qYXfe353Irasw8qyU4uY8k+Tqa4hMd269DnHgh0bDx4JLx6YA LXEEpKMkpPN8KSyPRCkOA4tjb+hiI8les8Ac67lZeJhtoVbr/9REzg4UJa0WTYFfgX8g kjArwSNcOMU9SGlK8AOlaD0HUBx2AkXeGaUmOU5NLa7Pudg7BBte61OFWW/u6cTCHy36 M493DrzSaVcBb4tM21fWR/tihGwsJlXgLUFW2n6YVACud2XWHsBfZiAAmTtMuO9UOUXG rGKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765399426; x=1766004226; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MdaxxiCV8H2eHhp9NcagGoogiR+1MMFcEmWlgLwt4lY=; b=or65ftjLLCC9oBKh8kgtljX9PvSdsINI3QF0/arXxV0LIBqREOixf3gnT2xN9y+Ftw EiNhdeA4XLhxQYd6DB/v1qm+TiXfUaue2bLG4ok2C5cmsWyMxvCv/6gNyx90QVc7S/LW NeDNnDMiUhpk1zwBIawlGeRIdbP0MyxFA44O4ON6FWmhfyQl92df56FFBSWTly8rUNwT WLwUNweD6GK9G0KGms3EFVns0zrSTaifcXD/cldKDor23tDxr/357mXW4pVDuU8usYZa qRW5hmnAznoMlPFCyEsxMuV8KX/pETDpe9cDcpOwuMd0cxeC5JQfk9U3ZN3wEP9skPwY O3cQ== X-Forwarded-Encrypted: i=1; AJvYcCWfXNzADhzWiL9U206uD55xqpuR4hJ3QaejHTDN1RHNwyYaO97nT7drdzuBiJf4Y/4Sfhn8410itJQA1Bc=@vger.kernel.org X-Gm-Message-State: AOJu0YzG1LBADhbIVwLBmFHETSJIzp5S2xo8kYg5U18C/SSh3+uyLPEC tsULs4nfLcV3FJiO3GL1aNf4fQSzYO7fPqhTKf00F0iC6+UFcKz9XjWsPph+7GDUdTLh9cUfYxz MDhY4KZFixCveY34z37fqBk8AL2JfvJwa++kq1g+YjRcorcqOiOXOw20XE5JoGSCsDg== X-Gm-Gg: AY/fxX51ce45KwS8uq/CuPHZ2zjZOjCKb5C6WHBwzDnpLXvrOzZ7BLQRPQFzwDqQPKh 9GG5yOVybzgVx5H/6IylSbB76abYXArXgND1hfB23uJykpVEnb+jWDqsUwNqK2tR+ZMfXiGtm3X LJ3XPBVyiYaxojafigOS/9KlOTFFkSrgr5jtWjflJDRiIGVc/UhM9c/FSiKQdVLiXd/GCF9YF7K 5VljvCrf2Bt0bUR4sU+Tdu0fqwr3dPvDuZjVJfy+0MvcAv14nkgAC8prffihmmCu2gY6yNagpc9 mYYWcshM2cyh8NzNdnaDWp4VouWuUD9SuLuW2yjAibVMe7ZQPL9a1Sq9E8kAGU8rFFRYTgEnerG skvk= X-Received: by 2002:a05:6214:21e1:b0:880:51f0:5ba0 with SMTP id 6a1803df08f44-8886f2c85efmr12874316d6.26.1765399425686; Wed, 10 Dec 2025 12:43:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IEMlXnfPx1nwXlxZMeogmX6e8U0uMByKwt0vnr5JltJxNMWxHqIgAvaalVCu7oIuo0OaauRzQ== X-Received: by 2002:a05:6214:21e1:b0:880:51f0:5ba0 with SMTP id 6a1803df08f44-8886f2c85efmr12873866d6.26.1765399425121; Wed, 10 Dec 2025 12:43:45 -0800 (PST) Received: from x1.local ([142.188.210.156]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8886ef30441sm6054106d6.56.2025.12.10.12.43.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Dec 2025 12:43:44 -0800 (PST) Date: Wed, 10 Dec 2025 15:43:43 -0500 From: Peter Xu To: Jason Gunthorpe Cc: kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Nico Pache , Zi Yan , Alex Mastro , David Hildenbrand , Alex Williamson , Zhi Wang , David Laight , Yi Liu , Ankit Agrawal , Kevin Tian , Andrew Morton Subject: Re: [PATCH v2 4/4] vfio-pci: Best-effort huge pfnmaps with !MAP_FIXED mappings Message-ID: References: <20251204151003.171039-1-peterx@redhat.com> <20251204151003.171039-5-peterx@redhat.com> 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=utf-8 Content-Disposition: inline In-Reply-To: On Sun, Dec 07, 2025 at 12:26:37PM -0400, Jason Gunthorpe wrote: > On Thu, Dec 04, 2025 at 10:10:03AM -0500, Peter Xu wrote: > > > +/* > > + * Hint function for mmap() about the size of mapping to be carried out. > > + * This helps to enable huge pfnmaps as much as possible on BAR mappings. > > + * > > + * This function does the minimum check on mmap() parameters to make the > > + * hint valid only. The majority of mmap() sanity check will be done later > > + * in mmap(). > > + */ > > +int vfio_pci_core_get_mapping_order(struct vfio_device *device, > > + unsigned long pgoff, size_t len) > > +{ > > + struct vfio_pci_core_device *vdev = > > + container_of(device, struct vfio_pci_core_device, vdev); > > + struct pci_dev *pdev = vdev->pdev; > > + unsigned int index = pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); > > + unsigned long req_start; > > + size_t phys_len; > > + > > + /* Currently, only bars 0-5 supports huge pfnmap */ > > + if (index >= VFIO_PCI_ROM_REGION_INDEX) > > + return 0; > > + > > + /* > > + * NOTE: we're keeping things simple as of now, assuming the > > + * physical address of BARs (aka, pci_resource_start(pdev, index)) > > + * should always be aligned with pgoff in vfio-pci's address space. > > + */ > > + req_start = (pgoff << PAGE_SHIFT) & ((1UL << VFIO_PCI_OFFSET_SHIFT) - 1); > > + phys_len = PAGE_ALIGN(pci_resource_len(pdev, index)); > > + > > + /* > > + * If this happens, it will probably fail mmap() later.. mapping > > + * hint isn't important anymore. > > + */ > > + if (req_start >= phys_len) > > + return 0; > > + > > + phys_len = MIN(phys_len - req_start, len); > > + > > + if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) && phys_len >= PUD_SIZE) > > + return PUD_ORDER; > > + > > + if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP) && phys_len >= PMD_SIZE) > > + return PMD_ORDER; > > + > > This seems a bit weird, the vma length is already known, it is len, > why do we go to all this trouble to recalculate len in terms of phys? > > If the length is wrong the mmap will fail, so there is no issue with > returning a larger order here. > > I feel this should just return the order based on pci_resource_len()? IIUC there's a trivial difference when partial of a huge bar is mapped. Example: 1G bar, map range (pgoff=2M, size=1G-2M). If we return bar size order, we'd say 1G, however then it means we'll do the alignment with 1G. __thp_get_unmapped_area() will think it's not proper, because: loff_t off_end = off + len; loff_t off_align = round_up(off, size); if (off_end <= off_align || (off_end - off_align) < size) return 0; Here what we really want is to map (2M, 1G-2M) with 2M huge, not 1G, nor 4K. > > And shouldn't the mm be the one aligning it to what the arch can do > not drives? Note that here checking CONFIG_ARCH_SUPPORTS_P*D_PFNMAP is a vfio behavior, pairing with the huge_fault() of vfio-pci driver. It implies if vfio-pci's huge pfnmap is enabled or not. If it's not enabled, we don't need to report larger orders here. Said that, this is still a valid point, that core mm should likely also check against the configs when the kernel was built, though it should not check against CONFIG_ARCH_SUPPORTS_PMD_PFNMAP.. Instead, it should check HAVE_ARCH_TRANSPARENT_HUGEPAGE*. But then... I really want to avoid adding more dependencies to THPs in core mm on pfnmaps. I used to decouple THP and huge mappings, that series wasn't going anywhere, but adding these checks will add more dependencies.. Shall I keep it simple to leave it to drivers, until we have something more solid (I think we need HAVE_ARCH_HUGE_P*D_LEAVES here)? Even with that config ready, drivers should always still do proper check on its own (drivers need to support huge pfnmaps here first before reporting high orders). So what I can add into core mm to check arch support would only be an extra layer of safety net, not much real help but burn some cpu cycles, IMHO... Thanks, -- Peter Xu