From patchwork Thu Nov 16 01:29:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13457484 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 2B520C072A2 for ; Thu, 16 Nov 2023 01:29:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E02D96B03ED; Wed, 15 Nov 2023 20:29:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DB3916B03EF; Wed, 15 Nov 2023 20:29:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C05BF6B03F0; Wed, 15 Nov 2023 20:29:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A861F6B03ED for ; Wed, 15 Nov 2023 20:29:32 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 89EDC1CACBB for ; Thu, 16 Nov 2023 01:29:32 +0000 (UTC) X-FDA: 81462084984.12.B72DEFB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 77C168000B for ; Thu, 16 Nov 2023 01:29:30 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IdcAM6XS; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700098170; a=rsa-sha256; cv=none; b=Jq8lwPJxgi4gT2NR+Wv9PoVVwDcxUbOsau6t8c2xEKa06j9BakH2TTy7RWgyJonZKoXn9I jZ2CnUhmDC2I/1Zd0yedsyZjTG5hZBRqMWGpKcZmFF+dod9v1/XBWmAwrrZzX503sLWJZ0 6YFM14p1T4W70ZGp7BB2glRWLJiNYmU= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IdcAM6XS; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700098170; 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=KEE21en6sv5Z4IJFnP9A/1kLw7BwwEfESnJrJ/3CqcU=; b=g50iO3OhW38skWOPguqZjhuiE/gHew2t5HAjYQ1hJQUGf/yqX7UwiYunN557LSv2h2UC7S wkbKjLs5nuCtayN3nG6rm489qC2s7EaCoNdj+61EB0NlLmtFleIuYmhvLQcSpgWppVngQp /cHvRkVOqWx+8yi7DFeEL6nr8cwwS7A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700098169; 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=KEE21en6sv5Z4IJFnP9A/1kLw7BwwEfESnJrJ/3CqcU=; b=IdcAM6XSAAXf9GmhU2ShqxdgWgHYGdhPdzRBZue366+0/HHABAW3e4WXiZtrg2P1Gyg25S 8KjwlII7f2TjBCRUOY9CeaUGjZ4KarOQ9MF7RHqPtbH6uX05f52QcZDRx1yGJvV7xQNkba gA0ln+k6BUTmOYNqUtYrYnDgcZwippo= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-568-DxV7d27JOwGgcUg1kkMqPg-1; Wed, 15 Nov 2023 20:29:28 -0500 X-MC-Unique: DxV7d27JOwGgcUg1kkMqPg-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-420c7034fddso916111cf.0 for ; Wed, 15 Nov 2023 17:29:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700098168; x=1700702968; 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=KEE21en6sv5Z4IJFnP9A/1kLw7BwwEfESnJrJ/3CqcU=; b=lTcPHdDvn4xWvtXIcYczOWWDMX7FImnA4W6LVx3NBQjYrLoaXTaZHV29cZ2R1c5mR7 B6t1wSPeXj7S0iUNpYsxTsgXR3H57QfOETuFNH783flSUY7katHnFa/BCHU7THnYQd89 JxLXrsuSRz3ZuYQtJX0ZrjCsTND7zQ9xEeThJJPJJeIMf5xPpWIUo2mTXKVpV0IkdIQ5 XAZvVDv8HgIv46mjOLbfr7t9nhn5xDIbDuePTNKpG0ygC+8pbruiuaQgxEpGgzi8CZpq mcKbBgy0+cic12bDg6aBFyuMYHlm4egvXW0o6DSGUS5RznlBJPVRue5pIENXusCys2Gm t4sw== X-Gm-Message-State: AOJu0YxD3Ill8YFgqS7JZ8G1CulhWv6OPHycorD90hFyomtlbC7ctGS8 m+WYugGKprCdO3SITGN1BZTr8p3FuZZ1O5tZo9Zr0HeS2txRs6Ss9hgMNdc1fKZUsgOacxgW30l xgP1RIllv2Y0= X-Received: by 2002:ac8:5254:0:b0:421:c7ad:bb08 with SMTP id y20-20020ac85254000000b00421c7adbb08mr8123201qtn.6.1700098167769; Wed, 15 Nov 2023 17:29:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0pY05bEW1kuLTvTDTDFSfvrB9/wLj2Toyy995bIe7hSkDprXdYQMw37m3Xz3vWwWvq8gVXw== X-Received: by 2002:ac8:5254:0:b0:421:c7ad:bb08 with SMTP id y20-20020ac85254000000b00421c7adbb08mr8123183qtn.6.1700098167489; Wed, 15 Nov 2023 17:29:27 -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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 17:29:27 -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 10/12] mm/gup: Handle huge pmd for follow_pmd_mask() Date: Wed, 15 Nov 2023 20:29:06 -0500 Message-ID: <20231116012908.392077-11-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-Server: rspam08 X-Rspamd-Queue-Id: 77C168000B X-Stat-Signature: 1sukcss5y1hj4qnmea8cxgzp496nmnjb X-Rspam-User: X-HE-Tag: 1700098170-225130 X-HE-Meta: U2FsdGVkX1/fig331/bMlWb+KlDnCv2hhQZc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y/0eIrSTKGRWPVeX2U1dowffS8Twt140W49BDAHvABDxGWUtghwOhDlwi+Lv4hPy9dn3pFUJMYYjS/q5PcxHtYmNqenB5g799ZbZgaVzMQewZNCUWiJaszMR5S/GbWh8RtfM7rU1rMKJ/4Fb0rPdwbiO0favE0h2JLJGvEjtgyk+WM09XbtUaPvuTuRYxedFQRgdHe8Q8MGqqQlnu8h8Lbxn2IigMEOrxiq5szi4bIhuPBS0ifvFW9J+kdCgKrS/uS5UU/mjwn2CfDZfwEHcuICSCs12IspGokmrxgAiFTIqA1U8QeU3CBzpztTb1xRHmeA== 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: Do proper replacement of pmd_trans_huge() by using pmd_thp_or_huge() 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 handle hugetlb pages, renaming it into follow_huge_pmd() to match what it does. 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==NULL), because follow_page_mask() callers always ignores page_mask if so. But doing so makes the code cleaner. Signed-off-by: Peter Xu --- mm/gup.c | 12 ++++++------ mm/huge_memory.c | 19 ++++++++++--------- mm/internal.h | 6 +++--- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 55a2ae55f00f..7c210206470f 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -713,31 +713,31 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma, spin_unlock(ptl); return page; } - if (likely(!pmd_trans_huge(pmdval))) + if (likely(!pmd_thp_or_huge(pmdval))) return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) return no_page_table(vma, flags, address); ptl = pmd_lock(mm, pmd); - if (unlikely(!pmd_present(*pmd))) { + pmdval = *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_thp_or_huge(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 = follow_trans_huge_pmd(vma, address, pmd, flags); + page = follow_huge_pmd(vma, address, pmd, flags, ctx); spin_unlock(ptl); - ctx->page_mask = HPAGE_PMD_NR - 1; return page; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6748ef5f3fd9..43fb81218c5e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1486,32 +1486,32 @@ static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page, 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 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 = vma->vm_mm; + pmd_t pmdval = *pmd; struct page *page; int ret; assert_spin_locked(pmd_lockptr(mm, pmd)); - page = pmd_page(*pmd); + page = pmd_page(pmdval); VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page); if ((flags & FOLL_WRITE) && - !can_follow_write_pmd(*pmd, page, vma, flags)) + !can_follow_write_pmd(pmdval, page, vma, flags)) return NULL; /* Avoid dumping huge zero page */ - if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) + 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(*pmd) && gup_must_unshare(vma, flags, page)) + if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, page)) return ERR_PTR(-EMLINK); VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && @@ -1521,10 +1521,11 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, if (ret) return ERR_PTR(ret); - if (flags & FOLL_TOUCH) + if (pmd_trans_huge(pmdval) && (flags & FOLL_TOUCH)) touch_pmd(vma, addr, pmd, flags & FOLL_WRITE); page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; + ctx->page_mask = HPAGE_PMD_NR - 1; VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page); return page; diff --git a/mm/internal.h b/mm/internal.h index 8450562744cf..bf0dc896c274 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1007,9 +1007,9 @@ int __must_check try_grab_page(struct page *page, unsigned int flags); /* * mm/huge_memory.c */ -struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags); +struct page *follow_huge_pmd(struct vm_area_struct *vma, unsigned long addr, + pmd_t *pmd, unsigned int flags, + struct follow_page_context *ctx); /* * mm/mmap.c