From patchwork Thu Nov 21 16:28:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13882136 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 AA27AD7878D for ; Thu, 21 Nov 2024 16:28:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 00C0D6B0089; Thu, 21 Nov 2024 11:28:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EFC766B008A; Thu, 21 Nov 2024 11:28:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4F086B008C; Thu, 21 Nov 2024 11:28:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B31B36B0089 for ; Thu, 21 Nov 2024 11:28:35 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 75E8F80BF2 for ; Thu, 21 Nov 2024 16:28:35 +0000 (UTC) X-FDA: 82810631694.20.10873C3 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 70DBB20008 for ; Thu, 21 Nov 2024 16:28:09 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=A0n2Ud+1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3sF8_ZwYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3sF8_ZwYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732206446; a=rsa-sha256; cv=none; b=EoonnGq3919pTbv8P+lKxDJQLy96wlFmPsTOeXI5SEq336C+tc4uTA7ZlGNsEr5FCwRd7v ZVwtbuq/TD0v31eUe7+gYCw2qI9HPxIMnm78kAENpLGQ4gDgT36HimDZ/UfHE3hZXC9LX/ mhjc7gZnMKcIoHZcbDo8FaKCjLjqBBA= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=A0n2Ud+1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3sF8_ZwYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3sF8_ZwYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732206446; 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:in-reply-to:references:references:dkim-signature; bh=aR8IuZPS55VJxAfGr69rX+AU9gT7VgeBh1eRZ8ctshE=; b=TlMUyk85cNPN8vzi3v8duIe+bTMNBtOMClX9yFoI14t4zzC4MxxRYhMmKRrieeLEFdDECA zykJutP/8wvIDXAWKFctmMETFGjwJxvVky5WwVGqM2T8FtwOWwnNyA1jwlhLqsE6yU/0J5 eW8tTKoacvl9i1jaYQvlaozb405fv5Y= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e3891b4a68bso1834694276.2 for ; Thu, 21 Nov 2024 08:28:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732206513; x=1732811313; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aR8IuZPS55VJxAfGr69rX+AU9gT7VgeBh1eRZ8ctshE=; b=A0n2Ud+18niTYPBJfUttpMMcnYY3LWUiSWGrHdv3ELWfupnbI5XOQd7kHcDwgqsINl q9oddv/WAM6p03IhX//EaB4ZOSHEZTQZxuW5GU8qKSYYeRVV4sYVltpFSPztvuzxUCB0 6kxGWe7OjQoLiAcNT3nNNE5AmKrxDMjjvBbXN1bZQxf5CeTS7wxJugIfvIx5Dzrp51XZ vX0Y/P228aHJScN2IqdA2xAUw1QO7YXZWM5CTWgf61e8dm6YBMazQj5B3Mwiz5qGWmfT do5QdnWSavrHuw1b7J8jiJBCJIjp4rwoKTynyL7NWAlM61YO7zzVhtpQEWJK0f+z1q+2 DuAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732206513; x=1732811313; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aR8IuZPS55VJxAfGr69rX+AU9gT7VgeBh1eRZ8ctshE=; b=e/vZOPs/NhqE/D0PSaDU/bBVOEZbJCvcF8Qi+jq6B35TOzFc+E+A+hF1QNiHSEQZor xMPMsk0Py84Q/ZF36JR1kQM8NF8vN/7KCQA2HiPubcQBa7j4JB9Y7v45QsU4OCkVOavj 1RYV/LYsxwFRLVr9RO5Loyg9+Zu7IVnOfGgOYO8/Tf5kVWC4sLjrOc99Zc9JbjEwuAko z7ABE6dPDPvVTRt1ilkIbSiayhStPPXvy24TypdRrkIdvK1Q+4OVtpg1jBVvS4LufWJt DzZ0LpLcP8Ho9YJ5G6WsgzlfH+tkypWSFTb09gC3o0nJdKi/Fi17Z37pMVtlFCEIJpYU bzaQ== X-Forwarded-Encrypted: i=1; AJvYcCXJFYs+8GptNybmV2LV9JrJ/jKD1RvPxrI5Hn5ZTMIePonMj6478AXezLHkAY4gJ4G3vZhmz4+Tyw==@kvack.org X-Gm-Message-State: AOJu0YwhBpFl5bq6POV4xAEoA/Bsdf0YPHuiiR2hWLoITwFlFpakB/l9 Roqb+AvH+35s6dlzKTL13TQWvIx2HkmPkx9jtlHu1eGl0gCTYHwh+5u+3FcFNGwSe90elK72lDK LVg== X-Google-Smtp-Source: AGHT+IHUqHsAWi+Wxn16ObPiD6xo041RvCrQwuBa4MVJn53Kia7YFyxv9Td6tAJu+eIvJbInkjv+ljJDVwE= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:ab6:ec44:b69c:2388]) (user=surenb job=sendgmr) by 2002:a25:dcc5:0:b0:e30:be64:f7e6 with SMTP id 3f1490d57ef6-e38cb566988mr3028276.3.1732206512791; Thu, 21 Nov 2024 08:28:32 -0800 (PST) Date: Thu, 21 Nov 2024 08:28:26 -0800 In-Reply-To: <20241121162826.987947-1-surenb@google.com> Mime-Version: 1.0 References: <20241121162826.987947-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241121162826.987947-3-surenb@google.com> Subject: [PATCH v2 3/3] mm: introduce mmap_lock_speculate_{try_begin|retry} From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, andrii@kernel.org, jannh@google.com, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, mhocko@kernel.org, shakeel.butt@linux.dev, hannes@cmpxchg.org, david@redhat.com, willy@infradead.org, brauner@kernel.org, oleg@redhat.com, arnd@arndb.de, richard.weiyang@gmail.com, zhangpeng.00@bytedance.com, linmiaohe@huawei.com, viro@zeniv.linux.org.uk, hca@linux.ibm.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, surenb@google.com X-Rspam-User: X-Rspamd-Queue-Id: 70DBB20008 X-Rspamd-Server: rspam01 X-Stat-Signature: qg79gpa71ox4hbo4cre9urun977mmnk1 X-HE-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fzIFDNWtPrYMkI9+iUd0oCDBh42u6QUZCjisLjX9Aa+ejMRc3L4wdx+krKlDGv+myFNgdW5KsXm8oH/OdI30JDY82+1q1N+qElFYc9CanTzBYYPiTaq5I+FKO4OSnWh4Dw4futIZrjqjaiOu0Ghe3VAb7Oitx9kJ91WXFC0fu5NtBUiIhQSA6dXMzv9ji6uNpccuUDT2LUc3MKsEYegxLhZZUVWzr3IvHHWnl1SsdEgGaKJ6ZvqU/sahUvBNMHUnbmoOyViklNm5thetAVRwtPoLEbEE8MaBUIKYnDJEUOLNt7DtNwSg+qIjAIxLjW3CtIbe0tIo1XcpF+wE4arzhx1QBqERKD/Bw9WNbyH3X+kZUEjGoIK8no03ydZeqIz9txllYVxOlaBSbxsdrgrI5i/V5Lx/IZEke5voqyVRBaak1keq0WU1QSx9dAjMyOAY6kPKLLUCxL/48ttuKLEXsmdu38gsiftpHtk935+cCHFPaSWfsEC5MEgQgbuJLecZwRCoOGA2zXxpEDUxpVaOf0g5aa9KxPBj2WAxqGJRu9Mm9m+cKkqCx5OKXUdtdDRmvouPNC8Hz8Fs91AWqha18R/FdLwGeuRIUmgIfh6ykjOuiMZodBk1j6H5fzbMZIT/vO0HIn/AUE+Ql63VfBk2GsF7URkzLeZzHVgOHg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.001953, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add helper functions to speculatively perform operations without read-locking mmap_lock, expecting that mmap_lock will not be write-locked and mm is not modified from under us. Suggested-by: Peter Zijlstra Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- Changes since v1 [1] - Changed to use new raw_seqcount_try_begin() API, per Peter Zijlstra - Renamed mmap_lock_speculation_{begin|end} into mmap_lock_speculate_{try_begin|retry}, per Peter Zijlstra Note: the return value of mmap_lock_speculate_retry() is opposive to what it was in mmap_lock_speculation_end(). true now means speculation failed. [1] https://lore.kernel.org/all/20241024205231.1944747-2-surenb@google.com/ include/linux/mmap_lock.h | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index 083b7fa2588e..0b39a0f99a3b 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -71,6 +71,7 @@ static inline void mmap_assert_write_locked(const struct mm_struct *mm) } #ifdef CONFIG_PER_VMA_LOCK + static inline void mm_lock_seqcount_init(struct mm_struct *mm) { seqcount_init(&mm->mm_lock_seq); @@ -86,11 +87,39 @@ static inline void mm_lock_seqcount_end(struct mm_struct *mm) do_raw_write_seqcount_end(&mm->mm_lock_seq); } -#else +static inline bool mmap_lock_speculate_try_begin(struct mm_struct *mm, unsigned int *seq) +{ + /* + * Since mmap_lock is a sleeping lock, and waiting for it to become + * unlocked is more or less equivalent with taking it ourselves, don't + * bother with the speculative path if mmap_lock is already write-locked + * and take the slow path, which takes the lock. + */ + return raw_seqcount_try_begin(&mm->mm_lock_seq, *seq); +} + +static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int seq) +{ + return do_read_seqcount_retry(&mm->mm_lock_seq, seq); +} + +#else /* CONFIG_PER_VMA_LOCK */ + static inline void mm_lock_seqcount_init(struct mm_struct *mm) {} static inline void mm_lock_seqcount_begin(struct mm_struct *mm) {} static inline void mm_lock_seqcount_end(struct mm_struct *mm) {} -#endif + +static inline bool mmap_lock_speculate_try_begin(struct mm_struct *mm, unsigned int *seq) +{ + return false; +} + +static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int seq) +{ + return true; +} + +#endif /* CONFIG_PER_VMA_LOCK */ static inline void mmap_init_lock(struct mm_struct *mm) {