From mboxrd@z Thu Jan 1 00:00:00 1970 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 9EC5DEB64D9 for ; Sat, 17 Jun 2023 22:49:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3F978E0001; Sat, 17 Jun 2023 18:49:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF9326B0072; Sat, 17 Jun 2023 18:49:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B8F9F8E0001; Sat, 17 Jun 2023 18:49:07 -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 A58F26B0071 for ; Sat, 17 Jun 2023 18:49:07 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6FEE8C0240 for ; Sat, 17 Jun 2023 22:49:07 +0000 (UTC) X-FDA: 80913731934.23.2253CEC Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) by imf07.hostedemail.com (Postfix) with ESMTP id 806A640003 for ; Sat, 17 Jun 2023 22:49:05 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=T0Bx7HAt; spf=pass (imf07.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.167.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687042145; 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:in-reply-to:references:references:dkim-signature; bh=xv7EgGhL36l2E3gdf0tc0pHjyAPM7mI/kMuNzs8yeVk=; b=Bsb058j9Roypy3QGwojA4G5eTK27RrFhOxgzRvQJA465PwKOdgcGE6uXT1cZhAXFBK3HMk dZNYeZkXLOGx69EDEJHyjrnKclgZ73+rRRkSmVADzOGIB2yNhZpuCecTRx30fTgNFU1T3g aZaoZ9wmDgkv1PRQD9lkuJTTfavmn7Q= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=T0Bx7HAt; spf=pass (imf07.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.167.41 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687042145; a=rsa-sha256; cv=none; b=1GlI0PV2mV24eOZHdqA9hva1soMZ341vIuqzNgxXdLsk4lv2PgI0nPmVeuizzMk/LmwE90 fDmXjg4LaulIeTh+BASKmsrLwqY6RpXxiNT0MNWDkvOipNp5EpBpNls+mWfBJi4ZjiWhh2 sqX09YkfgOcw9dmaGsZBZ2LbGT8TFBE= Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-4f7677a94d1so2707284e87.0 for ; Sat, 17 Jun 2023 15:49:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687042143; x=1689634143; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=xv7EgGhL36l2E3gdf0tc0pHjyAPM7mI/kMuNzs8yeVk=; b=T0Bx7HAtoNsb2RhxIprcNzY1AvNE/10TP8/AueXP0lBoMkn0MwG3tziLqbd0/4hssg QZ27eLLDbQcIEYWNJjQnadOUMFHV+BPja3BOxMiI0tnGADs6WSG2enKHdrP1Uv1NcPcv 4dd5SPP0NG0R3S+YVFfllCQDwzn+eU4gwa00h/IO6JPEfh7StPGYZILbHvlPZh7IlSvk fJVnlmkVxefUszY1C7aeYGac8HTDeUELrcI2ioAfee9sIa1fp6Vkt8B1+1fo4RFhmKs6 vAmK7Bhgp24LKjlO0bJt0oQItInBB6Vz7LDcNAv3V7z0XnAU5eCpRbSvuSXkahg5Y60f 4rNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687042143; x=1689634143; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=xv7EgGhL36l2E3gdf0tc0pHjyAPM7mI/kMuNzs8yeVk=; b=QRh0yVF1q2gXE4ncJZ1SyP51cKiS2NTrHuIiyEt8yj1bNlRbc0iUZQkPYHRINge0XT iU554ODxCCaSCcn6MajYNHR/DJ3oXYodsl1REt0sLSzkbB5ZKK9XuM0c8WJGT8tTAHFV ar0VQdgaMqhH+bgznkNkHu0GTI+taO/lSSHsQBHPkhg1V14WaiRSAKHxcrMpDv62QzZw LLxY4HAvJdnG2X9THFiAJV7jCUMPfFX8oRsaSjsuV64+ijjyixExGRYhH553ErH9vZGp zqdED+xAsyLDpP45ycTKCISs5L2oAY+MHq6Z2uoHX2TD+qMxbM1YJV470oaIWdbLB9dk G+Ng== X-Gm-Message-State: AC+VfDx/GHlFQTx/pwtA19gaLcxjZxNXl8RL6hil2BwPxTbHxmcSR58W M4HgQ55kFNkPsRSUFEnkxRY= X-Google-Smtp-Source: ACHHUZ6CJUCD5yVPS+FecHR2u+OUus/P3dyrT5K3iQqnI0oC46YgND/NIJt3MWsebEnKIKKKZCU1KQ== X-Received: by 2002:a19:ca52:0:b0:4f7:68eb:7c27 with SMTP id h18-20020a19ca52000000b004f768eb7c27mr2865189lfj.29.1687042143106; Sat, 17 Jun 2023 15:49:03 -0700 (PDT) Received: from localhost ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.gmail.com with ESMTPSA id l15-20020a7bc44f000000b003f90846d8a7sm2090784wmi.6.2023.06.17.15.49.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jun 2023 15:49:02 -0700 (PDT) Date: Sat, 17 Jun 2023 23:49:01 +0100 From: Lorenzo Stoakes To: "Joel Fernandes (Google)" Cc: linux-kernel@vger.kernel.org, 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 , Vineeth Pillai Subject: Re: [PATCH v4 1/7] mm/mremap: Optimize the start addresses in move_page_tables() Message-ID: References: <20230531220807.2048037-1-joel@joelfernandes.org> <20230531220807.2048037-2-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230531220807.2048037-2-joel@joelfernandes.org> X-Rspamd-Queue-Id: 806A640003 X-Rspam-User: X-Stat-Signature: yhtnzfiormiho1y1rw7e9wbm1k88km4a X-Rspamd-Server: rspam01 X-HE-Tag: 1687042145-12188 X-HE-Meta: U2FsdGVkX18M/wFgVDQ8TJL2alHmmcQXHLD9jFYIbtT4U+yVIKy5oSLXBdmAq9dm6SLZy6GG/3w1GRgBwAeKZr2yNYKQ83q7nVSPtHnJg7lGGbytIFLKuYNnRRtvmxuUeQNU4bLZ2d1Y3pJM9bFgQ0sFDwkVwEPEVVEN6KS+5iEcaybkykAHO0djOYfaZoLMhnQan7yAatd+9kfCaB3nbCMMbG/gJPUdSiWC0bEdte2eSjDg8eNlMHD4QyFJElFD9qTFXHP5NTSpTclkMavRKk/rQ3rjYybxIBd8lAKGonKJjCbi34hBwewKSnLHg45wRVP4OwFdvLqqG9Fqs+k8vw5sEg2Hl8kkcZ9g4PWXx7vgqeW+lT+2VmIKF/DOPIrsvs1MdABUalc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752mvJIGkKZPXJ/M2XZXRaeYqBE8m93byeCng+VAdsJa7DGdwWLzUuMwkn1Yav3noLzgdLpVB1vcUuYNk4+0WAXTPsO5bsGk80yhxk8NGpakK/X/02lDmIPha6TMVaLkSsddml780= 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: On Wed, May 31, 2023 at 10:08:01PM +0000, Joel Fernandes (Google) wrote: > 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. #a 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 to align > is not the beginning of the VMA which that address corresponds to. > > [1] https://lore.kernel.org/all/ZB2GTBD%2FLWTrkOiO@dhcp22.suse.cz/ > [2] https://lore.kernel.org/all/CAHk-=whd7msp8reJPfeGNyt0LiySMT0egExx3TVZSX3Ok6X=9g@mail.gmail.com/ > > Suggested-by: Linus Torvalds > Signed-off-by: Joel Fernandes (Google) > --- > mm/mremap.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 61 insertions(+) > > diff --git a/mm/mremap.c b/mm/mremap.c > index 411a85682b58..bf355e4d6bd4 100644 > --- a/mm/mremap.c > +++ b/mm/mremap.c > @@ -478,6 +478,51 @@ 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; > + struct vm_area_struct *prev = NULL, *cur = NULL; > + > + /* > + * 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; See below, I think we can eliminate this check. > + > + /* > + * Find the VMA before @vma to see if it subsumes the masked address. > + * The mmap write lock is held here so the lookup is safe. > + */ > + cur = find_vma_prev(vma->vm_mm, vma->vm_start, &prev); > + if (WARN_ON_ONCE(cur != vma)) > + return false; > + > + return !prev || prev->vm_end <= addr_masked; This is a bit clunky, and I don't think we need the WARN_ON_ONCE() check if we're under the mmap_lock. How about something like:- return find_vma_intersection(vma->mm, addr_masked, vma->vm_start) == NULL; Which explicitly asserts that the range in [addr_masked, vma->vm_start) is empty. But actually, we should be able to go further and replace the previous check with:- return find_vma_intersection(vma->mm, addr_masked, addr_to_align) == NULL; Which will fail if addr_to_align is offset within the VMA. > +} > + > +/* Opportunistically realign to specified boundary for faster copy. */ > +static void realign_addr(unsigned long *old_addr, struct vm_area_struct *old_vma, Something of a nit, but this isn't _always_ realigning the address, so perhaps something like maybe_realign_addr() or try_realign_addr() is better? This is probably debatable, as the comment already explains it is opportunistic :) > + unsigned long *new_addr, struct vm_area_struct *new_vma, > + unsigned long mask) > +{ > + bool mutually_aligned = (*old_addr & ~mask) == (*new_addr & ~mask); > + > + if ((*old_addr & ~mask) && mutually_aligned I may be misunderstanding something here, but doesn't the first condition here disallow for offset into PMD == 0? Why? > + && can_align_down(old_vma, *old_addr, mask) > + && can_align_down(new_vma, *new_addr, mask)) { > + *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, > @@ -493,6 +538,15 @@ unsigned long move_page_tables(struct vm_area_struct *vma, > > old_end = old_addr + len; > > + /* > + * If possible, realign addresses to PMD boundary for faster copy. > + * Don't align for intra-VMA moves as we may destroy existing mappings. > + */ > + if ((vma != new_vma) Nit but these parens aren't needed. Also if we're deferring the decision as to whether we realign to this function, why are we doing this check here and not here? It feels like it'd be neater to keep all the conditions (including the length one) together in one place. > + && (len >= PMD_SIZE - (old_addr & ~PMD_MASK))) { You don't mention this condition in the above comment (if we have this altogether as part of the realign function could comment separately there) - so we only go ahead and do this optimisation if the length of the remap is such that the entire of old_addr -> end of its PMD (and thus the same for new_addr) is copied? I may be missing something/being naive here, but can't we just do a similar check to the one done for space _below_ the VMA to see if [end, (end of PMD)) is equally empty? > + realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK); > + } > + > if (is_vm_hugetlb_page(vma)) > return move_hugetlb_page_tables(vma, new_vma, old_addr, > new_addr, len); > @@ -565,6 +619,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; > + I find this a little iffy, I mean I see that if you align [old,new]_addr to PMD, then from then on in you're relying on the fact that the loop is just going from old_addr (now aligned) -> old_end and thus has the correct length. Can't we just fix this issue by correcting len? If you take my review above which checks len in [maybe_]realign_addr(), you could take that as a pointer and equally update that. Then you can drop this check. Also I am concerned in the hugetlb case -> len is passed to move_hugetlb_page_tables() which is now strictly incorrect, I wonder if this could cause an issue? Correcting len seems the neat way of addressing this. > return len + old_addr - old_end; /* how much done */ > } > > -- > 2.41.0.rc2.161.g9c6817b8e7-goog >