Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp380847rdh; Wed, 7 Feb 2024 07:27:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IH5C9g+8xTGeTBvTp94xZG34f7Lj983zK5CLWuioWiM3camYe2pdA/6quSgLjn+Jz2mcP3F X-Received: by 2002:a25:d88b:0:b0:dbe:9e00:a5d1 with SMTP id p133-20020a25d88b000000b00dbe9e00a5d1mr4144993ybg.28.1707319676127; Wed, 07 Feb 2024 07:27:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707319676; cv=pass; d=google.com; s=arc-20160816; b=b3U/gjMI0p0hbMqSTr8iwj/MgAlf590iWTlJtGFp6y2rNzRIrlEnqf1KUlEDFwuCbz PXFiC+z9gRB9ekHdH4TDiYm9aq+norBDNy+o390DHfxpF2fPcz/cgL2A2/UEok3ENShv QNPyqakAn8CEkw1QIf208cFQ2XO33HRuTq/cAC0SYlf6117mORjbcjoxtwCfUdxIZlEB yPpvuUEYhGc0/LDYVN+UXfu8K4EURNr4/6UHnnHDdkeFxxhJ306oPGrn0U3adRZn5+Sl LOENANGieqyehgTQa75Q9H8Le8ab1V7FzTjd+UJ6zGXT7Z1C439qVKXCyfMkC0GR9kjZ 2nnA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=KX2UT2kbb1zJuhP+lk26UOQQj3IhG0JcfoCp36/CCY8=; fh=7AstrSaIbcMf6/Ge/Y0MJ3t5eg0xQBgVjz1f0+DjsWA=; b=P9aVPxCxkwHyGezG6IA+Mc9E0GU2wAus0pUdxF1ekw6C+vPvpl+vQIUsqgWKoo6Vlk Gx9wkJWZmt2PjotJKHhWYdz7iV6JK9yymFteOteexrOxdlViz2tLsgdNuWE9AGW+OutM 9dX7ZC8pZqNbfVYBH1vXJzeyKn0YEtgC2rq1KNxm/ZKSa1Rztaf5iXK05B1tnrAkTHWg aAtp9B2m5dS0Ymr2RlS/iBSJrNkzBcOcYUc8QOiqe2i87PRFDmnlTX6xKGquj4t/vfkh 7aO/jGLfscwNz39I2Pk8LF6wVGHbYhAiwyDI21ZT98AGym1cDI2UzeZnh9S+XpVmqplL xjPQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PdJyBcfh; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-56716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56716-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCXgmUQSgbMGtgOsOkVAH/ZMLyNHo2pxDobWfCgVEtTybpn1iefGKjBm4OCaGCPZTaoUrlv0HUPBjt5Iq0UklY+TlHoT5MhaO+ie9R7ctA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id js5-20020a0562142aa500b0068cc38be5a5si286742qvb.407.2024.02.07.07.27.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 07:27:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PdJyBcfh; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-56716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56716-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DC68D1C20C40 for ; Wed, 7 Feb 2024 15:27:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0479F7FBA6; Wed, 7 Feb 2024 15:27:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PdJyBcfh" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 188BA7E775; Wed, 7 Feb 2024 15:27:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319665; cv=none; b=LmTByKhUqqNKpGz31RkeMRlW+jCMkd9czueCLfSKefVrlF7fA2F6f1EVWNg+kYh/5lHLrfKWGqdPbr2u1J/6B0K6+w36vPzwhMXDztlxzbdsMj+ejXpNVSed9F0cjioUkjIIxw81DtcMtljI+mXgm5uCgJxDKGvKOzQk/nD8F8c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707319665; c=relaxed/simple; bh=Wpz6YO32Qj0aa5MgpAxe/gu7xxnOiMKkMHU89Lo/BS8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HMBRApwkpxlHc12xx0PNqglLOJ91vi1WTyhCFNSfr1lTxENDaW+mShuAWgbad6p01ibHP/LQ93uYt7fCXZUNPL9qXtiNpYNpmGMSpJ0dfPEDmUHz0ZgLpqG3k9EBCPD9F1EvlRwE8F0cVn1yiMJj3ueAw365ssRdANjpcwiv5rw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PdJyBcfh; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1FDCBC433C7; Wed, 7 Feb 2024 15:27:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707319664; bh=Wpz6YO32Qj0aa5MgpAxe/gu7xxnOiMKkMHU89Lo/BS8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=PdJyBcfhBDugPqfPAHL60XArKC2R50gNcdevNydbNpntpB4hLQISmk+WvQz7ExJDC BnjEi16qiL+uUkmniRIt1RsDgBSF5k0jpliADyYSSFkFX0W/Pl2dewzvm+c8fpAvbu JQx7pQ+aXo7bsJIwGvplzWHtlbtl5mdKbGTUngISuIeYG3C/1J9eHtQqvg24oUDoJa MUuXi5UfS/5Jn1xIHp9P1A16Va9pv3WErzdoSRR6QErvLz99M99eJedf6zKHULEtww 3FFY+q2LKz3vMWSnNkBQIws27BPo8ZjZejR4lUtsf7CwtDNhTLL1DvA6c/0DyKJi3X D8zf0Ses6KFCQ== Date: Wed, 7 Feb 2024 17:27:23 +0200 From: Mike Rapoport To: Lokesh Gidra Cc: "Liam R. Howlett" , akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, selinux@vger.kernel.org, surenb@google.com, kernel-team@android.com, aarcange@redhat.com, peterx@redhat.com, david@redhat.com, axelrasmussen@google.com, bgeffon@google.com, willy@infradead.org, jannh@google.com, kaleshsingh@google.com, ngeoffray@google.com, timmurray@google.com Subject: Re: [PATCH v2 2/3] userfaultfd: protect mmap_changing with rw_sem in userfaulfd_ctx Message-ID: References: <20240129193512.123145-1-lokeshgidra@google.com> <20240129193512.123145-3-lokeshgidra@google.com> <20240129210014.troxejbr3mzorcvx@revolver> <20240130034627.4aupq27mksswisqg@revolver> <20240130172831.hv5z7a7bhh4enoye@revolver> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Mon, Feb 05, 2024 at 12:53:33PM -0800, Lokesh Gidra wrote: > On Sun, Feb 4, 2024 at 2:27 AM Mike Rapoport wrote: > > > > > 3) Based on [1] I see how mmap_changing helps in eliminating duplicate > > > work (background copy) by uffd monitor, but didn't get if there is a > > > correctness aspect too that I'm missing? I concur with Amit's point in > > > [1] that getting -EEXIST when setting up the pte will avoid memory > > > corruption, no? > > > > In the fork case without mmap_changing the child process may be get data or > > zeroes depending on the race for mmap_lock between the fork and > > uffdio_copy and -EEXIST is not enough for monitor to detect what was the > > ordering between fork and uffdio_copy. > > This is extremely helpful. IIUC, there is a window after mmap_lock > (write-mode) is released and before the uffd monitor thread is > notified of fork. In that window, the monitor doesn't know that fork > has already happened. So, without mmap_changing it would have done > background copy only in the parent, thereby causing data inconsistency > between parent and child processes. Yes. > It seems to me that the correctness argument for mmap_changing is > there in case of FORK event and REMAP when mremap is called with > MREMAP_DONTUNMAP. In all other cases its only benefit is by avoiding > unnecessary background copies, right? Yes, I think you are right, but it's possible I've forgot some nasty race that will need mmap_changing for other events. > > > > > > > > > @@ -783,7 +788,9 @@ bool userfaultfd_remove(struct vm_area_struct *vma, > > > > > > > > > return true; > > > > > > > > > > > > > > > > > > userfaultfd_ctx_get(ctx); > > > > > > > > > + down_write(&ctx->map_changing_lock); > > > > > > > > > atomic_inc(&ctx->mmap_changing); > > > > > > > > > + up_write(&ctx->map_changing_lock); > > > > > > > > > mmap_read_unlock(mm); > > > > > > > > > > > > > > > > > > msg_init(&ewq.msg); > > > > > > > > > > > > If this happens in read mode, then why are you waiting for the readers > > > > > > to leave? Can't you just increment the atomic? It's fine happening in > > > > > > read mode today, so it should be fine with this new rwsem. > > > > > > > > > > It's been a while and the details are blurred now, but if I remember > > > > > correctly, having this in read mode forced non-cooperative uffd monitor to > > > > > be single threaded. If a monitor runs, say uffdio_copy, and in parallel a > > > > > thread in the monitored process does MADV_DONTNEED, the latter will wait > > > > > for userfaultfd_remove notification to be processed in the monitor and drop > > > > > the VMA contents only afterwards. If a non-cooperative monitor would > > > > > process notification in parallel with uffdio ops, MADV_DONTNEED could > > > > > continue and race with uffdio_copy, so read mode wouldn't be enough. > > > > > > > > > > > > > Right now this function won't stop to wait for readers to exit the > > > > critical section, but with this change there will be a pause (since the > > > > down_write() will need to wait for the readers with the read lock). So > > > > this is adding a delay in this call path that isn't necessary (?) nor > > > > existed before. If you have non-cooperative uffd monitors, then you > > > > will have to wait for them to finish to mark the uffd as being removed, > > > > where as before it was a fire & forget, this is now a wait to tell. > > > > > > > I think a lot will be clearer once we get a response to my questions > > > above. IMHO not only this write-lock is needed here, we need to fix > > > userfaultfd_remove() by splitting it into userfaultfd_remove_prep() > > > and userfaultfd_remove_complete() (like all other non-cooperative > > > operations) as well. This patch enables us to do that as we remove > > > mmap_changing's dependency on mmap_lock for synchronization. > > > > The write-lock is not a requirement here for correctness and I don't see > > why we would need userfaultfd_remove_prep(). > > > > As I've said earlier, having a write-lock here will let CRIU to run > > background copy in parallel with processing of uffd events, but I don't > > feel strongly about doing it. > > > Got it. Anyways, such a change needn't be part of this patch, so I'm > going to keep it unchanged. You mean with a read lock? -- Sincerely yours, Mike.