From patchwork Sun Jan 1 23:00:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13086505 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 2EFE4C4167B for ; Sun, 1 Jan 2023 23:01:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 23CD58E0002; Sun, 1 Jan 2023 18:01:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1ECE68E0001; Sun, 1 Jan 2023 18:01:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B53C8E0002; Sun, 1 Jan 2023 18:01:07 -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 EE2CC8E0001 for ; Sun, 1 Jan 2023 18:01:06 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AE741405F8 for ; Sun, 1 Jan 2023 23:01:06 +0000 (UTC) X-FDA: 80307752532.05.EDE437C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf27.hostedemail.com (Postfix) with ESMTP id 384DE40005 for ; Sun, 1 Jan 2023 23:01:05 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QaXUiWn3; spf=pass (imf27.hostedemail.com: domain of 3sBCyYwoKCL0mwkrxjkwrqjrrjoh.frpolqx0-ppnydfn.ruj@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3sBCyYwoKCL0mwkrxjkwrqjrrjoh.frpolqx0-ppnydfn.ruj@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672614065; 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:in-reply-to: references:dkim-signature; bh=KIoFTjTTjDz9d1b1yxyyUqxAJ41QzJduAZlhlKAINmQ=; b=rPXkHK+H5gAvLXDogvCIjwfQrouj8k0XKXbAkHBdSJN7eTrxa1lOxEJjK94tK87BNO64lt YKzLxUtNHBcRqx4IiUb9Nh+K/4Qj+0B6HdKPzW7T06GL2mWwQpPSBijTh23FQOu/puCU8P 48j7FK5rEOZ9T1dYkUAh3AyxKcZf9kI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QaXUiWn3; spf=pass (imf27.hostedemail.com: domain of 3sBCyYwoKCL0mwkrxjkwrqjrrjoh.frpolqx0-ppnydfn.ruj@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3sBCyYwoKCL0mwkrxjkwrqjrrjoh.frpolqx0-ppnydfn.ruj@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672614065; a=rsa-sha256; cv=none; b=zjBOwem0Xx2W4SpDoAVH///5jGO4NlSAq6gXO1Fl9md3pyweOBu3FbfQ4bb+vG/LSrcA4A LB1OmQAodCbd8jLUe2TqGp3gdwSt6Y4WPAeqe0Uhra7HZ1TseXOvoaYhxhh/rKVO0PwbBQ gYWR7iB+0YuhsvsdmGwcRzkwQyRFR54= Received: by mail-yb1-f201.google.com with SMTP id h66-20020a252145000000b0071a7340eea9so26861220ybh.6 for ; Sun, 01 Jan 2023 15:01:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=KIoFTjTTjDz9d1b1yxyyUqxAJ41QzJduAZlhlKAINmQ=; b=QaXUiWn3XNUQEd6ip4vKheFQ35b9GlV46CrIuxtdTNeswfec+k/oSdlTKQJTDN7laS IXWJ84jgV80eS0dFPfxIK9buoMDZri3O1HyL8J3CvF+RGrFVhngmPoLHGwHlrLXDHT+W 6GMJ+zRQMhOyC8Dnd5ktYf2ujXYes76nARhGdQj/ZVMKhLQGWvNYqV6A8hBMZmxlPDdT Hxt98MeLSanNiBXBhi+ypdRnxFptGyezj8WV6BzlPslY7fuHXAGpzj3tDkvUtgsBz6zH h0TvosJg0ov2IxUEC24zMpQicQzUeWMR9gwaVEXy8nQ08c+3cb5sUSic/HIVU7zs0iuz YuzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=KIoFTjTTjDz9d1b1yxyyUqxAJ41QzJduAZlhlKAINmQ=; b=BFR+mHqJ/B2jJqz0ARnktmEYC1Q8PBtspEwliyX5mLSF0V3d85byORjtYOsbDt+2Nf FpXsoAPkg9p0vPf12VT9vrMI+bKxw0MEFNbiQptzzGiH4FQ0E3nkKqeG9yWXdfOixVGl KLLfckC/iys8w0X3BiqsDA9rfFsDhJLPgtkLDWTtPRWXTUN/tPQLTyDdmH9rrIGsR8CP ciCn9b0vpblwZ4qcxvICCNfKvcG9EIiT2Z2keU3ZmL9b3UJEGEhYWwFFRNNsTP0E2oMy UvmbR0KmDkICNHVghsNqTgcX01zd+m/AusA2X5zxSUafC4GYCqnpbUGaU0985NKK05BQ Sc0g== X-Gm-Message-State: AFqh2koWXtNlMmjTo+M/s8J7ZBvMHfyxkHbdClyEZgtRfCxTnhpjEJJ5 g0Up80th3JrEMT1kISH6FsuERAJIZ6GXC2pM X-Google-Smtp-Source: AMrXdXtqLpmC78soBcB8I1zQEwTynK8DAlqR/NEcP2QFzw0OMk8ECD9RqgeIJpwa8I6bqpXFoQ1rPVNSvTlCUnSQ X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:79f:b0:3c4:1f67:a2a2 with SMTP id bw31-20020a05690c079f00b003c41f67a2a2mr4768867ywb.234.1672614064278; Sun, 01 Jan 2023 15:01:04 -0800 (PST) Date: Sun, 1 Jan 2023 23:00:42 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230101230042.244286-1-jthoughton@google.com> Subject: [PATCH] hugetlb: unshare some PMDs when splitting VMAs From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu Cc: Axel Rasmussen , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 384DE40005 X-Rspam-User: X-Stat-Signature: umwp53xe7gjat478cah4dr4bjja4u8w8 X-HE-Tag: 1672614065-540166 X-HE-Meta: U2FsdGVkX18ZYJsMCFHhWXicHxTv0EDsQoVouQMkR61EsdmJHzOMswmR403N9GqJxg8CWudAyp4wmeDRHUZA9v3Mn+IQgVi3XiOeM5URLxOGd483DU4yV0nwkWmLiL8sv2dnqvipqlGFcZu026HHujI46FRh+KMO2DLFYYbwxIEVgyX7FmyJOFcIyF2jZLQGFMl2CT/MT3PJ5ynmUDuXg3KJFnrshjNJ0/ZNod4v+ciYHipCS2qDYlVg7642ejxK8ZDC6UEYOEgj20nTxpYXdvgjbTcimV02uch0BrMqh3zcQKgTzXPpcmR5y1qHAfq7A3aKaEPQn4PY3+r7Q2EiMUICAdUWNlpQiUPwqJVHvmB68ZzY6mMCwASBa8XKiFQqK3rXrdUlZWpGDj6GQj7oP/g4ofGpY6ktk0m+r+oCD5H/cwFTjLSdcmLVn+ir1mu90w6F7sTICiiJUt+9yPOivAi1+jclwa58X6j4MUSRXddtioVm11Cy1BwG4jL8JV1ssGVcBQ8yCMmEZ4BgxJ6Fg1eKN8skVzC9QSdAMS6y8pmzPnr9ZXqR1HwhkbI7V+UQ2Tynq6FeFtV6i3oXbuKCtLNlA3h70BSW75iVtkmYz4FRPSwxZTi/zMW3Kk7rD/29A4JD4DagACgrWgi5NmQtVyk0m6NpSKXrCA5NjmuqkPHSgMB1vV1XQS3lFSYMRBdcbfEXJAmqdHYUsl3Q+rW9S70hCtfQBNYM8zKDPrQ6u7VH6mhspOIXrp9h8OnvOQ71lSTszo+Ku6USvwbaQzoeQcgHXbPhWEzSKHcYwAjjA1yE2uv75LLayK5yvINUEqSjcRlYYRI+ax/u5tVatkSWL++nFte6JBhOHsHLuNGrQNs+wz1coxF9jE1XccQb2BmKHnDN6NhtNosWC/ok0mb1y+0NV4zg634DvHpLiQ7dv/4veRMGHboJxgmZXcFZRZGc6VXo53Jw1kbYouVecoT 4590zHm8 0Y1bJGZfHqNG18YLoPWxA8oEhRIs5jjA3AxGTvAhLJmYOfTIQA3gsKdZzLNTGtNa9C0FXcPsOGsoDBJiWYsO4iTpKE8h76SqQ3C41uyXBoWlrZIK36Qi7MOlpcsgqSHoTWtKI/qJ3e/n1eOnl5utsimjOFgUpqknuuXj7Ju9TT7ytY2Pyq84Pk6Veqc7g8otQUJ1UCjA/o3qDttx7oKwJECkTgq68gERYVXf/Wi8V2GTs4Zu09XwV5o9yGroC7n/4CLnemga/CLsM1CLZKpqLNRzMoLC0M4L57XevlI+36QnAhcPKXU+f/b7ork5bOPNvTFB7kBycsZSS0M2pI15izljEA2FAcjJt5w5O9sgk1asCSZ0FyMVNCa3ZFgf4e2anZ/PqDXcrXWx+G7o= 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: PMD sharing can only be done in PUD_SIZE-aligned pieces of VMAs; however, it is possible that HugeTLB VMAs are split without unsharing the PMDs first. In some (most?) cases, this is a non-issue, like userfaultfd_register and mprotect, where PMDs are unshared before anything is done. However, mbind() and madvise() (like MADV_DONTDUMP) can cause a split without unsharing first. It might seem ideal to unshare in hugetlb_vm_op_open, but that would only unshare PMDs in the new VMA. Signed-off-by: James Houghton --- mm/hugetlb.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b39b74e0591a..bf7a1f628357 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -94,6 +94,8 @@ static int hugetlb_acct_memory(struct hstate *h, long delta); static void hugetlb_vma_lock_free(struct vm_area_struct *vma); static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma); static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma); +static void hugetlb_unshare_pmds(struct vm_area_struct *vma, + unsigned long start, unsigned long end); static inline bool subpool_is_free(struct hugepage_subpool *spool) { @@ -4828,6 +4830,23 @@ static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr) { if (addr & ~(huge_page_mask(hstate_vma(vma)))) return -EINVAL; + + /* We require PUD_SIZE VMA alignment for PMD sharing. */ + if (addr & ~PUD_MASK) { + /* + * hugetlb_vm_op_split is called right before we attempt to + * split the VMA. We will need to unshare PMDs in the old and + * new VMAs, so let's unshare before we split. + */ + unsigned long floor = addr & PUD_MASK; + unsigned long ceil = floor + PUD_SIZE; + + if (floor < vma->vm_start || ceil >= vma->vm_end) + /* PMD sharing is already impossible. */ + return 0; + hugetlb_unshare_pmds(vma, floor, ceil); + } + return 0; } @@ -7313,26 +7332,21 @@ void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int re } } -/* - * This function will unconditionally remove all the shared pmd pgtable entries - * within the specific vma for a hugetlbfs memory range. - */ -void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) +static void hugetlb_unshare_pmds(struct vm_area_struct *vma, + unsigned long start, + unsigned long end) { struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); struct mm_struct *mm = vma->vm_mm; struct mmu_notifier_range range; - unsigned long address, start, end; + unsigned long address; spinlock_t *ptl; pte_t *ptep; if (!(vma->vm_flags & VM_MAYSHARE)) return; - start = ALIGN(vma->vm_start, PUD_SIZE); - end = ALIGN_DOWN(vma->vm_end, PUD_SIZE); - if (start >= end) return; @@ -7364,6 +7378,16 @@ void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) mmu_notifier_invalidate_range_end(&range); } +/* + * This function will unconditionally remove all the shared pmd pgtable entries + * within the specific vma for a hugetlbfs memory range. + */ +void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) +{ + hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE), + ALIGN_DOWN(vma->vm_end, PUD_SIZE)); +} + #ifdef CONFIG_CMA static bool cma_reserve_called __initdata;