From patchwork Thu May 18 02:18:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 13246055 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 ABBA5C77B75 for ; Thu, 18 May 2023 02:18:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F6A4900007; Wed, 17 May 2023 22:18:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25806900006; Wed, 17 May 2023 22:18:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8C1B900007; Wed, 17 May 2023 22:18:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CCE8A900003 for ; Wed, 17 May 2023 22:18:48 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9A0471605AC for ; Thu, 18 May 2023 02:18:48 +0000 (UTC) X-FDA: 80801767536.07.53FD043 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) by imf13.hostedemail.com (Postfix) with ESMTP id CD6A420007 for ; Thu, 18 May 2023 02:18:45 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=gpiRUjEC; dmarc=none; spf=pass (imf13.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.222.170 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=1684376325; 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=sKe+AGy/Knc6S6d8Do5DN/gqM9Uy00SAbxH28Kp1LUQ=; b=vZAtt8Bkn285OTKP3Evo1MDDNFANkQOqrRZhmdapg2hHolkFT5xz2qC2semVmvefduVtUF 0oxsTpxPkRXJWVW03E6SFxgty5aQcyJRcAtvF2rTySlITTGuZijn0l1zyN9rjMcDMbdorn 1Gq+FIKJgnS2Ozj2TULwFR9s9w01pxo= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=gpiRUjEC; dmarc=none; spf=pass (imf13.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.222.170 as permitted sender) smtp.mailfrom=joel@joelfernandes.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684376325; a=rsa-sha256; cv=none; b=k6R9d1ZLTsUUuNIn/t9XjcyXTVHfFy7ilCzVibA0hE2dA/yGg3NP+GQibJI0SapMDxzkhl YOjvRUzwukCkddic9Qk4C1kXQl/T7m59t8wSPePJLXCesn2xrMMpuKvIs8Ete/2xqipa0E mFhPCopp6Ss5iBWVFtL3HmpfgTb0SW8= Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-7595561f2f0so85503585a.2 for ; Wed, 17 May 2023 19:18:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1684376325; x=1686968325; 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=sKe+AGy/Knc6S6d8Do5DN/gqM9Uy00SAbxH28Kp1LUQ=; b=gpiRUjEC25YrRlvGt299DCLZarxkVMlPppBRMeGjpsXJgixg9Mf1V9AuFtBCGh5WIO zYyRiKyN25sNF/Dcpz8SONWOiUN5ginUd3c7nz+IsRYtc1emCT1YYGJXOzWz7hQjxiRf eGy+COuTrQdS11tlfAZg8TDkFkoBJhCtLoMlI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684376325; x=1686968325; 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=sKe+AGy/Knc6S6d8Do5DN/gqM9Uy00SAbxH28Kp1LUQ=; b=Cnd6FFnIYvzYa1fCTA+hfvTK73CHZQl8gz/VUp/1MW/vXymZXohWblCH0v5Q1UHqmQ J5hSkD43ka6reWEW/lkFrW2lxysH042WVK4YAdJSBUcJu2g13wDHkhw4Vu3hRx0wz7C/ rJoQC56taeS49EWLITQo8ONTZcIxCCVwovWYMrbGwZ3fr7O9FZZlG0aQhpPILcQ0ggXl Np6I11aZK3eF/ZqwFNjI+sTpMOYACwbmSV02U52SNmZ6NL5Z0agYSpBJ1/to1oKf2AUC xYgJIyZnxIJFc7e1MyPYFXFK5c/Uqcphvo5ARAzTX+5JqvJUkZnuhE0aql77fhjiLqPb 88lA== X-Gm-Message-State: AC+VfDyf9zjCXNoI1cnGkTjyoGBpAzOgK29Ioei7pzGaLdCzEj7DamvP kHykAz5IYTOh5IynUDm6DwpnNQ== X-Google-Smtp-Source: ACHHUZ777yYcb28ukvf7wMe6arbQau3nQ7xByk5Y74MW/Ag+xPWbEEnjrqrm1N1GiSy28LtIBX+nRg== X-Received: by 2002:a05:6214:2349:b0:5e8:3496:e641 with SMTP id hu9-20020a056214234900b005e83496e641mr2657341qvb.13.1684376324809; Wed, 17 May 2023 19:18:44 -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 j13-20020a05620a000d00b007577ccf566asm85082qki.93.2023.05.17.19.18.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 19:18:44 -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" Subject: [PATCH RFC 1/4] mm/mremap: Optimize the start addresses in move_page_tables() Date: Thu, 18 May 2023 02:18:22 +0000 Message-ID: <20230518021825.712742-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230518021825.712742-1-joel@joelfernandes.org> References: <20230518021825.712742-1-joel@joelfernandes.org> MIME-Version: 1.0 X-Stat-Signature: fymh64y8s8y446n837ssqyi6h6ncmj4d X-Rspam-User: X-Rspamd-Queue-Id: CD6A420007 X-Rspamd-Server: rspam07 X-HE-Tag: 1684376325-316503 X-HE-Meta: U2FsdGVkX1/bncB9CIkbIyVMGUClwIK+lnxYWenV+eUK7IIqhsoKQ2cYj1PaF+Q17NHEmfwFq+1qMA5+HDM03FGr4LKQ7Dc8p6PXU7ZjXqj7+2yy+cfCt3HdFtNRlF1pwKXLsQ7xf4C0NR63dbPTSPHnHi7SOobJY3RmvjSVm8DG9QbJyDczKTI8PNYOqUjdInykyWnCgQjYl2qjxa9nSKd1wfOdsvUG/UblsvI/uYvmSVTgO9T+V2U7OuFXcs4cfi8C1/68INLUWHxnIA+oqLRQN8cT9Sh8cAqfj9HruMDFhBiDrg52FEDrytgGhPWdlWvV+h5uPeM7+fRuaBs7wZfXz7FcAwVl/h3nARt8LI1RrQb9CTCyiF+OV0WK0oR+mNDitgFDjLadOYC5ve2XR8Knj9DYTVrCE+1l3c6QNfilq63PGN+8SnvWMQqUaplzevuiDQxHUTrr3HcyO4i1eCjAByfdx+GGpKuZwARfExlLBDl2jU8THJB6RbEqsjlRNDDkLjTw0Wcmlo6AOQXhI9k/LYx6gYV7PhFBhmPka7R0tgABfu65K3D0TJv05iJAtm2Yh0X0d3LvZSCydXodspynkhCDVQJ0ggDrTtrKyOz785Pqv3bHa1eLelCeiVEOspRDPydJdjUkiUNNqf19mcrf7B5NHq2pxb2Zi0pPuouI5TLArp4wXvpM/roAvZQTD7Er3xf5c3XWZWQz0p/VJELPXTd/2hF+b9mv/o4a5OlEXpirba7RI6V8Rsy7qVgdT4YxQJ1jSmMo/UjiYadxQoIpMUvdo1yVtAfvNnrmYAoiQbs5pg21uDJoclLR8kEBV7ITfQqFh3J2Spi0s3Vgd03bH0HDdmerV+DXMtXbEhp9nLKNeWFUiKOAXjyGxiQ3s0ib+uz4AYMIXuIRPMjjfXLZs3KdJzFpFJNivLawnkjjSvHAI/k6HCskZkozzPQpFwwAh9vSXu+xNr2yHd5 Mg0gpamV xO+oEH4zSQyxKf40wfMnQvxTbhIq2UhIU1HeT2nMPBmW8f94nAAyVF7ae3o3rqTiR1iIzl8DKH5tAgsQjmRUSya3kb3FS22phhTtcCDxd/rljvEPqTR10CrrWkL6eZ9Gg1gc22bQttV4Ob9BXWtpmGEYiBGDCNnMCQR0CZ4iLHI4UfXiyIp21ztY0ONywIxBQwh2wflXywiyLW3fqIypJagNw0HVwltTjSHiH06pEBpY/4tMpDSsEMZzu911OGvo0kE9yGxvjpy2mpGieBfOx9Qlz5nxsYcEu3v/9TEqgXuU8iI9qrlkuLIxfEPdtuIdmCbBq241wRo3snqbQZ4fDOVHYW2IQAPl+fx01XVvCL+W1M9GVzwNAwVZiGnfn4Mr7kuIiSZsgcGEjB9qFvwntwHxt22VNWeHz4Lof1ryTP4ZJZU5EUnlr2hCAvhOM77dcdYluEswyTOT0byVuyaBPp0XRWAQMQUTjqHHOYhANDrL0F+LmD3RismzyJ1AXsCKe5pATRzUObqGkQ/ftiOagqAB+p35GqTN680hR 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: 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/ Signed-off-by: Joel Fernandes (Google) --- mm/mremap.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/mm/mremap.c b/mm/mremap.c index 411a85682b58..d7ee3b20e170 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -478,6 +478,51 @@ 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 check_addr_in_prev(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, there is no prev mapping of concern. */ + 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 true; + + /* The masked address fell within a previous mapping. */ + if (prev->vm_end > addr_masked) + return true; + + return false; +} + +/* 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) && + !check_addr_in_prev(old_vma, *old_addr, mask) && + !check_addr_in_prev(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 +538,10 @@ 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. */ + if (len >= PMD_SIZE) + 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);