From patchwork Sun Sep 3 15:13:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 13373271 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 B6D33CA0FE9 for ; Sun, 3 Sep 2023 15:13:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8183940009; Sun, 3 Sep 2023 11:13:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BB048D0002; Sun, 3 Sep 2023 11:13:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 85D81940009; Sun, 3 Sep 2023 11:13:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 756B18D0002 for ; Sun, 3 Sep 2023 11:13:40 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3C649404B1 for ; Sun, 3 Sep 2023 15:13:40 +0000 (UTC) X-FDA: 81195630600.18.52EDB22 Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) by imf04.hostedemail.com (Postfix) with ESMTP id 5AD214000D for ; Sun, 3 Sep 2023 15:13:38 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=odZGtHyJ; spf=pass (imf04.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.46 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693754018; a=rsa-sha256; cv=none; b=QXw1lbWPPJGE9O29WLQFGSoaC2nuLgHf5rESmWrHUIuGh7gpcD0Zc/I7ZTA7NDQ46GVji1 Cp4wcIi4NmMKqvUc8y6QUfXDVyHTdvAJbBx94p9kmNEcgul3SLu9m0sL8d0R4helKVPV8X 558kODjM6vo8pMsIYS8qn60befzQcA0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=odZGtHyJ; spf=pass (imf04.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.46 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693754018; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=L4plAmoxt0TIZz59K3TwGGgNGB354bzgqE5y3am6jPM=; b=dVjmtmVR2+P7oG+PFjGW8Oq2pRIJvw7gcFihJQK2TfreSRvLGYSwSI6nWa4MQUe2uJGP4I 4JXb4m5gF9vaTZh8FdpymbOQVmpXywn2KnFr0LUW/Hf0atoJO7MT0fsVn8sx1URjV+Xnj3 YMZ81Uc7OzkSoc419X94VTdfZE+DdsY= Received: by mail-io1-f46.google.com with SMTP id ca18e2360f4ac-7927f36120cso33613139f.1 for ; Sun, 03 Sep 2023 08:13:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1693754017; x=1694358817; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=L4plAmoxt0TIZz59K3TwGGgNGB354bzgqE5y3am6jPM=; b=odZGtHyJWDs4jHmUnw+l/YEJdeR5JjI3y37jHQ+4RkkrmJBZ2IC4ZLAc/qzkJCDe5d P07QpPzXjcmaBzPWwFghYn+XWlWSJ8NA4YXQuhRhPHH08Jqd7eXjsgYOX7a6+jvWZZzB nGe9yQSGMkZRbc5JzjQFTMKjDrwsvqIz1J0uw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693754017; x=1694358817; 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=L4plAmoxt0TIZz59K3TwGGgNGB354bzgqE5y3am6jPM=; b=froyyoS9ikxOGz+LWz1kuVwt42uZF3LTm65I/7NKAfGJrXFi90SrfI350MIppuhlHz hRVBr5x1WGx80TfT2BdgTIhLKc3fzYZbLOe++d/qLwYw+7Ntm+LuxERRjBjhD2lAsz9x jY64ovnXWL5n7e1ZGmFJZ2tfslpTqABuhk5Vxkf3gby8YWUSX1KKKVzucPB2RK4drnfz xcYkZLNu6d2pg2XvHQhsnVSe5c7t7RcnYLi2eZrXgKIgogdp2EIQ9nluZAnlgmzAWb70 6QtBFfemT3PEBTdnlctw3i23/cGLwzkwWbjwFtIu73RbcBk0/nzsvXapcNY19c+IDFx9 enMg== X-Gm-Message-State: AOJu0YxaA3TyOTJhhm1FpmqFKBC6xUSaYm7cJZYy7t47LhqfkE0DOfFH +wUYrd6cq+szZo1LvlGZZ1u0LeT2AX1yOE+2f0E= X-Google-Smtp-Source: AGHT+IHJGQrTQCQWJADO7oGCiqQCRPPFaXSY0O8+5g3/GiBjHqRcTCt3H4l78vIVACIFoG7YASx0Jw== X-Received: by 2002:a6b:7f46:0:b0:791:2db5:c779 with SMTP id m6-20020a6b7f46000000b007912db5c779mr11231804ioq.10.1693754017569; Sun, 03 Sep 2023 08:13:37 -0700 (PDT) Received: from joelboxx5.corp.google.com (156.190.123.34.bc.googleusercontent.com. [34.123.190.156]) by smtp.gmail.com with ESMTPSA id f5-20020a6b5105000000b007835a305f61sm2605897iob.36.2023.09.03.08.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 08:13:36 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Lorenzo Stoakes , Linus Torvalds , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra Subject: [PATCH v6 1/7] mm/mremap: Optimize the start addresses in move_page_tables() Date: Sun, 3 Sep 2023 15:13:22 +0000 Message-ID: <20230903151328.2981432-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog In-Reply-To: <20230903151328.2981432-1-joel@joelfernandes.org> References: <20230903151328.2981432-1-joel@joelfernandes.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5AD214000D X-Stat-Signature: 7397dsyes3pfqyfwbmjtp5mpa51xwzfy X-Rspam-User: X-HE-Tag: 1693754018-81145 X-HE-Meta: U2FsdGVkX1/Lb1cEWZkAVZK9IZc+1XA3cLIG+84eR+HmT71CvkqlfhTys2QwO7fLX+Ikl9iSP/CVYYoWOcRg6gIbNDl46tBOVE/AO0TtJ9gfizZbnii4o7yoafzc5xLXs0wU6gKn4qdgctoimApFnJnsF4TFv5r2n72HsbhA+8GCDdJPUZ2oS0GbnIDyIi3WD7CVR9AwXB9W0PB2ODrsf2fNpA1AoxV79K0kB+K222YBVMvU+nwYyr7syOIspKhaAPg5Lvi0hiqhDu7DaZ7JTExXV6SVXN6jcK0beqWhw514idz32902MPhm46tDapgjKbz3PEYkxGyMPuw/mYzvb1ZYanFDNe4IWxBp3sB/BEEm2dQFCZpjzYiIgm/0QXUP4i0NEziF/DNYQHLksIHJDtS106d5/hkuqAMSJL3x7UNO8Xhjt9PprigmbcIGc+eBD3xWWhdDgiVhSGitr8smseRq/2qXGbAWMLSLlOUxzvgT1T4o/fwrw+SRkAvRSrmzPEvanZ+uyaBwL8Smj13e1H79TVOfWhIEHMsFoGoqB5ZqmQaq9gHiqOYob1JK8TYhPG9jGPdVhbKwdZAInurTO/CePTul15kCAcLjOW5u1OqnR2OM3niux/DzGKyRPSq2P+vuFWlwMR4PiOAIMYT8XpKeun0ZQQuHyKniy4vd0MWrbC/Hi/0Hd0cN8gaHVtObu4Rml0V+dM2xmAP7t257G+u+zflYC/715rRa3Uw1eBOFFICUPf3kUQZpJWnRBX5A4DkvHPdtYFH3UFVVvFmgHW9ENN9P1+/8WRqaEbvIeiEYXaOHf9RP3nDRxKvHwnbHpe/Il7AYte54Gykf8EajqizRtUEARvUI+HKt+s6SYZw8shs2LbJt8COlMusT6sybXIqRRHygJJOH4tU1UqDY7uXaSGv4xTZgn+HbXCyXRXsuOTXRm3sbwRA/kschAEe8pWV7Mn8rrQKzIpkWHqh AovqLP9J eSdkWvtbVBsBt6wfjBwBaWPTbMErs+EE7HOdpyGHIEm9/J/NZ+8R0tlOYkF6xOmwAHYTX1bfr3bCVS4wFNtwIkzfusKTnj9MNmTTuzUl5kiJ1qS58UnziNERi4Xv+MGpszAOuzDrO0I63zc4aHQsxpHT34KHUi+YhMXjOV7c5ytd5GsrhmSMOydSSnK/FviOwyJ4fp5RdPbrrNnoNzj1FlL0fXm3ZVqpH5NbPQEcuiPqZULT2njujQ6hGVN6OvkfzSGUa++GTmVv6thw2sIZxS0hTthFKZr8BLIvWRbrDpXZcXhUr4elPmLQ/c2GAYqfAxg8voam/sXyfKZy+33BV4OrovjiRARv5O2G5zsj2qI/xumO8+jqhjPtc94ok54/+kQmBZFnMx+NBe8/+eNHR/BBJqkdasJfuvMsVhF4FbVU+W7VTeBHruCrfhfc/8lc9cM4oKfdTpY25itkZTSd6WuQmguCX5r7XSEU9Oomlk5OMvdLGkGSRNQcsoA== 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: Recently, we see reports [1] of a warning that triggers due to move_page_tables() doing a downward and overlapping move on a mutually-aligned offset within a PMD. By mutual alignment, I mean the source and destination addresses of the mremap are at the same offset within a PMD. This mutual alignment along with the fact that the move is downward is sufficient to cause a warning related to having an allocated PMD that does not have PTEs in it. This warning will only trigger when there is mutual alignment in the move operation. A solution, as suggested by Linus Torvalds [2], is to initiate the copy process at the PMD level whenever such alignment is present. Implementing this approach will not only prevent the warning from being triggered, but it will also optimize the operation as this method should enhance the speed of the copy process whenever there's a possibility to start copying at the PMD level. Some more points: a. The optimization can be done only when both the source and destination of the mremap do not have anything mapped below it up to a PMD boundary. I add support to detect that. b. #1 is not a problem for the call to move_page_tables() from exec.c as nothing is expected to be mapped below the source. However, for non-overlapping mutually aligned moves as triggered by mremap(2), I added support for checking such cases. c. I currently only optimize for PMD moves, in the future I/we can build on this work and do PUD moves as well if there is a need for this. But I want to take it one step at a time. d. We need to be careful about mremap of ranges within the VMA itself. For this purpose, I added checks to determine if the address after alignment falls within its VMA itself. [1] https://lore.kernel.org/all/ZB2GTBD%2FLWTrkOiO@dhcp22.suse.cz/ [2] https://lore.kernel.org/all/CAHk-=whd7msp8reJPfeGNyt0LiySMT0egExx3TVZSX3Ok6X=9g@mail.gmail.com/ Reviewed-by: Lorenzo Stoakes Suggested-by: Linus Torvalds Signed-off-by: Joel Fernandes (Google) Acked-by: Michal Hocko --- mm/mremap.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/mm/mremap.c b/mm/mremap.c index 11e06e4ab33b..1011326b7b80 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -489,6 +489,53 @@ static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma, return moved; } +/* + * A helper to check if a previous mapping exists. Required for + * move_page_tables() and realign_addr() to determine if a previous mapping + * exists before we can do realignment optimizations. + */ +static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_align, + unsigned long mask) +{ + unsigned long addr_masked = addr_to_align & mask; + + /* + * If @addr_to_align of either source or destination is not the beginning + * of the corresponding VMA, we can't align down or we will destroy part + * of the current mapping. + */ + if (vma->vm_start != addr_to_align) + return false; + + /* + * Make sure the realignment doesn't cause the address to fall on an + * existing mapping. + */ + return find_vma_intersection(vma->vm_mm, addr_masked, vma->vm_start) == NULL; +} + +/* Opportunistically realign to specified boundary for faster copy. */ +static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old_vma, + unsigned long *new_addr, struct vm_area_struct *new_vma, + unsigned long mask) +{ + /* Skip if the addresses are already aligned. */ + if ((*old_addr & ~mask) == 0) + return; + + /* Only realign if the new and old addresses are mutually aligned. */ + if ((*old_addr & ~mask) != (*new_addr & ~mask)) + return; + + /* Ensure realignment doesn't cause overlap with existing mappings. */ + if (!can_align_down(old_vma, *old_addr, mask) || + !can_align_down(new_vma, *new_addr, mask)) + return; + + *old_addr = *old_addr & mask; + *new_addr = *new_addr & mask; +} + unsigned long move_page_tables(struct vm_area_struct *vma, unsigned long old_addr, struct vm_area_struct *new_vma, unsigned long new_addr, unsigned long len, @@ -508,6 +555,14 @@ unsigned long move_page_tables(struct vm_area_struct *vma, return move_hugetlb_page_tables(vma, new_vma, old_addr, new_addr, len); + /* + * If possible, realign addresses to PMD boundary for faster copy. + * Only realign if the mremap copying hits a PMD boundary. + */ + if ((vma != new_vma) + && (len >= PMD_SIZE - (old_addr & ~PMD_MASK))) + try_realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK); + flush_cache_range(vma, old_addr, old_end); mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, old_addr, old_end); @@ -577,6 +632,13 @@ unsigned long move_page_tables(struct vm_area_struct *vma, mmu_notifier_invalidate_range_end(&range); + /* + * Prevent negative return values when {old,new}_addr was realigned + * but we broke out of the above loop for the first PMD itself. + */ + if (len + old_addr < old_end) + return 0; + return len + old_addr - old_end; /* how much done */ }