Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp550718rdh; Wed, 7 Feb 2024 12:26:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWue+cjepOhUDFccSZwQC4fjNYP6dNSzc2xabwRCiyspdkhqOgmrrKIhMVFfaecSuKA0yQrp8zmpqXw4delHwAnvORqGd9wnhXD5+QpoA== X-Google-Smtp-Source: AGHT+IGZH20IiQ6uWVibGPZzpNEuRfZU0wrsdyk5lQvbcIwuJpQWdQhGUOM9XCZK3f/KlNueye8B X-Received: by 2002:a17:906:e28c:b0:a36:4cc3:1138 with SMTP id gg12-20020a170906e28c00b00a364cc31138mr4647576ejb.75.1707337616501; Wed, 07 Feb 2024 12:26:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707337616; cv=pass; d=google.com; s=arc-20160816; b=nQq28EYbPfDdbNhLeObcXxJjTiGYoAb1jfHIkjLwiRIwQ/fp+/6+8ovxCygyyZQzuA CX+kVhU60o9fnpgWx6IP9+lyAlyBTHfcFvKP+rrXnVNvmuPGLAXaKjMxFgpkwQZCj14E hSo6Ddrh0rJaQms1Ay2P2l8aousEPdgeNo8NRCxaDbYhxudMiA+o5ufCMBh1W9LSIb0i Fs1ChJ/5EtB80SJV2QiCo8TjeNihkEP6mapC2lgmVrtjlOkTqvGQh5M2MCs9qzSj7A3u Q7Xy3fiGxF6XIUahs/3veeZ0rThO9hQmGngteNC0ScpprwGdSopumclwCrM5NQuC1K7C /PEQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=RAp9QYmAagl4q8JstQaVuR+UzMqpGvUOafiaUGshjJs=; fh=sdDo+veDsa3kGjAzGmAa/0c90vWxgA7dkCzQgk04NqI=; b=ju97oGSVOyyEFa9eIBm3j/qvFVxFq9FFZ+F7KeL9qsm89Je65KhCI7oA7TUZ03fESH Kkw/poJSabFEizomoeaBKw7anUC4ELxfCzshbODxDb7Yy5Ira6CM7tfs+je5yBkBb4Hb 4p8bJXK1TkCXkdGAFiZsBmIEN7N/rKA3pGvc313MLCOQ6yaJaoKpPOKnoyNWRna1v6eO SV+5mrAfNcz5xZGcBX+7HTMJutOHBknw8IctL2rsw4n633x5E20z6JeOh3WBBjpPbwdG dZyuWO2Zyp3t9oIU3JrapxcpozYZO6Xryr093s6Ef8h+a5Vv5bvT8DYiu9/7uvOInmpE c3Jg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PaleEkjV; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-57024-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57024-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXr6OZm2wMRjpPsCky7Wd23pHWEHNMyNsa7069Is7HteGXuuyyiEHpEVbfYQU49QXw26OzDU1NTPkQzarEuZz+uqLPvQ8uKGA4VkcDJmg== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id cf17-20020a170906b2d100b00a3a5146a32csi17009ejb.797.2024.02.07.12.26.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 12:26:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-57024-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PaleEkjV; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-57024-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57024-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 136C11F2273D for ; Wed, 7 Feb 2024 20:26:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 20AB4129A78; Wed, 7 Feb 2024 20:25:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PaleEkjV" Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAC1A127B51 for ; Wed, 7 Feb 2024 20:25:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337508; cv=none; b=RIUjqsx0DOUk1PJeJOeBmGa71LqYNa51myGDbdPBW5EsSKSK0DT6QYufnMAYfKyt9hRMRoOM9HGWwqlEhsnSy0xkY7GAFjZMzQVCN4f5GuCNLggeRpe38JYXIHruzUEP1lLyv0SkSzymV0fMKyPIdTmqHs8CgkWZTj/gNOG+Olg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707337508; c=relaxed/simple; bh=vFnk6DQ8EIJEAZ/DRRBigdf0kxotx3XgDLJZVdGyWlw=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=taEio2EdWsd3AZmc/usljEuGYyzUIhYctzihSXdtwbR2qokba5Fl7GoG72ddvyZIrXDj1Evr0k/rMNzN5NqyT8uO9r3CvDzPoUMrX+P6BXVDgp0iOYDLWQTi0vzBNvxK+bFtb4/pcrfFYnccgndd7lbhOPhzw58M8ewa7unukwo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PaleEkjV; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-33b5736b7c9so45069f8f.1 for ; Wed, 07 Feb 2024 12:25:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707337505; x=1707942305; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RAp9QYmAagl4q8JstQaVuR+UzMqpGvUOafiaUGshjJs=; b=PaleEkjVXxGy63Ac5y+TKCmm1XTs6lU90cBz6EKTDiixC8bMAajNTFdbP/uBiFLuc6 1fSeICo9XL6/4DsMG4oQVTviu5Z0givtNpXrtGaHkiFSiZbyjVRMRXGOBPgvivHeyDry ziZXUHPTDftXbxAnkw/t+AjODhrGARwGNeKk8TSgZ0KMio+yab3vVm4hZqIbM8ZVGeaB 8zPQ8ky9N5JqVnCvn93JVm9rgJN3Sc/cKV1qRJmIdfIkFPTnC4oBv4U97xf9nr9GrrkH fKJWSRdGossRtu9mvcWPzPlMnBVBocf/U0xZrotiMOJFShN+yRVOztwXAbsW/kDKMHjN I3Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707337505; x=1707942305; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RAp9QYmAagl4q8JstQaVuR+UzMqpGvUOafiaUGshjJs=; b=nExD555pY7yTxqImSFIEe80EVYJ3HyXbQPlJQ/bmf00W6K8a8ZWivopuwKcTAyPQUw WS6XbceM+pG7kvvSNSy2PGKfnIr+5UfzXqzoEGEh9mNu7FaQu/VK1tiJOVCOMgRzhzeE lF1bWlv211ql3esb1iJ2lk6/3LT9HxeK8BVntp2tgLgHBWmBo5q2MlUT4YP1jFPfX6w2 3KLSGSC6+JwCxJ78tzWQWfd1hb4ioBpgSnphc867UCkonFWEVSzdeixpva2W2Sla4qis s92Oblz3UoC5XibSBh6vlDj7AeprXIJrcPoI2vxkTIGjs2BMJYYK32mEniB+ffQeRe00 B7ww== X-Forwarded-Encrypted: i=1; AJvYcCVPI9bHVjGJuQeqwWRfWg6R3WeL4GO/Y/6Sbysyrr72BX7U5mBEy95vUUNrWhrLkxrLtdq1Mos/QWWGVPzdQvUWHLouMwnaZNAlrMiQ X-Gm-Message-State: AOJu0YyYsqhQmiXqPJCRdIXPVWSwhKGT7I+75zvF4LF3D4ot+OS54084 n0+GYOl3AHtwEb9/QvU+ZV0HjX1ZlkokYcxQx8DQ6Ycr1YeJyBxYN2Z8bbQR7BCveSJsPl/xc7y F6P/dlYxIPQmam+lpHNe5E8Wdxrowa6gpseAe X-Received: by 2002:adf:ff91:0:b0:33b:2177:2228 with SMTP id j17-20020adfff91000000b0033b21772228mr4665957wrr.59.1707337504834; Wed, 07 Feb 2024 12:25:04 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240129193512.123145-1-lokeshgidra@google.com> <20240129193512.123145-3-lokeshgidra@google.com> <20240129210014.troxejbr3mzorcvx@revolver> <20240130034627.4aupq27mksswisqg@revolver> <20240130172831.hv5z7a7bhh4enoye@revolver> In-Reply-To: From: Lokesh Gidra Date: Wed, 7 Feb 2024 12:24:52 -0800 Message-ID: Subject: Re: [PATCH v2 2/3] userfaultfd: protect mmap_changing with rw_sem in userfaulfd_ctx To: Mike Rapoport 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Feb 7, 2024 at 7:27=E2=80=AFAM Mike Rapoport wrot= e: > > On Mon, Feb 05, 2024 at 12:53:33PM -0800, Lokesh Gidra wrote: > > On Sun, Feb 4, 2024 at 2:27=E2=80=AFAM Mike Rapoport = wrote: > > > > > > > 3) Based on [1] I see how mmap_changing helps in eliminating duplic= ate > > > > 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 d= ata 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_a= rea_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 th= e readers > > > > > > > to leave? Can't you just increment the atomic? It's fine ha= ppening 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 rem= ember > > > > > > 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 moni= tor 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 en= ough. > > > > > > > > > > > > > > > > Right now this function won't stop to wait for readers to exit th= e > > > > > critical section, but with this change there will be a pause (sin= ce 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 re= moved, > > > > > where as before it was a fire & forget, this is now a wait to tel= l. > > > > > > > > > I think a lot will be clearer once we get a response to my question= s > > > > 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? No, I think write lock is good as it enables parallel background copy. Also because it brings consistency in blocking userfaultfd operations. I meant encapsulating remove operations within userfaultfd_remove_prep() and userfaultfd_remove_complete(). I couldn't figure out any need for that. > > -- > Sincerely yours, > Mike.