From mboxrd@z Thu Jan 1 00:00:00 1970 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 6B9F0C5AE59 for ; Tue, 3 Jun 2025 18:29:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 06C176B04EB; Tue, 3 Jun 2025 14:29:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 01CF56B04EC; Tue, 3 Jun 2025 14:29:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E76CF6B04ED; Tue, 3 Jun 2025 14:29:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C7F356B04EB for ; Tue, 3 Jun 2025 14:29:22 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 65577161333 for ; Tue, 3 Jun 2025 18:29:22 +0000 (UTC) X-FDA: 83514926964.22.C26D946 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf07.hostedemail.com (Postfix) with ESMTP id 262E140009 for ; Tue, 3 Jun 2025 18:29:18 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=JSWfvysW; spf=none (imf07.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1748975360; 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=ImMDjWNstCyJ2H7+5mr8HwNgK0oVItP1yOyP4gDpoPU=; b=fGxt9eEgz/KKOlImIest+I4wVzrL29Re94VujqlRvQRbmqoob9dIXpiY5I1vPXn+Yerh1O E5iNu2aYYt0BLMBnI6pDNxVMP2y1it78nJdh1k829RG0No3UsiT6koJ1xdYYpR2OD1y0NR LLIM78VeAYQFQrx+Y1c+jqecSY1PeP4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748975360; a=rsa-sha256; cv=none; b=5dBv+DrMWARetPgziNdgUmJX49oXpnPc/7HS2DYHG1TbOk6iTjGQ9+Gfrd8cB9ymyzMOxv hf5tDTn6ncHwHvFGasXsUriiRdUapDoae0jBOhzWpmta+X7vE9Ib1RNsuYoxITeAz+5Gom nIUpx6cDAPS5zEeKlHhv1+vd72abBmw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=JSWfvysW; spf=none (imf07.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=ImMDjWNstCyJ2H7+5mr8HwNgK0oVItP1yOyP4gDpoPU=; b=JSWfvysWoZwUtky/z9A6dmEJbT zHv3rNwHjYYwk4Ug41vHosyQmycSxYJrnBgOeARIXNUTe4zAeT45W9PJuLVqqy3UuY9EMBlWNELBR DR2JOq212vFGcv9xmBEpxpqu27IXBSu/ATU/ZY9coKzjCwTZaqm5EeAmjj3nvFOnBrp1Jhp7JLSB1 i7JRvqh31a4iKHfc9p6AJW1Im9Kv1LDEtCTwIsYKXy6ZuHaQcVHlaRmaDomEK0VJWcHiI9GfUbXVD v0yVTEXFKzc4qg3YLjc0GUOsyInfdvhRj60OlJ0DHlou8mFK11ouEqFQvRmTQYO0vqJdVbf3Z+4FY mtcLrsZg==; Received: from willy by casper.infradead.org with local (Exim 4.98.2 #2 (Red Hat Linux)) id 1uMWNu-00000002KZE-0qm4; Tue, 03 Jun 2025 18:29:14 +0000 Date: Tue, 3 Jun 2025 19:29:14 +0100 From: Matthew Wilcox To: Jann Horn Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, Peter Xu , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 1/2] mm/memory: ensure fork child sees coherent memory snapshot Message-ID: References: <20250603-fork-tearing-v1-0-a7f64b7cfc96@google.com> <20250603-fork-tearing-v1-1-a7f64b7cfc96@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250603-fork-tearing-v1-1-a7f64b7cfc96@google.com> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 262E140009 X-Stat-Signature: hhwnrq831fy97s6uq7fmaaey5ipmxufb X-Rspam-User: X-HE-Tag: 1748975358-481264 X-HE-Meta: U2FsdGVkX1+nBmpwyW2Wvs5F67rauK311A+xxg/H0VrxkRZRmSw0fYCslXNWiMk7iP9q0uC3+0knxkFGx5MgSkaRaRcLgqjUNxF7bE6V0+ab3DkQ2yuEPCFFJVfBCaVqk4drrjn6BSsYZv3XkrLCwITAv5/kffNy4Wo7h8NIYNsTIe6Uq9GdbM4ta8mWykESs7hOcn86e8p4pQM+vq5W50erNp9oGx1lt+NtNZjKFNvDY/gofb1MMMAwcnImT3zcikjL1IkIKntXHMcfdEgHwHTJDzuiifpra37URahVF6moWWOw+TyHawSrHpaDFE2/lVYB2GcP7A/VEBkYq6bLBrXr03ARkiGVa3q4Bouw3311reaDKMKjaPzcasntmdvaApHYoCBJfAwBoRVbThSXKfrxx8IUelFZ2b1aCnTHkXJ2uWkSGgQNkltOStJ/rDTDz7rFeMAl0WlWkB8MvJu6PpQvU0A0GC2zG8+jJj2h6dI0ur5c/xp5xIrRluweSq5F8a55ariJvUfCvQ0rZpefhWWOojSqwZgQl1ZLYs4xXXQbJ/GNYDwRVnR6RzPESzsH7Y4XRcB3o9wGCSvfYu1MLksxSj92jrO3AF+Sf7gJDfdol18USp9Q80zbvGKQpqeN5p98sUOz0NiJGnw/lrUzOot1lDEA8okOsUj7w23Pn3QQtNLbNgEQvRfSLyxUqb6cCXPrbe3jsXPwP/9MUzfLqcYgGSLXlC5Wb5TFbOUoS9rupNsxydAa50XoYnjHXeGgFBj3Eshcbb5rewFF5gz/wzJo5oDZ1C4l5M4erQvNnkCnuZlBqBeK8VaiE1x/0BZe1fNTk+q0oBVTR+sVXb7chFk320mtLJNFnnFkSC7v3c4WUSWuuwwMYQttb78Mk0/bimCPJm5g+D7ybFiiAcumPCcQv+0d8uMsRJ1fFEBa6PTOHOL3ByWQEnEHJDLe3tU4f2ubHncd1HdMeI7ABM1 05fsQ84k Wzh32Hk2Rrfbc2NHtyQb2BxVdgWM4hjg0viTF8+dynsy77oCSiKwp12hIMkhTnBA/vu4dLbZMmgzH5hc7wyuKa7A2AcywVZBlDs4BgLLDnypEXZYibJ5umwlSSsK1/LA5paeqGvTyGLAa1jbJyIWUblH9NWceRZioMtwDTTpNeap8Xhq1uAb5vSR07teje4ls8xSAQbf9bHFqhcuFKkrwvGo03IH3+JNc1GNkWA1pROhM3g4= 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: List-Subscribe: List-Unsubscribe: On Tue, Jun 03, 2025 at 08:21:02PM +0200, Jann Horn wrote: > When fork() encounters possibly-pinned pages, those pages are immediately > copied instead of just marking PTEs to make CoW happen later. If the parent > is multithreaded, this can cause the child to see memory contents that are > inconsistent in multiple ways: > > 1. We are copying the contents of a page with a memcpy() while userspace > may be writing to it. This can cause the resulting data in the child to > be inconsistent. > 2. After we've copied this page, future writes to other pages may > continue to be visible to the child while future writes to this page are > no longer visible to the child. > > This means the child could theoretically see incoherent states where > allocator freelists point to objects that are actually in use or stuff like > that. A mitigating factor is that, unless userspace already has a deadlock > bug, userspace can pretty much only observe such issues when fancy lockless > data structures are used (because if another thread was in the middle of > mutating data during fork() and the post-fork child tried to take the mutex > protecting that data, it might wait forever). Um, OK, but isn't that expected behaviour? POSIX says: : A process shall be created with a single thread. If a multi-threaded : process calls fork(), the new process shall contain a replica of the : calling thread and its entire address space, possibly including the : states of mutexes and other resources. Consequently, the application : shall ensure that the child process only executes async-signal-safe : operations until such time as one of the exec functions is successful. It's always been my understanding that you really, really shouldn't call fork() from a multithreaded process.