Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2748044rdb; Mon, 5 Feb 2024 17:09:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3w4lZhR13HYwwt5wobW9AWOe/Wcy7j70lOwG7yAgyjBRVb+U8BJXz0LjCt9UE056HG5x6 X-Received: by 2002:a05:620a:992:b0:785:613f:6d34 with SMTP id x18-20020a05620a099200b00785613f6d34mr1114904qkx.31.1707181785481; Mon, 05 Feb 2024 17:09:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707181785; cv=pass; d=google.com; s=arc-20160816; b=ML1vA5x7cZrgJVIUbG12kwDIvMTqKOr/HhjgLcyHHVh9tqrF4mTZ6UU+Of8aM0QKf/ PPaycQaQIqw8q+SZJXBG7LE8nku6JOZAe0Jap9mc2c0/2kKbDOr5tT2A/XEcvi/7YtMD lD6Nv/AiKMR75jEc2YidD/oNnEl/cWmdyGo+SSGegOMw8NJ9GMZawNdFZkFc7Vy2uwL4 EvlYxKoCwnplLYPmOAlYMhwkVbw1TA/Nect2jHsNMibVM/WXX6jSIuBa3O8ouYeL63VR nhIntLLia6/xOCs3da3yMiIBgHKbFqOjdOQgoGt6KH8YatAlMZIR+qM8hCsg0LhKGAN0 pI7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=zFHBvxcNZW2HfjsGIsVhZk87PaG1v642sbVGsClWfxY=; fh=Ar2RqvVXVaoaXF74t/Rw3thdJgrMtIRq38Cakw6/hwE=; b=NxxuTRdRQ3koK2KwFgRFHNgHul7OghvzOvaDYvykCwDsREv5wfTx/2CV0XSBcm1YJX Dcb74aeal0ACPOr5P1O2y3E+xzZ9wb8yw8zqCDoj2PO0G2kuDTDasFPUfhiErIeVTbxA PZ78DkRhwS+bjuJgKSpVEJDI1gyDBBM59lLfsYfH6iIT/+Q2owHMeDWHq67MvYlIs20l YF6z90Knpbw91fGgXPQMoBkszEiDHdAVMh6TUn206pWX4E0G8varIVzPi5SzCi5uHgMF MTbqQVFa6zUETYmcfrHSWOwVIYKpaKrd8GaYHpfDFjzmzIF8NBDuj/p+KxkmHePkEiBh Ojkw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vuBnNS2D; arc=pass (i=1 spf=pass spfdomain=flex--lokeshgidra.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-54139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54139-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWFvpyK39f567TzngPjqxf3qgDkSGv1i4i8Mp6vvr0wA9++Em9wDx93NCwFof1ukwv8GthyljJpff4bADI69G/8zjCu0Gd7eizkJ2Fz6w== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id vq8-20020a05620a558800b0078407cfac22si1137099qkn.376.2024.02.05.17.09.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 17:09:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vuBnNS2D; arc=pass (i=1 spf=pass spfdomain=flex--lokeshgidra.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-54139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54139-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 3F2B61C23A0B for ; Tue, 6 Feb 2024 01:09:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 73C9F53A6; Tue, 6 Feb 2024 01:09:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vuBnNS2D" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 29B474A06 for ; Tue, 6 Feb 2024 01:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707181774; cv=none; b=oh5n7D06UmQwT/tFDWsP85AUMKvvi1qbL0Bg29HPNqSoXYzmfAlluM1jiGMWxd2VaYxHxkXZUknY9T5Z1bpxrRGzxQOwo3Rp4GeD5DRNwEDaeH5j7kXekEGVnyypALZQl25ViXFfGxJdrG33pYx0q+l0pjF5MpIYU6IR8qcmWLc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707181774; c=relaxed/simple; bh=GdHdniQnbMOXtlmLfBAIzFnjtasDR34ui/wEy9tYr8s=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=VguWZVE5xi/y81Ud5BQ0LlWU3rbDPdSxGEEnTFGK9AlTEVPlY15I3U6N3Vwq8IoliXH0uw58OuVcj2UTqHWPRk0HZBkcELTWsAmFBQTXjhhSBd1GR2YI5Qj3K2G0RSgykr1tgrTLOWoBzMkuh12gua0ISF1IY2qdUSuaF5XrHQQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--lokeshgidra.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vuBnNS2D; arc=none smtp.client-ip=209.85.219.202 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=flex--lokeshgidra.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26845cdso8490240276.3 for ; Mon, 05 Feb 2024 17:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707181772; x=1707786572; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=zFHBvxcNZW2HfjsGIsVhZk87PaG1v642sbVGsClWfxY=; b=vuBnNS2DuUXlqVJ15g75711OjVR9EqNrUnrFoy4+vDmz1KxfJipACqokd3EEMtYFOB M5L949iLtPgfaTpdv/s6ybOCrPn63NCdgkj+WRLJwKB1WYaDFtJVdmnuW8WH/t7BMv5v wDARcLSIRKiiZCCUo0BDJP0mRe44kv9PWKi4BGvd6K4KqOQhZ82UYdHzRG0iuabDfHsN qDbk7B4YVgqfffm2iYoqbngLvu94GKcw5lBcHffBzaeJlO4ciKgs9QkrsC5nuWPnBIif j1JrnSnQXzTU1ugBJx5Qr5QQdONZPI7M0+q5M47CrT8bVd3raBNa6NGigsEyaXNmo5B9 SyAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707181772; x=1707786572; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zFHBvxcNZW2HfjsGIsVhZk87PaG1v642sbVGsClWfxY=; b=d7nAMFe9l03YvgyIkuraTuLwWY38miAte5fjyQDfV+i+mqC7ep4BlE8w7LAUHIMEV8 lZuu7DdmAz2wx7WMdPZvcNWx4GcWdck2YPJ+FaE86aWGsds2rhAVAs8Y/iAC7o6qbY+W YxOj+sCeYl2+4hT2UgT7J3k0wcVHWZmd6pTWrRpszM9UjNMrC7tpWD4ffcSMJkMe85fn hwoidDqlahRJhOkMub0VmO0MUmsCjMXYgDlt89z8223yHsYRYgZKdVAVzVCbFOH0x4Nm 9veM8V0vRjbQWvOY4G3Gbm9aJ6eEbffbJ/qSV7Es6HFfFFySfYemqpyKyCz7FRrahjA6 /sfg== X-Gm-Message-State: AOJu0YwF6423712JKREl7P2W4rswDH/LOPl8E/99PhB+vVLrsTbDiPqy /e0Wds68qrItwpfZU43Nlx6tNXJS+fXtQj1g2bmBfOPVohg4ciCXKnbUXdw4kkwKCv39KAhekaV 0inxIahMsYe0COrpPj691mA== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:713:bb2c:e0e8:becb]) (user=lokeshgidra job=sendgmr) by 2002:a05:6902:2842:b0:dc6:519b:5425 with SMTP id ee2-20020a056902284200b00dc6519b5425mr38783ybb.11.1707181772174; Mon, 05 Feb 2024 17:09:32 -0800 (PST) Date: Mon, 5 Feb 2024 17:09:16 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240206010919.1109005-1-lokeshgidra@google.com> Subject: [PATCH v3 0/3] per-vma locks in userfaultfd From: Lokesh Gidra To: akpm@linux-foundation.org Cc: lokeshgidra@google.com, 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, rppt@kernel.org, Liam.Howlett@oracle.com Content-Type: text/plain; charset="UTF-8" Performing userfaultfd operations (like copy/move etc.) in critical section of mmap_lock (read-mode) causes significant contention on the lock when operations requiring the lock in write-mode are taking place concurrently. We can use per-vma locks instead to significantly reduce the contention issue. Android runtime's Garbage Collector uses userfaultfd for concurrent compaction. mmap-lock contention during compaction potentially causes jittery experience for the user. During one such reproducible scenario, we observed the following improvements with this patch-set: - Wall clock time of compaction phase came down from ~3s to <500ms - Uninterruptible sleep time (across all threads in the process) was ~10ms (none in mmap_lock) during compaction, instead of >20s Changes since v2 [2]: - Implement and use lock_vma() which uses mmap_lock critical section to lock the VMA using per-vma lock if lock_vma_under_rcu() fails, per Liam R. Howlett. This helps simplify the code and also avoids performing the entire userfaultfd operation under mmap_lock. Changes since v1 [1]: - rebase patches on 'mm-unstable' branch [1] https://lore.kernel.org/all/20240126182647.2748949-1-lokeshgidra@google.com/ [2] https://lore.kernel.org/all/20240129193512.123145-1-lokeshgidra@google.com/ Lokesh Gidra (3): userfaultfd: move userfaultfd_ctx struct to header file userfaultfd: protect mmap_changing with rw_sem in userfaulfd_ctx userfaultfd: use per-vma locks in userfaultfd operations fs/userfaultfd.c | 86 +++------- include/linux/mm.h | 16 ++ include/linux/userfaultfd_k.h | 75 +++++++-- mm/memory.c | 48 ++++++ mm/userfaultfd.c | 300 +++++++++++++++++++++------------- 5 files changed, 331 insertions(+), 194 deletions(-) -- 2.43.0.594.gd9cf4e227d-goog