From patchwork Tue Aug 22 01:54:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 13360075 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 3C97CEE49A6 for ; Tue, 22 Aug 2023 01:55:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47E22940020; Mon, 21 Aug 2023 21:55:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 391A6940008; Mon, 21 Aug 2023 21:55:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16EC9940020; Mon, 21 Aug 2023 21:55:25 -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 EE770940008 for ; Mon, 21 Aug 2023 21:55:24 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C9EF114019D for ; Tue, 22 Aug 2023 01:55:24 +0000 (UTC) X-FDA: 81150073368.04.E832AEF Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) by imf15.hostedemail.com (Postfix) with ESMTP id EBEB5A000D for ; Tue, 22 Aug 2023 01:55:22 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b="A1fiRL9/"; spf=pass (imf15.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.182 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692669323; a=rsa-sha256; cv=none; b=RCgJxneHqaPDlGCKdVtiKeCEtDoCPUoUIEljz5KxDjhHS8/eF689QAMAVn/zUffmJApQcr Rb8BQ05YPeG4OzUXJyEyrL2ipWPXO/BoJndHbtHNANDCnjh6hMOh7JGfPbzCEsMpqb/adG vaq8VsdlqhorWINton22y5HeaTXz2Ls= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b="A1fiRL9/"; spf=pass (imf15.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.182 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=1692669323; 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=UmTCssMd/OkLNKuh8A+l/1O9kIjaudHJc+AQCzKHLlk=; b=SvaOj1HlNPfHxj/X7CVA4b5lBHlqrUPLVlKVwcmKMIS1lTLD0yej+gS4PQ8R01b/UiCshk vkcFETdA/sIxl49eHGfnHhjBiPpQ3sT/h1ZFzIUe1WzuR4i4wa9T0XXZVcI1XVySAyYUT9 6J2wNkVZNMToJ+jqTC+QMyhW1H3RUP8= Received: by mail-il1-f182.google.com with SMTP id e9e14a558f8ab-34992058e95so12335395ab.1 for ; Mon, 21 Aug 2023 18:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1692669322; x=1693274122; 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=UmTCssMd/OkLNKuh8A+l/1O9kIjaudHJc+AQCzKHLlk=; b=A1fiRL9/gyGYLlkYcaWvN0fBKnQebciTf0B21CCdmsU1xEeOwPHiF1/xra5llF4iqQ QGiqLGX70eBqHNJ5suqvz4NbbitFDLqcy3w6HKepVcS2DlgpUM+PMtv50jv4oJX6cS0Z Y3Gnd49h1/o+WqJ4Dlgt0SeNoAFckPqhzGZ+Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692669322; x=1693274122; 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=UmTCssMd/OkLNKuh8A+l/1O9kIjaudHJc+AQCzKHLlk=; b=SHwZcpAObWKF9NcwHYOZ7BOew6SQyo8opdEdVT9YF+OWYXLr6q1u5DRlL40pS4/w56 BRA7/E1EtgM6tPiWKEK/nt90A61TVm4ylT0TSaCxpWAnb+LCxOVfA8BLNbumEqroiItO 2A5hfCEIresQmn9zhTMQUZekYeDHnHiM5EC9Wp3r7JWPlVCXUm5r/YsfD5mgCLkLSJPK pn+Bw2dUWNXS5usApW2iUPskOlI/H1lOOYfCfY0gn1AssMYEXXJGgs5xmQTuG8JjX2o+ 9Yjlj8pGLkyzBpzhYK21kFT8sJ5GkTMGwiB+RkLDire8OTRWql4V8KiZk7MWyEsQleZv yjQQ== X-Gm-Message-State: AOJu0Yw6zVUzgfyT6YUFhs3mKC0Mk1G+YAmn456XI/ft+VPGNo93azAT G/Dmj1xFse5XzeSGQ4gPcv7jrw== X-Google-Smtp-Source: AGHT+IF3qvn51n/32NdS40F5GBEiAi2QwNfdcj7jaTRmqbaEocW2e2V/vyWF81PIpFzzD8zMdl/02g== X-Received: by 2002:a05:6e02:1a4b:b0:349:8dfa:ad64 with SMTP id u11-20020a056e021a4b00b003498dfaad64mr13813434ilv.15.1692669322088; Mon, 21 Aug 2023 18:55:22 -0700 (PDT) Received: from joelboxx5.c.googlers.com.com (156.190.123.34.bc.googleusercontent.com. [34.123.190.156]) by smtp.gmail.com with ESMTPSA id q11-20020a92d40b000000b0034886587bdcsm2907390ilm.18.2023.08.21.18.55.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 18:55: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 Subject: [PATCH v5 2/7] mm/mremap: Allow moves within the same VMA Date: Tue, 22 Aug 2023 01:54:55 +0000 Message-ID: <20230822015501.791637-3-joel@joelfernandes.org> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog In-Reply-To: <20230822015501.791637-1-joel@joelfernandes.org> References: <20230822015501.791637-1-joel@joelfernandes.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: EBEB5A000D X-Stat-Signature: gto614dibrc3ieusoiefnhzji3znkp4m X-Rspam-User: X-HE-Tag: 1692669322-770679 X-HE-Meta: U2FsdGVkX19obQsYrqtEwPuSjKxq0ApJDplWo6xWIkyPKT6cF276aOmY98EDu5ft9DXK9norhKq4KCvTBmc72UfNtf/5fv83HOYUXi1Y6rOMgYiA2h6HABwcNk1AyLOboBrcJEY+cQHDOwBP3yMZi1wblbAWY47NzW8Z/53zztDDx3AGGjjvEMCkafG4led0k218lf8G6cN9qwkktnhoqSQbXkDy4T5qxH/2f8CEZ/Kz5OUfC5B9cgniLigmYnBivJyDJTXyRGCeG0SdBy5Hz/aie8sT6uPgjFg7yUcKTmyp/RtsXi2b4GrTnNeBNGsgy4KTMrfm5Dous3Xa4Z9Y32Fijn50AzFHdiurFCLn9COmjDp88RLtZ2uf7x5YF+ptRAXF2ZTID0I9oAptLjO/4ZHwdveqIR+wsnf7olcAWXQc8+kB0rQgL1QWwlUSlYZrvalMg0i2ABw1Udf1mmxJbJUnLY13YlHhvqWKVgUjCBYkX4vafUV4NPs4kIXHMk24kaLNkg8N5CrYIc/e0qKUNDwnHJZdIVImuiFpV74abJOK9Vd8eUUl+J/nHjyBHqG30a0rPFIdln08tO8PE0Gz+4XVfbcZjCYrz/Dgs7UN8tCCP2WZWB8w4CQfV/kLYQ2yS4xs0TTBL0E+Dz9r+9LCO01haxxT0H4Wb9sWWxrZQxfVNR17eLUT5yv+/u1qGQsiKF0n6lYPbLX2lZLelC0BNqbtdArS6dh2iX8/doOLUEyojv4K3ayFtc81BOG9gOjwbVuhgrdhLZXtudKudYyASbjPmjD4vO4C5bn3kpErM16zk3AD1uUniJ/fud4UI3qF4dX12jQnTu0rmgJh8ojYa3cjSRjzZNp9QuLMEQooD3CvVYj50WwqG13QxzlJ/A2+Kx0AHD+YE9YgjXzeWeds76hNLh3gxAxRsfVhPHMCOuiWrhAmAxq3+UxNd+i7/2WEsKEOwlWvb/d7zuL3LgQ z29cwxNN GDmEgAD1kuBdPIUO1cXxWeuMulLj2JztTMoehqsdh1aUYkC2gzb2x9N9+1C746A1/JiJcZahUmg9PrPyiSR6uBZzayjT/YD29qGbfYZWkyVbf0hWcQ/P2oTPkEQ4831LzEIU7cPBi7ook+R2AVnHNFFTqi7MB9LQpy4Vp/sTQkv8xDdo4Idh34J5qoAMc090zxS0yzwyuUpuwrRdBz8FP43CBMreiEkg8HEB5RyBVnTfZAnD/6NGX1WmRallOBgw+PAV69h1z6hvv80/z6mhzBFb+9wP61VwbfD0B5LsxALwU//Z6FTyjH89GJgd54YZINP4R1AvxKDivsFJIReTh/YfAFLHBXMT0PKG0seCcWjv894w7T42YRiQsv8t69ISZDLmAquJkefmuvVikDyriZ8/GIw== 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: 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); /* * 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; /* @@ -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;