From patchwork Tue Mar 14 13:13:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13174411 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 168FBC6FD1D for ; Tue, 14 Mar 2023 13:15:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E81888E0001; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E30F76B0075; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF9628E0001; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) 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 AA8006B0074 for ; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4E5E6A0BFD for ; Tue, 14 Mar 2023 13:15:38 +0000 (UTC) X-FDA: 80567550756.25.C70E26F Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf01.hostedemail.com (Postfix) with ESMTP id DF2AD40026 for ; Tue, 14 Mar 2023 13:15:33 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678799734; 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; bh=nbkREkUXjavMZLWrcbnXqcI0k9zEMdNai8bCnP1WlF0=; b=D09GM7QbtBqbnekr7jAo+m9CKvn2vy59GY40fyJclAB3O5IG/rOhfY2PpLsNqx7ng0xrJ5 DoAx0SnsQCc9VBIt6GR7mJ7KkyQN48aaFozC+i/52prh+moTS4trhnJzvqqxNI/GZ7Yidn R/CX4Fcmbb14oxAn8Ri9o5qFHuzOhFs= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678799734; a=rsa-sha256; cv=none; b=QNVVNvsYKpjsbki6lxQdjsDI1CX0kKM+rsN+NwLa2a7Io2kEJDPbkgqXEC4XLMmbqxNl3g gDRrwCw++S5GHWqttV4oCDzJmzLYC1eDXLx8EiTqRUDpX9gcd2RvB1PmQxOtnXctDJiaAW qCseZRwVw+Hr7dVKs0f+h38yl6LMKes= Received: from kwepemm600020.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4PbYsv3jx7zKmjN; Tue, 14 Mar 2023 21:15:15 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Tue, 14 Mar 2023 21:15:28 +0800 From: Peng Zhang To: , , CC: , , , , , , ZhangPeng Subject: [PATCH v2 1/3] userfaultfd: convert mcopy_atomic_pte() to use a folio Date: Tue, 14 Mar 2023 13:13:48 +0000 Message-ID: <20230314131350.924377-2-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230314131350.924377-1-zhangpeng362@huawei.com> References: <20230314131350.924377-1-zhangpeng362@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600020.china.huawei.com (7.193.23.147) X-CFilter-Loop: Reflected X-Stat-Signature: 8yp1t441rtyqazjdwaca9shb6zsfb5mf X-Rspam-User: X-Rspamd-Queue-Id: DF2AD40026 X-Rspamd-Server: rspam06 X-HE-Tag: 1678799733-52398 X-HE-Meta: U2FsdGVkX1/77A/RY5kXumRGIAjB9aw87JcpXkI8zvx2GHxnd4hfxkydsmN07ZpgC4RAC8Ua+c0Z6glcxp+OTlXOcO1p3EpZh6woytC+0JFA8bNxzwAkYI94kfrkHpB/CeL6k7Q/PNZkYJt71nnu/Jlt4J1u/0GUTWUWsJ1gYGidg+EhdorY+fjqBad342rdVbJGPnHTlWWiPpTHk/UclW33MJ9nPABzKzls4AOgJRiRen0L0YJmZ/GfMNn1x4ccFVC5r32RiTnC5fV8Y99wJcLuHH/fXGgVyWUIqyXy/0401RDqyGrasQPNNXqEkPaek5mVMnE7IyLi5jy15dTOcJsiIuVdDr4zsxMy8IpvWmbwbY6x1SlQqIuBsvoSLRqc8fru9uF78gtv6S/def8is77UJSBBzoKCENTCgcV0/FEFJaSRgEkcJ4C/WITJWvvT6RS+IzHYyXKff0HIbdebEL3Y656YdZqBfgs4YDRn9CEcJ7Ctd6En3tvJwUI/PRNujMFRhY4YYItN4Y70lL2faHhHzY41VXx8J72x9GFTdqGdiuk/lu1BlUP84yTHW9/i7f0+Rf3xHJs0H/cUvAMvKJwn0+2WCrR5jHSvHB99zkJcJkRTwcBo58yPgo0ONft1Zb/G+oMEEShRYEkHPKD+ewRTjiPszpZhW5qwfkJov3dvNUMBpJ+1mU63rotn6gAL7NYoANq8s71u/kkAuj+Kg+Te8n1qLw55amgrOv+ll6lWG7Z7rKcYjKvLc5BU492xd1tjh4AA/JmixT2pdSuNjeHCqqNGBy0QYDkjeZbTSjtPGzlttlXiVAmWhZGDV+p8LQYtMYDPa6A/Mkam5IGapoUxiVeEPcBjEyMVkszNOrursD0OBGmEtGIKlttsFNegQYx381llL6B1dEUpKQv3SzGZ7BS96DiywZkao10eQi7cN+QGDtV6mFGTXPdFgMWmq57Ot92IzfhJ0ZYHmLx Y5KSde8j mUx0zKUhlzP2mOnMzRBEEn5P39stVAqRrh35GGidyXpiJVYZWTp0SmgoM/omtdlwvu1/v6qrxqSnKaKEAGkXfaLWoXQJTxS13ReyeHHKnjJ2rbroh/g7237ypObIQ/Qf50ifOwlecYkTUEoLqS+wPUpYT0bMlQ3Yn+YfNeu+HEZBrKFWynHMJ8lLuvra8f0Io5dJ303wt46FGrSmN4f7oVsTR62z2vr2UtCBTzIvL+zhUKxTSqQHJT40CEHtGkDfgK1N0/7N+t5AHU6SD5TjC6kyoFA== 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: From: ZhangPeng Call vma_alloc_folio() directly instead of alloc_page_vma(). Add an assertion that this is a single-page folio and removes several calls to compound_head(). Signed-off-by: ZhangPeng --- mm/userfaultfd.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 53c3d916ff66..263191c37fb5 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -137,15 +137,15 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm, { void *page_kaddr; int ret; - struct page *page; + struct folio *folio; if (!*pagep) { ret = -ENOMEM; - page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, dst_vma, dst_addr); - if (!page) + folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma, dst_addr, false); + if (!folio) goto out; - page_kaddr = kmap_local_page(page); + page_kaddr = kmap_local_folio(folio, 0); /* * The read mmap_lock is held here. Despite the * mmap_lock being read recursive a deadlock is still @@ -171,36 +171,37 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm, /* fallback to copy_from_user outside mmap_lock */ if (unlikely(ret)) { ret = -ENOENT; - *pagep = page; + *pagep = &folio->page; /* don't free the page */ goto out; } - flush_dcache_page(page); + flush_dcache_folio(folio); } else { - page = *pagep; + folio = page_folio(*pagep); + VM_BUG_ON_FOLIO(folio_test_large(folio), folio); *pagep = NULL; } /* - * The memory barrier inside __SetPageUptodate makes sure that + * The memory barrier inside __folio_mark_uptodate makes sure that * preceding stores to the page contents become visible before * the set_pte_at() write. */ - __SetPageUptodate(page); + __folio_mark_uptodate(folio); ret = -ENOMEM; - if (mem_cgroup_charge(page_folio(page), dst_mm, GFP_KERNEL)) + if (mem_cgroup_charge(folio, dst_mm, GFP_KERNEL)) goto out_release; ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, - page, true, wp_copy); + &folio->page, true, wp_copy); if (ret) goto out_release; out: return ret; out_release: - put_page(page); + folio_put(folio); goto out; } From patchwork Tue Mar 14 13:13:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13174413 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 BC26BC7618A for ; Tue, 14 Mar 2023 13:15:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBEDC6B0074; Tue, 14 Mar 2023 09:15:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF9118E0003; Tue, 14 Mar 2023 09:15:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B74CF6B0078; Tue, 14 Mar 2023 09:15:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9315D6B0074 for ; Tue, 14 Mar 2023 09:15:41 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4DFE8160EA5 for ; Tue, 14 Mar 2023 13:15:41 +0000 (UTC) X-FDA: 80567550882.01.3A16602 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf06.hostedemail.com (Postfix) with ESMTP id C1B8118001F for ; Tue, 14 Mar 2023 13:15:37 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678799738; 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; bh=Z9oXbFnouLu3UJ7M+jlusf4zWTmPRKVpTvfRvfSohEA=; b=5mUAnVL9/jQJdEv6OWQtEGaQdSR3IUhiKOQ/piVDJZA/p5U/yy7XbrdilyTTI/n/gCyanP ICzR1axWLzLTpAsgt5dVKyl+R9ExuzwFloEft/WO21jBsBX5jq9+DnCrqiPcXMgUogkf9B 8z4ZV3HnisUj4biu23HQBuTLMSkNnJc= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf06.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678799738; a=rsa-sha256; cv=none; b=zc7pUB7LTTh5spK3mtNWytvhrE7APFJcAGbjV4omytJQWmGbRRthqZa3OqfWkpKGbEO5M7 z7Abc7sdVOC+UrHc38HEXjuG045RCm6Yx1Ic0Sy0Pzbh7O+06oTDsRzbI+Z4XUeztcdY5R WNT/tuZnZY57biVkxTlQ/3a84VNCOd4= Received: from kwepemm600020.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4PbYs90fb7zrT4F; Tue, 14 Mar 2023 21:14:37 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Tue, 14 Mar 2023 21:15:29 +0800 From: Peng Zhang To: , , CC: , , , , , , ZhangPeng Subject: [PATCH v2 2/3] userfaultfd: convert __mcopy_atomic_hugetlb() to use a folio Date: Tue, 14 Mar 2023 13:13:49 +0000 Message-ID: <20230314131350.924377-3-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230314131350.924377-1-zhangpeng362@huawei.com> References: <20230314131350.924377-1-zhangpeng362@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600020.china.huawei.com (7.193.23.147) X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C1B8118001F X-Stat-Signature: qewhz8adb5istxctab6fbacmqy1h9a6n X-HE-Tag: 1678799737-6295 X-HE-Meta: U2FsdGVkX183a1AWI2a0GZoEGYFJAT9EwIecZKwvB2LXT1upwL7RXxLEV6OQt4CoShcvpIK6FL5zhOQDYZdkRkITy6iSsJ1H7Hctn2Ioy2idamc5kZU4NZk8/D7J2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yHVy9tQJXkunrg899yEGNPOtnESF28MpOLMSczS0MTHizsluqyaV0ZD2HVHb1GsfW1t 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: From: ZhangPeng Change copy_huge_page_from_user() to copy_folio_from_user(). Convert hugetlb_mcopy_atomic_pte() and __mcopy_atomic_hugetlb() to use a folio. Signed-off-by: ZhangPeng --- include/linux/hugetlb.h | 4 ++-- include/linux/mm.h | 3 +-- mm/hugetlb.c | 25 ++++++++++++------------- mm/memory.c | 27 ++++++++++++--------------- mm/userfaultfd.c | 20 +++++++++----------- 5 files changed, 36 insertions(+), 43 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 7c977d234aba..030252ce51bd 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -163,7 +163,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte, unsigned long dst_addr, unsigned long src_addr, enum mcopy_atomic_mode mode, - struct page **pagep, + struct folio **foliop, bool wp_copy); #endif /* CONFIG_USERFAULTFD */ bool hugetlb_reserve_pages(struct inode *inode, long from, long to, @@ -399,7 +399,7 @@ static inline int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, unsigned long dst_addr, unsigned long src_addr, enum mcopy_atomic_mode mode, - struct page **pagep, + struct folio **foliop, bool wp_copy) { BUG(); diff --git a/include/linux/mm.h b/include/linux/mm.h index 1f79667824eb..428cff424d26 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3546,9 +3546,8 @@ extern void copy_user_huge_page(struct page *dst, struct page *src, unsigned long addr_hint, struct vm_area_struct *vma, unsigned int pages_per_huge_page); -extern long copy_huge_page_from_user(struct page *dst_page, +long copy_folio_from_user(struct folio *dst_folio, const void __user *usr_src, - unsigned int pages_per_huge_page, bool allow_pagefault); /** diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 07abcb6eb203..ea1f3b448b7a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6163,7 +6163,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, unsigned long dst_addr, unsigned long src_addr, enum mcopy_atomic_mode mode, - struct page **pagep, + struct folio **foliop, bool wp_copy) { bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE); @@ -6185,7 +6185,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, if (!folio) goto out; folio_in_pagecache = true; - } else if (!*pagep) { + } else if (!*foliop) { /* If a page already exists, then it's UFFDIO_COPY for * a non-missing case. Return -EEXIST. */ @@ -6201,9 +6201,8 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, goto out; } - ret = copy_huge_page_from_user(&folio->page, - (const void __user *) src_addr, - pages_per_huge_page(h), false); + ret = copy_folio_from_user(folio, + (const void __user *) src_addr, false); /* fallback to copy_from_user outside mmap_lock */ if (unlikely(ret)) { @@ -6222,7 +6221,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, ret = -ENOMEM; goto out; } - *pagep = &folio->page; + *foliop = folio; /* Set the outparam pagep and return to the caller to * copy the contents outside the lock. Don't free the * page. @@ -6232,23 +6231,23 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, } else { if (vm_shared && hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { - put_page(*pagep); + folio_put(*foliop); ret = -EEXIST; - *pagep = NULL; + *foliop = NULL; goto out; } folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0); if (IS_ERR(folio)) { - put_page(*pagep); + folio_put(*foliop); ret = -ENOMEM; - *pagep = NULL; + *foliop = NULL; goto out; } - copy_user_huge_page(&folio->page, *pagep, dst_addr, dst_vma, + copy_user_huge_page(&folio->page, &((*foliop)->page), dst_addr, dst_vma, pages_per_huge_page(h)); - put_page(*pagep); - *pagep = NULL; + folio_put(*foliop); + *foliop = NULL; } /* diff --git a/mm/memory.c b/mm/memory.c index f456f3b5049c..78fe9ef0c058 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5769,37 +5769,34 @@ void copy_user_huge_page(struct page *dst, struct page *src, process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); } -long copy_huge_page_from_user(struct page *dst_page, +long copy_folio_from_user(struct folio *dst_folio, const void __user *usr_src, - unsigned int pages_per_huge_page, bool allow_pagefault) { void *page_kaddr; unsigned long i, rc = 0; - unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; + unsigned int nr_pages = folio_nr_pages(dst_folio); + unsigned long ret_val = nr_pages * PAGE_SIZE; struct page *subpage; - for (i = 0; i < pages_per_huge_page; i++) { - subpage = nth_page(dst_page, i); - if (allow_pagefault) - page_kaddr = kmap(subpage); - else - page_kaddr = kmap_atomic(subpage); + for (i = 0; i < nr_pages; i++) { + subpage = folio_page(dst_folio, i); + page_kaddr = kmap_local_page(subpage); + if (!allow_pagefault) + pagefault_disable(); rc = copy_from_user(page_kaddr, usr_src + i * PAGE_SIZE, PAGE_SIZE); - if (allow_pagefault) - kunmap(subpage); - else - kunmap_atomic(page_kaddr); + if (!allow_pagefault) + pagefault_enable(); + kunmap_local(page_kaddr); ret_val -= (PAGE_SIZE - rc); if (rc) break; - flush_dcache_page(subpage); - cond_resched(); } + flush_dcache_folio(dst_folio); return ret_val; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 263191c37fb5..a7da26cdb731 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -324,7 +324,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, pte_t *dst_pte; unsigned long src_addr, dst_addr; long copied; - struct page *page; + struct folio *folio; unsigned long vma_hpagesize; pgoff_t idx; u32 hash; @@ -344,7 +344,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, src_addr = src_start; dst_addr = dst_start; copied = 0; - page = NULL; + folio = NULL; vma_hpagesize = vma_kernel_pagesize(dst_vma); /* @@ -413,7 +413,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, } err = hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, - dst_addr, src_addr, mode, &page, + dst_addr, src_addr, mode, &folio, wp_copy); hugetlb_vma_unlock_read(dst_vma); @@ -423,12 +423,10 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, if (unlikely(err == -ENOENT)) { mmap_read_unlock(dst_mm); - BUG_ON(!page); + BUG_ON(!folio); - err = copy_huge_page_from_user(page, - (const void __user *)src_addr, - vma_hpagesize / PAGE_SIZE, - true); + err = copy_folio_from_user(folio, + (const void __user *) src_addr, true); if (unlikely(err)) { err = -EFAULT; goto out; @@ -438,7 +436,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, dst_vma = NULL; goto retry; } else - BUG_ON(page); + BUG_ON(folio); if (!err) { dst_addr += vma_hpagesize; @@ -455,8 +453,8 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, out_unlock: mmap_read_unlock(dst_mm); out: - if (page) - put_page(page); + if (folio) + folio_put(folio); BUG_ON(copied < 0); BUG_ON(err > 0); BUG_ON(!copied && !err); From patchwork Tue Mar 14 13:13:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13174410 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 3F928C6FD1C for ; Tue, 14 Mar 2023 13:15:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3DE36B0072; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9EB6C8E0002; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B39F8E0001; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7B78B6B0072 for ; Tue, 14 Mar 2023 09:15:38 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 28C3B40E94 for ; Tue, 14 Mar 2023 13:15:38 +0000 (UTC) X-FDA: 80567550756.24.D59701D Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf25.hostedemail.com (Postfix) with ESMTP id E0530A002C for ; Tue, 14 Mar 2023 13:15:34 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf25.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678799735; 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; bh=4136v2APcdU07w0yCBBOMIOnGJnkvsplbIJMs1oGyto=; b=vxGUm2B5iHe+ojHe4zFMpJa3yFi8QNgNDFJrZIqT8kHpMlA+dgP5R91KDWgJg4QzTC56VB MQs6v87Bwb4KLVsNytRegT2yWFMU3/FEgz5RcYaNyYY0S8QboZVHiqT5sekCjC5Q9DhwIj JxV99w/AetkbD4GngyC4k2u7ClWBmPY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf25.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678799735; a=rsa-sha256; cv=none; b=RlWe3aB1NSrelqUIoy3zcnKkOH62/rlfK8Hcs0y2zlYnR5odLU+JmJbw2iifV3BLdDO7YM 7C7HHrR0Kq60MnfH1hb3tVTbPOK8+PT2GOdjOMJ4vOR7R2G9uK3oeEYpni51R8kd82jp/c RSTmGipwF0iru9Hu25fJy45n4VWTWpI= Received: from kwepemm600020.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4PbYqh1thZzHwdP; Tue, 14 Mar 2023 21:13:20 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Tue, 14 Mar 2023 21:15:29 +0800 From: Peng Zhang To: , , CC: , , , , , , ZhangPeng Subject: [PATCH v2 3/3] userfaultfd: convert __mcopy_atomic() to use a folio Date: Tue, 14 Mar 2023 13:13:50 +0000 Message-ID: <20230314131350.924377-4-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230314131350.924377-1-zhangpeng362@huawei.com> References: <20230314131350.924377-1-zhangpeng362@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600020.china.huawei.com (7.193.23.147) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E0530A002C X-Rspam-User: X-Stat-Signature: zprcx6esjxpfakd1mfjbwefzohjdo5hw X-HE-Tag: 1678799734-596283 X-HE-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VmfksaxahZ+ISA1bAiUhrK6hiPeMuikC+434FeFPK3QTnQGXmKh9cAQQYN5AvWUn1Ec1VtBZI0bYQuQpbrRTndYiKNbLb4eJxrHe2joIL80Y3O/Xiaim9z3xre3hPf6EQJ2GGs0yQYxW1CI6BDtM8vRNajMAyH9Iy0EbMmDHF6z5iiysNxbL98I0k7hXVFn 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: From: ZhangPeng Convert mcopy_atomic_pte(), shmem_mfill_atomic_pte() and mfill_atomic_pte() to pass in a folio pointer. Convert __mcopy_atomic() to use a folio. Signed-off-by: ZhangPeng --- include/linux/shmem_fs.h | 2 +- mm/shmem.c | 17 ++++++++--------- mm/userfaultfd.c | 35 +++++++++++++++++------------------ 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 103d1000a5a2..580af0e3bf02 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -156,7 +156,7 @@ extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd, unsigned long dst_addr, unsigned long src_addr, bool zeropage, bool wp_copy, - struct page **pagep); + struct folio **foliop); #else /* !CONFIG_SHMEM */ #define shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, \ src_addr, zeropage, wp_copy, pagep) ({ BUG(); 0; }) diff --git a/mm/shmem.c b/mm/shmem.c index 448f393d8ab2..986267bf34ef 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2421,7 +2421,7 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, unsigned long dst_addr, unsigned long src_addr, bool zeropage, bool wp_copy, - struct page **pagep) + struct folio **foliop) { struct inode *inode = file_inode(dst_vma->vm_file); struct shmem_inode_info *info = SHMEM_I(inode); @@ -2439,14 +2439,14 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, * and now we find ourselves with -ENOMEM. Release the page, to * avoid a BUG_ON in our caller. */ - if (unlikely(*pagep)) { - put_page(*pagep); - *pagep = NULL; + if (unlikely(*foliop)) { + folio_put(*foliop); + *foliop = NULL; } return -ENOMEM; } - if (!*pagep) { + if (!*foliop) { ret = -ENOMEM; folio = shmem_alloc_folio(gfp, info, pgoff); if (!folio) @@ -2478,7 +2478,7 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, /* fallback to copy_from_user outside mmap_lock */ if (unlikely(ret)) { - *pagep = &folio->page; + *foliop = folio; ret = -ENOENT; /* don't free the page */ goto out_unacct_blocks; @@ -2489,9 +2489,8 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, clear_user_highpage(&folio->page, dst_addr); } } else { - folio = page_folio(*pagep); - VM_BUG_ON_FOLIO(folio_test_large(folio), folio); - *pagep = NULL; + VM_BUG_ON_FOLIO(folio_test_large(*foliop), folio); + *foliop = NULL; } VM_BUG_ON(folio_test_locked(folio)); diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index a7da26cdb731..cfe2d527f1c0 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -132,14 +132,14 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - struct page **pagep, + struct folio **foliop, bool wp_copy) { void *page_kaddr; int ret; struct folio *folio; - if (!*pagep) { + if (!*foliop) { ret = -ENOMEM; folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, dst_vma, dst_addr, false); if (!folio) @@ -171,16 +171,15 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm, /* fallback to copy_from_user outside mmap_lock */ if (unlikely(ret)) { ret = -ENOENT; - *pagep = &folio->page; + *foliop = folio; /* don't free the page */ goto out; } flush_dcache_folio(folio); } else { - folio = page_folio(*pagep); - VM_BUG_ON_FOLIO(folio_test_large(folio), folio); - *pagep = NULL; + VM_BUG_ON_FOLIO(folio_test_large(*foliop), folio); + *foliop = NULL; } /* @@ -476,7 +475,7 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long dst_addr, unsigned long src_addr, - struct page **page, + struct folio **foliop, enum mcopy_atomic_mode mode, bool wp_copy) { @@ -500,7 +499,7 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, if (!(dst_vma->vm_flags & VM_SHARED)) { if (mode == MCOPY_ATOMIC_NORMAL) err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, - dst_addr, src_addr, page, + dst_addr, src_addr, foliop, wp_copy); else err = mfill_zeropage_pte(dst_mm, dst_pmd, @@ -509,7 +508,7 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm, err = shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, src_addr, mode != MCOPY_ATOMIC_NORMAL, - wp_copy, page); + wp_copy, foliop); } return err; @@ -528,7 +527,7 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, pmd_t *dst_pmd; unsigned long src_addr, dst_addr; long copied; - struct page *page; + struct folio *folio; bool wp_copy; /* @@ -544,7 +543,7 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, src_addr = src_start; dst_addr = dst_start; copied = 0; - page = NULL; + folio = NULL; retry: mmap_read_lock(dst_mm); @@ -641,16 +640,16 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, BUG_ON(pmd_trans_huge(*dst_pmd)); err = mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, - src_addr, &page, mcopy_mode, wp_copy); + src_addr, &folio, mcopy_mode, wp_copy); cond_resched(); if (unlikely(err == -ENOENT)) { void *page_kaddr; mmap_read_unlock(dst_mm); - BUG_ON(!page); + BUG_ON(!folio); - page_kaddr = kmap_local_page(page); + page_kaddr = kmap_local_folio(folio, 0); err = copy_from_user(page_kaddr, (const void __user *) src_addr, PAGE_SIZE); @@ -659,10 +658,10 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, err = -EFAULT; goto out; } - flush_dcache_page(page); + flush_dcache_folio(folio); goto retry; } else - BUG_ON(page); + BUG_ON(folio); if (!err) { dst_addr += PAGE_SIZE; @@ -679,8 +678,8 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, out_unlock: mmap_read_unlock(dst_mm); out: - if (page) - put_page(page); + if (folio) + folio_put(folio); BUG_ON(copied < 0); BUG_ON(err > 0); BUG_ON(!copied && !err);