From nobody Sat Feb 7 19:45:11 2026 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 9447B134CC8 for ; Thu, 21 Mar 2024 22:08:10 +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=1711058892; cv=none; b=UvikXCm+90+qIMvu/WaAs7wzNq6P05pTEaaneiS4xSPVYIfzAfaVrbycd7uSGOK2k/ZQYTb5hbXL5HJw/tfwJcZgqygJKjPIjqGkAdIfB6HHgHtFaWBct74H3c/YhNOb5uFwD+aIhjpvfhudyppF03c+U5O/7n2kXQUIOn2BsSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058892; c=relaxed/simple; bh=aHfZvZI3hblnvK7sGliw4vracY8c0yNtRNBxsmY5Mag=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nunDnHjzrEthnco7hrAxIMUAbIB7KuUEa0LmolveAa2B76gclrcVezNAFREq7m7vP5+Xd9qVpzTqCHCSWU+m5010LQ0d08iEbwqNPqUsXT/rvsv60TIaTstBks0s2WEtSu0SqllHzBF+lSDUKYuxKFfHGxh3IQGMtoCYWaTCapI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=WFBqd3Pr; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="WFBqd3Pr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058889; 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=bzTkuuEUZ2f7C9YouF2ORWZwnTLSeJ8Y5AJhy7kkISQ=; b=WFBqd3PrcfzDUqwzRERf+ocDwAn35oYVR+h6HNReRIoJUgEGWqiUxFqmCsBsC371vvCL9t WaygFvC/wWgOjBq1hrrCqAy9gaXi3YKZtRVPwWSwAnmdzXcY6jo6MYP/wI6Rs5FH0iiDau KSWZNkg3FEY6fYef5lOtIsoXGHW9EB4= 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-317-3gX3Z4ycMTaI6_yU27yKoA-1; Thu, 21 Mar 2024 18:08:08 -0400 X-MC-Unique: 3gX3Z4ycMTaI6_yU27yKoA-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-430c9bbe925so2914751cf.0 for ; Thu, 21 Mar 2024 15:08:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058887; x=1711663687; 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=bzTkuuEUZ2f7C9YouF2ORWZwnTLSeJ8Y5AJhy7kkISQ=; b=R32hgGHf7C/2I2LJNP0PDQpYlXNpMUqiAlclog+r8QUHPyN0KFzv5GJmP4lRvsHaZM mC8Fb3azu9dpTGQFXxAZTp2We7ONyz18tHkOy81mlgJMQ7GlGFZmm6EVwskodUvnF31m 6fqBXFttJYeuPSGfKYh0xaBqnOt2FiFhi5rVRCKudRfagxdSZTPoCBrmdyR2OfM1JFzz OsFVul3+6aQX2IpcyPleStpbl/jnjNMl3Gal/qHlo73VnF+pl4XbzqH70PLLnu/Y2MhH WWrXFf5m6htJpppTQqdJ34/NdxChQfTsCGUSZn4l1JDF2pb/S8q158+Ukwm+wCtv7d8b Xw9Q== X-Forwarded-Encrypted: i=1; AJvYcCVlEfwa4sQAtODWKJ7oZQPY6hFrZQ2LRXMdYYjho8fSrl8B9eiNto/yTP8+N+rOaAHFBd8t/i8ISn0cpeyIQYGfgbpcVUbxSznugMaB X-Gm-Message-State: AOJu0YzcAU9LT+A3ujYFyEF2gZYUlJM67NreMd1NYLnpV7AS3lLdEoPb 9VFE3kw/M2UowC9F8kdXAs50dAufFdyuA92oFUJWa2m0w5/bkV10tDD6hTcQCFx9LlspWluM/lD 3QHJ1T1OCn3BRvXZIBeyzTyTyO+iFaJoLqAQlU2Y/mv3YRjI6ViOQ1odrGBlYbA== X-Received: by 2002:a0c:e58b:0:b0:691:87dd:f836 with SMTP id t11-20020a0ce58b000000b0069187ddf836mr428477qvm.5.1711058887529; Thu, 21 Mar 2024 15:08:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbvsPPVNStAWHyU+pKNLEFDZ4j8yXA8+9tn+eCe8z7LjIA9ieYt5evzB7ZRQzGTHD3gHSYEw== X-Received: by 2002:a0c:e58b:0:b0:691:87dd:f836 with SMTP id t11-20020a0ce58b000000b0069187ddf836mr428451qvm.5.1711058887085; Thu, 21 Mar 2024 15:08:07 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:06 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 01/12] mm/Kconfig: CONFIG_PGTABLE_HAS_HUGE_LEAVES Date: Thu, 21 Mar 2024 18:07:51 -0400 Message-ID: <20240321220802.679544-2-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Introduce a config option that will be selected as long as huge leaves are involved in pgtable (thp or hugetlbfs). It would be useful to mark any code with this new config that can process either hugetlb or thp pages in any level that is higher than pte level. Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/Kconfig | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/Kconfig b/mm/Kconfig index b924f4a5a3ef..497cdf4d8ebf 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -850,6 +850,12 @@ config READ_ONLY_THP_FOR_FS =20 endif # TRANSPARENT_HUGEPAGE =20 +# +# The architecture supports pgtable leaves that is larger than PAGE_SIZE +# +config PGTABLE_HAS_HUGE_LEAVES + def_bool TRANSPARENT_HUGEPAGE || HUGETLB_PAGE + # # UP and nommu archs use km based percpu allocator # --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 905C913698D for ; Thu, 21 Mar 2024 22:08:13 +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=1711058895; cv=none; b=DljqkKUShiT5Ml56sREk8+SIKWxD41+17AO4tgI7JSvyvllufOuNvrrQkIfUBGIgB+VeVqN/lFsYZe769g+d+HwzmsVGaWmKrK6n2by+CyXJGW31fdTiCzmH5YE9tS6fr9x/xR8NbsIPFw0PBVl+FJPuUNiEC322JBYh0GMTA3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058895; c=relaxed/simple; bh=+4tZFXGWGkSnTz93a1WxUt5Jp3XdX9cL1+SLoKZJKc0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c80mW/P5WJS3Wpq/F8Ksurlxriy9S9XGWsqSvMxj9YW9Iw2UsSsQROAKOEKo3bE3g/WLgRB2HpVpc3wfo7M2uafRslP0xXvOKZcOHqn77oakzNp0BMEWCpYAQPE5NlUQm/mgWmoxowk0V1QNOe68rkvHpl0Pp1mpD+seL8zGG+8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=KmqDaG3Q; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="KmqDaG3Q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058892; 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=Jz9rMLZ3uXx9gi3+G5qYwalRYpIGEA6fjYlj3iYlq7A=; b=KmqDaG3QJeibo6THK7uh9vMDJPbTPl9oAJVqUC1c0NNTrBsq7k+odxG/an31Kp+ZvKtRsh vYleRoMOOsdESN2dPgVXHUoTBqOwykMr+3gvuL46b6XxQdqCXpqB38aVYaSkCV8uJ0EXHE gkno7CQVHfTPM+0O6DuMhec4KjOuhu0= Received: from mail-vs1-f72.google.com (mail-vs1-f72.google.com [209.85.217.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-70-L_DSMoqsNk2TWtPgRTkENg-1; Thu, 21 Mar 2024 18:08:10 -0400 X-MC-Unique: L_DSMoqsNk2TWtPgRTkENg-1 Received: by mail-vs1-f72.google.com with SMTP id ada2fe7eead31-4769277a5fbso157091137.1 for ; Thu, 21 Mar 2024 15:08:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058889; x=1711663689; 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=Jz9rMLZ3uXx9gi3+G5qYwalRYpIGEA6fjYlj3iYlq7A=; b=hVvUlf0nz12zSS1bjFWic2G+CU0YdJ/9nQcHR1M5oVSa4TeyQKH1izkRRF5JaxJ9he IFwiEwCRWJPH8HiMf2kjiUZQkqrauUb95XsMvxzb1mFSy0irVwEN4X8aPyK73TEP+tOR k9firVsnsg8kUU2+VFWRSd3X/K8tJlJuObEIzskfh9kDQLhshQSUEJvWizadwCmt3skb LQ/VkPOAhKaqlu5Olak/iAaHZOX24ilehSI675C3jsTHc9F9pQfRNsUJuw1ZEFsgTCMN juUkKCZoviEOMTt5T7ZyQvcsInfD3AJE5/ao9sFEi4AynNcSbuYLMk3OTOECxS+sRtnv hcAA== X-Forwarded-Encrypted: i=1; AJvYcCV3PdcQU9pZyeR07MZV3ML6fSm8LzKzUZm6wKkt0YWEnBHrNZxTwlw9insC7qgms2YnUFqJz3Nd+c6yn/XQ07nvmW1y5pPZBnNaVcfF X-Gm-Message-State: AOJu0Ywnfw9/ADsNCPvfghDGkjNBJbmEUw/Np45iCO/C/dT47fAPTy68 HGcWiVHSeVus/QhCxMPW7qtx/GVg352E4TwP6DnN/QBV4RYYXkluVaWNlJsbz/LuvIf++6f9hPN wBRUtnwiG9WtGEfLQBU4ISdQXkp3dJXTyz2N7xOe4AIrEpZ3iIEyfDXzrXongEA== X-Received: by 2002:a05:6122:6b2:b0:4d3:3a78:60e2 with SMTP id r18-20020a05612206b200b004d33a7860e2mr108423vkq.1.1711058889439; Thu, 21 Mar 2024 15:08:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYDbaW1J9E1O0bYtn9bzXmgf1WJ4RRo+0b7nGhM7jp1NUFRItVBXPR4HtJOs8C30IHezp50w== X-Received: by 2002:a05:6122:6b2:b0:4d3:3a78:60e2 with SMTP id r18-20020a05612206b200b004d33a7860e2mr108396vkq.1.1711058889030; Thu, 21 Mar 2024 15:08:09 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:08 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 02/12] mm/hugetlb: Declare hugetlbfs_pagecache_present() non-static Date: Thu, 21 Mar 2024 18:07:52 -0400 Message-ID: <20240321220802.679544-3-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu It will be used outside hugetlb.c soon. Signed-off-by: Peter Xu --- include/linux/hugetlb.h | 9 +++++++++ mm/hugetlb.c | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 300de33c6fde..52d9efcf1edf 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -174,6 +174,9 @@ u32 hugetlb_fault_mutex_hash(struct address_space *mapp= ing, pgoff_t idx); =20 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, pud_t *pud); +bool hugetlbfs_pagecache_present(struct hstate *h, + struct vm_area_struct *vma, + unsigned long address); =20 struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage); =20 @@ -1197,6 +1200,12 @@ static inline void hugetlb_register_node(struct node= *node) static inline void hugetlb_unregister_node(struct node *node) { } + +static inline bool hugetlbfs_pagecache_present( + struct hstate *h, struct vm_area_struct *vma, unsigned long address) +{ + return false; +} #endif /* CONFIG_HUGETLB_PAGE */ =20 static inline spinlock_t *huge_pte_lock(struct hstate *h, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 23ef240ba48a..abec04575c89 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6129,8 +6129,8 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, st= ruct vm_area_struct *vma, /* * Return whether there is a pagecache page to back given address within V= MA. */ -static bool hugetlbfs_pagecache_present(struct hstate *h, - struct vm_area_struct *vma, unsigned long address) +bool hugetlbfs_pagecache_present(struct hstate *h, + struct vm_area_struct *vma, unsigned long address) { struct address_space *mapping =3D vma->vm_file->f_mapping; pgoff_t idx =3D linear_page_index(vma, address); --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 9B8DB13699F for ; Thu, 21 Mar 2024 22:08:14 +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=1711058896; cv=none; b=oGWRGTevXzh74k3LsPDlTuQS5kTpzCcklIXByHYHPVNVxU/1fHxyYF9RREz2bN4cn5OlNl/1hVHwbBuOGtCI61LjCbRF3N3B37gsmQ5jBV8WmtReWVuZxFrZyuiLBXZ9B4jy5gwJJ4MW9U2cNu6ZgbyEA0mVUfSsh5GUDk2X46w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058896; c=relaxed/simple; bh=d+CzKNkrB4Sb8dVg0BsB74DxnyM36sf/PIc4dUau+go=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z+LIB/bu2P9Py8gbtag17vRIxnY+9QLclPYB/c4g0yjFAsEM8jzz7K3EzrFHFL5wM1KQWmdR726jHYyWnmQm4ELLmK/RLlGJA4JUWCtBWY3H7EtcyEEkKiKuU7Pl3arq2CcTC+vZ0okqouOuHPDUn8BKMinvNL4bIXnsNBRKvDQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=MPecyvx2; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="MPecyvx2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058893; 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=wdRUTOTGji9K1EOtUzCYNHXUg1STGKkafiwUdqA6oYA=; b=MPecyvx2PxH5WQgAKJl4QE0aW2tCOSRnRvjzPlmRtr9A158IalKQTJrWPGXPuz3Or1Sy2T g2nFRHSyE++OVnlMPbNO1h2e7eB05u8IaQpcusQJYhUZlMFyNRlEIUW0wIqOGYYRTV87aS Aw31Q/dwmt8Nk/T0Mxz9qe1JpDq2R5o= 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-114-O7V0yWH8M_-nvw72YskUDw-1; Thu, 21 Mar 2024 18:08:11 -0400 X-MC-Unique: O7V0yWH8M_-nvw72YskUDw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-69152af7760so3924776d6.1 for ; Thu, 21 Mar 2024 15:08:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058891; x=1711663691; 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=wdRUTOTGji9K1EOtUzCYNHXUg1STGKkafiwUdqA6oYA=; b=fzeQYe6clR/z88qYHPh4SRnVhG+y2NwvCoYhf5tq2jKERUb9OSFah2xqbpCaQ4D5uC zYCR5bSi4Qs2gEinzgzKre1aDZx5P6/edAfEJFaTJEZL/W9T9vZ4XoWxvjh6AlVIlbW2 axGGq17sxvstc+rLGA75MdibipW+0fFxYj9uHS7J4O80M3/59bioYfBZ/afx/qnbKsdg n+0FMSxL/uFnUZN1BxUfGc60Jypvn3Y3gxgFtjNUCUOoNB7dpOwGcpN1qKowK0zzKvPX /OtaNqetll2Kno1VRG5IEPONxRaq7pQcEny+VNcdyR9hIXuOwBNENfRKrtuEKtVWumUc 9L5w== X-Forwarded-Encrypted: i=1; AJvYcCV83SZhC/herNRvb7q8zlGavD+AYV4JKa7ThrgChl+H99sbDvUXZQk80z1QmC0Z3Xilsi51zJjmsYtTySay6EbaJmUGO3Tbn+L05YCP X-Gm-Message-State: AOJu0Yx2iVXioemfc13p9WDcQc2Hjb6TnsBueaO/BqrqfDnxVHsoiMc4 esDzAbI+gPgslItqhijxYvVlQqr9ysbH1jEjo/BWYa4p6gsAJsHmxxsPz+XNiWEzvAPfurlZewo REwINVFCB8nO7YhyjWtxCdLYhGBUYWdqjLttMAe0RqKtQ223r59VCeA+/1anHxA== X-Received: by 2002:a05:6214:3a8a:b0:690:3c85:c5b with SMTP id nz10-20020a0562143a8a00b006903c850c5bmr195551qvb.3.1711058891330; Thu, 21 Mar 2024 15:08:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG16ns9jJ9MJ80ThL7hh1B1qvLdYLgSNyXX5iyvBQC6076/qVHetDei+F5v2+719IrZ5asQ9g== X-Received: by 2002:a05:6214:3a8a:b0:690:3c85:c5b with SMTP id nz10-20020a0562143a8a00b006903c850c5bmr195527qvb.3.1711058890838; Thu, 21 Mar 2024 15:08:10 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:10 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 03/12] mm: Make HPAGE_PXD_* macros even if !THP Date: Thu, 21 Mar 2024 18:07:53 -0400 Message-ID: <20240321220802.679544-4-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu These macros can be helpful when we plan to merge hugetlb code into generic code. Move them out and define them even if !THP. We actually already defined HPAGE_PMD_NR for other reasons even if !THP. Reorganize these macros. Reviewed-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- include/linux/huge_mm.h | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index de0c89105076..3bcdfc7e5d57 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -64,9 +64,6 @@ ssize_t single_hugepage_flag_show(struct kobject *kobj, enum transparent_hugepage_flag flag); extern struct kobj_attribute shmem_enabled_attr; =20 -#define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT) -#define HPAGE_PMD_NR (1<; Thu, 21 Mar 2024 22:08:15 +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=1711058897; cv=none; b=DbuEqZSuyvd3CCAqTB4JZzQxanMWDLJOUxJwbO/xCy2AU3KfrsDikblT7SPGx2rZIm/U5XeptVUhTaeIR/WrqF88BaXXUSTFfQc9CIavws0qXvSC2L1Id0lV5I5AVdp1/Sz0CerTr+Vspvunv1QWW6O7ob+4RCyMhEY1ybQRCOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058897; c=relaxed/simple; bh=Kixc0NMymkuIn9yHI5UxvEO7k03XXmYFgYWQVgrv4R8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FVp8jvOUyzOsJbYG+echIBtXKmU6WgwYgrnYRn0SlprV3II7tLiT0kb3HgRXBgx7uZ4RpAzsDWPESYufzRJAarK6soC0gRhdggQyIGvbbcP/YMgHulPJ/0uhN5sPGOPrlmBZGzHTtcD+MprnSn4f1aIsyaHRG3HKGqij60OYW5s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=gTEh77H6; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="gTEh77H6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058894; 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=TwZ/C3Y+mq5srgVBOLaWiB530mwVuOVA1xifIUFcqIU=; b=gTEh77H6jYYKjNin2pl2EQN1I/WTJRQ3NVBs2IHAsPBY4D/8nDnlJz9KZ2deWbQqpnN2Gw AVYjcvd6rFrvpv7yrw32BVLOgXiLs2/U66PxI8W8uw1ITRE/6R6VjW5bCKOuB0TmPS5+0r 6ED+ezhMv6m2u5/+gB2QqsDSR85iSlA= 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-66-coX52MI6OLC2UDlMig_9DA-1; Thu, 21 Mar 2024 18:08:13 -0400 X-MC-Unique: coX52MI6OLC2UDlMig_9DA-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-430b4572a97so4829221cf.1 for ; Thu, 21 Mar 2024 15:08:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058893; x=1711663693; 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=TwZ/C3Y+mq5srgVBOLaWiB530mwVuOVA1xifIUFcqIU=; b=Dnu4vgFl97Yfr6DMgdytKYQwPeSrIqftjwa1VvK5/Y0Cf0flYnPNzIdMgXJmG/PpU+ L9jIHTrIPoTmAUp/LCgcAfOo3T9FbXzT+8TAR3zh+DtSTBbOTOHrg6jAQJEtDWcZHm2h VACA20M/cNqS2glXEzGcV51Hq21hkWKq2/8JsUeDU9mGA5UaFGNQVguMmD+Ad4MLDkpF q4Hcz12FLJSxv26PWPWZtDpuOzKvIuUun4bommMnlDDHAi0GhPjeo4xtYu6w2ToFdr5K B4WSJ5k5QJAlrOGbMZtG5OShrwyJD9qunJWDRdJQNo7NntW89VsRbgcPZOpRJVw3L8UH B5dQ== X-Forwarded-Encrypted: i=1; AJvYcCXYu+3zb5Q/vES3OFSr/1a9LNKRY4/Y9g8JtPe6Jjwgd5JdvS4EwxNEuAFSbhjS/E310ZInWVWE15tVSc2b+22OKtjKVEIuSc9X4c/c X-Gm-Message-State: AOJu0Yw8qHfJeUYfhs3wpPPu47VQsnMPYOWsZ9XNNfZIYnaXatHl/TXf nMxL/qaqv89Q8Rg9qulOMhNyQiSjAbns2KFDUWm13VKX/pzIPyitSGRDdSiWBSPHRGet8oG8n1K 4XVwgm9sUsboo5x9pic+M7nsblmp6rfl5ycPgOWQc0s80mBxjdYRcbdh0vy507w== X-Received: by 2002:a05:6214:3f89:b0:690:c35c:7590 with SMTP id ow9-20020a0562143f8900b00690c35c7590mr218137qvb.2.1711058893171; Thu, 21 Mar 2024 15:08:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGl1Qmf0wBzWRoAAACR5bZA/I9hFYAFMFu/sm/LF6+WMAng7TWyPkikdWZMgBZCHnmtXnvmwg== X-Received: by 2002:a05:6214:3f89:b0:690:c35c:7590 with SMTP id ow9-20020a0562143f8900b00690c35c7590mr218110qvb.2.1711058892727; Thu, 21 Mar 2024 15:08:12 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:12 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 04/12] mm: Introduce vma_pgtable_walk_{begin|end}() Date: Thu, 21 Mar 2024 18:07:54 -0400 Message-ID: <20240321220802.679544-5-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Introduce per-vma begin()/end() helpers for pgtable walks. This is a preparation work to merge hugetlb pgtable walkers with generic mm. The helpers need to be called before and after a pgtable walk, will start to be needed if the pgtable walker code supports hugetlb pages. It's a hook point for any type of VMA, but for now only hugetlb uses it to stablize the pgtable pages from getting away (due to possible pmd unsharing). Reviewed-by: Christoph Hellwig Reviewed-by: Muchun Song Signed-off-by: Peter Xu Reviewed-by: Jason Gunthorpe --- include/linux/mm.h | 3 +++ mm/memory.c | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8147b1302413..d10eb89f4096 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4198,4 +4198,7 @@ static inline bool pfn_is_unaccepted_memory(unsigned = long pfn) return range_contains_unaccepted_memory(paddr, paddr + PAGE_SIZE); } =20 +void vma_pgtable_walk_begin(struct vm_area_struct *vma); +void vma_pgtable_walk_end(struct vm_area_struct *vma); + #endif /* _LINUX_MM_H */ diff --git a/mm/memory.c b/mm/memory.c index 9bce1fa76dd7..4f2caf1c3c4d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6438,3 +6438,15 @@ void ptlock_free(struct ptdesc *ptdesc) kmem_cache_free(page_ptl_cachep, ptdesc->ptl); } #endif + +void vma_pgtable_walk_begin(struct vm_area_struct *vma) +{ + if (is_vm_hugetlb_page(vma)) + hugetlb_vma_lock_read(vma); +} + +void vma_pgtable_walk_end(struct vm_area_struct *vma) +{ + if (is_vm_hugetlb_page(vma)) + hugetlb_vma_unlock_read(vma); +} --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 3BACC137766 for ; Thu, 21 Mar 2024 22:08:17 +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=1711058901; cv=none; b=AH4ul1D94SAZFh1/eKrbLlZThSP68fHDsPuFeVyRo8GUYWGqKAZgoM73wG6vySw30Nqn4tD4iob7ruI2ulZNUdbA9mkkhkq4P61YrINA7vnrUJYxqJhxEWxyW5A7cTHgj1iIVYv718/Eik8w86JVFfV9s3THolTpiKzGAtHVpwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058901; c=relaxed/simple; bh=kGkGjH9m/y0RS3YSsTAfsfr/ljhTQ3+fy8C6MUt+UTw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=By59petyimTX7QHRJejgHb2iRHvz0fqgA8FBCfxHPNwY7jnE0v4UjUXO++BiOn5XN/uTXLVQdWQfCLbx7frCxAZtyxnPfYqjNufHUVzlHIs7N4fInFuq0zA/rr1Q2XDIOYAqFklCAvM5+1vUOfVqdPChg8Il/lPVY2ld2bflsxo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=bRWAdnJe; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="bRWAdnJe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058897; 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=SUPAT0rXQJh3Nc2mzYzsdwstyixoWF7iCZ8XsZkm6u0=; b=bRWAdnJeZM60ToFbKNNGK/wmobEWhc+HluD2f2gxBOK21zZ+3Hws4ZcnQLGjlEaCVG8deZ Q9pbU9iQXYL8USAbirGe8Al0l1nhdhEuRLWOEj2ENW+/mGkrH3fiTs1yLqlxt7jHB4//Me f+9sOfCkMxs2HtzRs+XqpAsRBoUFpgk= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-372-IzMwvxmePIWfBKkIqdaVmw-1; Thu, 21 Mar 2024 18:08:15 -0400 X-MC-Unique: IzMwvxmePIWfBKkIqdaVmw-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-78a364064b7so14872585a.1 for ; Thu, 21 Mar 2024 15:08:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058895; x=1711663695; 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=SUPAT0rXQJh3Nc2mzYzsdwstyixoWF7iCZ8XsZkm6u0=; b=lh9CJF3NuMPv3ci1ffDruQgr9x9LmXrENk/9QYAnKgIkN8MtbUr15X/3dxL3myqaVw 69vXhu6i8gKO9JLpE5lRZDC7EaN61BJIJP2v6v9X/PhM9VpeH7JqNx/Rvjw5hy6tB+0G xWFSxZ/72gMsj1to5Z+pUrkvJmerQK7qm5rRxlS9HFUtHfTwbH/0An+3Nw/sdEq6qOQp 1zgxRDUtXkd1t+x+jlZw3+Ij01qtbwNbc0FNa2sDsL+YhyqEyMHqkJWfIGxD/Jy4M/iL iZooaDnySF8RTRGq2117/v/9gJ5eu0ihI6eRs692axcQtc4/RdxSPXLwxvEjT/8HLp4C YPYA== X-Forwarded-Encrypted: i=1; AJvYcCWVg3BdkidEhEJgIuQO+ToTcUx23Mm4dcwcyMPZnzdbZuu81uTZ0g3McFxaPZlusukeclpSasxb3xm1PSITBL/ZsVAXUDoYSci2RwLj X-Gm-Message-State: AOJu0YzfmoXQiy+091hg3zOBqzPQp3R33CXs3jOK293E0Mh+9tgzYYWd zI8T9Ix5dscBixwnEt4rch+oy0xPzJJuX6eYqD+9z+YEozcaFz2D4MsT30+nT9++jYSk8qTeM6F RO/odcAZ1TwBPw2C2CremwqoUafL9dGvXzM5IA5bpKmJWMO4+f9/PuW8vwSX6cg== X-Received: by 2002:a05:6214:3f89:b0:691:2524:2d87 with SMTP id ow9-20020a0562143f8900b0069125242d87mr214719qvb.2.1711058895189; Thu, 21 Mar 2024 15:08:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHuIdayMB3K1ezoBSvCD0gJVl53LosR8J8AUw6cJS5L0fxLggs0ufmC9b8hoGFmk2jAJqKnIA== X-Received: by 2002:a05:6214:3f89:b0:691:2524:2d87 with SMTP id ow9-20020a0562143f8900b0069125242d87mr214690qvb.2.1711058894780; Thu, 21 Mar 2024 15:08:14 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:14 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 05/12] mm/gup: Drop folio_fast_pin_allowed() in hugepd processing Date: Thu, 21 Mar 2024 18:07:55 -0400 Message-ID: <20240321220802.679544-6-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Hugepd format for GUP is only used in PowerPC with hugetlbfs. There are some kernel usage of hugepd (can refer to hugepd_populate_kernel() for PPC_8XX), however those pages are not candidates for GUP. Commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings") added a check to fail gup-fast if there's potential risk of violating GUP over writeback file systems. That should never apply to hugepd. Considering that hugepd is an old format (and even software-only), there's no plan to extend hugepd into other file typed memories that is prone to the same issue. Drop that check, not only because it'll never be true for hugepd per any known plan, but also it paves way for reusing the function outside fast-gup. To make sure we'll still remember this issue just in case hugepd will be extended to support non-hugetlbfs memories, add a rich comment above gup_huge_pd(), explaining the issue with proper references. Cc: Christoph Hellwig Cc: Lorenzo Stoakes Cc: Michael Ellerman Cc: linuxppc-dev@lists.ozlabs.org Signed-off-by: Peter Xu Reviewed-by: Jason Gunthorpe --- mm/gup.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 484a7c70d121..9127ec5515ac 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2831,11 +2831,6 @@ static int gup_hugepte(pte_t *ptep, unsigned long sz= , unsigned long addr, return 0; } =20 - if (!folio_fast_pin_allowed(folio, flags)) { - gup_put_folio(folio, refs, flags); - return 0; - } - if (!pte_write(pte) && gup_must_unshare(NULL, flags, &folio->page)) { gup_put_folio(folio, refs, flags); return 0; @@ -2846,6 +2841,14 @@ static int gup_hugepte(pte_t *ptep, unsigned long sz= , unsigned long addr, return 1; } =20 +/* + * NOTE: currently GUP for a hugepd is only possible on hugetlbfs file + * systems on Power, which does not have issue with folio writeback against + * GUP updates. When hugepd will be extended to support non-hugetlbfs or + * even anonymous memory, we need to do extra check as what we do with most + * of the other folios. See writable_file_mapping_allowed() and + * folio_fast_pin_allowed() for more information. + */ static int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned int pdshift, unsigned long end, unsigned int flags, struct page **pages, int *nr) --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 37DFA137909 for ; Thu, 21 Mar 2024 22:08:20 +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=1711058903; cv=none; b=WZ6D0gEDaBKPawE+ydntycgHfnBI5ZF8cKXbZ/Gy2pQYPMIkfp242plboWCYI0Ivl9olBv73IJT6KHejiwjinKDAGK8/UQhgPvRIBvy7Ey+F8khvvJTB/WqP5iBhc+aEMY0bdPwldpO+BUt080eNzT5hnev7CZkrTyQH4PDaeQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058903; c=relaxed/simple; bh=QkztVZc/S8PWXxZ29LUGyIvEPUuXd1KAQiedbYq07Sk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EoqBsaDfWH4StyZZSZhNavL3CS+50+eANhelwbm3Pfoe7WIGSrzZ7x4/IZaSlSmOY3xD2/M4htbfE8MEP/GpBNT8Kft8zQ32R9zvqvrOXRXQwIdH3so8ERXChJO6l6CFfMbpYsY3g/7Nuc8mGofWW7D8Y1oL7e2/1Kq3Fd1JdEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=fSRaYRRh; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="fSRaYRRh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058899; 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=dI3BkO2nCtQEnBDt3PLoa+NL/Gig0J3ZsiByK6YiGEU=; b=fSRaYRRh0mb3sOrJiUnGZ73F2yFGbm0nbdHTkjFmvEafXJR+Ou3kolGbfIh1kwAoXrYSNc CkWwt0XeZO8Vq/w+Z3NczGbx7QF3qi4dx7Q0FdqUGNsce9b+lWuwhtuE8nTJWmjL1DHmWs Mh5GPc8ksik6WH0Mv/ba2LUm2di1vcI= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-649-BudFdKofOrONiL_HQr_1rA-1; Thu, 21 Mar 2024 18:08:17 -0400 X-MC-Unique: BudFdKofOrONiL_HQr_1rA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7830635331bso30345885a.1 for ; Thu, 21 Mar 2024 15:08:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058897; x=1711663697; 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=dI3BkO2nCtQEnBDt3PLoa+NL/Gig0J3ZsiByK6YiGEU=; b=eBQBaevr/kOwGggZO6+zqAvVDOBd8O73uQt17wvsvXJEbA8X0i2pMjX02Fe2X5BmtH FHHBXjEeyjS6xcsKzirsjzWDHN7HlOd3SpNzKfLNPCXs53OcK4sSfxKpli1ncq1aFbE8 vOzWFfRtudkBIRC1QAPtjwgd26PgH16IIa364FWBbYZS/nZ99KvOBzkktyyP34LGiAbV KwgG1+X/z23vJqVrS+5g3KbwlbvPfz8sGkTY6AQ3YsVqFJ8bjXRPQ3heKI+xo4mfTAxS /K6nxp/vXfLkE0ftyR4DjLIEGq8/RG0LGtL2kOBukIEQNataMiWaI7FVQCGpU+lvjXNV d7uA== X-Forwarded-Encrypted: i=1; AJvYcCVC/AGG1z/tyNGx1BZ+JxG5RI+/9j2UcOd6wekMlgDEy5lmoyovAPMDOAdrXlWxrmEm8PXWP3EdSH30vkdPYhWDKY3xb7J87UhdyBZ2 X-Gm-Message-State: AOJu0YxsFo/P2NG4/HdegfRrTUEDb+IJ7fYuD0xAymmbPfHDq/HcNW7h 29KWLH5yoRHupRi3oWWzVYudMzYo1vKZYOpvTxMkdt2eaLpSy2dQGBvPNWhVEYz6mJ66rqNnBXQ BlaYsTD7MkDf/ZXLeTGyPstPaBqxlcx/2IU8KP8QWZGOx7fzjO85AEI4oofAbLg== X-Received: by 2002:a05:6214:4489:b0:68f:e779:70f2 with SMTP id on9-20020a056214448900b0068fe77970f2mr443533qvb.3.1711058897149; Thu, 21 Mar 2024 15:08:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHGuIUGmtGDilXkO+Txm1IkzpifWGQBAYBHvb3QP196cSPWL36y2snARbq2rVPrLWqoQw6FQA== X-Received: by 2002:a05:6214:4489:b0:68f:e779:70f2 with SMTP id on9-20020a056214448900b0068fe77970f2mr443515qvb.3.1711058896774; Thu, 21 Mar 2024 15:08:16 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:16 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 06/12] mm/gup: Refactor record_subpages() to find 1st small page Date: Thu, 21 Mar 2024 18:07:56 -0400 Message-ID: <20240321220802.679544-7-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu All the fast-gup functions take a tail page to operate, always need to do page mask calculations before feeding that into record_subpages(). Merge that logic into record_subpages(), so that it will do the nth_page() calculation. Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/gup.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 9127ec5515ac..f3ae8f6ce8a4 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2778,13 +2778,16 @@ static int __gup_device_huge_pud(pud_t pud, pud_t *= pudp, unsigned long addr, } #endif =20 -static int record_subpages(struct page *page, unsigned long addr, - unsigned long end, struct page **pages) +static int record_subpages(struct page *page, unsigned long sz, + unsigned long addr, unsigned long end, + struct page **pages) { + struct page *start_page; int nr; =20 + start_page =3D nth_page(page, (addr & (sz - 1)) >> PAGE_SHIFT); for (nr =3D 0; addr !=3D end; nr++, addr +=3D PAGE_SIZE) - pages[nr] =3D nth_page(page, nr); + pages[nr] =3D nth_page(start_page, nr); =20 return nr; } @@ -2819,8 +2822,8 @@ static int gup_hugepte(pte_t *ptep, unsigned long sz,= unsigned long addr, /* hugepages are never "special" */ VM_BUG_ON(!pfn_valid(pte_pfn(pte))); =20 - page =3D nth_page(pte_page(pte), (addr & (sz - 1)) >> PAGE_SHIFT); - refs =3D record_subpages(page, addr, end, pages + *nr); + page =3D pte_page(pte); + refs =3D record_subpages(page, sz, addr, end, pages + *nr); =20 folio =3D try_grab_folio(page, refs, flags); if (!folio) @@ -2893,8 +2896,8 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsi= gned long addr, pages, nr); } =20 - page =3D nth_page(pmd_page(orig), (addr & ~PMD_MASK) >> PAGE_SHIFT); - refs =3D record_subpages(page, addr, end, pages + *nr); + page =3D pmd_page(orig); + refs =3D record_subpages(page, PMD_SIZE, addr, end, pages + *nr); =20 folio =3D try_grab_folio(page, refs, flags); if (!folio) @@ -2937,8 +2940,8 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsi= gned long addr, pages, nr); } =20 - page =3D nth_page(pud_page(orig), (addr & ~PUD_MASK) >> PAGE_SHIFT); - refs =3D record_subpages(page, addr, end, pages + *nr); + page =3D pud_page(orig); + refs =3D record_subpages(page, PUD_SIZE, addr, end, pages + *nr); =20 folio =3D try_grab_folio(page, refs, flags); if (!folio) @@ -2977,8 +2980,8 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsi= gned long addr, =20 BUILD_BUG_ON(pgd_devmap(orig)); =20 - page =3D nth_page(pgd_page(orig), (addr & ~PGDIR_MASK) >> PAGE_SHIFT); - refs =3D record_subpages(page, addr, end, pages + *nr); + page =3D pgd_page(orig); + refs =3D record_subpages(page, PGDIR_SIZE, addr, end, pages + *nr); =20 folio =3D try_grab_folio(page, refs, flags); if (!folio) --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 4A16E137914 for ; Thu, 21 Mar 2024 22:08:22 +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=1711058904; cv=none; b=QiEYOWs2NhbryDzhINCB1U5wpViKaYtuOdh53UEGVlhWQtkpeUHB+bayj02nFydZ1ypkZOb18+ZxIDEEFdVn4wYlUhAWojsJMszFxJdTLLLlXl9DFHoUvg2GEHAYu7IziZmXlYGe66+zcrc0VJ7G6WLpSlNjoL12Dk4DK/G1vSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058904; c=relaxed/simple; bh=8dzAfFfd0rJLLlAOHkOIITBY0yDGO+q+j5nFR28BMCA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K2NedE5fqjYhGACMeWHi2LUWCJnqNPn/U8VEAUBd9epkr+4T6u2Sa4etVBjpJnpPeuzLmxGVUNJ4yRDqoWvK/kBpTLaJS6e3FB64a0waaNEgmrrlL9vB3NjCnHlT/vscGfRiXi5TbQkOLa3fGHYMK5HihOnKidYs7WaP3UCtYwA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=Z6M+PJYr; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="Z6M+PJYr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058901; 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=U+6dxwHwA9JOroUFOiyHgWbjpU2ZpFB4CR6YlbtDU08=; b=Z6M+PJYrCKwOBdZaM7dzxW1VWq3/BkrltsmAnsN7mWsL0uB2SKjIMBPqQANenKrnALdIlx ZV1yKodHU+dWU4J2fyaQlGqKgfoTOsG7yvWaQytuyf8lIoM+A1hINZfzCPI+QQHF5Eft1e mqdT7/9Xu9FmVbVxFE1045z66/T97U4= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-569-0EQSc2GUNw6Qr_RYEG17Ag-1; Thu, 21 Mar 2024 18:08:19 -0400 X-MC-Unique: 0EQSc2GUNw6Qr_RYEG17Ag-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6961a54234cso2674296d6.1 for ; Thu, 21 Mar 2024 15:08:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058899; x=1711663699; 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=U+6dxwHwA9JOroUFOiyHgWbjpU2ZpFB4CR6YlbtDU08=; b=AWDkQlJxPgl6ASqTU1ao6j9HY2DnH6vCOHbfOcSzVdqor+t20TdURARfYsHuRT9dAv HX+Xgi6rkI6afG7DMctLMjmyInirTD6/vrFFDyJn8u/9bqiI3g7rbNGsIu283OAxIoEd nApVEhjaw3JAP9VMOrrvGauS6bEDgSaTnagAz384HFVue+TxwPPVQACFPk6eluYS4VYV 4EXBO2RNzcuB2De2Ord+zDHqW576C9IaEZQmF326zbMM+3IpLEWoDc3jvY5EYDnbK1aS D25M22ccRXlKwBcTHDiVGY81qPnzmRp0vbUGOjISy9WlJrxmxKVZPCpvqLTAZJzSjynq giWw== X-Forwarded-Encrypted: i=1; AJvYcCWrEJuKiTw47fEbXqnlQEAU9pROkNg7MbdN+VOgoPyxxiV+uPJo1DOeywa8bzpvfYz45CtMJtJu+9IDmiwAU4TxmT6Y/dE9uXRBr/5W X-Gm-Message-State: AOJu0YxVAsPLZf/72jllCAwURvbYWzqQBGKqlMfa0wjl2bxp5b0Nd9H6 koFqE9TrCbD6pLSRTzh4YOAgDf7Hs/mHHWVWPJOwVEWB8m1nC5SX+hMJ6/24x7OD2ld7pwyR1T3 CCBUn4BkivirboksuI8k8hTZ6Nx6RvMaRDtlde5kIknIWu/qLraDVCMh0z5lhJw== X-Received: by 2002:a05:6214:3912:b0:691:456f:415a with SMTP id nh18-20020a056214391200b00691456f415amr179394qvb.4.1711058899093; Thu, 21 Mar 2024 15:08:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnkSxmKz4azbUELRR0cRTvXrQr4udCnJcoTFPGMx5l/qOyVfxe6G6Q7JjdIdQ6Zo0lZ2QFGA== X-Received: by 2002:a05:6214:3912:b0:691:456f:415a with SMTP id nh18-20020a056214391200b00691456f415amr179365qvb.4.1711058898616; Thu, 21 Mar 2024 15:08:18 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:18 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 07/12] mm/gup: Handle hugetlb for no_page_table() Date: Thu, 21 Mar 2024 18:07:57 -0400 Message-ID: <20240321220802.679544-8-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu no_page_table() is not yet used for hugetlb code paths. Make it prepared. The major difference here is hugetlb will return -EFAULT as long as page cache does not exist, even if VM_SHARED. See hugetlb_follow_page_mask(). Pass "address" into no_page_table() too, as hugetlb will need it. Reviewed-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/gup.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index f3ae8f6ce8a4..943671736080 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -501,19 +501,27 @@ static inline void mm_set_has_pinned_flag(unsigned lo= ng *mm_flags) =20 #ifdef CONFIG_MMU static struct page *no_page_table(struct vm_area_struct *vma, - unsigned int flags) + unsigned int flags, unsigned long address) { + if (!(flags & FOLL_DUMP)) + return NULL; + /* - * When core dumping an enormous anonymous area that nobody - * has touched so far, we don't want to allocate unnecessary pages or + * When core dumping, we don't want to allocate unnecessary pages or * page tables. Return error instead of NULL to skip handle_mm_fault, * then get_dump_page() will return NULL to leave a hole in the dump. * But we can only make this optimization where a hole would surely * be zero-filled if handle_mm_fault() actually did handle it. */ - if ((flags & FOLL_DUMP) && - (vma_is_anonymous(vma) || !vma->vm_ops->fault)) + if (is_vm_hugetlb_page(vma)) { + struct hstate *h =3D hstate_vma(vma); + + if (!hugetlbfs_pagecache_present(h, vma, address)) + return ERR_PTR(-EFAULT); + } else if ((vma_is_anonymous(vma) || !vma->vm_ops->fault)) { return ERR_PTR(-EFAULT); + } + return NULL; } =20 @@ -593,7 +601,7 @@ static struct page *follow_page_pte(struct vm_area_stru= ct *vma, =20 ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); if (!ptep) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); pte =3D ptep_get(ptep); if (!pte_present(pte)) goto no_page; @@ -685,7 +693,7 @@ static struct page *follow_page_pte(struct vm_area_stru= ct *vma, pte_unmap_unlock(ptep, ptl); if (!pte_none(pte)) return NULL; - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); } =20 static struct page *follow_pmd_mask(struct vm_area_struct *vma, @@ -701,27 +709,27 @@ static struct page *follow_pmd_mask(struct vm_area_st= ruct *vma, pmd =3D pmd_offset(pudp, address); pmdval =3D pmdp_get_lockless(pmd); if (pmd_none(pmdval)) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); if (!pmd_present(pmdval)) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); if (pmd_devmap(pmdval)) { ptl =3D pmd_lock(mm, pmd); page =3D follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); spin_unlock(ptl); if (page) return page; - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); } if (likely(!pmd_trans_huge(pmdval))) return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); =20 if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); =20 ptl =3D pmd_lock(mm, pmd); if (unlikely(!pmd_present(*pmd))) { spin_unlock(ptl); - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); } if (unlikely(!pmd_trans_huge(*pmd))) { spin_unlock(ptl); @@ -752,17 +760,17 @@ static struct page *follow_pud_mask(struct vm_area_st= ruct *vma, =20 pud =3D pud_offset(p4dp, address); if (pud_none(*pud)) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); if (pud_devmap(*pud)) { ptl =3D pud_lock(mm, pud); page =3D follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); spin_unlock(ptl); if (page) return page; - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); } if (unlikely(pud_bad(*pud))) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); =20 return follow_pmd_mask(vma, address, pud, flags, ctx); } @@ -777,10 +785,10 @@ static struct page *follow_p4d_mask(struct vm_area_st= ruct *vma, p4dp =3D p4d_offset(pgdp, address); p4d =3D READ_ONCE(*p4dp); if (!p4d_present(p4d)) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); BUILD_BUG_ON(p4d_leaf(p4d)); if (unlikely(p4d_bad(p4d))) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); =20 return follow_pud_mask(vma, address, p4dp, flags, ctx); } @@ -830,7 +838,7 @@ static struct page *follow_page_mask(struct vm_area_str= uct *vma, pgd =3D pgd_offset(mm, address); =20 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) - return no_page_table(vma, flags); + return no_page_table(vma, flags, address); =20 return follow_p4d_mask(vma, address, pgd, flags, ctx); } --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 70E1913792C for ; Thu, 21 Mar 2024 22:08:24 +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=1711058906; cv=none; b=KHI3MlGNElb4WQJcSyb32+KkLQqy2H7eG0Ap8AvMcpt8gbKEfhlYwWOG3Bodm1CSdEHgwikenxcwBH18hRpMFS27p7ch2ccgVdCdpxfZrX6qp0dyb5HA6tTc4sX7dzX9ClImrGRfJupoaNFMivdJOQslgnXCKbi5JgiiQYucld4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058906; c=relaxed/simple; bh=wKfRUGs3K5Ab+ccxiD9h4kv+tcYnw5IVBdQPdiucKgM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lro5Xvmfoj6KkdJ5HoFozBjdf32F4LrXbLy/4LlyyMVDNsZ2Gd5jVCg356FSZykhIepQvCVYs4JDzgmaIS0ogxHuCXasMvkfUjCcuTp5xBNAYlW8//r7DfxZQg4eTcTYdHa3Q2PvP4DVKmcDlpXBd1zT9rCqftFuRTynHFq8EsE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=II4mt2Pf; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="II4mt2Pf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058903; 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=ha5DGM0IN3r6kIFCrMZt60pb/ho2ZGKh4AhFb2qqPrM=; b=II4mt2PfxszPtdlzKAceCecc5PQSX5AEpsaaZu4xnPO1LcdZPfyR3N2b0Fklpvz44qGwA6 4HpQJj/l3MHBqu+YZV8ExD7Z2aEe8uIqLj3Zn+hHqaS5642AUmSZwqLDmMyp67J5g59xQf g97mBJZAW+KhsX8tY1qnthUjh2V+Iy0= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-78-ZkPjh8X0PPGVa9Mgrf_nHw-1; Thu, 21 Mar 2024 18:08:21 -0400 X-MC-Unique: ZkPjh8X0PPGVa9Mgrf_nHw-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-690d3f5af86so3056936d6.0 for ; Thu, 21 Mar 2024 15:08:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058901; x=1711663701; 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=ha5DGM0IN3r6kIFCrMZt60pb/ho2ZGKh4AhFb2qqPrM=; b=NLJQNosJoG9Zxql0crHr+JLfQKG1VSW/Pt0LhdY/qb5sYZvZm1wDXHYQ4eEwpLNyQo dVoIXTlsJDSfjjmXwHH8HUOTRw3qBhk/RfceHEALtVFI4qbcw0irQ4Oo2gbPYLIQD8Ol mEqi/CdXdhxZ85v7WJefUIh1YesrbuuZMY0z0xrAKLE0tHmt9su6hGgFCH4s7ubK6Vbz Nj0wlwXHLAHKmrg4tBCOYxZ7WIfmDTXaWsM8cdSdXioU5XrvoWEC6fdhhppYKi7PJoT3 BFDBJS8UnfmvBUk7lXuZW27TChoN34yADpg6XtKr6dhnY+BQos0VIsQiGqm31bSBjf0H zT+A== X-Forwarded-Encrypted: i=1; AJvYcCWx8BRxCuMvl/HhE8wf2w6RGmUfdq7dVb175Axo8SgeB3Kwl+TLRRPJtu2arBF2IZ1SrXoUVDa2u6fptNja5MCDjdwrqCzZ3wulCkZb X-Gm-Message-State: AOJu0YwQbqUqrKs8QZ36TlS/2XTiMfoCvZn8TkQPEK5C9NRPEoKr9Pei lD594S8yxOihC55K7aU+rw0SE7YUxlVt+cuCkdRyuUliMmQ/TTjHalbX5tz0fGYOT19T1MuaRbM dj4yap4uni4pRIIjn3kniIOUARMBf5FETnLVybZlTjG7iqxNWEjTGk/5/NsoSnA== X-Received: by 2002:a05:6214:3f89:b0:690:de72:316f with SMTP id ow9-20020a0562143f8900b00690de72316fmr214835qvb.1.1711058900924; Thu, 21 Mar 2024 15:08:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG9Op81dSYI2sDIbFIL6Ox+mlvTFr9echbLfQb+zyBeeZ6FRPjC6M2m9xSIjZkrEy5V/lebFg== X-Received: by 2002:a05:6214:3f89:b0:690:de72:316f with SMTP id ow9-20020a0562143f8900b00690de72316fmr214791qvb.1.1711058900463; Thu, 21 Mar 2024 15:08:20 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:20 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 08/12] mm/gup: Cache *pudp in follow_pud_mask() Date: Thu, 21 Mar 2024 18:07:58 -0400 Message-ID: <20240321220802.679544-9-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Introduce "pud_t pud" in the function, so the code won't dereference *pudp multiple time. Not only because that looks less straightforward, but also because if the dereference really happened, it's not clear whether there can be race to see different *pudp values if it's being modified at the same time. Acked-by: James Houghton Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/gup.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 943671736080..a338944e4425 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -753,26 +753,27 @@ static struct page *follow_pud_mask(struct vm_area_st= ruct *vma, unsigned int flags, struct follow_page_context *ctx) { - pud_t *pud; + pud_t *pudp, pud; spinlock_t *ptl; struct page *page; struct mm_struct *mm =3D vma->vm_mm; =20 - pud =3D pud_offset(p4dp, address); - if (pud_none(*pud)) + pudp =3D pud_offset(p4dp, address); + pud =3D READ_ONCE(*pudp); + if (pud_none(pud)) return no_page_table(vma, flags, address); - if (pud_devmap(*pud)) { - ptl =3D pud_lock(mm, pud); - page =3D follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); + if (pud_devmap(pud)) { + ptl =3D pud_lock(mm, pudp); + page =3D follow_devmap_pud(vma, address, pudp, flags, &ctx->pgmap); spin_unlock(ptl); if (page) return page; return no_page_table(vma, flags, address); } - if (unlikely(pud_bad(*pud))) + if (unlikely(pud_bad(pud))) return no_page_table(vma, flags, address); =20 - return follow_pmd_mask(vma, address, pud, flags, ctx); + return follow_pmd_mask(vma, address, pudp, flags, ctx); } =20 static struct page *follow_p4d_mask(struct vm_area_struct *vma, --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 9FE0E137C49 for ; Thu, 21 Mar 2024 22:08:27 +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=1711058909; cv=none; b=BSScEXfvZk/0kC0OUUCbyhcDfSZIFOt4pvdFPfhTHw1d1q79WKG7z9gRC/xyVbL37rlQplAUNukPjbG/EbkBttMtlVe2RgPZJzjn2NzQKQTAOhllTb8e92jP9qdkSJi6S9+6TS+Q9bLd/0LB7qN2ScgGBthh4+xbStXehFJm/7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058909; c=relaxed/simple; bh=Mue/44vgd6mPZ43QWexdWgiIluvbkdZ2Z9p4n62rqTw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IehHuO5YcC7YNfC7uFvIeukkaHOmnyk+IbwZXA1Nm0oN80ke3uZWAYzMBm2EVWoLsPlqIAxw9K7tuUIzzb07UO2LgM56yRD2Q4yii900/m78fb08auogxTKSar/FaKJsnSZbZna0N67/ccBPJvrGa6r3B4mJVweC0n6E+Yoe8u0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=MFWIp+k/; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="MFWIp+k/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058906; 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=JwtVKmcef1NV2SEc6Dd7pe+XzH6c3RDiTdVw8lXpTc0=; b=MFWIp+k/IAAt7Y90E1wjo3LRNAbL5qW/yvGErPf8WG/+5u+R6GkTFK7ufUfnnlDVj8VoCU 9HF2L4R8eKotgNg3ockuq9f6tMjUpiLunhgzea7jpkNKw0QZe5NNj4UY4vsnD1+E8Vmo2x KRBXqXe14SAZ+yOmffou/yRXVhMjUC0= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-633-4LlkOeFmNcmEre-3f6_BVA-1; Thu, 21 Mar 2024 18:08:23 -0400 X-MC-Unique: 4LlkOeFmNcmEre-3f6_BVA-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-690d3f5af86so3057226d6.0 for ; Thu, 21 Mar 2024 15:08:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058903; x=1711663703; 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=JwtVKmcef1NV2SEc6Dd7pe+XzH6c3RDiTdVw8lXpTc0=; b=WVeU6+PiSnr2YyMsmTM2oBOw+Jg9PtCTIwGjzJCGoVrt/CrcMQuVwJjkIIqt/hp/qT FCQhJOLXea6VW52ztocm6KBtLc0lXW2Wz+X/w4ezODUBDBXABxipMl/DNWPH8ICPRxpP S8UihSKwa6dwBrTUxsU+3nng5PegHFzn253010pv03FpCZ72F9C8PuUFS5au7XRa02nI IISZnmphVSXpkl7IRB7Sit3Chwlx3p2gjkbzRU6THuSyQLk5ThMwHjnO4bBO30u8PqlU 6tUk1sud9e/3ctwYYntxNcjlSOrrVSXxFinQJ6X5Qd6Xgaq0zuIiY2GrWqBu1ZskNcxe 52ng== X-Forwarded-Encrypted: i=1; AJvYcCWW/LqFFtRcYNRW8uhgqh0QDO152aKWJZB2dElJckRLZ1qEw6EaEi8y/1vYhJTtLy4n8sibcShgGO2yQL8BCF1s0NnuF4wsrT/7Y1ve X-Gm-Message-State: AOJu0Ywz835msWszwHapHx62n8z7RBhuJ3nxUBKM7i+7u4ov9dfTsGl1 ivLwRNwfW71Ic3mgRvF+zUzgxAEu4kvyYHrRcQo5DrvWrVYM0I/BQMbCF0pAbtj5jCbe2yn5m0J aj3h81GNzB0AfNiTasX8QvGeJNYkExtzkUnH/PfEDgI61NlDFZjhRNojWzmrQJg== X-Received: by 2002:a05:6214:4403:b0:690:b47e:60e3 with SMTP id oj3-20020a056214440300b00690b47e60e3mr171644qvb.5.1711058902742; Thu, 21 Mar 2024 15:08:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFefVkn71VpIW4uNQ3LlUv67q0iftDvBfI3myHkSibZanv1/17GTXunJ1XxyTP0oQ7cl/tpbA== X-Received: by 2002:a05:6214:4403:b0:690:b47e:60e3 with SMTP id oj3-20020a056214440300b00690b47e60e3mr171615qvb.5.1711058902326; Thu, 21 Mar 2024 15:08:22 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:21 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 09/12] mm/gup: Handle huge pud for follow_pud_mask() Date: Thu, 21 Mar 2024 18:07:59 -0400 Message-ID: <20240321220802.679544-10-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Teach follow_pud_mask() to be able to handle normal PUD pages like hugetlb. Rename follow_devmap_pud() to follow_huge_pud() so that it can process either huge devmap or hugetlb. Move it out of TRANSPARENT_HUGEPAGE_PUD and and huge_memory.c (which relies on CONFIG_THP). Switch to pud_leaf() to detect both cases in the slow gup. In the new follow_huge_pud(), taking care of possible CoR for hugetlb if necessary. touch_pud() needs to be moved out of huge_memory.c to be accessable from gup.c even if !THP. Since at it, optimize the non-present check by adding a pud_present() early check before taking the pgtable lock, failing the follow_page() early if PUD is not present: that is required by both devmap or hugetlb. Use pud_huge() to also cover the pud_devmap() case. One more trivial thing to mention is, introduce "pud_t pud" in the code paths along the way, so the code doesn't dereference *pudp multiple time. Not only because that looks less straightforward, but also because if the dereference really happened, it's not clear whether there can be race to see different *pudp values when it's being modified at the same time. Setting ctx->page_mask properly for a PUD entry. As a side effect, this patch should also be able to optimize devmap GUP on PUD to be able to jump over the whole PUD range, but not yet verified. Hugetlb already can do so prior to this patch. Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- include/linux/huge_mm.h | 8 ----- mm/gup.c | 70 +++++++++++++++++++++++++++++++++++++++-- mm/huge_memory.c | 47 ++------------------------- mm/internal.h | 2 ++ 4 files changed, 71 insertions(+), 56 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 3bcdfc7e5d57..39195f7c5269 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -346,8 +346,6 @@ static inline bool folio_test_pmd_mappable(struct folio= *folio) =20 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long a= ddr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap); -struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long a= ddr, - pud_t *pud, int flags, struct dev_pagemap **pgmap); =20 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); =20 @@ -504,12 +502,6 @@ static inline struct page *follow_devmap_pmd(struct vm= _area_struct *vma, return NULL; } =20 -static inline struct page *follow_devmap_pud(struct vm_area_struct *vma, - unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap) -{ - return NULL; -} - static inline bool thp_migration_supported(void) { return false; diff --git a/mm/gup.c b/mm/gup.c index a338944e4425..ae21afb9434e 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -525,6 +525,70 @@ static struct page *no_page_table(struct vm_area_struc= t *vma, return NULL; } =20 +#ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES +static struct page *follow_huge_pud(struct vm_area_struct *vma, + unsigned long addr, pud_t *pudp, + int flags, struct follow_page_context *ctx) +{ + struct mm_struct *mm =3D vma->vm_mm; + struct page *page; + pud_t pud =3D *pudp; + unsigned long pfn =3D pud_pfn(pud); + int ret; + + assert_spin_locked(pud_lockptr(mm, pudp)); + + if ((flags & FOLL_WRITE) && !pud_write(pud)) + return NULL; + + if (!pud_present(pud)) + return NULL; + + pfn +=3D (addr & ~PUD_MASK) >> PAGE_SHIFT; + + if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && + pud_devmap(pud)) { + /* + * device mapped pages can only be returned if the caller + * will manage the page reference count. + * + * At least one of FOLL_GET | FOLL_PIN must be set, so + * assert that here: + */ + if (!(flags & (FOLL_GET | FOLL_PIN))) + return ERR_PTR(-EEXIST); + + if (flags & FOLL_TOUCH) + touch_pud(vma, addr, pudp, flags & FOLL_WRITE); + + ctx->pgmap =3D get_dev_pagemap(pfn, ctx->pgmap); + if (!ctx->pgmap) + return ERR_PTR(-EFAULT); + } + + page =3D pfn_to_page(pfn); + + if (!pud_devmap(pud) && !pud_write(pud) && + gup_must_unshare(vma, flags, page)) + return ERR_PTR(-EMLINK); + + ret =3D try_grab_page(page, flags); + if (ret) + page =3D ERR_PTR(ret); + else + ctx->page_mask =3D HPAGE_PUD_NR - 1; + + return page; +} +#else /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ +static struct page *follow_huge_pud(struct vm_area_struct *vma, + unsigned long addr, pud_t *pudp, + int flags, struct follow_page_context *ctx) +{ + return NULL; +} +#endif /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ + static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long addres= s, pte_t *pte, unsigned int flags) { @@ -760,11 +824,11 @@ static struct page *follow_pud_mask(struct vm_area_st= ruct *vma, =20 pudp =3D pud_offset(p4dp, address); pud =3D READ_ONCE(*pudp); - if (pud_none(pud)) + if (!pud_present(pud)) return no_page_table(vma, flags, address); - if (pud_devmap(pud)) { + if (pud_leaf(pud)) { ptl =3D pud_lock(mm, pudp); - page =3D follow_devmap_pud(vma, address, pudp, flags, &ctx->pgmap); + page =3D follow_huge_pud(vma, address, pudp, flags, ctx); spin_unlock(ptl); if (page) return page; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index c77cedf45f3a..f8bd2012bc27 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1363,8 +1363,8 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm= _struct *src_mm, } =20 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD -static void touch_pud(struct vm_area_struct *vma, unsigned long addr, - pud_t *pud, bool write) +void touch_pud(struct vm_area_struct *vma, unsigned long addr, + pud_t *pud, bool write) { pud_t _pud; =20 @@ -1376,49 +1376,6 @@ static void touch_pud(struct vm_area_struct *vma, un= signed long addr, update_mmu_cache_pud(vma, addr, pud); } =20 -struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long a= ddr, - pud_t *pud, int flags, struct dev_pagemap **pgmap) -{ - unsigned long pfn =3D pud_pfn(*pud); - struct mm_struct *mm =3D vma->vm_mm; - struct page *page; - int ret; - - assert_spin_locked(pud_lockptr(mm, pud)); - - if (flags & FOLL_WRITE && !pud_write(*pud)) - return NULL; - - if (pud_present(*pud) && pud_devmap(*pud)) - /* pass */; - else - return NULL; - - if (flags & FOLL_TOUCH) - touch_pud(vma, addr, pud, flags & FOLL_WRITE); - - /* - * device mapped pages can only be returned if the - * caller will manage the page reference count. - * - * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here: - */ - if (!(flags & (FOLL_GET | FOLL_PIN))) - return ERR_PTR(-EEXIST); - - pfn +=3D (addr & ~PUD_MASK) >> PAGE_SHIFT; - *pgmap =3D get_dev_pagemap(pfn, *pgmap); - if (!*pgmap) - return ERR_PTR(-EFAULT); - page =3D pfn_to_page(pfn); - - ret =3D try_grab_page(page, flags); - if (ret) - page =3D ERR_PTR(ret); - - return page; -} - int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, pud_t *dst_pud, pud_t *src_pud, unsigned long addr, struct vm_area_struct *vma) diff --git a/mm/internal.h b/mm/internal.h index f8b31234c130..63e4f6e001be 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1102,6 +1102,8 @@ int __must_check try_grab_page(struct page *page, uns= igned int flags); /* * mm/huge_memory.c */ +void touch_pud(struct vm_area_struct *vma, unsigned long addr, + pud_t *pud, bool write); struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd, unsigned int flags); --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 1FBF8137C50 for ; Thu, 21 Mar 2024 22:08:27 +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=1711058909; cv=none; b=Du04FJkmcl22zp1BtDr/vX3a3QLy4oAN1oYD60MO2WhP4u9E/Q62yC+xuku5qKKwH8mKCUx/0gp/9AG4ueb5ag055hFYIed2HQGF3rpP6r0IVFMgaAt6MydOsDUFYit95NXOVOlgVD5bM0XMJpb8ZYUMn5PjFYe0c4/uvKb944w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058909; c=relaxed/simple; bh=RitX2L5oxcTLCtcTacYVOZOKOLSZfotpY7TNTJ5lfcE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XxDnG39b9+IE+lWZ/Qc8LhMMvmqW47d2oJyw3sXc8rSpAlcCQ6sB30XOiIG7hfOFMsSe1FJcrCzvw12rrnUfL8qA1Q0VphgdGR42yw31mtRDFqMwo45aonE+NWm+ZoSOBmuljgxGXm0Y96chrto4zO05nu7GQE+JLA1kQnsMtFc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=OSM2YzKC; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="OSM2YzKC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058907; 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=ESTHlGxPxGuUFdX3p2oamk2h4NMABnT27/X9cpNES3o=; b=OSM2YzKCNPNlfe/VmpZ0tOdwRfLd1ZrNp5PsgerlKc5zR0F6kNg5tqqY2fZAMHKOQUGAhY 4SEQJYLoj3yJei/RgWSdkFW3vC/1srbfe+1FgQ3o3x5a1/VAa8yaD/OHI5LrPwronkdDka 6HfjrEPgi7fRJd6pISVH6s/A7uCsxLs= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-206-6zbDKf4rN3SMCOr3RtVMMg-1; Thu, 21 Mar 2024 18:08:25 -0400 X-MC-Unique: 6zbDKf4rN3SMCOr3RtVMMg-1 Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-6e67ee38ce7so413867a34.1 for ; Thu, 21 Mar 2024 15:08:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058905; x=1711663705; 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=ESTHlGxPxGuUFdX3p2oamk2h4NMABnT27/X9cpNES3o=; b=lKCITpALbsCC6RTzN3KlCGPoyX4y+s0cCU+iUF7jtTy7WY8qz94uScuAwANMl3e621 Lt0H2yJbYGOuyNtRjBZzKHgWJShHK83NvyvWP3P4sbnGZ+deAoEr9dowOmDvD+IVueN3 2CNfj8Xxj1nAIAJ5bsOEtEnj3sG/q/R0eJ2nxAQH2Cqb7+GbcGfpXYJTAaUjaUIz1xDC 4+tz7tp7EajB+0bxDztqt33GJwaSmV+XbZWZGQPsw0xNt9sgmrD20YGwlEGALtyNNgI7 yCHJJLlJFPp9jVU2FpSjqdM4THqGpAuXPs9wFFF25FJTJNVu/HThwxu4CHXYlDXfgLGQ +egw== X-Forwarded-Encrypted: i=1; AJvYcCUdyXDb3PCK3un150KmXQLcrQokBQijr81EbAI+xl+cT29h+qPkqCSiwSfZhtzXHTbEHwDb9FR1I4g9WO8smzOYyT24P42D95IbtXK9 X-Gm-Message-State: AOJu0YwHvIVMtPfC5IhF8eaLohm06IE/IaVPC1ZO0fODoRfoKGhLAHLE A1SV6dnAO6Fb44t3cL/hRN84De0BL1zAQ+eiTz52HV3/QVYepiAeWqWUPzloivzDhW56ztAc9FK uxSKLeWmwWZthMJ6KIwajoAKPjLZzzic2ARv+O9ZDQMmzflyfB4RMtPRoWFUpwg== X-Received: by 2002:a4a:ca85:0:b0:5a5:1645:90f4 with SMTP id x5-20020a4aca85000000b005a5164590f4mr622764ooq.1.1711058904602; Thu, 21 Mar 2024 15:08:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE4H/bBBZLy/REcdrgCXmaDlClCIl1Ey/SRhhRBkoU7cfytCZoQGoym/o3YpdiOnUWtv8D4UQ== X-Received: by 2002:a4a:ca85:0:b0:5a5:1645:90f4 with SMTP id x5-20020a4aca85000000b005a5164590f4mr622720ooq.1.1711058904123; Thu, 21 Mar 2024 15:08:24 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:23 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 10/12] mm/gup: Handle huge pmd for follow_pmd_mask() Date: Thu, 21 Mar 2024 18:08:00 -0400 Message-ID: <20240321220802.679544-11-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Replace pmd_trans_huge() with pmd_leaf() to also cover pmd_huge() as long as enabled. FOLL_TOUCH and FOLL_SPLIT_PMD only apply to THP, not yet huge. Since now follow_trans_huge_pmd() can process hugetlb pages, renaming it into follow_huge_pmd() to match what it does. Move it into gup.c so not depend on CONFIG_THP. When at it, move the ctx->page_mask setup into follow_huge_pmd(), only set it when the page is valid. It was not a bug to set it before even if GUP failed (page=3D=3DNULL), because follow_page_mask() callers always ignores page_mask if so. But doing so makes the code cleaner. Reviewed-by: Jason Gunthorpe Signed-off-by: Peter Xu --- mm/gup.c | 107 ++++++++++++++++++++++++++++++++++++++++++++--- mm/huge_memory.c | 86 +------------------------------------ mm/internal.h | 5 +-- 3 files changed, 105 insertions(+), 93 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index ae21afb9434e..00cdf4cb0cd4 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -580,6 +580,93 @@ static struct page *follow_huge_pud(struct vm_area_str= uct *vma, =20 return page; } + +/* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */ +static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page, + struct vm_area_struct *vma, + unsigned int flags) +{ + /* If the pmd is writable, we can write to the page. */ + if (pmd_write(pmd)) + return true; + + /* Maybe FOLL_FORCE is set to override it? */ + if (!(flags & FOLL_FORCE)) + return false; + + /* But FOLL_FORCE has no effect on shared mappings */ + if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED)) + return false; + + /* ... or read-only private ones */ + if (!(vma->vm_flags & VM_MAYWRITE)) + return false; + + /* ... or already writable ones that just need to take a write fault */ + if (vma->vm_flags & VM_WRITE) + return false; + + /* + * See can_change_pte_writable(): we broke COW and could map the page + * writable if we have an exclusive anonymous page ... + */ + if (!page || !PageAnon(page) || !PageAnonExclusive(page)) + return false; + + /* ... and a write-fault isn't required for other reasons. */ + if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd)) + return false; + return !userfaultfd_huge_pmd_wp(vma, pmd); +} + +static struct page *follow_huge_pmd(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmd, + unsigned int flags, + struct follow_page_context *ctx) +{ + struct mm_struct *mm =3D vma->vm_mm; + pmd_t pmdval =3D *pmd; + struct page *page; + int ret; + + assert_spin_locked(pmd_lockptr(mm, pmd)); + + page =3D pmd_page(pmdval); + VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page); + + if ((flags & FOLL_WRITE) && + !can_follow_write_pmd(pmdval, page, vma, flags)) + return NULL; + + /* Avoid dumping huge zero page */ + if ((flags & FOLL_DUMP) && is_huge_zero_pmd(pmdval)) + return ERR_PTR(-EFAULT); + + if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags)) + return NULL; + + if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, page)) + return ERR_PTR(-EMLINK); + + VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && + !PageAnonExclusive(page), page); + + ret =3D try_grab_page(page, flags); + if (ret) + return ERR_PTR(ret); + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (pmd_trans_huge(pmdval) && (flags & FOLL_TOUCH)) + touch_pmd(vma, addr, pmd, flags & FOLL_WRITE); +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + + page +=3D (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; + ctx->page_mask =3D HPAGE_PMD_NR - 1; + VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page); + + return page; +} + #else /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ static struct page *follow_huge_pud(struct vm_area_struct *vma, unsigned long addr, pud_t *pudp, @@ -587,6 +674,14 @@ static struct page *follow_huge_pud(struct vm_area_str= uct *vma, { return NULL; } + +static struct page *follow_huge_pmd(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmd, + unsigned int flags, + struct follow_page_context *ctx) +{ + return NULL; +} #endif /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ =20 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long addres= s, @@ -784,31 +879,31 @@ static struct page *follow_pmd_mask(struct vm_area_st= ruct *vma, return page; return no_page_table(vma, flags, address); } - if (likely(!pmd_trans_huge(pmdval))) + if (likely(!pmd_leaf(pmdval))) return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); =20 if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) return no_page_table(vma, flags, address); =20 ptl =3D pmd_lock(mm, pmd); - if (unlikely(!pmd_present(*pmd))) { + pmdval =3D *pmd; + if (unlikely(!pmd_present(pmdval))) { spin_unlock(ptl); return no_page_table(vma, flags, address); } - if (unlikely(!pmd_trans_huge(*pmd))) { + if (unlikely(!pmd_leaf(pmdval))) { spin_unlock(ptl); return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); } - if (flags & FOLL_SPLIT_PMD) { + if (pmd_trans_huge(pmdval) && (flags & FOLL_SPLIT_PMD)) { spin_unlock(ptl); split_huge_pmd(vma, pmd, address); /* If pmd was left empty, stuff a page table in there quickly */ return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) : follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); } - page =3D follow_trans_huge_pmd(vma, address, pmd, flags); + page =3D follow_huge_pmd(vma, address, pmd, flags, ctx); spin_unlock(ptl); - ctx->page_mask =3D HPAGE_PMD_NR - 1; return page; } =20 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f8bd2012bc27..e747dacb5051 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1206,8 +1206,8 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, p= fn_t pfn, bool write) EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud); #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ =20 -static void touch_pmd(struct vm_area_struct *vma, unsigned long addr, - pmd_t *pmd, bool write) +void touch_pmd(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmd, bool write) { pmd_t _pmd; =20 @@ -1562,88 +1562,6 @@ static inline bool can_change_pmd_writable(struct vm= _area_struct *vma, return pmd_dirty(pmd); } =20 -/* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */ -static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page, - struct vm_area_struct *vma, - unsigned int flags) -{ - /* If the pmd is writable, we can write to the page. */ - if (pmd_write(pmd)) - return true; - - /* Maybe FOLL_FORCE is set to override it? */ - if (!(flags & FOLL_FORCE)) - return false; - - /* But FOLL_FORCE has no effect on shared mappings */ - if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED)) - return false; - - /* ... or read-only private ones */ - if (!(vma->vm_flags & VM_MAYWRITE)) - return false; - - /* ... or already writable ones that just need to take a write fault */ - if (vma->vm_flags & VM_WRITE) - return false; - - /* - * See can_change_pte_writable(): we broke COW and could map the page - * writable if we have an exclusive anonymous page ... - */ - if (!page || !PageAnon(page) || !PageAnonExclusive(page)) - return false; - - /* ... and a write-fault isn't required for other reasons. */ - if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd)) - return false; - return !userfaultfd_huge_pmd_wp(vma, pmd); -} - -struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, - pmd_t *pmd, - unsigned int flags) -{ - struct mm_struct *mm =3D vma->vm_mm; - struct page *page; - int ret; - - assert_spin_locked(pmd_lockptr(mm, pmd)); - - page =3D pmd_page(*pmd); - VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page); - - if ((flags & FOLL_WRITE) && - !can_follow_write_pmd(*pmd, page, vma, flags)) - return NULL; - - /* Avoid dumping huge zero page */ - if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) - return ERR_PTR(-EFAULT); - - if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags)) - return NULL; - - if (!pmd_write(*pmd) && gup_must_unshare(vma, flags, page)) - return ERR_PTR(-EMLINK); - - VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && - !PageAnonExclusive(page), page); - - ret =3D try_grab_page(page, flags); - if (ret) - return ERR_PTR(ret); - - if (flags & FOLL_TOUCH) - touch_pmd(vma, addr, pmd, flags & FOLL_WRITE); - - page +=3D (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; - VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page); - - return page; -} - /* NUMA hinting page fault entry point for trans huge pmds */ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) { diff --git a/mm/internal.h b/mm/internal.h index 63e4f6e001be..d47862e6d968 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1104,9 +1104,8 @@ int __must_check try_grab_page(struct page *page, uns= igned int flags); */ void touch_pud(struct vm_area_struct *vma, unsigned long addr, pud_t *pud, bool write); -struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags); +void touch_pmd(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmd, bool write); =20 #ifdef CONFIG_MEMCG static inline --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 C080413792C for ; Thu, 21 Mar 2024 22:08:29 +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=1711058911; cv=none; b=F9hSEZjJNrQnROKAC1nCqfaW7dv+oJOtSTXrAdTs8WGQKo64gbXLKhoSg3KKcDRt4Mj8nl8mfV/FGZn6ea3lb5Qi+XPVjcPAdW3CBuHxvYhZ9WlUlltYaYDsYSmb64+wfFWOW0dVGLC3Q9FjEd+D8DmAxbGjG82NB26gXnPc+Qg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058911; c=relaxed/simple; bh=vIJxD1xmNJcziuLdkeQlq8aVMJXM76SBgb0FOm9yvPE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fjiNhPmeXj5c3pIkt2XPO62pAp4dl8bZdjtbt1/l5i9sGZJMvjOr6ZKGrzDwu2V+6YjGDyBrEEFUwI5nOezvRfhVrttpgF+3FRRCJfbge80NrNIxrvfowhqdwjv6wX4aS16nN/iE5XANy7cktUMWFyqzvXoFeCyP7xUY3gUsznI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=dIiA8dA1; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="dIiA8dA1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058909; 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=o0KbP6b9RlV02LlNjzHhHyMPZ1vhd/KMB6w1UdD4tH4=; b=dIiA8dA1EoTmPTTsoe2Log5FBWyUKBYLvL87GFBN9x5jG/K2G3vHoYtA0qYNKd9BZl90Bg msbFEhE8JwxYwXDIr0XESd9gyTgvvehJfyz2L+9+twEU2d9aK0yt5p1QqarGsJDH17s1pf 1DqzY+Kis0sUCINqiksQt1xN6a6aznc= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-644-reuvHv2pP4Khd5wKcVdyLg-1; Thu, 21 Mar 2024 18:08:27 -0400 X-MC-Unique: reuvHv2pP4Khd5wKcVdyLg-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-222b55202f4so525966fac.1 for ; Thu, 21 Mar 2024 15:08:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058907; x=1711663707; 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=o0KbP6b9RlV02LlNjzHhHyMPZ1vhd/KMB6w1UdD4tH4=; b=jC14gpS/CgPQNb5ChCgoU5iRpIVKetlfH/YJSN1XJ5JTkCHFR5BaGtgNyTRerqcp9H KafjlT2OknbA/cnorb7Y4pi8X1VP93f39adOqTRz5heP54l3TLJZlEBjsR+OOelLilna 2QvEIjZXmEfd9Zox1CGq0/h0+2yYhmF0LPL6u8F/khHDuVmTyERzoHpj9yRCCZDqnSaH VoWMLRUixOmgxRX78Tllbq+26JjAFckXtNxGqVqoTFa8sjUniFFf9pTs0lWqNRqyUANn fLXb/bUJbZVk1Yw6o4z0R8UCa+4/lh7u6N1fwadKTehivWNG0AdUuGOQOEwkXl7VlwQq 6X+w== X-Forwarded-Encrypted: i=1; AJvYcCX3Mv4nZaEhEml5vmXpRjaeqd4yqqjA7stnzckyi5RSSHEj9X+mPT1jN45Ts9/8aAQxy9yDOKweGSa4UT4/vWht6m0D1AOh9bVNTEb0 X-Gm-Message-State: AOJu0YzytB/rmE3EGBAsQi11Lv+X+4dD0yz75ssvAISB+RFbt5FXEwSZ 0Cf6CE+pCwPdex64sfCIeUbCgEAfkMk9fYaZnO+14PVWgHelnvdhZikBDFIMb1ZdgBoG5V/V3Xg zi/Mvvd+ii0eA07FsXX3Sv4VWcdgpckMjaXcUm4rJ2gY0MXSKW11+ltc6b33fNw== X-Received: by 2002:a05:6870:4189:b0:229:8575:875b with SMTP id y9-20020a056870418900b002298575875bmr520309oac.5.1711058906669; Thu, 21 Mar 2024 15:08:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHyRVGIH/+XEvquuQvEBSJicroe5v4UAFWGp7nk+sM4AQMJKL66g328LmVlireG12paxfzEyQ== X-Received: by 2002:a05:6870:4189:b0:229:8575:875b with SMTP id y9-20020a056870418900b002298575875bmr520279oac.5.1711058906111; Thu, 21 Mar 2024 15:08:26 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:25 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 11/12] mm/gup: Handle hugepd for follow_page() Date: Thu, 21 Mar 2024 18:08:01 -0400 Message-ID: <20240321220802.679544-12-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Hugepd is only used in PowerPC so far on 4K page size kernels where hash mmu is used. follow_page_mask() used to leverage hugetlb APIs to access hugepd entries. Teach follow_page_mask() itself on hugepd. With previous refactors on fast-gup gup_huge_pd(), most of the code can be easily leveraged. There's something not needed for follow page, for example, gup_hugepte() tries to detect pgtable entry change which will never happen with slow gup (which has the pgtable lock held), but that's not a problem to check. Since follow_page() always only fetch one page, set the end to "address + PAGE_SIZE" should suffice. We will still do the pgtable walk once for each hugetlb page by setting ctx->page_mask properly. One thing worth mentioning is that some level of pgtable's _bad() helper will report is_hugepd() entries as TRUE on Power8 hash MMUs. I think it at least applies to PUD on Power8 with 4K pgsize. It means feeding a hugepd entry to pud_bad() will report a false positive. Let's leave that for now because it can be arch-specific where I am a bit declined to touch. In this patch it's not a problem as long as hugepd is detected before any bad pgtable entries. Signed-off-by: Peter Xu --- mm/gup.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 7 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 00cdf4cb0cd4..43a2e0a203cd 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -30,6 +30,11 @@ struct follow_page_context { unsigned int page_mask; }; =20 +static struct page *follow_hugepd(struct vm_area_struct *vma, hugepd_t hug= epd, + unsigned long addr, unsigned int pdshift, + unsigned int flags, + struct follow_page_context *ctx); + static inline void sanity_check_pinned_pages(struct page **pages, unsigned long npages) { @@ -871,6 +876,9 @@ static struct page *follow_pmd_mask(struct vm_area_stru= ct *vma, return no_page_table(vma, flags, address); if (!pmd_present(pmdval)) return no_page_table(vma, flags, address); + if (unlikely(is_hugepd(__hugepd(pmd_val(pmdval))))) + return follow_hugepd(vma, __hugepd(pmd_val(pmdval)), + address, PMD_SHIFT, flags, ctx); if (pmd_devmap(pmdval)) { ptl =3D pmd_lock(mm, pmd); page =3D follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); @@ -921,6 +929,9 @@ static struct page *follow_pud_mask(struct vm_area_stru= ct *vma, pud =3D READ_ONCE(*pudp); if (!pud_present(pud)) return no_page_table(vma, flags, address); + if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) + return follow_hugepd(vma, __hugepd(pud_val(pud)), + address, PUD_SHIFT, flags, ctx); if (pud_leaf(pud)) { ptl =3D pud_lock(mm, pudp); page =3D follow_huge_pud(vma, address, pudp, flags, ctx); @@ -944,10 +955,13 @@ static struct page *follow_p4d_mask(struct vm_area_st= ruct *vma, =20 p4dp =3D p4d_offset(pgdp, address); p4d =3D READ_ONCE(*p4dp); - if (!p4d_present(p4d)) - return no_page_table(vma, flags, address); BUILD_BUG_ON(p4d_leaf(p4d)); - if (unlikely(p4d_bad(p4d))) + + if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) + return follow_hugepd(vma, __hugepd(p4d_val(p4d)), + address, P4D_SHIFT, flags, ctx); + + if (!p4d_present(p4d) || p4d_bad(p4d)) return no_page_table(vma, flags, address); =20 return follow_pud_mask(vma, address, p4dp, flags, ctx); @@ -981,7 +995,7 @@ static struct page *follow_page_mask(struct vm_area_str= uct *vma, unsigned long address, unsigned int flags, struct follow_page_context *ctx) { - pgd_t *pgd; + pgd_t *pgd, pgdval; struct mm_struct *mm =3D vma->vm_mm; =20 ctx->page_mask =3D 0; @@ -996,11 +1010,17 @@ static struct page *follow_page_mask(struct vm_area_= struct *vma, &ctx->page_mask); =20 pgd =3D pgd_offset(mm, address); + pgdval =3D *pgd; =20 - if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) - return no_page_table(vma, flags, address); + if (unlikely(is_hugepd(__hugepd(pgd_val(pgdval))))) + page =3D follow_hugepd(vma, __hugepd(pgd_val(pgdval)), + address, PGDIR_SHIFT, flags, ctx); + else if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) + page =3D no_page_table(vma, flags, address); + else + page =3D follow_p4d_mask(vma, address, pgd, flags, ctx); =20 - return follow_p4d_mask(vma, address, pgd, flags, ctx); + return page; } =20 struct page *follow_page(struct vm_area_struct *vma, unsigned long address, @@ -3037,6 +3057,37 @@ static int gup_huge_pd(hugepd_t hugepd, unsigned lon= g addr, =20 return 1; } + +static struct page *follow_hugepd(struct vm_area_struct *vma, hugepd_t hug= epd, + unsigned long addr, unsigned int pdshift, + unsigned int flags, + struct follow_page_context *ctx) +{ + struct page *page; + struct hstate *h; + spinlock_t *ptl; + int nr =3D 0, ret; + pte_t *ptep; + + /* Only hugetlb supports hugepd */ + if (WARN_ON_ONCE(!is_vm_hugetlb_page(vma))) + return ERR_PTR(-EFAULT); + + h =3D hstate_vma(vma); + ptep =3D hugepte_offset(hugepd, addr, pdshift); + ptl =3D huge_pte_lock(h, vma->vm_mm, ptep); + ret =3D gup_huge_pd(hugepd, addr, pdshift, addr + PAGE_SIZE, + flags, &page, &nr); + spin_unlock(ptl); + + if (ret) { + WARN_ON_ONCE(nr !=3D 1); + ctx->page_mask =3D (1U << huge_page_order(h)) - 1; + return page; + } + + return NULL; +} #else static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned int pdshift, unsigned long end, unsigned int flags, @@ -3044,6 +3095,14 @@ static inline int gup_huge_pd(hugepd_t hugepd, unsig= ned long addr, { return 0; } + +static struct page *follow_hugepd(struct vm_area_struct *vma, hugepd_t hug= epd, + unsigned long addr, unsigned int pdshift, + unsigned int flags, + struct follow_page_context *ctx) +{ + return NULL; +} #endif /* CONFIG_ARCH_HAS_HUGEPD */ =20 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, --=20 2.44.0 From nobody Sat Feb 7 19:45:11 2026 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 722041386A9 for ; Thu, 21 Mar 2024 22:08:34 +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=1711058916; cv=none; b=d0DJATnfHL+Mzoitbzfq53eq1qUOEz5fVgRfCnh64imBx2r9JzEV4lv6K4jsKu/WBxojmOyKg26axEpCNyMCn1heBBWjy8XJrztuZr+DvQvzWwwrf5RFwvFxDao/L+ofrfPZj7GZq3yQZtuAJ8BQHMwchFqRymjcmQdqql3hkDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058916; c=relaxed/simple; bh=JCGnxD1BohZitMTzx7yYKiVmGWHn7FCotr9St3beHS4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eWFzVGoDRn9+BjDLZEtBitiLfuosAIoIDodIJFP9d0p26LwRisOJZtC2ev14Mf52hvz99ba5tQEzWcr5eHo4PAMc8rS1Ze97VwMog1OuLc7YynBB3gHDzlYYpOXcyGOJR6FTebgjujEI1oYsfwEgOlKvA5/I6uk9pK2ozQOjiJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=Jd6/fIsO; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="Jd6/fIsO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058913; 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=kKAkS039JEm9eSlNjnxGInSB8mOTV3IHPzDUFjGHTVk=; b=Jd6/fIsOSh/xM+NSmFc9k2PILZ0AQz0Rd+zhfRve2OVQNF1hBiKkDHVnbfdzmTcjCHDTus rXENBlpcxPxVHg04MetFWzcJfZh0uS/QyiE+mJcLWeumw/kUqaZcu7p2cZDnFnEHFmy5tP wBEdePRwCW9PlssxMQNLWcCz/UcowjI= Received: from mail-oa1-f72.google.com (mail-oa1-f72.google.com [209.85.160.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-85-Y7qGGGu6OF29F7vdo4oEZQ-1; Thu, 21 Mar 2024 18:08:32 -0400 X-MC-Unique: Y7qGGGu6OF29F7vdo4oEZQ-1 Received: by mail-oa1-f72.google.com with SMTP id 586e51a60fabf-221a566a435so551712fac.0 for ; Thu, 21 Mar 2024 15:08:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711058909; x=1711663709; 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=kKAkS039JEm9eSlNjnxGInSB8mOTV3IHPzDUFjGHTVk=; b=ds3YpMiROOfyDGcH8OjK39Wq71GWmyQ8oKsC/DXhy3bqnTJaDZY4LzxTjknhDnF4Sj r76xNvGglrWCnY7IUsi5iMid0kyc5Xz/XBfP0cUvlZQuUCJm+66FPrPSH7fztPSJT2fF 1qH3ntAY0u94OlPq6lZDho+lT+r2rt5tU9f3BMbWZvXA9gk9DXxzDTDkvxm6motDm7kG na4DoW+nYqIMmpM6nhfb/tfUmsk6QOFZ8dyHJczQjPgYNTLooOYPqre0/WuGdmDz1gQB FZvDSzUn7388qhYqM++yT3jzrPWJVJ8USOPst0XRqBFIsDoLedYQPGDQ58kEuv/E6iOF qnVQ== X-Forwarded-Encrypted: i=1; AJvYcCW6VYRJfIs61YeYBrIa9H7lcr4M5MMmnivg4hUKHyWI5JO+Tx9/v2qphpF/IjbxTN3ROVV8zkq0lSqH2Dd39tA26Oa3NF5v5WORXZWL X-Gm-Message-State: AOJu0YxFVZZP2heZl28wiEu0oszdGgbMf7WookMMM45bdAvUt/FQMuRS XOBuDCutHmMqytDNtpY9Mlz1SJ42uTI+c7BztqZwp1Qob89oQ5yyEW+KK1bkAHfxLya/I53Dmun carcUk5YtfKGb4Bxkl4G3xJ37KgMXYifZVPcpowyb9erGfpCncIvYGhepB7iVPw== X-Received: by 2002:a4a:d5ce:0:b0:5a2:26c7:397b with SMTP id a14-20020a4ad5ce000000b005a226c7397bmr912023oot.0.1711058909583; Thu, 21 Mar 2024 15:08:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7bJa0sTEC4z3o8Ei9oO8UNdi6+GHr5ekBXamqtFS26QUeqg4nthASLLtCNNWPAVCCH5AJXA== X-Received: by 2002:a4a:d5ce:0:b0:5a2:26c7:397b with SMTP id a14-20020a4ad5ce000000b005a226c7397bmr911987oot.0.1711058909121; Thu, 21 Mar 2024 15:08:29 -0700 (PDT) Received: from x1n.redhat.com ([99.254.121.117]) by smtp.gmail.com with ESMTPSA id o6-20020a0562140e4600b00690baf5cde9sm351663qvc.118.2024.03.21.15.08.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:08:28 -0700 (PDT) From: peterx@redhat.com To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Christophe Leroy , Matthew Wilcox , Rik van Riel , Lorenzo Stoakes , Axel Rasmussen , peterx@redhat.com, Yang Shi , John Hubbard , linux-arm-kernel@lists.infradead.org, "Kirill A . Shutemov" , Andrew Jones , Vlastimil Babka , Mike Rapoport , Andrew Morton , Muchun Song , Christoph Hellwig , linux-riscv@lists.infradead.org, James Houghton , David Hildenbrand , Jason Gunthorpe , Andrea Arcangeli , "Aneesh Kumar K . V" , Mike Kravetz Subject: [PATCH v3 12/12] mm/gup: Handle hugetlb in the generic follow_page_mask code Date: Thu, 21 Mar 2024 18:08:02 -0400 Message-ID: <20240321220802.679544-13-peterx@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240321220802.679544-1-peterx@redhat.com> References: <20240321220802.679544-1-peterx@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" From: Peter Xu Now follow_page() is ready to handle hugetlb pages in whatever form, and over all architectures. Switch to the generic code path. Time to retire hugetlb_follow_page_mask(), following the previous retirement of follow_hugetlb_page() in 4849807114b8. There may be a slight difference of how the loops run when processing slow GUP over a large hugetlb range on cont_pte/cont_pmd supported archs: each loop of __get_user_pages() will resolve one pgtable entry with the patch applied, rather than relying on the size of hugetlb hstate, the latter may cover multiple entries in one loop. A quick performance test on an aarch64 VM on M1 chip shows 15% degrade over a tight loop of slow gup after the path switched. That shouldn't be a problem because slow-gup should not be a hot path for GUP in general: when page is commonly present, fast-gup will already succeed, while when the page is indeed missing and require a follow up page fault, the slow gup degrade will probably buried in the fault paths anyway. It also explains why slow gup for THP used to be very slow before 57edfcfd3419 ("mm/gup: accelerate thp gup even for "pages !=3D NULL"") lands, the latter not part = of a performance analysis but a side benefit. If the performance will be a concern, we can consider handle CONT_PTE in follow_page(). Before that is justified to be necessary, keep everything clean and simple. Signed-off-by: Peter Xu Reviewed-by: Jason Gunthorpe --- include/linux/hugetlb.h | 7 ---- mm/gup.c | 15 +++------ mm/hugetlb.c | 71 ----------------------------------------- 3 files changed, 5 insertions(+), 88 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 52d9efcf1edf..85e1c9931ae5 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -328,13 +328,6 @@ static inline void hugetlb_zap_end( { } =20 -static inline struct page *hugetlb_follow_page_mask( - struct vm_area_struct *vma, unsigned long address, unsigned int flags, - unsigned int *page_mask) -{ - BUILD_BUG(); /* should never be compiled in if !CONFIG_HUGETLB_PAGE*/ -} - static inline int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *dst_vma, diff --git a/mm/gup.c b/mm/gup.c index 43a2e0a203cd..2eb5911ba849 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -997,18 +997,11 @@ static struct page *follow_page_mask(struct vm_area_s= truct *vma, { pgd_t *pgd, pgdval; struct mm_struct *mm =3D vma->vm_mm; + struct page *page; =20 - ctx->page_mask =3D 0; - - /* - * Call hugetlb_follow_page_mask for hugetlb vmas as it will use - * special hugetlb page table walking code. This eliminates the - * need to check for hugetlb entries in the general walking code. - */ - if (is_vm_hugetlb_page(vma)) - return hugetlb_follow_page_mask(vma, address, flags, - &ctx->page_mask); + vma_pgtable_walk_begin(vma); =20 + ctx->page_mask =3D 0; pgd =3D pgd_offset(mm, address); pgdval =3D *pgd; =20 @@ -1020,6 +1013,8 @@ static struct page *follow_page_mask(struct vm_area_s= truct *vma, else page =3D follow_p4d_mask(vma, address, pgd, flags, ctx); =20 + vma_pgtable_walk_end(vma); + return page; } =20 diff --git a/mm/hugetlb.c b/mm/hugetlb.c index abec04575c89..2e320757501b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6883,77 +6883,6 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, } #endif /* CONFIG_USERFAULTFD */ =20 -struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma, - unsigned long address, unsigned int flags, - unsigned int *page_mask) -{ - struct hstate *h =3D hstate_vma(vma); - struct mm_struct *mm =3D vma->vm_mm; - unsigned long haddr =3D address & huge_page_mask(h); - struct page *page =3D NULL; - spinlock_t *ptl; - pte_t *pte, entry; - int ret; - - hugetlb_vma_lock_read(vma); - pte =3D hugetlb_walk(vma, haddr, huge_page_size(h)); - if (!pte) - goto out_unlock; - - ptl =3D huge_pte_lock(h, mm, pte); - entry =3D huge_ptep_get(pte); - if (pte_present(entry)) { - page =3D pte_page(entry); - - if (!huge_pte_write(entry)) { - if (flags & FOLL_WRITE) { - page =3D NULL; - goto out; - } - - if (gup_must_unshare(vma, flags, page)) { - /* Tell the caller to do unsharing */ - page =3D ERR_PTR(-EMLINK); - goto out; - } - } - - page =3D nth_page(page, ((address & ~huge_page_mask(h)) >> PAGE_SHIFT)); - - /* - * Note that page may be a sub-page, and with vmemmap - * optimizations the page struct may be read only. - * try_grab_page() will increase the ref count on the - * head page, so this will be OK. - * - * try_grab_page() should always be able to get the page here, - * because we hold the ptl lock and have verified pte_present(). - */ - ret =3D try_grab_page(page, flags); - - if (WARN_ON_ONCE(ret)) { - page =3D ERR_PTR(ret); - goto out; - } - - *page_mask =3D (1U << huge_page_order(h)) - 1; - } -out: - spin_unlock(ptl); -out_unlock: - hugetlb_vma_unlock_read(vma); - - /* - * Fixup retval for dump requests: if pagecache doesn't exist, - * don't try to allocate a new page but just skip it. - */ - if (!page && (flags & FOLL_DUMP) && - !hugetlbfs_pagecache_present(h, vma, address)) - page =3D ERR_PTR(-EFAULT); - - return page; -} - long hugetlb_change_protection(struct vm_area_struct *vma, unsigned long address, unsigned long end, pgprot_t newprot, unsigned long cp_flags) --=20 2.44.0