From patchwork Fri Nov 22 17:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13883496 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 27179E69183 for ; Fri, 22 Nov 2024 17:44:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B59456B008A; Fri, 22 Nov 2024 12:44:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AE3E86B008C; Fri, 22 Nov 2024 12:44:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 981A36B0092; Fri, 22 Nov 2024 12:44:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 721B86B008A for ; Fri, 22 Nov 2024 12:44:26 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3314C1416A1 for ; Fri, 22 Nov 2024 17:44:26 +0000 (UTC) X-FDA: 82814454240.16.1AFE68A Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf28.hostedemail.com (Postfix) with ESMTP id 4A728C0002 for ; Fri, 22 Nov 2024 17:43:25 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rZkHLVRE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 398JAZwYKCMAy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=398JAZwYKCMAy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732297371; 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=JmMtuCkRvyQ52Tg7fGJRZ5Y2Wsu/zSjhmlAZ0eI81A0=; b=NuzFRFt4zdwiFV4HMD9ERjkgjKKlUTjIY96uYiBnZfvf735MYuYhrE0E+uPgGSQ2UFXg7V lxHFgIgEdaunYXqec4pRy6GLFVczXHfBWzV6WerPQVLn5te5xaBk1cShSVJ2zs16omadjT 9Yo04Vy2RgZzBk5KtvFtZMtNszPwXFg= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rZkHLVRE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 398JAZwYKCMAy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=398JAZwYKCMAy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732297371; a=rsa-sha256; cv=none; b=1CzY22z/w2mBvAI3wv4KXRVEeaUd7HBh8N1AP5ACciVtQehMdaOjqMTSH/g/G2g7OnU3qy p+rCv1+WZz7bY6BcvgEYrOo06HTFw4UK3PNAkUEgc1EUnp+CDqAS8OpIqxEbEOnQ4DxhJ1 zG+tjy21mI/+Fp44drZ0f2YLL3ogvlI= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e30b8fd4ca1so3660093276.3 for ; Fri, 22 Nov 2024 09:44:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732297463; x=1732902263; 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=JmMtuCkRvyQ52Tg7fGJRZ5Y2Wsu/zSjhmlAZ0eI81A0=; b=rZkHLVREejmHLjD9H++IxLGBJc++fY9giC41/6+E7kgc9S9v4EXitQdaTqVuu/HuyV ci5S2+SRo9y32ZUk1prvpsCjuFHtz/RFKrnnlxMPBARqv9QC7W49+/t+Sw9KqSzXiw0O jFFw9VXvnHPNrjL9thOAopJ8lhdVYh+egDC+FYK3eeWfIcICtzaRVZGpvKhRgqkezSOy VF2Yiz+WNc9b6jtBxL17CYtCcuVvCvwUe0IFwNeiECZY7Tvrgq12Ex4p0fPFkQ9Zn5PE yihNk5UHBqc0BV2CRBCmZo8v6YeqqY84e0DLEfrzLJUnC8k/sIICBMom2BBeLw+DK5TJ ym2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732297463; x=1732902263; 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=JmMtuCkRvyQ52Tg7fGJRZ5Y2Wsu/zSjhmlAZ0eI81A0=; b=E5KGqqaPoCzUuNRtDYsB1AZNCxzlLl3lWZxNC9kl7/fMkB8jlVkYx4otBlSoncHs9y sGDe3bYolv52xe+nQjI+yhyP30ia+x1NEyJwJZEKRNoDcBFPlR6MuiaUqCW7L3B6du0B 4NFzXDRw0fOjTPqhEBfRv7NCRjs2sqkg+nXcZpHJmYhRnT+YeC+om20hpXzV9tf8kiqs TvseEpNFTVgpKxS95DGojaKQ8aT+WxXSGedvMSeOfkDfFN79zwkYwHSRZnFntUhlkk1+ tKDpivcBS4DUd0fkZvYwlakvGNmY6ioDRiBV1zttXK/+orG5gZyTFN7xNq/1bm0oJKGn 7hLw== X-Forwarded-Encrypted: i=1; AJvYcCUbKwqjCz6Qa/t/oBuFKZt35l9r2Zob1S+JFBU2VFhcR0Bv+naJODdS78UtT5drqiqA9Fa5WJzp1Q==@kvack.org X-Gm-Message-State: AOJu0Yzq3DA4nqABMTamVadMZCVPxhAgOV6CLt4jH42xpQ00GTGyzWHW rWhxeH+WAYd8gmAiC3k1hW1+1LFRGGQ2XQIsOMXD9UvaeAaVgP/o6aMKOuj+8+vTbu2pLeUGZTc Pxw== X-Google-Smtp-Source: AGHT+IEV9E/JH5zB3aH55fu2gyNobb2STJxxHLoQPN4QfW7QVDU8lvjjtSetWq5+AkJvlm8IMcLFXFtwqtU= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:8f07:6a96:7af9:8fe6]) (user=surenb job=sendgmr) by 2002:a05:6902:1745:b0:e33:111b:c6a4 with SMTP id 3f1490d57ef6-e38f8acbe09mr1492276.1.1732297463091; Fri, 22 Nov 2024 09:44:23 -0800 (PST) Date: Fri, 22 Nov 2024 09:44:16 -0800 In-Reply-To: <20241122174416.1367052-1-surenb@google.com> Mime-Version: 1.0 References: <20241122174416.1367052-1-surenb@google.com> X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241122174416.1367052-3-surenb@google.com> Subject: [PATCH v3 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, "Liam R. Howlett" X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4A728C0002 X-Stat-Signature: 93b9spxjzmsm8i7c3eabbpfk5seq3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kO17+uBxNsNPptqgRp/UXzziiNw/jhM0cl0T180S2yrb+a3oc97FFnzDH364vFwnDS11rLZYYIYYgm0Be75KfRL4cxL/Ig7ySnXvMRo7Ioe+QxQ0KHsJd+bgNyusFzeQ4yyjFk9pXoGJIOlcz8IiuEHoAxF1su4IKCsxg5S1jEznw9fZz7nljjSO2b2cqzcjD6DwBmfcjFM/kVO2uvspfRNnth4WLDBQQqGlgwTWUGmmtx4Tlmfh/EsgpUJkeKUcpzzH9Kmx6tF5438QMcc3DgZlGJktXdkHncGzH0ptsBBXXUuG0zM4nSV7rdhfL/h7FDJqXZRsMzvBZINxS4gSps0IcQcOWvKr2KC524du6CTdihEv30qJ6GZmf9izkwoxuRM9Yn8fL3Z0V16KQUFdB3QqMaCIppn+KAivbgiEtcfM2Z20/kHI74a8ekRtYe7OiXTHa+lhiSDYeqxU4ACoukaZSxSztcxas4XxgOwNwsuwpZhx3flzhmtcF5kh59Q11dexNWDboLzGptL2qHzkBx4PHRX4jmq33OzCjoBqsLGayMSdStUk8JqSQqhXTtwa1/Ln6PQkLRGRcMSFinEZUQl63nKppWMbXJfanSSOinNXM8XrC6FQOpq5obuERvK+JKuxe1Cnd5zPza+O9emxZbuXFRHBdLlmTdi2eL5NffigQzhIcvvIlKuOkdNhyA3TWk1f4DGzsXE/MBxy9UIfi8lOvwRrh2/iUf4= 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: 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 v2 [1] - Added SOB, per Liam Howlett [1] https://lore.kernel.org/all/20241121162826.987947-3-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 9715326f5a85..8ac3041df053 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); @@ -87,11 +88,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) {