From patchwork Wed May 24 15:32:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 98567 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:3046:b0:115:7a1d:dabb with SMTP id p6csp2916549rwl; Wed, 24 May 2023 08:45:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5k+4eOtnVMn0G/EPeEPqyyr3Ckp/f23GIheiPBmKQbowXWtuo77O6O6yidWrKKuQ1mPsBd X-Received: by 2002:a17:90a:8c10:b0:250:1905:ae78 with SMTP id a16-20020a17090a8c1000b002501905ae78mr17139112pjo.15.1684943130989; Wed, 24 May 2023 08:45:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684943130; cv=none; d=google.com; s=arc-20160816; b=irI52shskZoQsyJTDoLlRu3bdzPAIospsdx9ezI41vUgXpH/ov2of60J9IcVmPs+Wg rzsMY/u88dLdtyhthD04o+uZaZ+CgOYcffAwbsLl+3t8t7pJx5u9S+sjjGsmEhr5yOBc KU2Y0qnwxO+TIGgODMPzRenJL1kCGZZePRT7PS2+3NKCUYwNtDsax22rWeS/yyRfobrE dOisyjpTBzE7SNXxSbeWDo2CgvoraQlovRZvtlxlA0zVD4dAxFydaVF5OSlLSJSpEHN/ 05FEUJ7CpahP3Ne+gWni9XzAQdEHNx8ZZ8PaWljpP5beG8bghccc8H1gIIVy1Vd6iON4 r+9Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=TYAWyrxgPtqWXYxm6cJhdFwBx5wHciPZ/aYd0labdNY=; b=ekCA71eFm0S+ORou45P7m6LMhoptmcjjbDC/3RsbU6cHvZ3VCndE1u8qSAFwZgbD27 8chy2W1E0zf9va0vtLYuoonxmYberWFGgSYwhHQoK2vfuz7YvDp4g+sBvaw5pRSc6Ust 6LF4aTu4m5XIK/mq51QwVuZiukVcovOOdvq6ePPnBFNe1AUwY37XI9BdvAM/nYdk9x87 0JiH3x+OJdpCB0VAos4zy2XwRfr8CED7rTSveyXR2PRuX9e6TiKZpELAcRyaFZOI6LJQ KmvjwURrJtcZPkJNGWLvLJ0dgOITcI+ls9n0W6vbGifr+5Y91ep1h0DAz5kKKH2DfSYh L2MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=On7I5ayO; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b22-20020a637156000000b0053ef0f20fe3si3290439pgn.600.2023.05.24.08.45.18; Wed, 24 May 2023 08:45:30 -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=On7I5ayO; 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 S232903AbjEXPdB (ORCPT + 99 others); Wed, 24 May 2023 11:33:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235951AbjEXPc4 (ORCPT ); Wed, 24 May 2023 11:32:56 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6C2F122 for ; Wed, 24 May 2023 08:32:53 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id af79cd13be357-75b132ad421so152851285a.1 for ; Wed, 24 May 2023 08:32:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1684942372; x=1687534372; 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=TYAWyrxgPtqWXYxm6cJhdFwBx5wHciPZ/aYd0labdNY=; b=On7I5ayOaYhi5YG/GPdB9nxaPm77pHmMCGllRGYcQUbOR9RaWe8+E1kwIyz/Gm1IO2 lzlpVKQhkPTjP/JCXbURVFFWQ3jgbHYHGgepr9uzBOnTK1CeaL3pkQv0Y3ntC9d96Fw5 Q/V2PreQmRTT8rUh3sL+unZfL9jgCw+Ofoan0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684942372; x=1687534372; 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=TYAWyrxgPtqWXYxm6cJhdFwBx5wHciPZ/aYd0labdNY=; b=jqKVswMS4MJiZ9VQUYkEgZzAfL3xdLW735gG7PpzlDqjkNTr1rXFocdCnFl6iLF03U a/22acoZdx4rKETaWAvUsAD8SZYUKHh1XFXxOYhizLyfSTToaPxrplK1wlVS+P7jpdtu XCYCvKTXqHDg2MXndQcLfg2UTcK+pQJXy5Luj4d02RMcByam98455x85eGkx93J/dlWn 7NaM0FIYcw9S9/B9CoNk1P/sFrveo6PRCVQ300nPteY+CbyA12pf3blOS7LLkDHtaHIL pRSAIDnSb4BUMOEkrNkJfcYWERFmto28B7k3qojiezcsxhtXvfATD2JvQjwlCnb/gtJ7 N4vQ== X-Gm-Message-State: AC+VfDz2MK5FnwvNThGJJ4bgtDJRRP3OypYKAS1o6jtB3sUO4ORnVwd4 yuyFXBSAcvxaUo0GyoTqvdAdDc2OVN+yDwJt5yE= X-Received: by 2002:a05:620a:172a:b0:75b:23a1:3634 with SMTP id az42-20020a05620a172a00b0075b23a13634mr9248078qkb.69.1684942372538; Wed, 24 May 2023 08:32:52 -0700 (PDT) Received: from joelboxx.c.googlers.com.com (129.239.188.35.bc.googleusercontent.com. [35.188.239.129]) by smtp.gmail.com with ESMTPSA id v18-20020a05620a123200b007590aa4b115sm3296906qkj.87.2023.05.24.08.32.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 08:32:51 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Linus Torvalds , 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 , Kalesh Singh , Lokesh Gidra Subject: [PATCH v3 1/6] mm/mremap: Optimize the start addresses in move_page_tables() Date: Wed, 24 May 2023 15:32:34 +0000 Message-ID: <20230524153239.3036507-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog In-Reply-To: <20230524153239.3036507-1-joel@joelfernandes.org> References: <20230524153239.3036507-1-joel@joelfernandes.org> MIME-Version: 1.0 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,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766790928215063156?= X-GMAIL-MSGID: =?utf-8?q?1766790928215063156?= 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. 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. d. We need to be careful about mremap of ranges within the VMA itself. For this purpose, I added checks to determine if the address after alignment falls within its VMA itself. [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) --- mm/mremap.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/mm/mremap.c b/mm/mremap.c index 411a85682b58..184d52f83b19 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -478,6 +478,53 @@ static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma, return moved; } +/* + * 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. + */ +static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_align, + unsigned long mask) +{ + unsigned long addr_masked = addr_to_align & 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 finding prev, assume there is a mapping and return false + * which will turn off any optimizations. Yes, we're conservative! + * The 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 some 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) +{ + bool mutually_aligned = (*old_addr & ~mask) == (*new_addr & ~mask); + + if ((*old_addr & ~mask) && mutually_aligned + && 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, @@ -493,6 +540,15 @@ 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. + */ + if ((vma != new_vma) + && (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 +621,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 */ }