From patchwork Thu Nov 16 01:29:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13457483 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 430C2C2BB3F for ; Thu, 16 Nov 2023 01:29:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CB8E6B03EB; Wed, 15 Nov 2023 20:29:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 754856B03ED; Wed, 15 Nov 2023 20:29:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 499236B03EE; Wed, 15 Nov 2023 20:29:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2E88D6B03EB for ; Wed, 15 Nov 2023 20:29:31 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0CCBE160213 for ; Thu, 16 Nov 2023 01:29:31 +0000 (UTC) X-FDA: 81462084942.18.D696E7E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf22.hostedemail.com (Postfix) with ESMTP id EBD17C000F for ; Thu, 16 Nov 2023 01:29:28 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Sp5+Rq+g; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf22.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700098169; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gQy6k5CEHGKiOJI0ugKFVUkk8XVFLkY9zLGCNjIpOps=; b=33zwD4J2Q5ZxEay49i8aNUVovy+jKiaF+VBGJLpsQy+4bwyobpk5oiTEIOssoVSICwRVvp PsCoioXk1jpk6j6P3gIPx89ouSlWUh/BZMGrdtRfmdF6uN4FKhXNTRHJOr9LS+JslfgTS+ e+kPucAp5EADrEGeDHAWD1CYH4mVGnI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Sp5+Rq+g; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf22.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700098169; a=rsa-sha256; cv=none; b=ArFbVJHoWGoDfpcI9VWcrhhnlnreo58MWArUdZRxRZhbJhQXbtS46RuPXmR7cQdXleuoF9 +7xjXze4EAcqrCilVnG1aROxg+cdi0wNSBufBoSslbX/kQd1n4AQ3CqIv7yX5PHiB0xDVU NWQFPb+Br0/JDSKMrcev98duVEu2a3o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700098168; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gQy6k5CEHGKiOJI0ugKFVUkk8XVFLkY9zLGCNjIpOps=; b=Sp5+Rq+gsh9x5CH4Pj6Cbe5AD2W5jrFJ8Sb477unfTkNOPMFU+rIvyaLr7zdCu0db++s3i laG7QpqJf1uzoxb6oyngln0N9Sq9bTW4zc8O6OrEPXk1P404LkwrephDrGeM1CP237+AmB QXMeGFnjKsf46GQXSRcLneurnAO64DE= Received: from mail-vk1-f199.google.com (mail-vk1-f199.google.com [209.85.221.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-45-wPQuVHxbPyWPY-sZWgXHqA-1; Wed, 15 Nov 2023 20:29:26 -0500 X-MC-Unique: wPQuVHxbPyWPY-sZWgXHqA-1 Received: by mail-vk1-f199.google.com with SMTP id 71dfb90a1353d-4abee584390so39591e0c.1 for ; Wed, 15 Nov 2023 17:29:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700098166; x=1700702966; 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=gQy6k5CEHGKiOJI0ugKFVUkk8XVFLkY9zLGCNjIpOps=; b=Gxw+uWVMabo3FoWhX2jJkLSKausQKwqk1mJxZlKKWWPec+vEIo6p5cZGBsuV/U6qVW WnP87npltmTCNeAZNcI2JogVchzFZHLO7VgVkw7QrLjM4bNjDc1cD6Dh+ihLqeGUfCLZ 2sQ7rNepde+/7zjUahqrMUl2IhyC7D9a4ze8KpQgio2WXYFORKNKfglcSQApqvJ0Dbc2 zDXetvwTLpCCpe+0ZnMalTZa7GPGBzdxMJIJI4zRiGaI+IlgG11Gp270D3fKKAMrqQGI rEMydfsXgjfCF2EzohlfbJNVqJP9e0DGLvg4EAYdn+Ep9Ce0mrlkwqwm1cIdnTAQlFWL iA5g== X-Gm-Message-State: AOJu0YyaPhH3vsMYQINXNn88nQ7DZeHUleYP9cKa3Nhqr/4tRzwc+ItN CMqC0U6KY0TrqzSK6Lgo2xPzseCcQH/NS8DwUv+SE1HDyo5nkceCyvULHgwWprFkL9jVMVCsbjo M3gyhQdEauTk= X-Received: by 2002:a05:6122:1484:b0:4ab:f831:98d4 with SMTP id z4-20020a056122148400b004abf83198d4mr7555123vkp.0.1700098166248; Wed, 15 Nov 2023 17:29:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHNrGR90/jKzfekxV/YscaCCcqT0aN5ZcRXVaKloa9cNGZ2HnRMduZYMn8+Y4FcR5oknUprWw== X-Received: by 2002:a05:6122:1484:b0:4ab:f831:98d4 with SMTP id z4-20020a056122148400b004abf83198d4mr7555100vkp.0.1700098165901; Wed, 15 Nov 2023 17:29:25 -0800 (PST) Received: from x1n.redhat.com (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id c24-20020ac85198000000b0041e383d527esm3922598qtn.66.2023.11.15.17.29.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 17:29:25 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , "Kirill A . Shutemov" , Lorenzo Stoakes , Axel Rasmussen , Matthew Wilcox , John Hubbard , Mike Rapoport , peterx@redhat.com, Hugh Dickins , David Hildenbrand , Andrea Arcangeli , Rik van Riel , James Houghton , Yang Shi , Jason Gunthorpe , Vlastimil Babka , Andrew Morton Subject: [PATCH RFC 09/12] mm/gup: Handle huge pud for follow_pud_mask() Date: Wed, 15 Nov 2023 20:29:05 -0500 Message-ID: <20231116012908.392077-10-peterx@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231116012908.392077-1-peterx@redhat.com> References: <20231116012908.392077-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: EBD17C000F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: gsbrfbh3sjek1yh1zdmp7xhdiicpi9ij X-HE-Tag: 1700098168-452744 X-HE-Meta: U2FsdGVkX18kOsDhDdcFyM0k8G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5XOEmztxUw6EClgXpUpj18kY2zL9TgBCI6G8iGamm3nqI9NkBqQq+XTN7n9Te2ucBne/YRi1h+tUwLeuy0q4dQtVUqm1gafUkyHX2dehfOI/A80qqKY9O3wZFGOmgBGngBVknPL4QKMDgj6AnznBPKKzquMMHue4gjLq2/qzC9Ik2+dd/WYM6MooqKeSlGTyjk0cihY135x6DMKVWIIfKIk7AM4JaNEssiu4zpSmdMqEi6sKoiqWEexeQLtCpBzsjwLD7ioIMOnsdv8DQh1nGLwF+LU7Eee3TaGo3jW4KhoVwReelnx49AqwuHJd6SnUfDWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Teach follow_pud_mask() to be able to handle normal PUD pages like hugetlb. Rename follow_devmap_pud() to follow_huge_pud(), move it out of config CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD, instead let that macro only covers the devmap special operations, like pgmap. In the new follow_huge_pud(), taking care of possible CoR for hugetlb if necessary. 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. We need to export "struct follow_page_context" along the way, so that huge_memory.c can understand it. One trivial more 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 should also be able to optimize devmap GUP on PUD to be able to jump over the whole PUD range, but not yet verified. Signed-off-by: Peter Xu --- include/linux/huge_mm.h | 17 +++---- mm/gup.c | 22 ++++----- mm/huge_memory.c | 98 +++++++++++++++++++++++------------------ 3 files changed, 73 insertions(+), 64 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index ec463410aecc..84815012d3cf 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -7,6 +7,11 @@ #include /* only for vma_is_dax() */ +struct follow_page_context { + struct dev_pagemap *pgmap; + unsigned int page_mask; +}; + vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf); int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, @@ -222,8 +227,6 @@ static inline bool folio_test_pmd_mappable(struct folio *folio) struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap); -struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr, - pud_t *pud, int flags, struct dev_pagemap **pgmap); vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); @@ -372,18 +375,16 @@ static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma, return NULL; } -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; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +struct page *follow_huge_pud(struct vm_area_struct *vma, unsigned long addr, + pud_t *pud, int flags, + struct follow_page_context *ctx); + static inline int split_folio_to_list(struct folio *folio, struct list_head *list) { diff --git a/mm/gup.c b/mm/gup.c index 89c1584d68f0..55a2ae55f00f 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -25,11 +25,6 @@ #include "internal.h" -struct follow_page_context { - struct dev_pagemap *pgmap; - unsigned int page_mask; -}; - static inline void sanity_check_pinned_pages(struct page **pages, unsigned long npages) { @@ -751,24 +746,25 @@ static struct page *follow_pud_mask(struct vm_area_struct *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 = vma->vm_mm; - pud = pud_offset(p4dp, address); - if (pud_none(*pud)) + pudp = pud_offset(p4dp, address); + pud = *pudp; + if (pud_none(pud) || !pud_present(pud)) return no_page_table(vma, flags, address); - if (pud_devmap(*pud)) { - ptl = pud_lock(mm, pud); - page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); + if (pud_huge(pud)) { + ptl = pud_lock(mm, pudp); + page = follow_huge_pud(vma, address, pudp, flags, ctx); spin_unlock(ptl); return page; } - if (unlikely(pud_bad(*pud))) + if (unlikely(pud_bad(pud))) return no_page_table(vma, flags, address); - return follow_pmd_mask(vma, address, pud, flags, ctx); + return follow_pmd_mask(vma, address, pudp, flags, ctx); } static struct page *follow_p4d_mask(struct vm_area_struct *vma, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6eb55f97a3d2..6748ef5f3fd9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1207,49 +1207,6 @@ static void touch_pud(struct vm_area_struct *vma, unsigned long addr, update_mmu_cache_pud(vma, addr, pud); } -struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr, - pud_t *pud, int flags, struct dev_pagemap **pgmap) -{ - unsigned long pfn = pud_pfn(*pud); - struct mm_struct *mm = 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 += (addr & ~PUD_MASK) >> PAGE_SHIFT; - *pgmap = get_dev_pagemap(pfn, *pgmap); - if (!*pgmap) - return ERR_PTR(-EFAULT); - page = pfn_to_page(pfn); - - ret = try_grab_page(page, flags); - if (ret) - page = 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) @@ -1305,6 +1262,61 @@ void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) } #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ +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 = vma->vm_mm; + struct page *page; + pud_t pud = *pudp; + unsigned long pfn = 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 += (addr & ~PUD_MASK) >> PAGE_SHIFT; + +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD + if (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 = get_dev_pagemap(pfn, ctx->pgmap); + if (!ctx->pgmap) + return ERR_PTR(-EFAULT); + } +#endif + page = pfn_to_page(pfn); + + if (!pud_devmap(pud) && !pud_write(pud) && + gup_must_unshare(vma, flags, page)) + return ERR_PTR(-EMLINK); + + ret = try_grab_page(page, flags); + if (ret) + page = ERR_PTR(ret); + else + ctx->page_mask = HPAGE_PUD_NR - 1; + + return page; +} + void huge_pmd_set_accessed(struct vm_fault *vmf) { bool write = vmf->flags & FAULT_FLAG_WRITE;