From patchwork Tue Feb 13 21:57:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lokesh Gidra X-Patchwork-Id: 13555744 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 ABA8FC48260 for ; Tue, 13 Feb 2024 21:57:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 185118D0021; Tue, 13 Feb 2024 16:57:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 10DE68D001A; Tue, 13 Feb 2024 16:57:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF45E8D0021; Tue, 13 Feb 2024 16:57:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DB9338D001A for ; Tue, 13 Feb 2024 16:57:55 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B690D1405CE for ; Tue, 13 Feb 2024 21:57:55 +0000 (UTC) X-FDA: 81788143710.28.5593ED1 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf22.hostedemail.com (Postfix) with ESMTP id 068FAC0009 for ; Tue, 13 Feb 2024 21:57:53 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2t5rQDzt; spf=pass (imf22.hostedemail.com: domain of 34eXLZQsKCBI362wAzy0v9sy66y3w.u64305CF-442Dsu2.69y@flex--lokeshgidra.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=34eXLZQsKCBI362wAzy0v9sy66y3w.u64305CF-442Dsu2.69y@flex--lokeshgidra.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707861474; 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: references:dkim-signature; bh=ir4+0EAbk6DmHSJMCOQ+63RTEjcKM6OBoUCHYQ558Qo=; b=a82MpH2auoYq3QwiN4jOWDDfMM4sYIZGium2WzmCHAe22CKouLMp+qaxvAibh+82XEywFy 1ieOk7VA1xW3WZf7KBvELn+2FMq05qB1I8o8q+jzGm/Pi+a1hgHMdg7uuNFcJ0BPIrItRM f4MesFDoTuqBvXlaygp2tGow9GUVVFg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707861474; a=rsa-sha256; cv=none; b=rIDjR6xmjoyiDCW5nU1cr60OSphytKr2MB7JdSkJMl3d3KrvxMNf6mSWm2RBxgRJBAlbOR wO/7Lew2MX5V1r6mCrKFoUvj3VFjB2zV+6kJcNuqdmeG9fmI+smQY1nkpmepbYtdUifFpm NCyJZLw3n0PIIMQyTrvW0D+yXlDEhJQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2t5rQDzt; spf=pass (imf22.hostedemail.com: domain of 34eXLZQsKCBI362wAzy0v9sy66y3w.u64305CF-442Dsu2.69y@flex--lokeshgidra.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=34eXLZQsKCBI362wAzy0v9sy66y3w.u64305CF-442Dsu2.69y@flex--lokeshgidra.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60753c3fab9so3009727b3.0 for ; Tue, 13 Feb 2024 13:57:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707861473; x=1708466273; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=ir4+0EAbk6DmHSJMCOQ+63RTEjcKM6OBoUCHYQ558Qo=; b=2t5rQDztecgx8FxVIQuCgr3a5MgJ+kTHoKopGLzod0qN18gZ+EnCHY6O0RoWYmY/Dz eNhQkxXVNgCpIJGuCGy1JHuGcElq+ORD6/uF3ScZ0ZQhkRF7PRbyYBIHxXbpP2+z3JbC bHp3x/RghgiZ/4Yr6xuip4Otl7f7mSYtAxZtLz8YdzXZewDyeG8wdjaxeAeddCgNyVnv GG4BLt4hIpubV8d41BPczmoEM1lodhJy4qzWIODgeOVsaiybTKIufSoJxCKSgPh9khL8 ZuAGdFpVzkSenWqei1ojSWlzXnBJypmjkt9eFliuIYaMD8zVkM+CjirQ+6bCP5JLQNLa mZ+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707861473; x=1708466273; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ir4+0EAbk6DmHSJMCOQ+63RTEjcKM6OBoUCHYQ558Qo=; b=tsKjYoVe/FMHUu4b/0KaLP3JrdBqBZvNHGR+5puaMzbgPlFTbFnn3/XGOvWWIK+zsU D8bHLcd+eXCrGUNqT0NIa+rKD3jp+wa5Ff2955GeEjPtbeoVKoN2wful5a6QocorX0Wq 3ytBiwGUYDWVgWwLL2QQkO2qh/VSZO3jfwbs8oH46dyV28/GLEnRlJWO50zzIwKp1w56 CMPlkJ+mflMgGwTV9MlsX21B9Nh2MBnZveeEX2pAT3mVXHCD28ee6MifP8t7PsGkXbew H3yMv8U3Ty4JDxX4C5L5zoTsxTOKICkooOsqZlmZ5CjQyfEGo7W+OU6FxyYamEAL8i3H oU6Q== X-Forwarded-Encrypted: i=1; AJvYcCVX7FAsUE49gGY52sS5xD4rvpO46+Eoe+y1zf5Y6icGdoKXPS5n9MAxy3pPEoVZfkE4XgIAJT6SPqmSaAK6d4rMUZc= X-Gm-Message-State: AOJu0YzZui7Z1KdicSmVeOaRp0Z8tCn2/Rn3XERMau0cAXSw7pZ6+DNr 6RKJq6K+LnbdRa/AfdXyUZH1IdWdWmXxKoQx4MSULnoSGKI2D+N4eIBJAm1RhpI/E0Y95TdVawT 0fA0Gs4bMNtjpUfD3E1hqsw== X-Google-Smtp-Source: AGHT+IFDSd5xu2FB72V4FXpKTmeqW1QesEeAS6gwaRywm0PkcXIJA1oGvUM9nR5x/IAVPoUhZ5UVHHp7Mrodt7B/AA== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:ce6c:821f:a756:b4b8]) (user=lokeshgidra job=sendgmr) by 2002:a81:5702:0:b0:5ff:6ec3:b8da with SMTP id l2-20020a815702000000b005ff6ec3b8damr12240ywb.1.1707861473107; Tue, 13 Feb 2024 13:57:53 -0800 (PST) Date: Tue, 13 Feb 2024 13:57:38 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240213215741.3816570-1-lokeshgidra@google.com> Subject: [PATCH v6 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 X-Rspamd-Queue-Id: 068FAC0009 X-Rspam-User: X-Stat-Signature: yf6iakji7t7gimp7o8d7ehsxmurnwzjp X-Rspamd-Server: rspam03 X-HE-Tag: 1707861473-810428 X-HE-Meta: U2FsdGVkX1++kwA9ym3Z1RezrCHBI77WNcS5ciqQUdgG7vzSNZElI7o7ImWTu+mTnobYfdiMPYtGXl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iKO7rYNNZQZAPxcHvBisuubv4jODPaK+192qi8mutvjNGhxCVuz5lJElD1GJzLM0EBJ5XcQzA341iimF9LhBt+McfkoMGkm8DH+Cf8LLQ/8cXZfDcn129iOgXbbaggeUl3xVuBpiJCrfcBlaEjtIH/iXKxubR1sTQ6x16JOwesn11UphconfbDNm1W7xzEvQPUX5SdWPLrgVh+XFT0jFpkXDhG7uUnqJ/8RNC3H0t9oayhj2i/Lb8QVJQ6F29oDlkdMo3RJ1qLZYYeADxLptDCb4IFPzkQvxWvaovFnUZwqUM5YAj/Wd/rBuesgI1kB3p2Z6K1pG7nllxbxbVTvBN5GSi0TTvyVR1mQQInyfnXABuxIaDgeTDyiTJFpaA4QbkZTzTaBn1yD7FyzlnZHCQKQlPiorUqMEY1WV5YmF5rcQk/rzXWMgZk/5QKzN/RH959xklxDPCLWtRi/XUwLm0bU/Oxd0KD9rnG4Pw8tit5GhaEsfbwSnIlSZDSPddNT3nIt0f1TPxXRXas8wqjdEnZSrQaZIQmo3+N1SY= 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: 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 v5 [5]: - Use abstract function names (like uffd_mfill_lock/uffd_mfill_unlock) to avoid using too many #ifdef's, per Suren Baghdasaryan and Liam Howlett - Use 'unlikely' (as earlier) to anon_vma related checks, per Liam Howlett - Eliminate redundant ptr->err->ptr conversion, per Liam Howlett - Use 'int' instead of 'long' for error return type, per Liam Howlett Changes since v4 [4]: - Fix possible deadlock in find_and_lock_vmas() which may arise if lock_vma() is used for both src and dst vmas. - Ensure we lock vma only once if src and dst vmas are same. - Fix error handling in move_pages() after successfully locking vmas. - Introduce helper function for finding dst vma and preparing its anon_vma when done in mmap_lock critical section, per Liam Howlett. - Introduce helper function for finding dst and src vmas when done in mmap_lock critical section. Changes since v3 [3]: - Rename function names to clearly reflect which lock is being taken, per Liam Howlett. - Have separate functions and abstractions in mm/userfaultfd.c to avoid confusion around which lock is being acquired/released, per Liam Howlett. - Prepare anon_vma for all private vmas, anonymous or file-backed, per Jann Horn. 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/ [3] https://lore.kernel.org/all/20240206010919.1109005-1-lokeshgidra@google.com/ [4] https://lore.kernel.org/all/20240208212204.2043140-1-lokeshgidra@google.com/ [5] https://lore.kernel.org/all/20240213001920.3551772-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/userfaultfd_k.h | 75 ++++-- mm/userfaultfd.c | 438 +++++++++++++++++++++++++--------- 3 files changed, 405 insertions(+), 194 deletions(-) Reviewed-by: Liam R. Howlett