From patchwork Fri Jan 27 19:40:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13119259 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 7A719C54EAA for ; Fri, 27 Jan 2023 19:42:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AEFDD6B0098; Fri, 27 Jan 2023 14:42:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC70D6B0099; Fri, 27 Jan 2023 14:42:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 966A46B009A; Fri, 27 Jan 2023 14:42:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 880AD6B0098 for ; Fri, 27 Jan 2023 14:42:03 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6391B41014 for ; Fri, 27 Jan 2023 19:42:03 +0000 (UTC) X-FDA: 80401599726.27.8EC6F76 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf04.hostedemail.com (Postfix) with ESMTP id 945CC4000C for ; Fri, 27 Jan 2023 19:42:00 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dIXUHHJy; spf=pass (imf04.hostedemail.com: domain of 3BynUYwYKCM4CEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3BynUYwYKCM4CEBy7v08805y.w86527EH-664Fuw4.8B0@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=1674848520; 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=PbZvlTacNRMtTYgLZEFyk5N8KO4rHMcD13osXtjRKQc=; b=77+figJFhK4BeQg66HUzCKtLZCl6HD+0P/vDyDAnBEzHDsvC3gAyXDvtwS2okYM53LqXEc p8zHtbTj+eAMeUxkxt6b+iaOnH45EVcPpgmFmboUkfm60CK0YmgrS3vixfARBbGsFsx5pw Elx8Wlyd9lVuH4SVVkzicfrildPRvJ8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dIXUHHJy; spf=pass (imf04.hostedemail.com: domain of 3BynUYwYKCM4CEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3BynUYwYKCM4CEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674848520; a=rsa-sha256; cv=none; b=DSVZKfSlFi5N/jcL8BMccvQOt4uFb4sRgksJvlUd4S83bZLY+7hbWU1m3oB48IzyeKxyiy ysLosVPAGdXOgOyxTG65y7bH3FgwD6Wdr8qgUMWDJOeRDcGBXUUlcFirRzXzgXeOQgVkiv kaO7ZgY4uvr2HRBW0f9XtH8NsAU5xQU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-4d4b54d0731so65443587b3.18 for ; Fri, 27 Jan 2023 11:42:00 -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=PbZvlTacNRMtTYgLZEFyk5N8KO4rHMcD13osXtjRKQc=; b=dIXUHHJyt3zldvcJ25OA8M/N2ekAkSadxrM4Vc86G1vAxHUN7FQNbSzlWzbH3EF3Ay Np/mC7lgd8yNrbdx+fJZdOiPs5Ax7Y+isaOSeA0W3slHJVunqRE0R5gvKOYJUJLOV6vy eusS7uxTBSUHthbBE/M+AcF029iuITRiXxkeDAE1bHTcC4qSxwHlMi32ImCbpO9h9EMV +wvjzOskJitBDhg128jcOLxlukATYCWhVPwB6m3ynKWMcyTvnD5SZLfuK2oGD5H+Rl4/ hAABUVAQozzvBCIG2lhb5atfzqUlxx9w7Rjl6rs3/T9zVK0hATJevSe9VLcJ9uJd0aXA HxXQ== 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=PbZvlTacNRMtTYgLZEFyk5N8KO4rHMcD13osXtjRKQc=; b=fHHrXOQdMHAuAzNwE/+IuDVzbGPOGI9mUy3dIy0TTQxqSJ/DA75EKc9HtO/uQ5uHYN JNFQK8a8LcBJVuIcpDxc7waDcmnQK3oHOhPHuftRHu6hj88W75TFF9PWZzlmcsSFTLwG 9PfFX6ta5doHzbzHpKyEVqQhPC8X90vPqkzevnBSsjoHVc5lTe6F9Zi5wn0ccfc+LWHi B1LFsQvYxImAAJrE9neSIHSgCxjQFBp3xs/+5clkO7xEfCWuwsz9r45wNr4p+bU4d2ov bgmJe6jPDxEyBlrObDQAWuS4HWJLMC9v7knB5rndPGhm2C4Gkc3MskV++D9pNLw4Da/A ofJw== X-Gm-Message-State: AFqh2ko59eXdo444AzCWBtYSfVBigmWVWKgwsTWv9Q3+TjxtxaoTmx18 /GPlvGoexLxipWjPqvaJU4Zmv/r9cZA= X-Google-Smtp-Source: AMrXdXtxOfNby/WJjyD0PPf8WyFmfovtGs47EqfediusuKgjOEAtqZl1SXyt3bJWMQsRbQEhpFz5LQpAf70= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:4e19:be9:c5d0:8483]) (user=surenb job=sendgmr) by 2002:a81:cd4:0:b0:4dc:e929:40fa with SMTP id 203-20020a810cd4000000b004dce92940famr5633708ywm.331.1674848519684; Fri, 27 Jan 2023 11:41:59 -0800 (PST) Date: Fri, 27 Jan 2023 11:40:56 -0800 In-Reply-To: <20230127194110.533103-1-surenb@google.com> Mime-Version: 1.0 References: <20230127194110.533103-1-surenb@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230127194110.533103-20-surenb@google.com> Subject: [PATCH v2 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, 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, 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, surenb@google.com X-Stat-Signature: 8juc3ih8ump356fdz45si4ou5fqjehzh X-Rspam-User: X-Rspamd-Queue-Id: 945CC4000C X-Rspamd-Server: rspam06 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W0WComL043aXrUnXOM0GZuW4XctI+X9CxpOr3M8w4cmKfcEdFXEH5VzMXOFVvGLp8twy2LcOboTTqGTZPpqZqLdz8NXdQhGXFhp/1q9CGWW5BwdJA/Tnqn3Jvid7Ksk4GxgAGK2HcImjNc2Wf+fVpXkh27wVwcar9WSctT5lsaKojrt/biT4+Q3DY8NO8ZUGUXUX8HGiTM3Fcs+QGer8w6MH10UW53v4LTraEv4Q81+Jxobj930cGR0ByLPLK5K1x2AMSn7Mjm6+pksO9tM4N4EVnHLMw1j5ebRNT71aI+RIg31Tbkzw7brNwoOMMx1z04JJg72H4yIMdVFLeNcvC9wdywxsXXBzD2FjQtXkhT2cgjc6F6u69jV1Xz6lFedHNx4+HKIX2GPf06V2RKOqBPsWrJxaUdIAyjQXs0z36WU5bQ50RwqOGehDJOqvQePm7Itzk34qnQZlQiG5hrZz+u+RwugwY0SZ1XXMY51eMwW8ThHN0cNqtfMyEUMx/NrdQuc/r/8Kd0FzAOWy1Ldn329nNM4O4sQQ1GpLrwV10xDQwQx6NeM5X9fm4ndrnZEJvuBi2Sp/9wCeGx/x6UvuAbMH1VtzQij4o/vpQXe98rYnhs4iN2WZKK/LttJU= 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 90bb2078444c..52d7e9c2e58f 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 7a04a1130ec1..d48c76e9fa57 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 5bdfd087b632..57cb3a2ac9b1 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2157,7 +2157,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); } @@ -3069,7 +3070,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); /*