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 EE7D7C83F01 for ; Sun, 27 Aug 2023 09:21:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5ADED900003; Sun, 27 Aug 2023 05:21:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 55D42900002; Sun, 27 Aug 2023 05:21:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44C2A900003; Sun, 27 Aug 2023 05:21:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 34738900002 for ; Sun, 27 Aug 2023 05:21:21 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 057E5140184 for ; Sun, 27 Aug 2023 09:21:20 +0000 (UTC) X-FDA: 81169341162.12.E4A0D00 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) by imf17.hostedemail.com (Postfix) with ESMTP id 258C040013 for ; Sun, 27 Aug 2023 09:21:17 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=KUHE6OOJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.54 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693128078; 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=cdi86NKSvxAhAAY/JTOY1oVYSbdavS4j5GkMSJME2Bk=; b=RUdn644Fyj2CI2jQaC1mjDcveMeAjIGWsUeS64q1nHpC5Slea6mM2omIJmXqLt9x18Hvtf ovBo+WZJDRYgdJbNmIetl2yyop2TyzBqjizu0XK+gS42n62Y+ccuNco1KjmLJLijjvC3Dx b1TNTXobxfcANQbJZSw5JWv/nkC+beY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=KUHE6OOJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.54 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693128078; a=rsa-sha256; cv=none; b=Qm311mwfvoMtv1nmgh5zInZznZBGb8spMBBueLS0dJRKghRBCFOktXaJn8VyZMufCw9pGC ACqW7wzw/tN3x1+zjX3aLlVq7ddZhmcKorpgnql2PAKjtvFmyG4dF4qvCNr1zXVieYkiSg nsPiLRET+QUbodjtK3tRFwY8LWy3Nfo= Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4018af1038cso22153025e9.0 for ; Sun, 27 Aug 2023 02:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693128077; x=1693732877; 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=cdi86NKSvxAhAAY/JTOY1oVYSbdavS4j5GkMSJME2Bk=; b=KUHE6OOJ53D47VuVOLuUcnayMJ1O4rnjTnP2ypPA/XptRh1YoSVB6xUMtnX4/etK1v Ir4SvaZWzo8yC/l5eoko02NeWsfsalSVqAGrifW8DbA8GYdM6hXOGO6zr+6YMGdqmBrs HVBIOKBtNRnW8urE/CQLeHd/EWe30vXng5ujvVVEBU//sXNcDD3CwOc43M7TnYKAiwk/ IsqHtyhU5iEj2M40/ayoh2Bw2qeV6GvD1X2IVrHp85DtIxaCOdDkSXDiVRcWTMHP8zsf +jwRVRlH4lYJ1eQtdWIFrb0p+QUZMGKcFxQU185vzezwrvkceHHgYmtN7ZIHcGwlTavV uuZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693128077; x=1693732877; 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=cdi86NKSvxAhAAY/JTOY1oVYSbdavS4j5GkMSJME2Bk=; b=Nwq0jHcPIevxlJGeFpQjjJjNiN2BZd1X8wmqANNzMJZpOLOoi1Jc2hOR5LT7EBSTTS BSDuuM0ZCJz+zVp20PTboXHCWb3W7wDhzdtiTxekJPR4otd5yd4pmyUUbEx2LBKAmsNr 3L4ZkSWxPRTmgXdabu7AiKlXHJDpzee3CaUgutNholWuHW/1O9wdFcmitRbhHP0cXqCW Sql3f8fokNJuOQOLsbohJcPDQkmT3Mjafg3586Vvpi7+E7iSoRNCRGb6tmReXAx26SKh UhMeoNrI6JaDp7NBx9r5onGt3SK7jz60mFuO4wIEl95CG0Tyt54vJgNZgIR4ixjjzMSp TyLg== X-Gm-Message-State: AOJu0YySLIKbuRktC5rV6FkbXQl2NlXwqCHJBCL7A/oZAgVcA2/8yTPV C1PVX5cpyxrRolPaPMx8K3w= X-Google-Smtp-Source: AGHT+IFIBfHVzujAWrXltdbTmcIhslJaPuKAib/EtXs1THmZ5W8e+bbJMMy1vqIp1JIMrgMOQyEw2A== X-Received: by 2002:a05:600c:2491:b0:3fc:92:73d6 with SMTP id 17-20020a05600c249100b003fc009273d6mr18390810wms.11.1693128076411; Sun, 27 Aug 2023 02:21:16 -0700 (PDT) Received: from localhost ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.gmail.com with ESMTPSA id o10-20020a1c750a000000b003fe29dc0ff2sm7376171wmc.21.2023.08.27.02.21.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Aug 2023 02:21:15 -0700 (PDT) Date: Sun, 27 Aug 2023 10:21:14 +0100 From: Lorenzo Stoakes To: "Joel Fernandes (Google)" Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra Subject: Re: [PATCH v5 2/7] mm/mremap: Allow moves within the same VMA Message-ID: <46196ba1-c54d-4c1d-954f-a0006602af99@lucifer.local> References: <20230822015501.791637-1-joel@joelfernandes.org> <20230822015501.791637-3-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230822015501.791637-3-joel@joelfernandes.org> X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 258C040013 X-Stat-Signature: hcsuzde34t73pn7egshna8cw9rincj8b X-HE-Tag: 1693128077-953893 X-HE-Meta: U2FsdGVkX1/dRakcBjgpvLrD+516nrRy48ZICfExum6r/HXgICJn63vim2MNP9xsIw7DLxnlyx4jAqP5W2M8z6KjYEoGYHRqzifiuUlukFmYV2D5kqjvxq9RMwOhpDqG0F3sSToeEvhQmXlDsZCrjf9HhRmCYsOredITVMpGaXbVTeg0buQAEbHUDfcuHJBjW9nl/Ya7Q4v3wzATey+JxYxN5Fwtr35cVmKmLPx0RHPNflUqL7wbXzbkn2R3OpLm2g8vM/K/sZxLieJYVKaRz2GO8r6xVQKABIzxQSY8d4eu2+uk+JkmL7MrDa8+bQVZBdLpm2yVqGCVNrsym5DdFx1zM8v7/CpMWlKEJ8VDyM3tuilaIVBKrMVECW+WD6c+KDMvwDi2gMrrfEhpuMSML+0N0FhGf3HHOK0Z5NexGcgdRTSHezkECxOlkPGyNSXlVHYL6mWXVs1rI1Lw28Y5ruFTFLQS7z3SaiJsJM76gLLyxsZ8qsL5m28+5ov9Y1Yn5aA/WbBKxKNrvxJiZ9z8F57EpT9rNlP6nWyJiVXfU5iEkOOA4i4vtvubswafDi4XgqeO9L4bbq6VVgaQ8HdZOsAnEan37nJtEzQZ0aQCZiPfrvq3owloEm1KBYlZhwKf39E5F63gfyoiQ9zsCkWBaRS+R5/kwNk775sjiiDapIseejAd6WIISQLNo+gibFGUFBx4LazMho1H/ytsa69faURNoxMwAz4/24lOKvNFtPs4tNgBq8PZjjIiLwmBKg9jydH0Zfk6mAgtAJR7vmjdmp2ZBKs3iTllhqx+4kPCmax5OL0sBLih36Ibh0m3MVn6nBX/10uvi2nOWsyrNWQE0FMudae9NX3BjbIlS52IyOl0K4rESVUiMwiTtkpG9YxySDmgy/ZH1mEp3GqP8RNeaKKkCkoO5Px9jHzy91josYYUlV+zLREIboZSDJMkO6Qt3aaGP28xz20Ijgqme5k d3S4MLQX YBSuuaKhy6yB1KRmLLVwQc11g3ySlJaOdbQjzvlgv57WN7/fVyEHejB5Sweu91JWuqOQAY0b6ZBi/i5rKaMabb6+I3qcUyADSDTSELT/b7IyQNR2StvL0XrrMpkQ9X189HHZw6oSPYPGZ4ESBQOFW6miGmc41WxcxJYLVK23PhSxNDLRAwao607eyozlaWmBUwJHOz6GxSyWAxqgnZEP84yQH76u3YTCWARciLaH+BWfGfk41KVrNI1+6u835BYloZ3d2B4IZOXBjJ/WHMuChnfcB/fXsL5SV2UlEhPRJmXKzXBpbxhCvBIxSjmnxobycisPTDzcdKinIEDN63Jhz7WjxeIViUOXAGM/aWU+oRo2I+8NcghH1pkDIHJmgvk5Ykyb6O6KDWLC1KCEZRpALNfv3REIVal5kUu5Cysi2bsRrohdAi2Z1cieoryhvqVP0NgTyKUD04tTRPuM= 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 Tue, Aug 22, 2023 at 01:54:55AM +0000, Joel Fernandes (Google) wrote: > 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 | 29 +++++++++++++++-------------- > 3 files changed, 17 insertions(+), 16 deletions(-) > > diff --git a/fs/exec.c b/fs/exec.c > index 1a827d55ba94..244925307958 100644 > --- a/fs/exec.c > +++ b/fs/exec.c > @@ -712,7 +712,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift) > * process cleanup to remove whatever mess we made. > */ > if (length != move_page_tables(vma, old_start, > - vma, new_start, length, false)) > + vma, new_start, length, false, true)) > return -ENOMEM; > > lru_add_drain(); > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 406ab9ea818f..e635d1fc73b6 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -2458,7 +2458,7 @@ int get_cmdline(struct task_struct *task, char *buffer, 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); It's a nit, but it'd be nice to not have 'mystery meat' booleans foo(bar, baz, true, false, true); always ends up being a pain to track down. However I think probably something better than that (flags or wrapper functions) might be too much noise here so perhaps we can live with this! > > /* > * Flags used by change_protection(). For now we make it a bitmap so > diff --git a/mm/mremap.c b/mm/mremap.c > index 035fbf542a8f..06baa13bd2c8 100644 > --- a/mm/mremap.c > +++ b/mm/mremap.c > @@ -490,12 +490,13 @@ static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma, > } > > /* > - * 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 aligned address should fall > + * on *no mapping*. For the stack moving down, that's a special move within > + * the VMA that is created to span 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 = addr_to_align & mask; > > @@ -504,7 +505,7 @@ static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_ali > * 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) > + if (!for_stack && vma->vm_start != addr_to_align) > return false; I'm a little confused by this exception, is it very specifically for the shift_arg_pages() case where can assume we are safe to just discard the lower portion of the stack? Wouldn't the find_vma_intersection() line below fail in this case? I may be missing something here :) > > /* > @@ -517,7 +518,7 @@ 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 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) > + unsigned long mask, bool for_stack) > { > /* Skip if the addresses are already aligned. */ > if ((*old_addr & ~mask) == 0) > @@ -528,8 +529,8 @@ static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old > 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)) > + if (!can_align_down(old_vma, *old_addr, mask, for_stack) || > + !can_align_down(new_vma, *new_addr, mask, for_stack)) > return; > > *old_addr = *old_addr & mask; > @@ -539,7 +540,7 @@ static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old > 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; > @@ -559,9 +560,9 @@ unsigned long move_page_tables(struct vm_area_struct *vma, > * 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); > + if (len >= PMD_SIZE - (old_addr & ~PMD_MASK)) > + try_realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK, > + for_stack); > > flush_cache_range(vma, old_addr, old_end); > mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm, > @@ -708,7 +709,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, > } > > moved_len = 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 = -ENOMEM; > } else if (vma->vm_ops && vma->vm_ops->mremap) { > @@ -722,7 +723,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, > * 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 = new_vma; > old_len = new_len; > old_addr = new_addr; > -- > 2.42.0.rc1.204.g551eb34607-goog >