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 E1964C5B543 for ; Wed, 4 Jun 2025 16:16:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 651508D002A; Wed, 4 Jun 2025 12:16:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 601028D0007; Wed, 4 Jun 2025 12:16:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F04E8D002A; Wed, 4 Jun 2025 12:16:17 -0400 (EDT) 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 2CD888D0007 for ; Wed, 4 Jun 2025 12:16:17 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C4ABA1D810F for ; Wed, 4 Jun 2025 16:16:16 +0000 (UTC) X-FDA: 83518220352.21.E4993A0 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf20.hostedemail.com (Postfix) with ESMTP id 8468A1C0002 for ; Wed, 4 Jun 2025 16:16:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=JW3s5E4w; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=GiDYZjk3; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=qvVMw7e2; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=Ib421YMQ; spf=pass (imf20.hostedemail.com: domain of pfalcato@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=pfalcato@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749053774; 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=YmdkHicZ9vMglOkN9sId8WADOZ0AiEKxD+HKkXswifw=; b=N+Hx61iHI9sM7xwL8ME7V099+pejLYi/ddquRfcrORkE/p8U0P2lWyBl2UATC65+fIRq8E fcG/mHdNrJiDkPqY2eqzlOcGiaQpc7hesRd/CD4TUVLMSPYP35kwR+99MavkSTxu9D/nkJ gvI+F3CVeJZGv14tYDazQ8WLM2yhKZ8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=JW3s5E4w; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=GiDYZjk3; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=qvVMw7e2; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=Ib421YMQ; spf=pass (imf20.hostedemail.com: domain of pfalcato@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=pfalcato@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749053774; a=rsa-sha256; cv=none; b=zsXiP8LeAKqdEY2zRcitUAL2fomb7erjD8at5XcYYPrJAIBkeHYC/KrEoQT4y19EDsOr8j AB+k5IrbajM349fhBPtHjYfget3bM6hgpmvp0KI566IrqfBR/aCJC+Aefdm3nsr/XD1ITd IHGUiqRkJiLwGm4Sd03dUWhzkZmAlYs= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id DEB912033D; Wed, 4 Jun 2025 16:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749053773; h=from:from:reply-to: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; bh=YmdkHicZ9vMglOkN9sId8WADOZ0AiEKxD+HKkXswifw=; b=JW3s5E4w/Lq8bnMW7jFK4SnNpKvFd6SXiT2E5iKZJSsFk75BrW0vJSwSpL+o9geysesFIf 8saaOt3PhJj/bYi16+BZ2p1pDIfcTU8d1K2D3QaLnzWK5VPGEC1BnBxbqzedDmyKkKumrP fdkwgF/NfXZ7iHY5+9MW07K1gZkegVY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749053773; h=from:from:reply-to: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; bh=YmdkHicZ9vMglOkN9sId8WADOZ0AiEKxD+HKkXswifw=; b=GiDYZjk3W/3b75T4+UMWn/ckf3lpcrUELe7rDdgSyeE4QJz59fduZu9ndYMV0lTLB5KTTg Bmea3lFDHcTYpoDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749053772; h=from:from:reply-to: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; bh=YmdkHicZ9vMglOkN9sId8WADOZ0AiEKxD+HKkXswifw=; b=qvVMw7e2XoaVcUE2le8pfxYUhFk5XJfc1fLEqRPChbEQ2ko9L3bWU7sSua9U3yYkZChYj8 FNONXjiWXRwXRKKeKdoHyUTZKRyxn09x4R/J2yhO1uyyB3090bijaEwEhX+tbGjTrOH3Op xYOYUD2e1+7+hOltBZXxlfOYTr16prI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749053772; h=from:from:reply-to: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; bh=YmdkHicZ9vMglOkN9sId8WADOZ0AiEKxD+HKkXswifw=; b=Ib421YMQE2vfLRFvp8nrk30gIIOH/ThJCJEgYSbQgQYBc+TNJ5/X5OaqRLUMRx9BRpn374 J2mCcWDj3aWdxTBA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1D13C13A63; Wed, 4 Jun 2025 16:16:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id GNQYBExxQGhiIAAAD6G6ig (envelope-from ); Wed, 04 Jun 2025 16:16:12 +0000 Date: Wed, 4 Jun 2025 17:16:10 +0100 From: Pedro Falcato 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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Action: no action X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 8468A1C0002 X-Stat-Signature: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Ij++MaaUvvthQq4I6E5NoGjTvboGyCb8iAjOqCa2DuATVzfKJ9kDRBpYVN44BiVh8I3zLhms9YGfog3BNTjgWpVB56ambeO+ZWdIINrQFXNEUVt5PRwELQA7gFNqq0S491lrpvEoGC8fRK/BIDn/zM9DznGKarVnijh2OvdeKL4/NebyXr0Nn+rnVxhJ74GhVDNJNNsYQP4vrplkx8JTtWk4xVNFN6+APPIcdvflNgxXbS6QTDajMp2IAjJwHk9TSXWQ6VCupzJSTHW8afofErccCSUiNL9lyvRngRdKlYOxiTHWWZMsikZ3GGg1jVnwDDE= 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 Wed, Jun 04, 2025 at 05:41:47PM +0200, Jann Horn wrote: > On Tue, Jun 3, 2025 at 10:32 PM Pedro Falcato wrote: > > 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. > > > > This is an interesting problem, but we'll get to it later. > > > > > 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. > > > > > > > Yes, and this is not fixable. It's also a problem for the regular write-protect > > pte path where inevitably only a part of the address space will be write-protected. > > I don't understand what you mean by "inevitably only a part of the > address space will be write-protected". Are you talking about how > shared pages are kept shared between parent in child? Or are you > talking about how there is a point in time at which part of the > address space is write-protected while another part is not yet > write-protected? In that case: Yes, that can happen, but that's not a > problem. > > > This would only be fixable if e.g we suspended every thread on a multi-threaded fork. > > No, I think it is fine to keep threads running in parallel on a > multi-threaded fork as long as all the writes they do are guaranteed > to also be observable in the child. Such writes are no different from > writes performed before fork(). > > It would only get problematic if something in the parent first wrote > to page A, which has already been copied to the child (so the child no > longer sees the write) and then wrote to page B, which is CoWed (so > the child would see the write). I prevent this scenario by effectively > suspending the thread that tries to write to page A until the fork is > over (by making it block on the mmap lock in the fault handling path). > Ah yes, I see my mistake - we write lock all VMAs as we dup them, so the problem I described can't happen. Thanks for the explanation :) -- Pedro