From nobody Sat Oct 11 04:18:08 2025 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 0522027AC57 for ; Wed, 11 Jun 2025 12:07:07 +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=1749643629; cv=none; b=Aq9b1b5QdJlgOhg0Lc86DFKhl+PBXjcpq5ptgSaKDkPjo0NKbvbflsjMvs1+rVjXd8rFBtmi4WBuZ/lTPT1ZxlIeKdwW4cQdcFRLeWt65wYAid6YfRO7hzGEKhsD3PFY1f3QTMHWKHNDLs596gUjy4D3aRWfJpltEiqR1PAd770= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749643629; c=relaxed/simple; bh=zKTs78RTRTqS5HP35DRDL4Y0b0mNW3Gu1tDXt0akX1A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eOjURrN5DC5NL9Di1MvBo9qkJlx12nEycC59TvgcA+Zj6xF02/xw3maSmbrWiwbcsUliNEK3m+3dxH4ERpuzl+j40FRCagjYGj+QxDSkeCXR4BT5cjSuIuxg2uy6ORDjaUh2Z6j9CJ1UCNyt3u7Pc+9QkhXGwi6yVCZe4iYNpSg= 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=RDKRTyUc; 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="RDKRTyUc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749643626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+PENtkLUfBH1Z8n7k4h6Ysph6sI1iUW3ZV5Hjz4OJ3o=; b=RDKRTyUcWuloDj/R+zaF3acRBOYjmTDutco0YMI7zpXpzyixGozhqlY2bVmTHcT+7CwznU XNh+kXglWO6K9JOiIbqeMpHHYmEJaSFHfrZ0CF9BxHqIPHeDALaLN7yhOoo0RSXtjw2y8x rA1+oab/kGd4mUEXldN4lZlEHfRUKcw= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-QZIuqbeQPdK5cVLmij6tkA-1; Wed, 11 Jun 2025 08:07:05 -0400 X-MC-Unique: QZIuqbeQPdK5cVLmij6tkA-1 X-Mimecast-MFC-AGG-ID: QZIuqbeQPdK5cVLmij6tkA_1749643625 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7d399070cecso471921985a.3 for ; Wed, 11 Jun 2025 05:07:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749643625; x=1750248425; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+PENtkLUfBH1Z8n7k4h6Ysph6sI1iUW3ZV5Hjz4OJ3o=; b=olIm6j9xWV9tytTbMdHB53bZt+XeW7MZ77U8BbWaHxjK0WQ8AkqclKSx+5uunpSRYw 26t1nz9Nogog95Pzd0QqLuRwdGBHBHL3030x+B6kPa7d7DBV5+jmVaeRL/SpLkRDEhxY DoNWtCnEDNx4scI54BN3LDTqcDU3iG0xBUljxzYq07dDkQscXO0nkhZg+ngASNSP8+Fm KP1YeKQStN6Z3BqZ7ZiPIQ+mGzBLCPsVYAyqTbwbRhxGBCFF2B81FtmUasSemcjg+eO2 yXC1kJi/gDHnFX9t5IITQJPNcimJITqjqoUyIa01ZHHkFgihg8ATyY5hfQ+iel9flnN0 lV/A== X-Gm-Message-State: AOJu0YxaYj+4OjDti8Wu35YLBs6OfG4M9qomYVvOWamt5cHLi2Vn27ze kwwIDV2eQQsZHPJACHf3mYc7QvQ1akO7YKce/sqmBKQZZTJyQ64BTMtrBDK478IxGZ2GOYtI3Nv bzzH8Vm6sJEnC4N3rp8VLS8ZZ/bsSN1iMSnT7kTJClDEUT2H2hDlFbPKbvx1iSxEfmJqRvT+3QL 8clPH0TJrHYMGcCG/i78G5VNS8/YrMlDzNSfr5qJcPHJs/2t/f X-Gm-Gg: ASbGncuQ1jTo6twCbn+QSpjdtuM3jdNW/5imXJ3N7dFjoRmbZMobVdZL8aPEJlyXB9z hOlghPRFn0AbwpARHBCv2BoJI1YWTNaizQhGEnS+gHHzRod5+A1/3kxTEdt6bcd/L6YmQP06pQZ gVDZ+asdIlNXs6LbJVo/WBFKIHzN9h3kUlur3Hx/XtByyrNOXoEQm1Q9UcviB0GrUZKZxScxFw8 4/+++IWMFhoT/01RKmAG0IdVlGRu8oJ9EP660K1CkQbl8mejg6RBB4gueAUEvqgSjO0Px3wa6vQ V4kY6dY7iYEN38ti9FIPAPrOz0djTUq6ORy9rnDTCA== X-Received: by 2002:a05:620a:3194:b0:7d0:9ffd:422f with SMTP id af79cd13be357-7d3a89b1b3bmr505264985a.54.1749643625176; Wed, 11 Jun 2025 05:07:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJLjpZ1uY3+YpiReTBDNjkNaFYo7+UKPSqXzU9BVywxK8ty0bHeAi1vwl8x6v5Oh4c1OLQSA== X-Received: by 2002:a05:620a:3194:b0:7d0:9ffd:422f with SMTP id af79cd13be357-7d3a89b1b3bmr505257485a.54.1749643624636; Wed, 11 Jun 2025 05:07:04 -0700 (PDT) Received: from localhost (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with UTF8SMTPSA id af79cd13be357-7d25a60c183sm847676485a.58.2025.06.11.05.07.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Jun 2025 05:07:04 -0700 (PDT) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, David Hildenbrand , Andrew Morton , Alistair Popple , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Dan Williams , Oscar Salvador , stable@vger.kernel.org Subject: [PATCH v2 1/3] mm/huge_memory: don't ignore queried cachemode in vmf_insert_pfn_pud() Date: Wed, 11 Jun 2025 14:06:52 +0200 Message-ID: <20250611120654.545963-2-david@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250611120654.545963-1-david@redhat.com> References: <20250611120654.545963-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We setup the cache mode but ... don't forward the updated pgprot to insert_pfn_pud(). Only a problem on x86-64 PAT when mapping PFNs using PUDs that require a special cachemode. Fix it by using the proper pgprot where the cachemode was setup. Identified by code inspection. Fixes: 7b806d229ef1 ("mm: remove vmf_insert_pfn_xxx_prot() for huge page-ta= ble entries") Cc: Signed-off-by: David Hildenbrand Reviewed-by: Dan Williams Reviewed-by: Jason Gunthorpe Reviewed-by: Lorenzo Stoakes Tested-by: Dan Williams --- mm/huge_memory.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d3e66136e41a3..49b98082c5401 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1516,10 +1516,9 @@ static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_= area_struct *vma) } =20 static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr, - pud_t *pud, pfn_t pfn, bool write) + pud_t *pud, pfn_t pfn, pgprot_t prot, bool write) { struct mm_struct *mm =3D vma->vm_mm; - pgprot_t prot =3D vma->vm_page_prot; pud_t entry; =20 if (!pud_none(*pud)) { @@ -1581,7 +1580,7 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, p= fn_t pfn, bool write) pfnmap_setup_cachemode_pfn(pfn_t_to_pfn(pfn), &pgprot); =20 ptl =3D pud_lock(vma->vm_mm, vmf->pud); - insert_pfn_pud(vma, addr, vmf->pud, pfn, write); + insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write); spin_unlock(ptl); =20 return VM_FAULT_NOPAGE; @@ -1625,7 +1624,7 @@ vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf,= struct folio *folio, add_mm_counter(mm, mm_counter_file(folio), HPAGE_PUD_NR); } insert_pfn_pud(vma, addr, vmf->pud, pfn_to_pfn_t(folio_pfn(folio)), - write); + vma->vm_page_prot, write); spin_unlock(ptl); =20 return VM_FAULT_NOPAGE; --=20 2.49.0 From nobody Sat Oct 11 04:18:08 2025 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 8DD1C280CFA for ; Wed, 11 Jun 2025 12:07:14 +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=1749643636; cv=none; b=YjUGnLQF47wPFzUMbGYwCadN+xyNy9dZPOPNRwpTa9xHgLIvmJyBjBrWN9cCJimezrfQRjkOONTv4p0QI2dAgmZUIHZtwsg8MGmdgwOOdVfbHimOhnDSS178bbITGP9n6XycVQbUq2DjCH2UOdj5JGFoO+3a4z1a4W/Wi9+nY3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749643636; c=relaxed/simple; bh=d7UzkQtVY7kuIxcI/EZc5YSadWXv/sCE0nDIA2XsSqk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Gm9if1Lxnz2mxqvxXQlcvFJXwO077pQaYCzS7lxyuk87ghX0PF8B54Py/R3j/4H7z5uHKgMSFblI1XeDx/c50a1B0FXALvGAaUMyqbFCdUzBqQq3g/8qBLfcX8BGgqP+1RMPQXmOBLYFPBENvHKTqxVSLN6gUSH9WbUh+7+zr4Y= 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=MZ/d1vD/; 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="MZ/d1vD/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749643633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gYmY9lqRnj4cw3caoSyKqMYCbf0QdwGbw8D5FFvmOek=; b=MZ/d1vD/JZQCVAXLmnvPpbxO35OwortBeI0CrL3eTOcF1B336plHEQ7atpZzw7gqVz7cNt Ki0yzVzzhX+8yVUrvVKRNha/V86Mxd/5+SXniOm3+/XvSGp9l1Q9hI4yzagqs7/py2Jy1L xQ88tfgV+Q0LOJcVq6ygq2N/ikcpiL0= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-385-3ixmKHoINIOUKv_ImWwKFw-1; Wed, 11 Jun 2025 08:07:10 -0400 X-MC-Unique: 3ixmKHoINIOUKv_ImWwKFw-1 X-Mimecast-MFC-AGG-ID: 3ixmKHoINIOUKv_ImWwKFw_1749643630 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-478f78ff9beso209855191cf.1 for ; Wed, 11 Jun 2025 05:07:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749643630; x=1750248430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gYmY9lqRnj4cw3caoSyKqMYCbf0QdwGbw8D5FFvmOek=; b=TN5HWmpn+i/poUA0Digk3YeVXfZHtYr4zN5xH3uWrUVumxXqLLf3FJ9sGRGIH7IXKZ u13bPtcXXT21U7if9MOTV3EAsfQx9S9Et4aqdpapS0cO2hm+RXqH6yoJxfXAqvo5juHf 7FtK4iwuXhMh0fK/Bro3vbnbllXKsRzxwQsYNOqgQ/D76A4w64eb2OjuL5BeqEqUtRCz 9qIhmNLZ8inYesMlI8ufBQXLulDfqnWjs9Hu6Hw56C58zfzI89KVYIeYeeNb5hfoYWnT bxxBAH6bLTFQmVeKYFQ0CqGedbS669XXgsbevgNGPBXvQOx8+hkSZ1AE4S9Q1F1VDll+ qpgg== X-Gm-Message-State: AOJu0Yxo/muSitnHRxhh0ZFOZMMyVXbqRi3YSqwgrcCRm+kCWQFwhULK qUlzPsmhtF1JyDK+Zq85YtQ7NP3g3082Ma1T12yt6mJhpLC1X4GaaU+BsYPkA6ahpLwtMJejhww H+YeDyo70CnBXUHs1R/14QUsUajoj//vrPUXOkusMeMnEbmq3EcCqg2DWaSSIbeBF6Xz80q+X56 Ur2Pn5t/bk86djOqqJi+9SQXiS8obuQqHCAajlV3z9wFGAGxId X-Gm-Gg: ASbGncuq8WBuZGaLLCfz+KC4+TjcJNxhVWZtzS5mqpkcLeMAYx8f5qHl1AoA1hsRwEK VgstZsx50oPv52Sa5rOj1rtNn4VKZsY3ZpvvMgeCN0Ift6PdtT99/mcd2dOm/3KFrsGnUvZ9e/f b8xU1osVbk/stS8AvuBimAq+Oe202oKq9IowvdUdKId3LQPrfSNnF8EVyDjPBKdfVkClZjd0xu1 idriDCmY6e8oypbjtDK9LE6VLLz0SccK2PVzBm8stqI8CqsimYrTY6YFdToeJmtDldWq5DYyGOO UVaDdR4YcqFViiyMHT99F24RA1LGJUUjTC8HXw5BIw== X-Received: by 2002:a05:622a:258c:b0:499:23e5:a39e with SMTP id d75a77b69052e-4a714b5f095mr41023141cf.18.1749643629449; Wed, 11 Jun 2025 05:07:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFkRu3pD31QY49jF7czwiN04VZspGiT3brE6ESvHTJ+EUAthmLASS1EV2SOMHG5iqhG7+Byg== X-Received: by 2002:a05:622a:258c:b0:499:23e5:a39e with SMTP id d75a77b69052e-4a714b5f095mr41022301cf.18.1749643628891; Wed, 11 Jun 2025 05:07:08 -0700 (PDT) Received: from localhost (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with UTF8SMTPSA id d75a77b69052e-4a619865e74sm87713091cf.61.2025.06.11.05.07.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Jun 2025 05:07:08 -0700 (PDT) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, David Hildenbrand , Andrew Morton , Alistair Popple , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Dan Williams , Oscar Salvador Subject: [PATCH v2 2/3] mm/huge_memory: don't mark refcounted folios special in vmf_insert_folio_pmd() Date: Wed, 11 Jun 2025 14:06:53 +0200 Message-ID: <20250611120654.545963-3-david@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250611120654.545963-1-david@redhat.com> References: <20250611120654.545963-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Marking PMDs that map a "normal" refcounted folios as special is against our rules documented for vm_normal_page(). Fortunately, there are not that many pmd_special() check that can be mislead, and most vm_normal_page_pmd()/vm_normal_folio_pmd() users that would get this wrong right now are rather harmless: e.g., none so far bases decisions whether to grab a folio reference on that decision. Well, and GUP-fast will fallback to GUP-slow. All in all, so far no big implications as it seems. Getting this right will get more important as we use folio_normal_page_pmd() in more places. Fix it by teaching insert_pfn_pmd() to properly handle folios and pfns -- moving refcount/mapcount/etc handling in there, renaming it to insert_pmd(), and distinguishing between both cases using a new simple "struct folio_or_pfn" structure. Use folio_mk_pmd() to create a pmd for a folio cleanly. Fixes: 6c88f72691f8 ("mm/huge_memory: add vmf_insert_folio_pmd()") Signed-off-by: David Hildenbrand Reviewed-by: Dan Williams Reviewed-by: Jason Gunthorpe Reviewed-by: Lorenzo Stoakes Tested-by: Dan Williams --- mm/huge_memory.c | 58 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 49b98082c5401..7e3e9028873e5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1372,9 +1372,17 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_faul= t *vmf) return __do_huge_pmd_anonymous_page(vmf); } =20 -static int insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr, - pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write, - pgtable_t pgtable) +struct folio_or_pfn { + union { + struct folio *folio; + pfn_t pfn; + }; + bool is_folio; +}; + +static int insert_pmd(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmd, struct folio_or_pfn fop, pgprot_t prot, + bool write, pgtable_t pgtable) { struct mm_struct *mm =3D vma->vm_mm; pmd_t entry; @@ -1382,8 +1390,11 @@ static int insert_pfn_pmd(struct vm_area_struct *vma= , unsigned long addr, lockdep_assert_held(pmd_lockptr(mm, pmd)); =20 if (!pmd_none(*pmd)) { + const unsigned long pfn =3D fop.is_folio ? folio_pfn(fop.folio) : + pfn_t_to_pfn(fop.pfn); + if (write) { - if (pmd_pfn(*pmd) !=3D pfn_t_to_pfn(pfn)) { + if (pmd_pfn(*pmd) !=3D pfn) { WARN_ON_ONCE(!is_huge_zero_pmd(*pmd)); return -EEXIST; } @@ -1396,11 +1407,19 @@ static int insert_pfn_pmd(struct vm_area_struct *vm= a, unsigned long addr, return -EEXIST; } =20 - entry =3D pmd_mkhuge(pfn_t_pmd(pfn, prot)); - if (pfn_t_devmap(pfn)) - entry =3D pmd_mkdevmap(entry); - else - entry =3D pmd_mkspecial(entry); + if (fop.is_folio) { + entry =3D folio_mk_pmd(fop.folio, vma->vm_page_prot); + + folio_get(fop.folio); + folio_add_file_rmap_pmd(fop.folio, &fop.folio->page, vma); + add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PMD_NR); + } else { + entry =3D pmd_mkhuge(pfn_t_pmd(fop.pfn, prot)); + if (pfn_t_devmap(fop.pfn)) + entry =3D pmd_mkdevmap(entry); + else + entry =3D pmd_mkspecial(entry); + } if (write) { entry =3D pmd_mkyoung(pmd_mkdirty(entry)); entry =3D maybe_pmd_mkwrite(entry, vma); @@ -1431,6 +1450,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, p= fn_t pfn, bool write) unsigned long addr =3D vmf->address & PMD_MASK; struct vm_area_struct *vma =3D vmf->vma; pgprot_t pgprot =3D vma->vm_page_prot; + struct folio_or_pfn fop =3D { + .pfn =3D pfn, + }; pgtable_t pgtable =3D NULL; spinlock_t *ptl; int error; @@ -1458,8 +1480,8 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, p= fn_t pfn, bool write) pfnmap_setup_cachemode_pfn(pfn_t_to_pfn(pfn), &pgprot); =20 ptl =3D pmd_lock(vma->vm_mm, vmf->pmd); - error =3D insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, - pgtable); + error =3D insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write, + pgtable); spin_unlock(ptl); if (error && pgtable) pte_free(vma->vm_mm, pgtable); @@ -1474,6 +1496,10 @@ vm_fault_t vmf_insert_folio_pmd(struct vm_fault *vmf= , struct folio *folio, struct vm_area_struct *vma =3D vmf->vma; unsigned long addr =3D vmf->address & PMD_MASK; struct mm_struct *mm =3D vma->vm_mm; + struct folio_or_pfn fop =3D { + .folio =3D folio, + .is_folio =3D true, + }; spinlock_t *ptl; pgtable_t pgtable =3D NULL; int error; @@ -1491,14 +1517,8 @@ vm_fault_t vmf_insert_folio_pmd(struct vm_fault *vmf= , struct folio *folio, } =20 ptl =3D pmd_lock(mm, vmf->pmd); - if (pmd_none(*vmf->pmd)) { - folio_get(folio); - folio_add_file_rmap_pmd(folio, &folio->page, vma); - add_mm_counter(mm, mm_counter_file(folio), HPAGE_PMD_NR); - } - error =3D insert_pfn_pmd(vma, addr, vmf->pmd, - pfn_to_pfn_t(folio_pfn(folio)), vma->vm_page_prot, - write, pgtable); + error =3D insert_pmd(vma, addr, vmf->pmd, fop, vma->vm_page_prot, + write, pgtable); spin_unlock(ptl); if (error && pgtable) pte_free(mm, pgtable); --=20 2.49.0 From nobody Sat Oct 11 04:18:08 2025 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 F25FA283128 for ; Wed, 11 Jun 2025 12:07:16 +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=1749643638; cv=none; b=SqLzd4+nmU0YzZZ6hZn2+TEhzyayz2ZiU8hicXTrcQycnwoU2JWvaObQcbNmgWbKBaWfgrSDGi2gzT8JzIgfTyngR+p3hKdIiOR4Xc4Z8+dJPM5XlPVtwmXuK+xK2lUDYt5aMtsCfDHVcZkzmPybBnmiCGe4ekJ881Bl5R/h9h4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749643638; c=relaxed/simple; bh=lDuMcIkqdVYfT9oTqnsRg9TwWuYN2/pKXvzQU1WFu8o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ighRdYQ7Pcgj0KWyV5Y5Mehl3EssE61dOTCUAXGV1S+PTW7ioY33FqMWX22PUIVFsQ9bT1tlzjT+wB+LKEcYc9UjtvXQ1KM80p75oGh0OgB/c9/p82mhF2NGQYEqJZET9Ox71TH/ltFz7PePFcO4Otsqd5duYsfI6h18twcQY1c= 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=MCXhMtLk; 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="MCXhMtLk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749643635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UtWlysWlQBjRkoN9+CJXCvGFy/ErJq/rTFp6EVa14FI=; b=MCXhMtLkwHeH5IeKJ7IKG0FCyK8Cc2qjTOiFRyC9B8lce9TvNoD4K1o2t0ZfyiNFx87m9T D46N1U207wg449tILjFL+ta+TnutRoRKg5B0II4Ifxpwh5e4dP/MDsMdu7o0yDosGFmpy8 PzgkjQDGP9vHJinWTv4sudr4xXx+Ka0= 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-75-9E0MLvhJNf-FSJ0Nurlm6g-1; Wed, 11 Jun 2025 08:07:14 -0400 X-MC-Unique: 9E0MLvhJNf-FSJ0Nurlm6g-1 X-Mimecast-MFC-AGG-ID: 9E0MLvhJNf-FSJ0Nurlm6g_1749643634 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6face1d593bso128674046d6.3 for ; Wed, 11 Jun 2025 05:07:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749643634; x=1750248434; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UtWlysWlQBjRkoN9+CJXCvGFy/ErJq/rTFp6EVa14FI=; b=WjAAtCPvT27MJ0rllmizew3iQ5sADFs9b6bd8JRMLgtehCexjCRMCkRn2AbPR70dtO 1lrU9qcgai76JD52volvkkHi1O/wyseaOQ8FB9yMTmDyysCV9ysvn1jAoRUgu6ObF18E e/751UoHcEbJDKvMla4DVrbzyMvH7gSIPtKoxqcLJ2Aq0bQVK+JopdJg1AOE/VT2M1+y s1y7/bSllNmi/A/BvWriVL46Iu1oQHuKprR2cJz2spqLUaAn4UEFJUQBeDV5x1CTqCBc 59xM4riPQOfvlk+y+WZ2tOKeOpKFs/ULppg8R1XgFhvpeiwvuXWeLYmY0kMGmohwbS5R lvgg== X-Gm-Message-State: AOJu0YyT9Kr1uLX5awwe2qIk9xezx36onDdaUgO2Y2g5D4Ro3LCkP0S2 0giH+pBzbavVW/Ol/cHMqrcuoFLLqtg16AjhS6sZ/UbsqExNi+HSwDfeejbR1+zWIMYMrxFmR/+ RCFcj34igLuRKYKk7dm4HQ952/ZsA++XzZVWGTeF6X7wb3OUxJKjqhnHoYUL2OW+U9HJJJjrPBc Vhzz3dwMuzKdCSenJ3fqXShhO5AeAhyEL7IYixwqmB/txAyZx/ X-Gm-Gg: ASbGncuhQDCm2behXwrOZMaROgxRkXra7Bm8tYlj2hTeOWnxzcXIfejfpyt5fWA/S04 9/Ok8CKLq58mKNB6fioHJgFZKpcKELWZJAc2r+4Wkq8/OI5ICHpcYdN10xyRN9pcnVWwFNqf4cX OgB8HZ2agemBD+oYed7sKhdUHo5oS5nDnzTfNG00SYqsOKbU5b3Q/O8gi9q9bxNiSu89aEAeaTw 5F2ItozDwXF9iR0g1UcWwg5a5+yQa6K7Ypn/rvIXL0/Bj7TO/ECsKybie03VESGVjwnfMkqGapR FxPoL8hhk5cJw0t3KSvTe14J1GQXv8JCvJXI6T4zrQ== X-Received: by 2002:a05:6214:2247:b0:6fb:eff:853f with SMTP id 6a1803df08f44-6fb2d1355e8mr38204156d6.11.1749643633952; Wed, 11 Jun 2025 05:07:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEM4p2qx6IwsF7ygt/fJLoNiy0HXlnShIRf40WhWevNTjDTOZVqQY8Q1r9EhVLBlQWkw0T8DQ== X-Received: by 2002:a05:6214:2247:b0:6fb:eff:853f with SMTP id 6a1803df08f44-6fb2d1355e8mr38203456d6.11.1749643633381; Wed, 11 Jun 2025 05:07:13 -0700 (PDT) Received: from localhost (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with UTF8SMTPSA id 6a1803df08f44-6fb09b2a1b6sm80566356d6.100.2025.06.11.05.07.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Jun 2025 05:07:12 -0700 (PDT) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, David Hildenbrand , Andrew Morton , Alistair Popple , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Dan Williams , Oscar Salvador Subject: [PATCH v2 3/3] mm/huge_memory: don't mark refcounted folios special in vmf_insert_folio_pud() Date: Wed, 11 Jun 2025 14:06:54 +0200 Message-ID: <20250611120654.545963-4-david@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250611120654.545963-1-david@redhat.com> References: <20250611120654.545963-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Marking PUDs that map a "normal" refcounted folios as special is against our rules documented for vm_normal_page(). Fortunately, there are not that many pud_special() check that can be mislead and are right now rather harmless: e.g., none so far bases decisions whether to grab a folio reference on that decision. Well, and GUP-fast will fallback to GUP-slow. All in all, so far no big implications as it seems. Getting this right will get more important as we introduce folio_normal_page_pud() and start using it in more place where we currently special-case based on other VMA flags. Fix it just like we fixed vmf_insert_folio_pmd(). Add folio_mk_pud() to mimic what we do with folio_mk_pmd(). Fixes: dbe54153296d ("mm/huge_memory: add vmf_insert_folio_pud()") Signed-off-by: David Hildenbrand Reviewed-by: Dan Williams Reviewed-by: Jason Gunthorpe Reviewed-by: Lorenzo Stoakes Tested-by: Dan Williams --- include/linux/mm.h | 19 ++++++++++++++++- mm/huge_memory.c | 51 +++++++++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fa538feaa8d95..912b6d40a12d6 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1816,7 +1816,24 @@ static inline pmd_t folio_mk_pmd(struct folio *folio= , pgprot_t pgprot) { return pmd_mkhuge(pfn_pmd(folio_pfn(folio), pgprot)); } -#endif + +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +/** + * folio_mk_pud - Create a PUD for this folio + * @folio: The folio to create a PUD for + * @pgprot: The page protection bits to use + * + * Create a page table entry for the first page of this folio. + * This is suitable for passing to set_pud_at(). + * + * Return: A page table entry suitable for mapping this folio. + */ +static inline pud_t folio_mk_pud(struct folio *folio, pgprot_t pgprot) +{ + return pud_mkhuge(pfn_pud(folio_pfn(folio), pgprot)); +} +#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif /* CONFIG_MMU */ =20 static inline bool folio_has_pincount(const struct folio *folio) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 7e3e9028873e5..4734de1dc0ae4 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1535,15 +1535,18 @@ static pud_t maybe_pud_mkwrite(pud_t pud, struct vm= _area_struct *vma) return pud; } =20 -static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr, - pud_t *pud, pfn_t pfn, pgprot_t prot, bool write) +static void insert_pud(struct vm_area_struct *vma, unsigned long addr, + pud_t *pud, struct folio_or_pfn fop, pgprot_t prot, bool write) { struct mm_struct *mm =3D vma->vm_mm; pud_t entry; =20 if (!pud_none(*pud)) { + const unsigned long pfn =3D fop.is_folio ? folio_pfn(fop.folio) : + pfn_t_to_pfn(fop.pfn); + if (write) { - if (WARN_ON_ONCE(pud_pfn(*pud) !=3D pfn_t_to_pfn(pfn))) + if (WARN_ON_ONCE(pud_pfn(*pud) !=3D pfn)) return; entry =3D pud_mkyoung(*pud); entry =3D maybe_pud_mkwrite(pud_mkdirty(entry), vma); @@ -1553,11 +1556,19 @@ static void insert_pfn_pud(struct vm_area_struct *v= ma, unsigned long addr, return; } =20 - entry =3D pud_mkhuge(pfn_t_pud(pfn, prot)); - if (pfn_t_devmap(pfn)) - entry =3D pud_mkdevmap(entry); - else - entry =3D pud_mkspecial(entry); + if (fop.is_folio) { + entry =3D folio_mk_pud(fop.folio, vma->vm_page_prot); + + folio_get(fop.folio); + folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma); + add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR); + } else { + entry =3D pud_mkhuge(pfn_t_pud(fop.pfn, prot)); + if (pfn_t_devmap(fop.pfn)) + entry =3D pud_mkdevmap(entry); + else + entry =3D pud_mkspecial(entry); + } if (write) { entry =3D pud_mkyoung(pud_mkdirty(entry)); entry =3D maybe_pud_mkwrite(entry, vma); @@ -1581,6 +1592,9 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, p= fn_t pfn, bool write) unsigned long addr =3D vmf->address & PUD_MASK; struct vm_area_struct *vma =3D vmf->vma; pgprot_t pgprot =3D vma->vm_page_prot; + struct folio_or_pfn fop =3D { + .pfn =3D pfn, + }; spinlock_t *ptl; =20 /* @@ -1600,7 +1614,7 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, p= fn_t pfn, bool write) pfnmap_setup_cachemode_pfn(pfn_t_to_pfn(pfn), &pgprot); =20 ptl =3D pud_lock(vma->vm_mm, vmf->pud); - insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write); + insert_pud(vma, addr, vmf->pud, fop, pgprot, write); spin_unlock(ptl); =20 return VM_FAULT_NOPAGE; @@ -1622,6 +1636,10 @@ vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf= , struct folio *folio, unsigned long addr =3D vmf->address & PUD_MASK; pud_t *pud =3D vmf->pud; struct mm_struct *mm =3D vma->vm_mm; + struct folio_or_pfn fop =3D { + .folio =3D folio, + .is_folio =3D true, + }; spinlock_t *ptl; =20 if (addr < vma->vm_start || addr >=3D vma->vm_end) @@ -1631,20 +1649,7 @@ vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf= , struct folio *folio, return VM_FAULT_SIGBUS; =20 ptl =3D pud_lock(mm, pud); - - /* - * If there is already an entry present we assume the folio is - * already mapped, hence no need to take another reference. We - * still call insert_pfn_pud() though in case the mapping needs - * upgrading to writeable. - */ - if (pud_none(*vmf->pud)) { - folio_get(folio); - folio_add_file_rmap_pud(folio, &folio->page, vma); - add_mm_counter(mm, mm_counter_file(folio), HPAGE_PUD_NR); - } - insert_pfn_pud(vma, addr, vmf->pud, pfn_to_pfn_t(folio_pfn(folio)), - vma->vm_page_prot, write); + insert_pud(vma, addr, vmf->pud, fop, vma->vm_page_prot, write); spin_unlock(ptl); =20 return VM_FAULT_NOPAGE; --=20 2.49.0