Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1929879rdb; Tue, 5 Sep 2023 09:05:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFFI3w51dAD9jtzQULjlc1vnojFz165lWvfEpTI0rbUA2hiIyWx3mIDpTSU/l9z8DXfOo8j X-Received: by 2002:aa7:c392:0:b0:523:2e63:b9b with SMTP id k18-20020aa7c392000000b005232e630b9bmr229981edq.24.1693929946959; Tue, 05 Sep 2023 09:05:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693929946; cv=none; d=google.com; s=arc-20160816; b=tpvHlgZf10pRP6M/DBiexhDth8dbTDGRFaXjpfhp8BzFv1eyyqO5l2Qx02FraRV5cm Qpzqhcud591iCjtuJvJoaioe3+1eMhKg3QjV4/zDA2S5DSe7QGfWHoI74xXw1934dljj qFyd+RKN85drSaq94ZGlTUZgZkdfGUahhbr/Zqxlj33LnxdVgTFK7LnQ1ejYVehHXf2n tCxCO/RzskxbgXWxSYRaqk2bZtU9ucz8AJIOcDcqC6WKb5iSmu0pti9JDscbZNaeCbyx bqMs/buKK/ge/qPn/fzNNOKFtWcHO9bu/GdqZoTJ479W/ZFGDhAz/T9+oxJPlXrutGJ2 8nkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=CCvQ9I3iX0QNbfprGz58qw4sI+PzMWVZGPRTGiDllgA=; fh=VFmCb9RqHMnVt3sRKvevIPp0QDAe8pe1oYE4eYQLJkM=; b=iUGNKgnR3uVYKB1bMa9NE1XAoFWS81SJ7jexaLBQEnC0f2oik5LRE3rFJo9UWFYuPV XKmL3WA1azRfuD7CyIHXnw0OrpxKl7tqqMSwi1TUED4DFDbAqG/VgMzP/Iue88C2bgup b0oH+/Hs1EXHTsaoae+LF6YdDpFPGySCmfY4ZLQq6tSMbKcuao55kPnIi2wTWmKIwY3J NHd0lMxCVpiJQrkL1npwjKap7FjDIKGtoFjWib2vCnBJKaC71EzXbQQycGYZFJbIcYQs gs7PYQYs7tvjd30A0SRQVKza9hW4WV3ntUT2lD5wYmRqFUtp28isQu1UPr0gL8/IDb8/ jc0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=L4K6rt5r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y11-20020a056402270b00b0052e312a60f9si2798052edd.35.2023.09.05.09.05.43; Tue, 05 Sep 2023 09:05:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=L4K6rt5r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241832AbjICPNl (ORCPT + 12 others); Sun, 3 Sep 2023 11:13:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231787AbjICPNk (ORCPT ); Sun, 3 Sep 2023 11:13:40 -0400 Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9752D115 for ; Sun, 3 Sep 2023 08:13:37 -0700 (PDT) Received: by mail-io1-xd32.google.com with SMTP id ca18e2360f4ac-794e0e8b588so40418939f.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=vger.kernel.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=L4K6rt5r9HcwPbf9IQ/+LEowMCr/1AjmF/SivHILUUEvX1UtxO0iy990ha3e0tnILX /315QAJ860cny6tOiJxMssvi/NSanB0y8vTBJTkDY2u7HLdMsgVDSmKN1e/XQ7XRtZkA VlU3uS+qLRGSR9pyTxd5HnOMEYL55/YeMYvO4= 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=Beg12RPHz9c4/ThtOsmXsxoCLpZQpJeceWMXvz78STNwOVReUw0umakQWOp1lhoKPP m9DLqb8TEZ746serj9LP5Ytn7Zeehrbzm7+VbYE4jmLcuPK0pqYaVyvmrvdrFHD0QgXZ qFKbxdjBrH7SRcq/oU4OvUVKMyT+ISkdG+qg1FGHaGl9jq8z7+tXCCexOWkxnJIy4yZL b8e9mVWOEb2wsGyL8kAnGr+5FrunNmzcGJhYeGN6NSoiavMbHEUob5nExsO7KdTC6T8I BgLH2+fYBsOldP91dJvpABNIr33rCG4gZ9XWmn3I1o9GnFYWRZZq90DtVjVuUUssAxVZ /8rA== X-Gm-Message-State: AOJu0YxOOBQChlkvb04mtQ3k2/SkNgL+aGpFFlvAUxbm52XjMWj3VI8S 5Nc1+zw9UwhmSTXfTyXauUaVzXSMkDVr5nB/9SA= 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(-) -- 2.42.0.283.g2d96d420d3-goog