From patchwork Wed Jul 5 06:37:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13301710 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 43076C001B0 for ; Wed, 5 Jul 2023 06:37:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE2D68D0002; Wed, 5 Jul 2023 02:37:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A92A16B0074; Wed, 5 Jul 2023 02:37:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95AC48D0002; Wed, 5 Jul 2023 02:37:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 88D346B0072 for ; Wed, 5 Jul 2023 02:37:22 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 45E56120544 for ; Wed, 5 Jul 2023 06:37:22 +0000 (UTC) X-FDA: 80976601524.17.9C46DF7 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf09.hostedemail.com (Postfix) with ESMTP id EF4D114000C for ; Wed, 5 Jul 2023 06:37:18 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=NRt67G4e; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3ng-lZAYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ng-lZAYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688539039; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kEcsCBzNF4o70240RbNtRsxkJMv3dgoeG47F/eJErR4=; b=YK+mZ8qccuoA65UUep6if4yCNodjg1wZQbLQxVbXCKJYt74/M91Ir8I9qPnSrITzjZKVPh Tk3BIZJqtCWiYzobtjoIrvwRizmulpDThYJgoTc6MHO1SjnMZEmDf50aSILGJZZ7YgHfrO XiSYHJyupmsicEOSGo/tn6ZfE5BWe8g= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=NRt67G4e; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3ng-lZAYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ng-lZAYKCH0tvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688539039; a=rsa-sha256; cv=none; b=MFjRw63Srguzlj1jvQnRLn7+kogoYVs2aZxksoylj5hMCqclHInnRIFBngamV+nnivuUr9 t3uwVJi0bawAVGFoTUuXroxQr+9CljBuC1qA4ob88ZKDUJEiYdCD6BRedxFPjGKV4rJ9+Y 2QGawOPaLasfsw5TGqj19BCDZFN8de0= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-c0d62f4487cso6568661276.0 for ; Tue, 04 Jul 2023 23:37:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688539038; x=1691131038; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=kEcsCBzNF4o70240RbNtRsxkJMv3dgoeG47F/eJErR4=; b=NRt67G4eRCmPFMXhxD3qW9K9EDj0xnVdQKTKufRvQkcPo7ZdLBv0zTj1I+0oIpa2hf RNbwbPBRRogvAWVpk5UaiqbfxSKUz15RoJ7CB+USDdwHmnmfpV6jkLxbEnsYpFFZaO/V SHkrXnyB7eFzXg59ZHInsE6Ag/21l5iBDRtfRUxDxPZ/FU1OKcIdaR+keSSWazvkJh5Q 7s+3RLuRM38qG/NcRSerlS841aoCJMp0+XIoBRVOSxggq4iZYaH+zqxDmhUtoJFuHlZu D53jP1OcRKF4mM/4S+ltgmuuTXWPdfIQ3Qrpc4LEr92kprtgKQxmqXBhxsp8tdb8AsgV 55rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688539038; x=1691131038; h=content-transfer-encoding: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=kEcsCBzNF4o70240RbNtRsxkJMv3dgoeG47F/eJErR4=; b=RRwkophQq0900wVccyQvYZwxNh+4Z80tFodwAOoILr+kAl5mBwGsvkZkuTHUPlla13 76rYxrNKv9v98AkFoaz4N/eQDiCWdvH6Kc4/tU7JMUVPXY5r5mmFhmRedpt4vNgV/7HS FgAquveqLCQRjaBQOWM99n0mC94381znHeH80S8kJPZsNWBPRE28BVFJ9XjDSjO8M0eF yL030Lc9WZ9mCsCHmr82dGOgAbF5PKrS+VIoSU7NT+ZPs/LFWvQZ6QJXxP3ETjtwCPqt O+Ef3/16uzMCzU+238nuy8S0qADhfoV4EWc6jTYXRUPv1q34IN6v1JOEXgNtyqVz0TdU 7o7g== X-Gm-Message-State: ABy/qLYAouUdPuXplmjjsKv6SSBZeDNHRAvejnJz1EsM+/WqVoxvPvQj ApTVzXh/oVW2hgEH0o6T+Gt75JInVZ8= X-Google-Smtp-Source: APBJJlFxf2AFzH5E1himxE/0ay/K/ncJrIflv9U1K8XKuqoxYpjdV/ZXI10Rc5HBkhf6oCEZCsT7XVeCpvM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:9164:ef9f:8918:e2b6]) (user=surenb job=sendgmr) by 2002:a25:ad96:0:b0:c5d:5b6f:f5c5 with SMTP id z22-20020a25ad96000000b00c5d5b6ff5c5mr31841ybi.4.1688539038122; Tue, 04 Jul 2023 23:37:18 -0700 (PDT) Date: Tue, 4 Jul 2023 23:37:10 -0700 In-Reply-To: <20230705063711.2670599-1-surenb@google.com> Mime-Version: 1.0 References: <20230705063711.2670599-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230705063711.2670599-2-surenb@google.com> Subject: [PATCH v2 1/2] fork: lock VMAs of the parent process when forking From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: jirislaby@kernel.org, jacobly.alt@gmail.com, holger@applied-asynchrony.com, hdegoede@redhat.com, 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, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Suren Baghdasaryan X-Rspamd-Queue-Id: EF4D114000C X-Rspam-User: X-Rspam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yKodiS8f6YDlUgDm+5DpklaZqk/+yNtnGL4JW1v6XfsWdpWDVsa/5gq5QLIqycZk+scJgUFJ6CpN2xTsdETX83OLg4eOIaZh+naps3v6g383IWz9VMdiJznMhP/c372cvuNC3CAbvaLH5Lw4MTR+hUZ/6sjnYr4XM1WzCM/TkAk/Cp0juoe5ogTpOa9vp66ZJZ3I9UOg7LHWdWfAEySYU1SKvz02gR07B8yTlTYakximKqIjcPnqIGd3D+C+tZVd7GFf32tyZ58BO1YtUbFHynN8uFRdlBxCG6+9WHIzrksNEcxyRXHmmV8g2lUNXtExYKmKZY4KxhjBaM7XXFtGexCH90bGkFnMzQVDIPEJXgEF4AGjrQ3Kizva/81szOZkPzWZluPF/VEm/PpbWxxRHBdByqobkdTM01a7P0EydTB6e2fJICYlImwQixMzjfdgI/RDJdujJQ0ObRValuUHbm7riurG+esQ7YT+01/YNiwJ0SHEuciLs8nCCzEj4nBRZjU7ZwTWPYSxCZD3wAWLx4+I2XuHPNkxKAhJZzguptld3EOCwRkPcazA4OgcVYm0YTrHRQs4v0TFSP7yGFHslo9HNyEB/dn3O2KQHfXece+W0WrElDAIERklUO1wlNPFUuyT/2JtsdWjel1smdUI3VnQ99pEDIt6vtphGxz19zjoVhbAZfkykYpHInvx3JOVl27lHA62Yq2by3vcdCHYld0GqjvsEY5XAF4ui34awm7nG/4BBo5vgX4K7AMAHAuSjrExmDuwGAZmzfPnAyA0paUy42XZ+YzmaaR7BUWaFydOS6Lh+Nk3JA1eCF3gu7GsF6qUPwrth2rV0ZqUv4yM3v0A== 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: When forking a child process, parent write-protects an anonymous page and COW-shares it with the child being forked using copy_present_pte(). Parent's TLB is flushed right before we drop the parent's mmap_lock in dup_mmap(). If we get a write-fault before that TLB flush in the parent, and we end up replacing that anonymous page in the parent process in do_wp_page() (because, COW-shared with the child), this might lead to some stale writable TLB entries targeting the wrong (old) page. Similar issue happened in the past with userfaultfd (see flush_tlb_page() call inside do_wp_page()). Lock VMAs of the parent process when forking a child, which prevents concurrent page faults during fork operation and avoids this issue. This fix can potentially regress some fork-heavy workloads. Kernel build time did not show noticeable regression on a 56-core machine while a stress test mapping 10000 VMAs and forking 5000 times in a tight loop shows ~5% regression. If such fork time regression is unacceptable, disabling CONFIG_PER_VMA_LOCK should restore its performance. Further optimizations are possible if this regression proves to be problematic. Suggested-by: David Hildenbrand Reported-by: Jiri Slaby Closes: https://lore.kernel.org/all/dbdef34c-3a07-5951-e1ae-e9c6e3cdf51b@kernel.org/ Reported-by: Holger Hoffstätte Closes: https://lore.kernel.org/all/b198d649-f4bf-b971-31d0-e8433ec2a34c@applied-asynchrony.com/ Reported-by: Jacob Young Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217624 Fixes: 0bff0aaea03e ("x86/mm: try VMA lock-based page fault handling first") Cc: stable@vger.kernel.org Signed-off-by: Suren Baghdasaryan --- kernel/fork.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/fork.c b/kernel/fork.c index b85814e614a5..d2e12b6d2b18 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -686,6 +686,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, for_each_vma(old_vmi, mpnt) { struct file *file; + vma_start_write(mpnt); if (mpnt->vm_flags & VM_DONTCOPY) { vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); continue;