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 612A6C77B7A for ; Sat, 20 May 2023 05:04:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B4F99280001; Sat, 20 May 2023 01:04:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFFF8900003; Sat, 20 May 2023 01:04:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A0BF280001; Sat, 20 May 2023 01:04:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 89D2E900003 for ; Sat, 20 May 2023 01:04:55 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4BEB11C7483 for ; Sat, 20 May 2023 05:04:55 +0000 (UTC) X-FDA: 80809443750.22.EB3FE58 Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) by imf19.hostedemail.com (Postfix) with ESMTP id 5D77F1A0010 for ; Sat, 20 May 2023 05:04:52 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=sUspdzVV; dmarc=none; spf=pass (imf19.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.219.46 as permitted sender) smtp.mailfrom=joel@joelfernandes.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684559092; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lEZNQccpwa0JJflLeFMzBTg7opuNzA80L/3GrGRRMLY=; b=o7wzPkDvatZRxj3obWJ/zcIDgBoYfhy/sdRzbhd4QG7Y97xealDHRK2NZ5eMATQamd/ca7 Q6LPVxE+UNfWDGdIKLdHWnNaO3X5qdA7hTI8DnXnVYUQfEjlcCnRKlW9+HPsTEBN522gCJ dPhCjoYwhbblc124ckc+l5w8LzRrV6U= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=sUspdzVV; dmarc=none; spf=pass (imf19.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.219.46 as permitted sender) smtp.mailfrom=joel@joelfernandes.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684559092; a=rsa-sha256; cv=none; b=rdcksw1zE6BXmn91+Xo2q+OG+Dj+PhDlwU8kTrKXhNqAPffyUm5ITn8GEbfuyx3axg4wY9 anX8UYfUeKtifW49mrIX8ccvayDyghALATFbLZ2Qvu8h1tKt08Qeg8XVXzrIUJ+fQdtyqF PyO0LMdx8D9CcTmAgmeV6Yf23g04EFQ= Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-623839d5c19so29709116d6.1 for ; Fri, 19 May 2023 22:04:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1684559091; x=1687151091; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=lEZNQccpwa0JJflLeFMzBTg7opuNzA80L/3GrGRRMLY=; b=sUspdzVVWNb3XjsNMFsBKzPmFOP+kFv8BRsMIJY1Q9AHGcFEZAfu5vP/WUAmaQYDm8 wXeWFgbQMvnIlxA+cUinkd5gIfhNJN3+27LaFT5wGRAn8q9/2ZLTXL8HoQDPpYrTKH/w aKUNodBp5R+mXwHYbHRWIciXD0B+gP+77/OdY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684559091; x=1687151091; h=in-reply-to:content-transfer-encoding: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=lEZNQccpwa0JJflLeFMzBTg7opuNzA80L/3GrGRRMLY=; b=MR3j1ArRpsLkLR8xxhVX5KW0X3bJ4XqNHi+P1s5e7BqaIOj97ATu+1bv8GElU4k1Ip A9NL+nqb5jL7GxV8GJ84T8Td/yFiEmKIAK5ZmQNcOVJ5vVlfaxHLZ+0GSMn8yi+/N88I ny7z5434KTu2haig5xEZJQXBdfxgsBOmEdoyo1W4WgrZ6KDJSss+UVhFPS61vQXm8keX tYT/VDYnIqXIsm51QQdVuS38xs9DPR/M/kjXQRGCcdNJCRsU/WwxdTQOJzw4B07uyb9q MNQtttw1jXc3QB9KXN3pHjXFuOcnj973L4ce1NHMGkuJQcM0QGt9N3kEu0YczWfc1R7L LSPg== X-Gm-Message-State: AC+VfDwzaBvdVJM76XkR+1fdox3mu97PxUkx8RPtOVjujVQixNF5vNGo FO8Uxas5BjRnQEUAw5tGoSBCRg== X-Google-Smtp-Source: ACHHUZ5zqeNnbhBkxoN+lhzwMP1nElqBQHyKNNdlQW573FCVdKfT46iYAZsIyJCFbbklipt/bZOTbQ== X-Received: by 2002:ad4:4eec:0:b0:621:599d:2fa0 with SMTP id dv12-20020ad44eec000000b00621599d2fa0mr9261488qvb.18.1684559091360; Fri, 19 May 2023 22:04:51 -0700 (PDT) Received: from localhost (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id mm13-20020a0562145e8d00b0062162d2c0d5sm324900qvb.68.2023.05.19.22.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 22:04:50 -0700 (PDT) Date: Sat, 20 May 2023 05:04:50 +0000 From: Joel Fernandes To: Linus Torvalds Cc: linux-kernel@vger.kernel.org, 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 Subject: Re: [PATCH v2 1/4] mm/mremap: Optimize the start addresses in move_page_tables() Message-ID: <20230520050450.GA504695@google.com> References: <20230519190934.339332-1-joel@joelfernandes.org> <20230519190934.339332-2-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Queue-Id: 5D77F1A0010 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: j934pt39mf937b6zubne7ryaj9mi5us8 X-HE-Tag: 1684559092-733747 X-HE-Meta: U2FsdGVkX18JzDsoUdWp8iKHhBd+6ZjLZq+C8lFjXjnrM2VksIhJNBXa7vt/sB/n9XpoYK8IaIo1iUnoMcHW+20sD4jsNtEKFofQZ39iJMZ4AQptVdLwcJ8eMNAeQV1yGqcWZGCkgOhgw8NLG0RfXpGmFOJLjQYkmvSinvkX10OIzX2K4xlIqckNKhS7Mp9EwOt0p19cFCFaLyiFax+OZnFogrFkxOPlibNnANdEnWxQ+BAr7It3i27rByQ/oAJckVHHkZDdUpKeh3ztc5GWpZQZS49D0/RGJo31sB0SNM+r4e3aJKQnt/wrubcMQ13efQ04eTeX9rUJfHTN2J6FJ+zY7E+VnWLoZZ6jdLOdCRthgwFqgKZFFpdwSFBPvD1mAwfVwik5YeMcHxZYAlKy8SJe1xEc7WmhkF0pKcA04Jy70lewEA8XiDrGI+yIm/Rd4K7/TjaD2Us76s6o/WiRtuNUhGyRVZwn4whzLGfFa47hR5LGCz/9hO8vQxrBDGpzsslIWyQlJlkCA38E4Y5IyiHLJsy0q5Vzm7KfdS/Ut3WI9JNdTgjukUPTTpqMRAegEeBusZD5xGfCaw2kCTnutRMNg91v1sMHPOMXYGnERM38l7J/5uLLhVwM0mlM1YF8HAXxxk36jix27S5LIKvobbY4aNelGmWOzecLBGvdjdlCk3t+nyY4whRcgTU5eBaa837YH//jg7YStKeOEJ9uWCKlL4ZemjCkmegEiPOVYh+mEO7I+hfouM/KaKUxEfnLjbHegDL4QAMewEE9GYaVLKGLiKFuSxhZHa3zoZXjU5qbszMhJxXcj9hd10E23Sdm0wvQ9Mr4Ayh9WFEaoDekQJc9h9FVXa46DTLVKI2Mz5x/Kiq9d0dCHqiyN9Mq7+xHIj+MAV8mq+29u8FGvGAsOqLtedz3F//kAh2mbYS2ta7Upnl6eFv3eoIeVEPsSp32cSQ2gBk3rykIZFszX3M yzlVuBNK gpaz1nTOBHfv+z9alfb+VUM56gbfW5w+awLJncjaBDYhhs/MmYbRPrcrHNGT3rFDeRy11l5oeYdBQ6Mqe+cU/Qhve6w4edAL8Pdghog308Tyr32xImzj8K3ZMBJvZu5Gb67wbHHCS8cD+R+mafik0+mqxkRQsqUbbTHALgXplzeU50OfrIrJc4ov693ckPmYXpm7jj/zvAovJr9bWtvAzRTgAxhLSxZ/Av7PrYg1zpKqyG3C0GwUchD8ms5Bc/2PE55Ompj3SCua+29Fk2/8ggKb0ZojV8OD/ffitXQQW4DyOwqCkF+x4F3yaBykMpO6/GueOSwM4Fh69iiUWdQP6JecGcd4nkEA5cEsv+5kRNdpzOtSABqOBFnBGKSFeNE6jadNkyayKNMXj1YYsUk0UtKfUgXs31iYiUezrF8612MyT83a5HiXqDZCj/y4TyvqXVKeo1saU2IIFl1OSwGzkXZCrqyvuS6k3dYeIKtfhzUjcfyyLUdNGjK6OgThwwi8oauLzQBxRLXpCb1FNLRgXrf2C0jxcH69jbze2XQ9zF/OUxs+g4fEvJFsRNw== 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 Fri, May 19, 2023 at 09:01:07PM -0700, Linus Torvalds wrote: > On Fri, May 19, 2023 at 8:57 PM Joel Fernandes wrote: > > > > I also realize that I don't really need to check whether the masked > > source address falls under a VMA neighboring to that of the source's. > > I don't think that's true. > > You can't start randomly moving other source vma's that may have other contents. Here is the rewritten patch, this is untested and I am just sharing as a preview (sorry if I have been noisy). I will test it soon and send a proper patch. Basically, I detect any moves within the same VMA except when it happens for stack moving down. And will add another patch later to test moves within a VMA. ---8<----------------------- From: "Joel Fernandes (Google)" Subject: [PATCH v3] mm/mremap: Optimize the start addresses in move_page_tables() Recently, we see reports [1] of a warning that triggers due to move_page_tables() doing a downward and overlapping move on a mutually-aligned offset within a PMD. By mutual alignment, I mean the source and destination addresses of the mremap are at the same offset within a PMD. This mutual alignment along with the fact that the move is downward is sufficient to cause a warning related to having an allocated PMD that does not have PTEs in it. This warning will only trigger when there is mutual alignment in the move operation. A solution, as suggested by Linus Torvalds [2], is to initiate the copy process at the PMD level whenever such alignment is present. Implementing this approach will not only prevent the warning from being triggered, but it will also optimize the operation as this method should enhance the speed of the copy process whenever there's a possibility to start copying at the PMD level. Some more points: a. The optimization can be done only when both the source and destination of the mremap do not have anything mapped below it up to a PMD boundary. I add support to detect that. b. #1 is not a problem for the call to move_page_tables() from exec.c as nothing is expected to be mapped below the source/destination. 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. [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) --- fs/exec.c | 2 +- include/linux/mm.h | 2 +- mm/mremap.c | 71 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 5 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 != 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 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 *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 411a85682b58..5f2e0c7fc839 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -478,10 +478,56 @@ static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma, return moved; } +/* + * A helper to check if we can align down. Required for + * move_page_tables() and realign_addr() to determine if there is any + * mapping to the newly aligned address. + */ +static bool can_align_down(struct vm_area_struct *vma, unsigned long addr, + unsigned long mask) +{ + int addr_masked = addr & mask; + struct vm_area_struct *prev = NULL, *cur = NULL; + + /* If the masked address is within vma, we cannot align the address down. */ + if (vma->vm_start <= addr_masked) + return false; + + /* + * Attempt to find vma before prev that contains the address. + * On any issue, assume the address is within a previous mapping. + * @mmap write lock is held here, so the lookup is safe. + */ + cur = find_vma_prev(vma->vm_mm, vma->vm_start, &prev); + if (!cur || cur != vma || !prev) + return false; + + /* The masked address fell within a previous mapping. */ + if (prev->vm_end > addr_masked) + return false; + + return true; +} + +/* Opportunistically realign to specified boundary for faster copy. */ +static void 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) +{ + if ((*old_addr & ~mask) && + (*old_addr & ~mask) == (*new_addr & ~mask) && + 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, - bool need_rmap_locks) + bool need_rmap_locks, + bool for_stack) { unsigned long extent, old_end; struct mmu_notifier_range range; @@ -493,6 +539,18 @@ 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. + * For the stack moving down, both the source and dest ranges are + * within the same vma but we don't expect anything to exist between + * start of the old stack and end of the new stack. + */ + if ((vma != new_vma || for_stack) + && (len >= 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 +623,13 @@ unsigned long move_page_tables(struct vm_area_struct *vma, mmu_notifier_invalidate_range_end(&range); + /* + * Prevent negative return values when {old,new}_addr was realigned + * but we broke out of the above loop for the first PMD itself. + */ + if (len + old_addr < old_end) + return 0; + return len + old_addr - old_end; /* how much done */ } @@ -633,7 +698,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) { @@ -647,7 +712,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.40.1.698.g37aff9b760-goog