From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 242B9C7EE23 for ; Wed, 31 May 2023 22:08:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbjEaWIn (ORCPT ); Wed, 31 May 2023 18:08:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230323AbjEaWIh (ORCPT ); Wed, 31 May 2023 18:08:37 -0400 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 628BB139 for ; Wed, 31 May 2023 15:08:19 -0700 (PDT) Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-6260bb94363so1687256d6.0 for ; Wed, 31 May 2023 15:08:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570898; x=1688162898; 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=xcpMDk08TnAKM11DZYaTQHoJHdMHYlx0PmCfJSmuqxQ=; b=wEwknpWuACfVk2BLZAAwhK1P+A8Agyg2LkaiMrZv/vk+Jqv3PjEA3zHf+KOHmK4Pxx sYYbfV/UmdJ7qbsavZh91UeTFvV/aLbtLtNOxKYXZP0bQ+FrDbx/ezrzjN+6DuOM96+6 CyyRczA9px0WqR6nr3eZ09ZYqir15Ii3x7h8o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570898; x=1688162898; 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=xcpMDk08TnAKM11DZYaTQHoJHdMHYlx0PmCfJSmuqxQ=; b=dxRU9l108q8FcG0R4PU8QybbwoRVEfjf/ynTnu5cJ6z5nQf+GRTBO8K5LBOBk41SKj 7ftxc7vub07Iazd/SX5n8Ip/U8DQcxpmNaUpJNskbfJETcEiPKdI0LqBY5BadfX0JKpj HmkJrj/RW6IWsu1a1w7VhSxQO0oVv0i5UVP8+NY88NxQMUP4xBeoQ3ezf3sykR+Nk4i0 hmQkCoz/JRNaqL4R8xvIhMm/vFybpHIcm9lzoDLheNbLHgS6iA5gXhc2osHhj7D73ZQo nSL4y8yIbdAQhyH3hQ2LfNlLfuSKWADwd5lldVtZZ8lcZcbwU77DFApV3Y4kbRNBF+Rj CofQ== X-Gm-Message-State: AC+VfDz1zS0LoMVNUNXOz7Xh3c8Z4n2pEbuGEaXpPL8TgFEkunipmdYH g4zDE5xtM7PkRKxJl09xBIDASZnZas9luMvJ604= X-Google-Smtp-Source: ACHHUZ6VD/g4jSj2d9B84SOdyc6mJVRalNKUWWpk2+9CxmTsHgUmiKwb5GcyFfj7v6tYE/kvvmPfuQ== X-Received: by 2002:a05:6214:509d:b0:570:5389:2216 with SMTP id kk29-20020a056214509d00b0057053892216mr16116075qvb.22.1685570898020; Wed, 31 May 2023 15:08:18 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:17 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Linus Torvalds , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 1/7] mm/mremap: Optimize the start addresses in move_page_tables() Date: Wed, 31 May 2023 22:08:01 +0000 Message-ID: <20230531220807.2048037-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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-=3Dwhd7msp8reJPfeGNyt0LiySMT0egExx3TVZ= SX3Ok6X=3D9g@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, struc= t vm_area_struct *vma, return moved; } =20 +/* + * 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 =3D addr_to_align & mask; + struct vm_area_struct *prev =3D NULL, *cur =3D 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 !=3D addr_to_align) + return false; + + /* + * 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 =3D find_vma_prev(vma->vm_mm, vma->vm_start, &prev); + if (WARN_ON_ONCE(cur !=3D vma)) + return false; + + return !prev || prev->vm_end <=3D addr_masked; +} + +/* Opportunistically realign to specified boundary for faster copy. */ +static void realign_addr(unsigned long *old_addr, struct vm_area_struct *o= ld_vma, + unsigned long *new_addr, struct vm_area_struct *new_vma, + unsigned long mask) +{ + bool mutually_aligned =3D (*old_addr & ~mask) =3D=3D (*new_addr & ~mask); + + if ((*old_addr & ~mask) && mutually_aligned + && can_align_down(old_vma, *old_addr, mask) + && can_align_down(new_vma, *new_addr, mask)) { + *old_addr =3D *old_addr & mask; + *new_addr =3D *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, =20 old_end =3D old_addr + len; =20 + /* + * 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 !=3D new_vma) + && (len >=3D PMD_SIZE - (old_addr & ~PMD_MASK))) { + 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, =20 mmu_notifier_invalidate_range_end(&range); =20 + /* + * 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 */ } =20 --=20 2.41.0.rc2.161.g9c6817b8e7-goog From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD12CC77B73 for ; Wed, 31 May 2023 22:08:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230437AbjEaWIq (ORCPT ); Wed, 31 May 2023 18:08:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230409AbjEaWIk (ORCPT ); Wed, 31 May 2023 18:08:40 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A81C1BE for ; Wed, 31 May 2023 15:08:20 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-75b3645fb1fso80240085a.1 for ; Wed, 31 May 2023 15:08:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570899; x=1688162899; 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=IKyKgK7RUrVgt5O/vBjlSKA4QqkZUlv4FmPm/U0omy4=; b=obKUiscoppUsJUJ3/7hX1SsM+6rEew9ZQfKAMh3vWfYW1rmT9yIKGAZubZxIr4LEiG +Al4BVanJStN5/cLaoxvQfUA8HotrTrtltYR3OAN7CO/qw/V5C9yoijvOQuFNRqFFvUd FvFegsoDVgzfl78OoRhTbHXYk02PvrhI2s2+A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570899; x=1688162899; 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=IKyKgK7RUrVgt5O/vBjlSKA4QqkZUlv4FmPm/U0omy4=; b=V+5TEjNbdtShdPjvv5uwL5w+Fi5VBjA00Y+bVRLxRpUmqD16jFSkKskT5SXrgRg3kU 5hF56Cua/3XNvVaQb/imd1/jAGDbjNNTHjzEBJuOhDW3Qi8eexmfEVIVWcZo4UpW7Qs6 z5a44SeJW0xSLvXshGi4eicVJ0Ko61yUpXcFHk5nP13VA2C2kZTXuSpQ+T8JEJqNhPVk Nv8P+xTUAU7j32z3h5STngcqyNevJEqXx1ww7Fmu7z3ypD/qtIMHd/HmDfqsanP69IJH AkveiEOBBYcRc2tkOmVY3Z5nappXcoIzVTbFPWhg0v4zvAmEIXAbsD/C0VJ9uNHJHlCJ WeHw== X-Gm-Message-State: AC+VfDwhuMUAIoblBBWuvPhU1WcN2GKEREzwnwNiCiZLyNMEt85pzaq9 RM2va0cSvlMIFwReCvjhyIgSw4ENPJ4/eJcn6nE= X-Google-Smtp-Source: ACHHUZ7AkCLZ1IadOtiNpTKRumVwd03RrWCxqJfgVOKG6KWsEhn44uxV0VQrpA8Ip4FERNK4pI0qpA== X-Received: by 2002:ac8:5a83:0:b0:3f5:1d97:1922 with SMTP id c3-20020ac85a83000000b003f51d971922mr15388023qtc.2.1685570899022; Wed, 31 May 2023 15:08:19 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:18 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 2/7] mm/mremap: Allow moves within the same VMA for stack Date: Wed, 31 May 2023 22:08:02 +0000 Message-ID: <20230531220807.2048037-3-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" For the stack move happening in shift_arg_pages(), the move is happening within the same VMA which spans the old and new ranges. In case the aligned address happens to fall within that VMA, allow such moves and don't abort the optimization. In the mremap case, we cannot allow any such moves as will end up destroying some part of the mapping (either the source of the move, or part of the existing mapping). So just avoid it for mremap. Signed-off-by: Joel Fernandes (Google) --- fs/exec.c | 2 +- include/linux/mm.h | 2 +- mm/mremap.c | 40 ++++++++++++++++++---------------------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 7c44d0c65b1b..7a7217353115 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -707,7 +707,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, = unsigned long shift) * process cleanup to remove whatever mess we made. */ if (length !=3D move_page_tables(vma, old_start, - vma, new_start, length, false)) + vma, new_start, length, false, true)) return -ENOMEM; =20 lru_add_drain(); diff --git a/include/linux/mm.h b/include/linux/mm.h index 1f79667824eb..dd415cd2493d 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2265,7 +2265,7 @@ int get_cmdline(struct task_struct *task, char *buffe= r, int buflen); extern 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, - bool need_rmap_locks); + bool need_rmap_locks, bool for_stack); =20 /* * Flags used by change_protection(). For now we make it a bitmap so diff --git a/mm/mremap.c b/mm/mremap.c index bf355e4d6bd4..0283f9f43d92 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -479,22 +479,23 @@ static bool move_pgt_entry(enum pgt_entry entry, stru= ct vm_area_struct *vma, } =20 /* - * 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. + * A helper to check if aligning down is OK. The newly aligned address sho= uld + * not fall on any existing mapping otherwise we don't align. For the stack + * moving down, that's a special move within the VMA that is created to sp= an + * the source and destination of the move, so we make an exception for it. */ static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_= to_align, - unsigned long mask) + unsigned long mask, bool for_stack) { unsigned long addr_masked =3D addr_to_align & mask; struct vm_area_struct *prev =3D NULL, *cur =3D NULL; =20 /* - * 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. + * Other than for stack moves, 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 !=3D addr_to_align) + if (!for_stack && vma->vm_start !=3D addr_to_align) return false; =20 /* @@ -511,13 +512,13 @@ static bool can_align_down(struct vm_area_struct *vma= , unsigned long addr_to_ali /* Opportunistically realign to specified boundary for faster copy. */ static void realign_addr(unsigned long *old_addr, struct vm_area_struct *o= ld_vma, unsigned long *new_addr, struct vm_area_struct *new_vma, - unsigned long mask) + unsigned long mask, bool for_stack) { bool mutually_aligned =3D (*old_addr & ~mask) =3D=3D (*new_addr & ~mask); =20 if ((*old_addr & ~mask) && mutually_aligned - && can_align_down(old_vma, *old_addr, mask) - && can_align_down(new_vma, *new_addr, mask)) { + && can_align_down(old_vma, *old_addr, mask, for_stack) + && can_align_down(new_vma, *new_addr, mask, for_stack)) { *old_addr =3D *old_addr & mask; *new_addr =3D *new_addr & mask; } @@ -526,7 +527,7 @@ static void realign_addr(unsigned long *old_addr, struc= t vm_area_struct *old_vma 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, - bool need_rmap_locks) + bool need_rmap_locks, bool for_stack) { unsigned long extent, old_end; struct mmu_notifier_range range; @@ -538,14 +539,9 @@ unsigned long move_page_tables(struct vm_area_struct *= vma, =20 old_end =3D old_addr + len; =20 - /* - * 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 !=3D new_vma) - && (len >=3D PMD_SIZE - (old_addr & ~PMD_MASK))) { - realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK); - } + /* If possible, realign addresses to PMD boundary for faster copy. */ + if (len >=3D PMD_SIZE - (old_addr & ~PMD_MASK)) + realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK, for_stack); =20 if (is_vm_hugetlb_page(vma)) return move_hugetlb_page_tables(vma, new_vma, old_addr, @@ -694,7 +690,7 @@ static unsigned long move_vma(struct vm_area_struct *vm= a, } =20 moved_len =3D move_page_tables(vma, old_addr, new_vma, new_addr, old_len, - need_rmap_locks); + need_rmap_locks, false); if (moved_len < old_len) { err =3D -ENOMEM; } else if (vma->vm_ops && vma->vm_ops->mremap) { @@ -708,7 +704,7 @@ static unsigned long move_vma(struct vm_area_struct *vm= a, * and then proceed to unmap new area instead of old. */ move_page_tables(new_vma, new_addr, vma, old_addr, moved_len, - true); + true, false); vma =3D new_vma; old_len =3D new_len; old_addr =3D new_addr; --=20 2.41.0.rc2.161.g9c6817b8e7-goog From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39A33C7EE29 for ; Wed, 31 May 2023 22:08:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230471AbjEaWIv (ORCPT ); Wed, 31 May 2023 18:08:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230422AbjEaWIm (ORCPT ); Wed, 31 May 2023 18:08:42 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61D38E41 for ; Wed, 31 May 2023 15:08:21 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id 6a1803df08f44-6260de7cfaeso2198446d6.2 for ; Wed, 31 May 2023 15:08:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570900; x=1688162900; 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=hVpzQtIDQfKWEqbseo+tZ+eEsBzkT6BTR9cKFnwSYiE=; b=UVc+juOdhvc+cdjx5ehHJJwwP68jGUuabxnGl2sSfw5+c3rM6PEEz7zmzj6t/Ykjlz IEsXCnKiFlo+h2JixjtD4OcEGGHRU/PJBz4uJQNMcO2ZId0sz4Pi773QRZBHIET4s7rS u0CUYL69ZSD7t8s8GCIDl4lvJdB5gvJ7I/eFc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570900; x=1688162900; 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=hVpzQtIDQfKWEqbseo+tZ+eEsBzkT6BTR9cKFnwSYiE=; b=PV8l5PDrEtOb1lgDDGuWGt0YfoyJXsoJY6OEPo1d2qu8u+W26yTVdS3wkcDKMfZLBD Fbum5qRSWZh86sjcIP4NtWM7cv3FfOQ/WbL8rsTMt6zq9ack00q/Be3FbY1Hu5axbCOb c6Nf9lWG6oFmEvwvVLqcFEWfoSVUtNXNXbOBD7q7K/jY8EmzlpvcN0wjwwV4IgKsyErh Tj3EfaNGwzHXBcXR36EcABDyjB29jp+VexCPc8q2hurcLjf+1z1rbb0MSLMwYLwx0TLv kyezXcpAJy3mRdzFDFpSCZ1xHCkc2UBraJRs7RQNN84yAbhuVuLldBVZIftHZS8SS/M3 R0lg== X-Gm-Message-State: AC+VfDyRFtSxIU70rwN4XkYWd0o3V7bR9V/byxkDMs2x5U1+mhVAobAf 2h/tBHgYzebFL4pxRWEA779BQXZOPUW4q3P7ShI= X-Google-Smtp-Source: ACHHUZ6MK0NGbKvNzGTkIT5KQRbjPCfTItoxAs0WkJkwEfUWMktvJqSRp0xRq7Tiv2SENcLbvIsx6Q== X-Received: by 2002:a05:6214:1ccc:b0:625:1c04:6761 with SMTP id g12-20020a0562141ccc00b006251c046761mr8116673qvd.27.1685570899912; Wed, 31 May 2023 15:08:19 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:19 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 3/7] selftests: mm: Fix failure case when new remap region was not found Date: Wed, 31 May 2023 22:08:03 +0000 Message-ID: <20230531220807.2048037-4-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When a valid remap region could not be found, the source mapping is not cleaned up. Fix the goto statement such that the clean up happens. Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/mm/mremap_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 5c3773de9f0f..6822d657f589 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -316,7 +316,7 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, if (addr + c.dest_alignment < addr) { ksft_print_msg("Couldn't find a valid region to remap to\n"); ret =3D -1; - goto out; + goto clean_up_src; } addr +=3D c.dest_alignment; } --=20 2.41.0.rc2.161.g9c6817b8e7-goog From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24478C7EE29 for ; Wed, 31 May 2023 22:08:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230481AbjEaWIz (ORCPT ); Wed, 31 May 2023 18:08:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230426AbjEaWIn (ORCPT ); Wed, 31 May 2023 18:08:43 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FCB719B for ; Wed, 31 May 2023 15:08:22 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id d75a77b69052e-3f7fdbdf68eso68344161cf.1 for ; Wed, 31 May 2023 15:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570901; x=1688162901; 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=dpicKoUd2HYKUWTIhMS6ThagQPd/JY7bC7rJfvoQE2Y=; b=C2nanwM4s2qBeaw7g/O1s/IU/DVR7Y2rNl+A3MbDPeVyLvdaIPdY6T2eaGhET6A0sF up/AOMYaez92DN3FPLb14JMGsjTAdM2hcriEezhuAu++ExO/CNN3lO1c6isZ2nAox6kS 6vZCyox2c8xGrqTdhmbAICcrkrOV8Zq0SaMjs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570901; x=1688162901; 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=dpicKoUd2HYKUWTIhMS6ThagQPd/JY7bC7rJfvoQE2Y=; b=IkiVZ3l19f3frdXT+iTm1uNxqdBs5tvXQROOlUK/+StHGuexQhl7rmz5CJXqYkvRJ8 +oPKSLhF+OTQRRNsPXhyGkjJbMplF9X8IZRvr2XIdfy1+sNUaLyGMD6p6IQFd/xqWwUM fliWajnv6ohRl+j6YBe27XLD7GJGAgA6uGc8j2CXzmiDjlthuFvAryuUP1W689AfeXEp VGCQukqt2O6P7C3rummFlub4DuawiQ5DHtLr8IeB9otYScE/TzVr0GcqqIg9rVN02mEQ x/V4hB7wuJoE/lIh5g5MtIQcw4zBI73zdB/p6C/fjkeR9/9hOkskToA2KvLiDGGZYt8A z7Ag== X-Gm-Message-State: AC+VfDxlQkSIOk1kJLodT8u7T7IZauXKkaT1sOUsM0xnU4ozJn/i/z8/ MJDeZqLvgQ0S5K9u0IG0zoLoWiLulQGaG0455dg= X-Google-Smtp-Source: ACHHUZ4fuBeiJwYmTyjlbh3vNdxD6U2rVYsiG5NEaciB66hwrL3r2K1rWBnhNt+DCnNTWPl/qP0dEg== X-Received: by 2002:a05:622a:189d:b0:3f4:cfed:96bd with SMTP id v29-20020a05622a189d00b003f4cfed96bdmr9701607qtc.21.1685570900832; Wed, 31 May 2023 15:08:20 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:20 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 4/7] selftests: mm: Add a test for mutually aligned moves > PMD size Date: Wed, 31 May 2023 22:08:04 +0000 Message-ID: <20230531220807.2048037-5-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch adds a test case to check if a PMD-alignment optimization successfully happens. I add support to make sure there is some room before the source mapping, otherwise the optimization to trigger PMD-aligned move will be disabled as the kernel will detect that a mapping before the source exists and such optimization becomes impossible. Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/mm/mremap_test.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 6822d657f589..6304eb0947a3 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -44,6 +44,7 @@ enum { _1MB =3D 1ULL << 20, _2MB =3D 2ULL << 20, _4MB =3D 4ULL << 20, + _5MB =3D 5ULL << 20, _1GB =3D 1ULL << 30, _2GB =3D 2ULL << 30, PMD =3D _2MB, @@ -235,6 +236,11 @@ static void *get_source_mapping(struct config c) unsigned long long mmap_min_addr; =20 mmap_min_addr =3D get_mmap_min_addr(); + /* + * For some tests, we need to not have any mappings below the + * source mapping. Add some headroom to mmap_min_addr for this. + */ + mmap_min_addr +=3D 10 * _4MB; =20 retry: addr +=3D c.src_alignment; @@ -434,7 +440,7 @@ static int parse_args(int argc, char **argv, unsigned i= nt *threshold_mb, return 0; } =20 -#define MAX_TEST 13 +#define MAX_TEST 14 #define MAX_PERF_TEST 3 int main(int argc, char **argv) { @@ -500,6 +506,10 @@ int main(int argc, char **argv) test_cases[12] =3D MAKE_TEST(PUD, PUD, _2GB, NON_OVERLAPPING, EXPECT_SUCC= ESS, "2GB mremap - Source PUD-aligned, Destination PUD-aligned"); =20 + /* Src and Dest addr 1MB aligned. 5MB mremap. */ + test_cases[13] =3D MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SU= CCESS, + "5MB mremap - Source 1MB-aligned, Destination 1MB-aligned"); + perf_test_cases[0] =3D MAKE_TEST(page_size, page_size, _1GB, NON_OVERLAP= PING, EXPECT_SUCCESS, "1GB mremap - Source PTE-aligned, Destination PTE-aligned"); /* --=20 2.41.0.rc2.161.g9c6817b8e7-goog From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B36DEC7EE23 for ; Wed, 31 May 2023 22:08:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230414AbjEaWI6 (ORCPT ); Wed, 31 May 2023 18:08:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230383AbjEaWIn (ORCPT ); Wed, 31 May 2023 18:08:43 -0400 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5514E45 for ; Wed, 31 May 2023 15:08:22 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id af79cd13be357-75b0b5c9eb8so3046885a.1 for ; Wed, 31 May 2023 15:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570901; x=1688162901; 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=5uxB3oRU/w04R3PYxIzVnLNEBr3g7H+Udo5r270v9fQ=; b=KZ9VWWx/xG7DewQ/8d+zcl3ld9GrYdlEo/fNzEJu2I3fo1LrpAp8r9/ibWQsUMx58r CoxF/f179s/puFhFnoF5Ey6W27dHDYy61tHYsBYjFECoFjwNB6KbmP6kwo8P20umokpb BwdyqxWQhW1JptP5/nrvDL8ITCLwrxHrx+5vE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570901; x=1688162901; 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=5uxB3oRU/w04R3PYxIzVnLNEBr3g7H+Udo5r270v9fQ=; b=A1P8jqZLfO70mxa7Xhi4Q+K+iTR2ZltaWRjPxveBsQ0aXzzd2R7Gy2KVgyVD6Avbtx e9PlDR8EGOWzdJSio061eyfZfQr06m9fvIJatB1cbojTp8WAvcRi9lhiU7aIIbzjDUFK FdLSsZVotK8FBYLLft9a6oX9XAWzu3WG2qDDFY7fSRS/+DCFb2ocz/V+Jc4xVCBuHB/b hx8a6mqH3pcwO2qdNdz0EmMKJVIlFenfI+nfSC80Pq/T9dHRlfiPSvpb1upruPPrJy+L 1vU4SztNbPTfaekVE1vYePnX7EbMb4Cz4QXz7BFggEggo+y+VRnMMsGtfdpJHlk2BlX5 WzAQ== X-Gm-Message-State: AC+VfDy7ff9J17PsuwOdn1mXmDSykwxilH4EYw+JGHLvDy0tDbTQQGLG UJO4jMRSt+xzdUT8VSpG3y2+68FJhZ2DfTifbWk= X-Google-Smtp-Source: ACHHUZ4C+oQ1AVbo4zSzekEvTy6KfJRvqrvWFBxnB8psxVFZyu0vYdrpSSs8wuMpjckZfKwWwYViLQ== X-Received: by 2002:a05:6214:2a46:b0:623:9a41:e9f9 with SMTP id jf6-20020a0562142a4600b006239a41e9f9mr9457344qvb.35.1685570901614; Wed, 31 May 2023 15:08:21 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:21 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 5/7] selftests: mm: Add a test for remapping to area immediately after existing mapping Date: Wed, 31 May 2023 22:08:05 +0000 Message-ID: <20230531220807.2048037-6-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch adds support for verifying that we correctly handle the situation where something is already mapped before the destination of the r= emap. Any realignment of destination address and PMD-copy will destroy that existing mapping. In such cases, we need to avoid doing the optimization. To test this, we map an area called the preamble before the remap region. Then we verify after the mremap operation that this region did not = get corrupted. Putting some prints in the kernel, I verified that we optimize correctly in different situations: Optimize when there is alignment and no previous mapping (this is tested by previous patch). can_align_down(old_vma->vm_start=3D2900000, old_addr=3D2900000, mask=3D-209= 7152): 0 can_align_down(new_vma->vm_start=3D2f00000, new_addr=3D2f00000, mask=3D-209= 7152): 0 =3D=3D=3D Starting move_page_tables =3D=3D=3D Doing PUD move for 2800000 -> 2e00000 of extent=3D200000 <-- Optimization Doing PUD move for 2a00000 -> 3000000 of extent=3D200000 Doing PUD move for 2c00000 -> 3200000 of extent=3D200000 Don't optimize when there is alignment but there is previous mapping (this is tested by this patch). Notice that can_align_down() returns 1 for the destination mapping as we detected there is something there. can_align_down(old_vma->vm_start=3D2900000, old_addr=3D2900000, mask=3D-209= 7152): 0 can_align_down(new_vma->vm_start=3D5700000, new_addr=3D5700000, mask=3D-209= 7152): 1 =3D=3D=3D Starting move_page_tables =3D=3D=3D Doing move_ptes for 2900000 -> 5700000 of extent=3D100000 <-- Unoptimized Doing PUD move for 2a00000 -> 5800000 of extent=3D200000 Doing PUD move for 2c00000 -> 5a00000 of extent=3D200000 Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/mm/mremap_test.c | 57 +++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 6304eb0947a3..d7366074e2a8 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -29,6 +29,7 @@ struct config { unsigned long long dest_alignment; unsigned long long region_size; int overlapping; + int dest_preamble_size; }; =20 struct test { @@ -283,7 +284,7 @@ static void *get_source_mapping(struct config c) static long long remap_region(struct config c, unsigned int threshold_mb, char pattern_seed) { - void *addr, *src_addr, *dest_addr; + void *addr, *src_addr, *dest_addr, *dest_preamble_addr; unsigned long long i; struct timespec t_start =3D {0, 0}, t_end =3D {0, 0}; long long start_ns, end_ns, align_mask, ret, offset; @@ -300,7 +301,7 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, goto out; } =20 - /* Set byte pattern */ + /* Set byte pattern for source block. */ srand(pattern_seed); for (i =3D 0; i < threshold; i++) memset((char *) src_addr + i, (char) rand(), 1); @@ -312,6 +313,9 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, addr =3D (void *) (((unsigned long long) src_addr + c.region_size + offset) & align_mask); =20 + /* Remap after the destination block preamble. */ + addr +=3D c.dest_preamble_size; + /* See comment in get_source_mapping() */ if (!((unsigned long long) addr & c.dest_alignment)) addr =3D (void *) ((unsigned long long) addr | c.dest_alignment); @@ -327,6 +331,24 @@ static long long remap_region(struct config c, unsigne= d int threshold_mb, addr +=3D c.dest_alignment; } =20 + if (c.dest_preamble_size) { + dest_preamble_addr =3D mmap((void *) addr - c.dest_preamble_size, c.dest= _preamble_size, + PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED, + -1, 0); + if (dest_preamble_addr =3D=3D MAP_FAILED) { + ksft_print_msg("Failed to map dest preamble region: %s\n", + strerror(errno)); + ret =3D -1; + goto clean_up_src; + } + + /* Set byte pattern for the dest preamble block. */ + srand(pattern_seed); + for (i =3D 0; i < c.dest_preamble_size; i++) + memset((char *) dest_preamble_addr + i, (char) rand(), 1); + } + clock_gettime(CLOCK_MONOTONIC, &t_start); dest_addr =3D mremap(src_addr, c.region_size, c.region_size, MREMAP_MAYMOVE|MREMAP_FIXED, (char *) addr); @@ -335,7 +357,7 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, if (dest_addr =3D=3D MAP_FAILED) { ksft_print_msg("mremap failed: %s\n", strerror(errno)); ret =3D -1; - goto clean_up_src; + goto clean_up_dest_preamble; } =20 /* Verify byte pattern after remapping */ @@ -353,6 +375,23 @@ static long long remap_region(struct config c, unsigne= d int threshold_mb, } } =20 + /* Verify the dest preamble byte pattern after remapping */ + if (c.dest_preamble_size) { + srand(pattern_seed); + for (i =3D 0; i < c.dest_preamble_size; i++) { + char c =3D (char) rand(); + + if (((char *) dest_preamble_addr)[i] !=3D c) { + ksft_print_msg("Preamble data after remap doesn't match at offset %d\n= ", + i); + ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, + ((char *) dest_preamble_addr)[i] & 0xff); + ret =3D -1; + goto clean_up_dest; + } + } + } + start_ns =3D t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec; end_ns =3D t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec; ret =3D end_ns - start_ns; @@ -365,6 +404,9 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, */ clean_up_dest: munmap(dest_addr, c.region_size); +clean_up_dest_preamble: + if (c.dest_preamble_size && dest_preamble_addr) + munmap(dest_preamble_addr, c.dest_preamble_size); clean_up_src: munmap(src_addr, c.region_size); out: @@ -440,7 +482,7 @@ static int parse_args(int argc, char **argv, unsigned i= nt *threshold_mb, return 0; } =20 -#define MAX_TEST 14 +#define MAX_TEST 15 #define MAX_PERF_TEST 3 int main(int argc, char **argv) { @@ -449,7 +491,7 @@ int main(int argc, char **argv) unsigned int threshold_mb =3D VALIDATION_DEFAULT_THRESHOLD; unsigned int pattern_seed; int num_expand_tests =3D 2; - struct test test_cases[MAX_TEST]; + struct test test_cases[MAX_TEST] =3D {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; time_t t; @@ -510,6 +552,11 @@ int main(int argc, char **argv) test_cases[13] =3D MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SU= CCESS, "5MB mremap - Source 1MB-aligned, Destination 1MB-aligned"); =20 + /* Src and Dest addr 1MB aligned. 5MB mremap. */ + test_cases[14] =3D MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SU= CCESS, + "5MB mremap - Source 1MB-aligned, Dest 1MB-aligned with 40MB Preambl= e"); + test_cases[14].config.dest_preamble_size =3D 10 * _4MB; + perf_test_cases[0] =3D MAKE_TEST(page_size, page_size, _1GB, NON_OVERLAP= PING, EXPECT_SUCCESS, "1GB mremap - Source PTE-aligned, Destination PTE-aligned"); /* --=20 2.41.0.rc2.161.g9c6817b8e7-goog From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01B20C7EE23 for ; Wed, 31 May 2023 22:09:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230509AbjEaWJA (ORCPT ); Wed, 31 May 2023 18:09:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230443AbjEaWIr (ORCPT ); Wed, 31 May 2023 18:08:47 -0400 Received: from mail-qv1-xf2e.google.com (mail-qv1-xf2e.google.com [IPv6:2607:f8b0:4864:20::f2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFD1B1A1 for ; Wed, 31 May 2023 15:08:23 -0700 (PDT) Received: by mail-qv1-xf2e.google.com with SMTP id 6a1803df08f44-62614f2eee1so2369846d6.0 for ; Wed, 31 May 2023 15:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570902; x=1688162902; 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=pDm/ZCxPHx9AYeeJr9uyqvYyWstDuMZWrpU9WcV6+zY=; b=qw8bed+Y5biDzHP7EfFtKZs57yNm2Xtw56wpbBOlZcJdMjQ/zNwrMclm47nOYNozFu tlAKh71SIC64nmj5L4aBGSYN+57DOusiaJrtitM4FMj4SCLpKTOJ+7N14sse+z9nRXxf Dfx6iFxi0M2RgWN7VIDJsMRJrYyRrNwtzkiXM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570902; x=1688162902; 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=pDm/ZCxPHx9AYeeJr9uyqvYyWstDuMZWrpU9WcV6+zY=; b=Z7Lpr/5gQRroPLms5TY9sFFPq8mQQO79Q3uaP8A1xW2DCHgodVWRGVSOmjo4iXxIsj W8HG1F5KLU4Isc2fcjCMMZm6ChpayTDrng6OHlpDUIobfl89mcpoYAIStqX3TmprC3lT McSdVLuYJBIG0fL8QlqmS7f15nh+HE5J8AfleSIszhDYPrvHbhB0GOwynmxbwDXjAM7l +UGoU0SX1BhTbLFy2EBjR1qkYYuCsrEPYIv4JtLNc4q1j/VZgAto/xzu5l9YYszN74cG g9b+vUmwEis04RxEvDR6VEmrUKqosu4F0XvSdxm7FyIkoZA/poy356v46F20eGMc1Lri 3K/A== X-Gm-Message-State: AC+VfDwjHQHivGrc5CEQisPCfLwD8RWD18P8F6iJP8gxit6UNzJZIV9W 3TPh3pU6CiRdhv5ZiJ1o3mmvzrbV4fi5sTbybt8= X-Google-Smtp-Source: ACHHUZ6l7pCJSIoI7lqzFcZ3gOJOyLfjdalrVqKZSDT9zEE7joJ3wMQU5TCIp3pppCRoQCP1coMxiQ== X-Received: by 2002:a05:6214:20a9:b0:626:1052:20f3 with SMTP id 9-20020a05621420a900b00626105220f3mr8694018qvd.18.1685570902568; Wed, 31 May 2023 15:08:22 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:21 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 6/7] selftests: mm: Add a test for remapping within a range Date: Wed, 31 May 2023 22:08:06 +0000 Message-ID: <20230531220807.2048037-7-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move a block of memory within a memory range. Any alignment optimization on the source address may cause corruption. Verify using kselftest that it works. I have also verified with tracing that such optimization does not happen due to this check in can_align_down(): if (!for_stack && vma->vm_start !=3D addr_to_align) return false; Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/mm/mremap_test.c | 79 +++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index d7366074e2a8..f45d1abedc9c 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -23,6 +23,7 @@ #define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */ =20 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define SIZE_MB(m) ((size_t)m * (1024 * 1024)) =20 struct config { unsigned long long src_alignment; @@ -226,6 +227,79 @@ static void mremap_expand_merge_offset(FILE *maps_fp, = unsigned long page_size) ksft_test_result_fail("%s\n", test_name); } =20 +/* + * Verify that an mremap within a range does not cause corruption + * of unrelated part of range. + * + * Consider the following range which is 2MB aligned and is + * a part of a larger 10MB range which is not shown. Each + * character is 256KB below making the source and destination + * 2MB each. The lower case letters are moved (s to d) and the + * upper case letters are not moved. The below test verifies + * that the upper case S letters are not corrupted by the + * adjacent mremap. + * + * |DDDDddddSSSSssss| + */ +static void mremap_move_within_range(char pattern_seed) +{ + char *test_name =3D "mremap mremap move within range"; + void *src, *dest; + int i, success =3D 1; + + size_t size =3D SIZE_MB(20); + void *ptr =3D mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (ptr =3D=3D MAP_FAILED) { + perror("mmap"); + success =3D 0; + goto out; + } + memset(ptr, 0, size); + + src =3D ptr + SIZE_MB(6); + src =3D (void *)((unsigned long)src & ~(SIZE_MB(2) - 1)); + + /* Set byte pattern for source block. */ + srand(pattern_seed); + for (i =3D 0; i < SIZE_MB(2); i++) { + ((char *)src)[i] =3D (char) rand(); + } + + dest =3D src - SIZE_MB(2); + + void *new_ptr =3D mremap(src + SIZE_MB(1), SIZE_MB(1), SIZE_MB(1), + MREMAP_MAYMOVE | MREMAP_FIXED, dest + SIZE_MB(1)); + if (new_ptr =3D=3D MAP_FAILED) { + perror("mremap"); + success =3D 0; + goto out; + } + + /* Verify byte pattern after remapping */ + srand(pattern_seed); + for (i =3D 0; i < SIZE_MB(1); i++) { + char c =3D (char) rand(); + + if (((char *)src)[i] !=3D c) { + ksft_print_msg("Data at src at %d got corrupted due to unrelated mremap= \n", + i); + ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, + ((char *) src)[i] & 0xff); + success =3D 0; + } + } + +out: + if (munmap(ptr, size) =3D=3D -1) + perror("munmap"); + + if (success) + ksft_test_result_pass("%s\n", test_name); + else + ksft_test_result_fail("%s\n", test_name); +} + /* * Returns the start address of the mapping on success, else returns * NULL on failure. @@ -491,6 +565,7 @@ int main(int argc, char **argv) unsigned int threshold_mb =3D VALIDATION_DEFAULT_THRESHOLD; unsigned int pattern_seed; int num_expand_tests =3D 2; + int num_misc_tests =3D 1; struct test test_cases[MAX_TEST] =3D {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; @@ -572,7 +647,7 @@ int main(int argc, char **argv) (threshold_mb * _1MB >=3D _1GB); =20 ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ? - ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests); + ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests + num_misc_tes= ts); =20 for (i =3D 0; i < ARRAY_SIZE(test_cases); i++) run_mremap_test_case(test_cases[i], &failures, threshold_mb, @@ -590,6 +665,8 @@ int main(int argc, char **argv) =20 fclose(maps_fp); =20 + mremap_move_within_range(pattern_seed); + if (run_perf_tests) { ksft_print_msg("\n%s\n", "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:"= ); --=20 2.41.0.rc2.161.g9c6817b8e7-goog From nobody Sun Feb 8 20:34:16 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70C3AC77B73 for ; Wed, 31 May 2023 22:09:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230523AbjEaWJD (ORCPT ); Wed, 31 May 2023 18:09:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230418AbjEaWIs (ORCPT ); Wed, 31 May 2023 18:08:48 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C89B712F for ; Wed, 31 May 2023 15:08:24 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id d75a77b69052e-3f6b2f1a04bso68296591cf.3 for ; Wed, 31 May 2023 15:08:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1685570903; x=1688162903; 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=SGzgxSyjCn8pa3p9gX1cUENGQh2HTVaDIrajDjGHrWE=; b=qJbeR3XeX0dyQ9w7zcHDC41lKw4DZl+B5m5ma1wY70zRgQql4bUjPrC4gMymasqXTK BM3QFGwkJEfYqGrUUz8QS6vJaOIG8X1W6MInIXdLQA50Nx8lnRPaUJqzQu+w7PsHFbIi L4d4GNIFwmhD3BECzPNdaa0JfvVlVcgox0QHY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685570903; x=1688162903; 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=SGzgxSyjCn8pa3p9gX1cUENGQh2HTVaDIrajDjGHrWE=; b=Q9ZX8Zy0pqUTNgEw2iNeAys2MRhQD/OuPiBjmFKM9hT/MbcserCBMRTkfLur2xinSJ 7wNxRTU8DmWTfYQiUHMjZiVRHdEWTjNKqvBb8rTxKZ9q9svRd/mY5/zMThEMlNBWL1gE E4NojgP3qPWpcQ5CH5ivdfNUcLmA2cnOUfCQjkP8G9hYOFyXpfDXhmNwm4LUnqywjQPM ZcfwaBURg3SLD/xU23fHaOVNrmBw+OLTfI5tXPnhmrmyKzFN1Q6luFKhm9ByicD5l9ia x9M6IJvEmnjDL1TF1zeP5CBf6LSmh9iq7O5B+OTOzHvF2jYhp7ci+U2uCuMIxhLBZ9Oh XaJQ== X-Gm-Message-State: AC+VfDyNHDnVhB6YaoBpMi3qht6AUwAVUsahc+8t1Rcv2ZDQj71dnO3w fNfW0UrxQ8xJjF+uD4D3LxJiOisnAIQiFRhMHY8= X-Google-Smtp-Source: ACHHUZ6+jFrqsN+RgvwHjevLZQaiKw86Y9ByO7H4nf0Ur0uc0THRQNYH80hkrr7ME5DDE/rEaaF0gw== X-Received: by 2002:ac8:7fc6:0:b0:3ef:25ad:27fb with SMTP id b6-20020ac87fc6000000b003ef25ad27fbmr6757537qtk.30.1685570903640; Wed, 31 May 2023 15:08:23 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id f2-20020ac87f02000000b003f6b0f4126fsm6666172qtk.8.2023.05.31.15.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 15:08:22 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra , Vineeth Pillai Subject: [PATCH v4 7/7] selftests: mm: Add a test for moving from an offset from start of mapping Date: Wed, 31 May 2023 22:08:07 +0000 Message-ID: <20230531220807.2048037-8-joel@joelfernandes.org> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog In-Reply-To: <20230531220807.2048037-1-joel@joelfernandes.org> References: <20230531220807.2048037-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" It is possible that the aligned address falls on no existing mapping, however that does not mean that we can just align it down to that. This test verifies that the "vma->vm_start !=3D addr_to_align" check in can_align_down() prevents disastrous results if aligning down when source and dest are mutually aligned within a PMD but the source/dest addresses requested are not at the beginning of the respective mapping containing these addresses. Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/mm/mremap_test.c | 189 ++++++++++++++++------- 1 file changed, 134 insertions(+), 55 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index f45d1abedc9c..c71ac8306c89 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -24,6 +24,7 @@ =20 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) #define SIZE_MB(m) ((size_t)m * (1024 * 1024)) +#define SIZE_KB(k) ((size_t)k * 1024) =20 struct config { unsigned long long src_alignment; @@ -148,6 +149,60 @@ static bool is_range_mapped(FILE *maps_fp, void *start= , void *end) return success; } =20 +/* + * Returns the start address of the mapping on success, else returns + * NULL on failure. + */ +static void *get_source_mapping(struct config c) +{ + unsigned long long addr =3D 0ULL; + void *src_addr =3D NULL; + unsigned long long mmap_min_addr; + + mmap_min_addr =3D get_mmap_min_addr(); + /* + * For some tests, we need to not have any mappings below the + * source mapping. Add some headroom to mmap_min_addr for this. + */ + mmap_min_addr +=3D 10 * _4MB; + +retry: + addr +=3D c.src_alignment; + if (addr < mmap_min_addr) + goto retry; + + src_addr =3D mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED, + -1, 0); + if (src_addr =3D=3D MAP_FAILED) { + if (errno =3D=3D EPERM || errno =3D=3D EEXIST) + goto retry; + goto error; + } + /* + * Check that the address is aligned to the specified alignment. + * Addresses which have alignments that are multiples of that + * specified are not considered valid. For instance, 1GB address is + * 2MB-aligned, however it will not be considered valid for a + * requested alignment of 2MB. This is done to reduce coincidental + * alignment in the tests. + */ + if (((unsigned long long) src_addr & (c.src_alignment - 1)) || + !((unsigned long long) src_addr & c.src_alignment)) { + munmap(src_addr, c.region_size); + goto retry; + } + + if (!src_addr) + goto error; + + return src_addr; +error: + ksft_print_msg("Failed to map source region: %s\n", + strerror(errno)); + return NULL; +} + /* * This test validates that merge is called when expanding a mapping. * Mapping containing three pages is created, middle page is unmapped @@ -300,60 +355,6 @@ static void mremap_move_within_range(char pattern_seed) ksft_test_result_fail("%s\n", test_name); } =20 -/* - * Returns the start address of the mapping on success, else returns - * NULL on failure. - */ -static void *get_source_mapping(struct config c) -{ - unsigned long long addr =3D 0ULL; - void *src_addr =3D NULL; - unsigned long long mmap_min_addr; - - mmap_min_addr =3D get_mmap_min_addr(); - /* - * For some tests, we need to not have any mappings below the - * source mapping. Add some headroom to mmap_min_addr for this. - */ - mmap_min_addr +=3D 10 * _4MB; - -retry: - addr +=3D c.src_alignment; - if (addr < mmap_min_addr) - goto retry; - - src_addr =3D mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE, - MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED, - -1, 0); - if (src_addr =3D=3D MAP_FAILED) { - if (errno =3D=3D EPERM || errno =3D=3D EEXIST) - goto retry; - goto error; - } - /* - * Check that the address is aligned to the specified alignment. - * Addresses which have alignments that are multiples of that - * specified are not considered valid. For instance, 1GB address is - * 2MB-aligned, however it will not be considered valid for a - * requested alignment of 2MB. This is done to reduce coincidental - * alignment in the tests. - */ - if (((unsigned long long) src_addr & (c.src_alignment - 1)) || - !((unsigned long long) src_addr & c.src_alignment)) { - munmap(src_addr, c.region_size); - goto retry; - } - - if (!src_addr) - goto error; - - return src_addr; -error: - ksft_print_msg("Failed to map source region: %s\n", - strerror(errno)); - return NULL; -} - /* Returns the time taken for the remap on success else returns -1. */ static long long remap_region(struct config c, unsigned int threshold_mb, char pattern_seed) @@ -487,6 +488,83 @@ static long long remap_region(struct config c, unsigne= d int threshold_mb, return ret; } =20 +/* + * Verify that an mremap aligning down does not destroy + * the beginning of the mapping just because the aligned + * down address landed on a mapping that maybe does not exist. + */ +static void mremap_move_1mb_from_start(char pattern_seed) +{ + char *test_name =3D "mremap move 1mb from start at 2MB+256KB aligned src"; + void *src =3D NULL, *dest =3D NULL; + int i, success =3D 1; + + /* Config to reuse get_source_mapping() to do an aligned mmap. */ + struct config c =3D { + .src_alignment =3D SIZE_MB(1) + SIZE_KB(256), + .region_size =3D SIZE_MB(6) + }; + + src =3D get_source_mapping(c); + if (!src) { + success =3D 0; + goto out; + } + + c.src_alignment =3D SIZE_MB(1) + SIZE_KB(256); + dest =3D get_source_mapping(c); + if (!dest) { + success =3D 0; + goto out; + } + + /* Set byte pattern for source block. */ + srand(pattern_seed); + for (i =3D 0; i < SIZE_MB(2); i++) { + ((char *)src)[i] =3D (char) rand(); + } + + /* + * Unmap the beginning of dest so that the aligned address + * falls on no mapping. + */ + munmap(dest, SIZE_MB(1)); + + void *new_ptr =3D mremap(src + SIZE_MB(1), SIZE_MB(1), SIZE_MB(1), + MREMAP_MAYMOVE | MREMAP_FIXED, dest + SIZE_MB(1)); + if (new_ptr =3D=3D MAP_FAILED) { + perror("mremap"); + success =3D 0; + goto out; + } + + /* Verify byte pattern after remapping */ + srand(pattern_seed); + for (i =3D 0; i < SIZE_MB(1); i++) { + char c =3D (char) rand(); + + if (((char *)src)[i] !=3D c) { + ksft_print_msg("Data at src at %d got corrupted due to unrelated mremap= \n", + i); + ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, + ((char *) src)[i] & 0xff); + success =3D 0; + } + } + +out: + if (src && munmap(src, c.region_size) =3D=3D -1) + perror("munmap src"); + + if (dest && munmap(dest, c.region_size) =3D=3D -1) + perror("munmap dest"); + + if (success) + ksft_test_result_pass("%s\n", test_name); + else + ksft_test_result_fail("%s\n", test_name); +} + static void run_mremap_test_case(struct test test_case, int *failures, unsigned int threshold_mb, unsigned int pattern_seed) @@ -565,7 +643,7 @@ int main(int argc, char **argv) unsigned int threshold_mb =3D VALIDATION_DEFAULT_THRESHOLD; unsigned int pattern_seed; int num_expand_tests =3D 2; - int num_misc_tests =3D 1; + int num_misc_tests =3D 2; struct test test_cases[MAX_TEST] =3D {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; @@ -666,6 +744,7 @@ int main(int argc, char **argv) fclose(maps_fp); =20 mremap_move_within_range(pattern_seed); + mremap_move_1mb_from_start(pattern_seed); =20 if (run_perf_tests) { ksft_print_msg("\n%s\n", --=20 2.41.0.rc2.161.g9c6817b8e7-goog