From patchwork Sun Sep 3 15:13:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 13373270 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 5ED03C83F3F for ; Sun, 3 Sep 2023 15:13:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 723C9940008; Sun, 3 Sep 2023 11:13:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D40F8D0002; Sun, 3 Sep 2023 11:13:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59B79940008; Sun, 3 Sep 2023 11:13:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 49F4A8D0002 for ; Sun, 3 Sep 2023 11:13:39 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1914E1404B8 for ; Sun, 3 Sep 2023 15:13:39 +0000 (UTC) X-FDA: 81195630558.30.B542DF4 Received: from mail-io1-f48.google.com (mail-io1-f48.google.com [209.85.166.48]) by imf16.hostedemail.com (Postfix) with ESMTP id 685E518001E for ; Sun, 3 Sep 2023 15:13:37 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=BXfZMih+; spf=pass (imf16.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.48 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=1693754017; 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:references:dkim-signature; bh=CCvQ9I3iX0QNbfprGz58qw4sI+PzMWVZGPRTGiDllgA=; b=zVyn/ohDTmFcwRuo9w5vHjX3O1f7ocilVHVDRcCDHi1FEj+7AOY5uWv5WHk81K5DMK9qSE 49hQEI9goSr5cuYTKC9Lk83Ds64rLa3kDH/gXVG5JJkisGiFSbsJsIwjpSkyZhP6T12YEz c0zwzSKwda2e/APOzh0+BfRbsT5pMZg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693754017; a=rsa-sha256; cv=none; b=aYXcXGt4KnMECKskeyGWfedP+eb6ESCgQEXqBLcM1C9unQfo8dngbaQUPU63V0TWSI7Giz ekZ/2rQ5beWNt2sB877CUannwftRKKxo3GhEkIC8vYl6n4z90PDFB2K8FqG4lo1OMHwusJ 76MMsZ2fsEfwjtmPayNyT8HmByN8CxE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=BXfZMih+; spf=pass (imf16.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.48 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none Received: by mail-io1-f48.google.com with SMTP id ca18e2360f4ac-794e0e8b588so40418739f.1 for ; Sun, 03 Sep 2023 08:13:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1693754016; x=1694358816; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=CCvQ9I3iX0QNbfprGz58qw4sI+PzMWVZGPRTGiDllgA=; b=BXfZMih+2AQVsk//SDErqZnq04D3C2VFAmSWjsTzxDfgq2iaQ68noVWSWF9HwxabjB LUcD9EZrsNWbR5niDJDYzsJbHT95LykO2xOLJQT3urlyddABUBC5CV6N5I1WyDpfnLaG rF6/CJi4ptohYqQnKheVrhkTgFi6RrnvE0cqU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693754016; x=1694358816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CCvQ9I3iX0QNbfprGz58qw4sI+PzMWVZGPRTGiDllgA=; b=Lwy6q/wRt4okJP14lsMOs5wkoDdusZ14Vvsk+BtgjZ9v9a6Od6r9w90fAWt8pmjhUR rgplOR53xAYa3sb6K3VKyF4eECboJiU29+tVFGVntzIfyYVN4Zf62TXoKpkbHr7Tvlqm Rb6XPr4jjOo6TUsHOS8cv6nMhjt+XitEyobAVwwK6MjiKSiMiwhC6HFPJJgHoE9rb9W9 4e5YPesm2adr0kcKGvqjfHgXeIHhiWRXRC8iHw5RPYwjK7YspFd4aOAdeOOPXgT0qwV5 4vmjzjFct4y6hS9kul/pYmw1nVjI8J1Y86lPY/y/6kHV7FBZBSBzyknQdwmZcKdGeg4G tIaQ== X-Gm-Message-State: AOJu0Ywl09SBU0hyflPxNL0Jgsf2cnfbRSRfp0XTxka1Qm8Y5irN7KKu tF4GAs+G8pd0fNmTlTxewA8uwg== X-Google-Smtp-Source: AGHT+IE/HjnLbiJxM1Lmb727IC4/RfuR7vJbdORXm2i4XVKm0REgtSjEy7C30GQyEeQtcHiz426FWQ== X-Received: by 2002:a5d:9b0d:0:b0:790:9f0c:3069 with SMTP id y13-20020a5d9b0d000000b007909f0c3069mr8461710ion.9.1693754016455; Sun, 03 Sep 2023 08:13:36 -0700 (PDT) Received: from joelboxx5.corp.google.com (156.190.123.34.bc.googleusercontent.com. [34.123.190.156]) by smtp.gmail.com with ESMTPSA id f5-20020a6b5105000000b007835a305f61sm2605897iob.36.2023.09.03.08.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 08:13:35 -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 v6 0/7] Optimize mremap during mutual alignment within PMD Date: Sun, 3 Sep 2023 15:13:21 +0000 Message-ID: <20230903151328.2981432-1-joel@joelfernandes.org> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog MIME-Version: 1.0 X-Stat-Signature: zbbs51eaqgphxuzdkgan9qym473wpopt X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 685E518001E X-Rspam-User: X-HE-Tag: 1693754017-256237 X-HE-Meta: U2FsdGVkX19XFmy22bphUP/Mi0PzNlSAtHjjv6TbDfjnOOpCxadYo0u7dJ8aAJ03+p8hHK+YDcCD9qobR8XVtiyJ2I0qpBb50cAZ8Z9s9TPkiuVvHGPIq40ACGpngMi2H6/hcFpxYZWroABVlBTF3XOY6WCdy+XiGIj0uiNd/KBHBSZNpMMy0U7mFDCY7Qbrx/0yhCexTb897ASMGM1ZVln6GfSzi8FfVG/7AOuRE231SzFw5ZTeJQz/6dXySTnFUYtajGu8BMPn5zzxSVoiKx3yu/pEYkziI2tj1yCsN2b/Q2mEjYoGWPWg5uHgI8grR9hg5z0H22bL8fc6uaKtWZ4dEBo7qN+Dh1KcFtK3D7+dmDu7B5C7ENNVOWfqFJf1HsB466MHJ8kn0Jl1QfuiryfLlxeVKs0HrQGLG0+ARNmCfl36tQ1EmxVn5XsLLcVyecIIMD29zvfZjKMazljqqMSImbgDC0iJRMJ/+DwSV+Lwv8pSt76/2jckGq18TG/v5qG6V46rqjPAVCdI/PWl/hYuOOb7R8/Zqzk5YlqZrikgqgFSS6NL5yI0q3aHyihtqYaEZnL70pD107zmap6Ocnr0y7UdnYSTxNUGHiIMkj6a6b3tkjbiUrOpJaRW+WGZGu+O9EnRjxpVW4UdetKq3vHkkZx6nD7UgjMDOXNms3BvMrJrYkWiQuGHYzQ/L8snrXwuWId20Y9BPMRNLwYomwkmX/+L8I9NMmgn8lyCV50dUIio7hmUIYfvyOQXYH7arYWjQ2p21eVCcP9AvbqotAtXg0GFHYHIEnREHTLNXYMeW4HZ2ZxlHxBICKDhb8NLpw+zVK9Bp0PQu2SLDe4SiFY7rTXM/BiYBLpKU6wu34aZYGazayr5cv750dLjKOtNrD3foG0e4praOeG5pJM2eGaEVTXesHwKaEzS2M/VLoxbAxphcRnSWIIWh9Vp6SYooOrJgNR7U4eA5Tpgms0 4NsW4hQm nlny0/Kt5D/Zd53MJSJKx9KPQoGDJBP/dpw0W2pfBcT6R/Yvh/DKZcid9YGtR3o1PRd+Ne2hzQ54pIkQeDx0HHf45KXIE17A0b0y9MdHsFlQqXn8Gazh6lPLEqOWLJ+KyHbkGF/0Rh7xO6YTMJLjgiHGmAtQQZy51nsO/FCiAv1TAR4J4uXPyQbKjCpOPTfQAj+GylXl+cwiBI3MZ+chtu4OjP8bZ2E/pwqCj7dh8XRt6OwDcwwFZMkg06vcVp0KkzevHCI5KvNmmiqQNMfNcaHMZrX/WFub5Cj5VyP+ewVmtZgoGMP9x9U3bVg== 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: Hello! Here is v6 of the mremap start address optimization / fix for exec warning. Should be hopefully final now and only 2/7 and 6/7 need a tag. Thanks a lot to Lorenzo and Linus for the detailed reviews. Description of patches ====================== These patches optimizes the start addresses in move_page_tables() and tests the changes. It addresses a warning [1] that occurs due to a downward, overlapping move on a mutually-aligned offset within a PMD during exec. By initiating the copy process at the PMD level when such alignment is present, we can prevent this warning and speed up the copying process at the same time. Linus Torvalds suggested this idea. Check the individual patches for more details. [1] https://lore.kernel.org/all/ZB2GTBD%2FLWTrkOiO@dhcp22.suse.cz/ History of patches: v5->v6: 1. Reworking the stack case a bit more and tested it (should be final now). 2. Other small nits. v4->v5: 1. Rebased on mainline. 2. Several improvement suggestions from Lorenzo. v3->v4: 1. Care to be taken to move purely within a VMA, in other words this check in call_align_down(): if (vma->vm_start != addr_masked) return false; As an example of why this is needed: 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. |DDDDddddSSSSssss| If we align down 'ssss' to start from the 'SSSS', we will end up destroying SSSS. The above if statement prevents that and I verified it. I also added a test for this in the last patch. 2. Handle the stack case separately. We do not care about #1 for stack movement because the 'SSSS' does not matter during this move. Further we need to do this to prevent the stack move warning. if (!for_stack && vma->vm_start <= addr_masked) return false; v2->v3: 1. Masked address was stored in int, fixed it to unsigned long to avoid truncation. 2. We now handle moves happening purely within a VMA, a new test is added to handle this. 3. More code comments. v1->v2: 1. Trigger the optimization for mremaps smaller than a PMD. I tested by tracing that it works correctly. 2. Fix issue with bogus return value found by Linus if we broke out of the above loop for the first PMD itself. v1: Initial RFC. Joel Fernandes (1): selftests: mm: Add a test for moving from an offset from start of mapping Joel Fernandes (Google) (6): mm/mremap: Optimize the start addresses in move_page_tables() mm/mremap: Allow moves within the same VMA for stack moves selftests: mm: Fix failure case when new remap region was not found selftests: mm: Add a test for mutually aligned moves > PMD size selftests: mm: Add a test for remapping to area immediately after existing mapping selftests: mm: Add a test for remapping within a range fs/exec.c | 2 +- include/linux/mm.h | 2 +- mm/mremap.c | 73 +++++- tools/testing/selftests/mm/mremap_test.c | 301 +++++++++++++++++++---- 4 files changed, 329 insertions(+), 49 deletions(-) Tested-by: Zhenyu Zhang Tested-by: Zhenyu Zhang --- 2.42.0.283.g2d96d420d3-goog