From patchwork Mon Feb 27 17:36:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13153982 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 F33F8C64ED8 for ; Mon, 27 Feb 2023 17:37:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FB8E280003; Mon, 27 Feb 2023 12:37:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8ACE3280001; Mon, 27 Feb 2023 12:37:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72497280003; Mon, 27 Feb 2023 12:37:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5CF93280001 for ; Mon, 27 Feb 2023 12:37:25 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D9210A0ABD for ; Mon, 27 Feb 2023 17:37:24 +0000 (UTC) X-FDA: 80513778408.15.103A128 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf19.hostedemail.com (Postfix) with ESMTP id 1333B1A001D for ; Mon, 27 Feb 2023 17:37:22 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bS7cuWST; spf=pass (imf19.hostedemail.com: domain of 3Uur8YwYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Uur8YwYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677519443; a=rsa-sha256; cv=none; b=sTrL25nQOnAzWmknSRZU9MUy/mUKY2o5y0Ax9xfoJMskWKzxQ1kRb4rXgsAICLE9T5Vyr9 fCivKl04Xf7+6BiFFYMf5pscX3F2Nz337+fCcII7b4UT41rbySJmw2cTWg6oUDl1W5sYOC 8iCtDO6UqvacVwpFQg6fa+5S07+ONH0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bS7cuWST; spf=pass (imf19.hostedemail.com: domain of 3Uur8YwYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Uur8YwYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677519443; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=q4IoNs8/2s63po158n4pCM20IldvB6LsORlyoWZYhDA=; b=q9LKX+bSF+y0eipIz0Q/UFYVTgCPSmSkhmLoLyW55UbWkCTVhDJ85Qusv/1Vami5aKFiPv IGDyPibnrzTbHvzVMS3jHTpp7I9DI7AOXF23ay7pCs51oeeSdnpcou4yIeBjCRdwP/qoMY XOn6Vg8lVfZbRGkGz6OfsePCik6LDis= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-536bbaa701aso152729547b3.3 for ; Mon, 27 Feb 2023 09:37:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q4IoNs8/2s63po158n4pCM20IldvB6LsORlyoWZYhDA=; b=bS7cuWSTvikNO5/1/KRQJLPoIX97MSDopLQjzIYhd9M4ATiilp+2nnzhJYwm+xkfAw CA4PkRjLFErdI6iJpWSrCW/0zzxhXa3aDXwe+0SrD6SM1VCA8VjhRHuY2QcHPeKNakQW P+DawdVxSLtrg/I5seo5dukO8m39Wj2s4hqiK8xve+bjTmkIkwxAaDpb3/Zo6UgfUlrQ EPB7WXnu31e6AaMIPOJ6t49a2aYPUJ8xG3eJeremwZc8bnMi2k5b+Q5ZfPziQ2FlHzzp w2I2hG7tsvd97Sj45eHKWDA+0nQshMfUHld0I+kGvTMYTZlSoT7VYdn14KEIlMTUGdtE e10A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q4IoNs8/2s63po158n4pCM20IldvB6LsORlyoWZYhDA=; b=d+DQcxHPGj974esEPFS7dbuIs49mzv3PKpB6WFECXY5HQjBLg5b+rUkJYZ0HjYCFtt 7KF6fJHIjEp9Q9rRULyMsZ/bc2tAgOjfaDHGTin1q4nmMg0ywmdJBKX7SmZFfJd73bah eslqOx6U/IW5rUPosuZxV4Ib00xhQmLbG0G9X8HLpjpC0jw7nZyg+XV3EEN43lC2ifH/ MrqWnLwfBfX0qlRKz6w0dyNdVsXMnpA/Cn2udazc28/nWSgyuf3pA9wjV+vZZyh2DwfI 0N4hhL/NjT3ZfnVWYcn8m0labaQ+NOBGwT4JcjJ6PqzeGW3S+WQL4cJIZJOcLl7FJH4X VB/g== X-Gm-Message-State: AO0yUKWHcqFkzCuhQfnHQMmWgpDGdrVdj0I9Rw8ATfhgupfVUmjQFZoX D9kaBY3R8WeHeBmNH+1slWUTiYm4Xwc= X-Google-Smtp-Source: AK7set/PpfdmNkoTcymoja/27Ir9z6PS18iSX+0Y7AEWmCWJmOIOQsn+Wv7zMECojEbGHKP0cPXfu+hFpqg= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:e1f6:21d1:eead:3897]) (user=surenb job=sendgmr) by 2002:a05:6902:1388:b0:855:fdcb:4467 with SMTP id x8-20020a056902138800b00855fdcb4467mr287176ybu.0.1677519442170; Mon, 27 Feb 2023 09:37:22 -0800 (PST) Date: Mon, 27 Feb 2023 09:36:18 -0800 In-Reply-To: <20230227173632.3292573-1-surenb@google.com> Mime-Version: 1.0 References: <20230227173632.3292573-1-surenb@google.com> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog Message-ID: <20230227173632.3292573-20-surenb@google.com> Subject: [PATCH v4 19/33] mm: conditionally write-lock VMA in free_pgtables From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, paulmck@kernel.org, mingo@redhat.com, will@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, chriscli@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, rppt@kernel.org, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, leewalsh@google.com, posk@google.com, michalechner92@googlemail.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com, Suren Baghdasaryan X-Rspam-User: X-Rspamd-Queue-Id: 1333B1A001D X-Rspamd-Server: r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EvpEwjYnQ4lhVPI7sZ7JeWEy0ZaW9Dbr0b5bqyMuGyYcXsQhppzbgTebORkYfzc6O2L42JRuk19ZPW1+x3scA4VyRWHv0O4TIee7KAgKl7NuywvDL48AJ/gl9mbpXg5CIjisC97Obk/beWM/c0goj4rtNr0KTVnqeXPXAfAkO04tHZSF+Tlv7DFSQkIYPHgK7Vf1BceoGsgXggWr1Jlw2M2Eg6I9mgFCOMl6q4o3LpD4+VvZqD3hosy7u4EqnGy1hqE2uxBmGz1UYK4mw8Mg86pUqZxJFAeItFs+zSC4jyiYLRuZ/41LoPvgX0MBDduefPVfFc2x1iE2ReG6I5ztb8wsdl7vs/n/cCjDiwDYSJHcM9gVks/0KeLomTks6wZ+1/rs/clO85wlOo9JmZo8OF+QlEEuEhUsJjNxZvd/YOozxHyS7g2OoDMnt/FYUWZVRXUuqZ47cWRWqrpV7NRoOGjsk1TM6wz50NVyL1S+HoR7kqj8Q7sSAPMUxca/9hw8UrwrX7rvbIBTpizxmE2DGD2jiOAXqGwKvPnzR8Yc9gx11QuSlaHAups6gHapug6icgHj3+bmHmN5ygfJichC7Ex/3kKzujXRVtq6bYYkSYDmTGd9lN+tgkNoU8F1GEt3FBbIYynXna0TmuHTSoY= 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: Normally free_pgtables needs to lock affected VMAs except for the case when VMAs were isolated under VMA write-lock. munmap() does just that, isolating while holding appropriate locks and then downgrading mmap_lock and dropping per-VMA locks before freeing page tables. Add a parameter to free_pgtables for such scenario. Signed-off-by: Suren Baghdasaryan --- mm/internal.h | 2 +- mm/memory.c | 6 +++++- mm/mmap.c | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 08ce56dbb1d9..fce94775819c 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -105,7 +105,7 @@ void folio_activate(struct folio *folio); void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt, struct vm_area_struct *start_vma, unsigned long floor, - unsigned long ceiling); + unsigned long ceiling, bool mm_wr_locked); void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); struct zap_details; diff --git a/mm/memory.c b/mm/memory.c index bfa3100ec5a3..f7f412833e42 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -348,7 +348,7 @@ void free_pgd_range(struct mmu_gather *tlb, void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt, struct vm_area_struct *vma, unsigned long floor, - unsigned long ceiling) + unsigned long ceiling, bool mm_wr_locked) { MA_STATE(mas, mt, vma->vm_end, vma->vm_end); @@ -366,6 +366,8 @@ void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt, * Hide vma from rmap and truncate_pagecache before freeing * pgtables */ + if (mm_wr_locked) + vma_start_write(vma); unlink_anon_vmas(vma); unlink_file_vma(vma); @@ -380,6 +382,8 @@ void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt, && !is_vm_hugetlb_page(next)) { vma = next; next = mas_find(&mas, ceiling - 1); + if (mm_wr_locked) + vma_start_write(vma); unlink_anon_vmas(vma); unlink_file_vma(vma); } diff --git a/mm/mmap.c b/mm/mmap.c index f7ed357056c4..ec745586785c 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2152,7 +2152,8 @@ static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, update_hiwater_rss(mm); unmap_vmas(&tlb, mt, vma, start, end, mm_wr_locked); free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, - next ? next->vm_start : USER_PGTABLES_CEILING); + next ? next->vm_start : USER_PGTABLES_CEILING, + mm_wr_locked); tlb_finish_mmu(&tlb); } @@ -3056,7 +3057,7 @@ void exit_mmap(struct mm_struct *mm) mmap_write_lock(mm); mt_clear_in_rcu(&mm->mm_mt); free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, - USER_PGTABLES_CEILING); + USER_PGTABLES_CEILING, true); tlb_finish_mmu(&tlb); /*