From patchwork Tue Jan 14 02:19:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13938321 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 F180CC02183 for ; Tue, 14 Jan 2025 02:19:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89106280004; Mon, 13 Jan 2025 21:19:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 83E5C280002; Mon, 13 Jan 2025 21:19:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B87E280004; Mon, 13 Jan 2025 21:19:49 -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 4D4F8280002 for ; Mon, 13 Jan 2025 21:19:49 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C1A041603DB for ; Tue, 14 Jan 2025 02:19:48 +0000 (UTC) X-FDA: 83004451656.09.0B3C0CD Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf06.hostedemail.com (Postfix) with ESMTP id CEC5D18000B for ; Tue, 14 Jan 2025 02:19:46 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WzTK72mL; spf=pass (imf06.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736821186; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w+Lbt2oOvlSTPMdIT0UeNEpvkZe5nRvEnawf5u/tH+w=; b=5LjbAMTqZBij5F2O3PvKZNagI3TFkWqCxkXD6iGITC3hCOjk/xMkKDTQ/fq6RhYGbo49ik IoQEE4YVfTZ5WQrm5b9WSTO7FbQC/1LvBxpWd0DfEl7Eco/aj8H/QyTmeLv1iA3nHAVB6t s3Pgj0T/K93lSJPHe4mMlyndEpG98eA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WzTK72mL; spf=pass (imf06.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736821186; a=rsa-sha256; cv=none; b=oUI8otA7T56hZjPOsR7rlG5L0+KTxpqEkdteKuXjJcOXMo8+3xPPn9hHTMsLI0csm8VON3 WshDGn7lpf1YJ28E2nGkR8lg+imUVATTxzSbBv9GfZFB5kVP+aayPS1Bx1MxPQsByh+N78 7/5x75XLAwQzkmkPcgtfyl/cW0Zb7lM= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2ef760a1001so8336587a91.0 for ; Mon, 13 Jan 2025 18:19:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736821185; x=1737425985; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w+Lbt2oOvlSTPMdIT0UeNEpvkZe5nRvEnawf5u/tH+w=; b=WzTK72mL3sk8DvDUa8caSFyIYmkqSBRyqY7WOdI/i4/+Q5ImzUiy64mdJTQ1fE16tu yeqkeLkFnCGgaznfIAX70O7NdQqhS2SEbrVW2UVbdNFwCWLKDR6ax3TNoQBXHs+WvvSS Pth8tE2q3Wl+bqrG5HHzs6aMnLCZGPoht7K7uBk7OvBzO2OvAEfIMHBHJQTOGk/FuR0d uT4ex9jfAdmH7Rd32vkGUZ6oHEc3u9H+M1NSn3eoNvxYyCIpo0L5HtGreplimHgtOb8W m2hAC5oMn7tT32KQmtzhNt7FUHVt1WNtVGRp1IEDqIihTuFw0YsERZrPnh6T9BEb0fK4 fV1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736821185; x=1737425985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w+Lbt2oOvlSTPMdIT0UeNEpvkZe5nRvEnawf5u/tH+w=; b=WJupTQtWDGu5y8CCwduPv4V3k4KKGhE101ePH/oNsCPhxDKXX8SXQYKhZUVtMMBuZ7 M66kWq4gckAaJj98yASzJZXLRI846w5/Fntd2oVnNzttiMzsKMUXwB/CeslpW2AgpswO C54H92F1qSZRyKrdhtkQvrsaBWGecsJNvQsuOoF7elQc/6XUeXUs6ZjF95eBZQqg5H+5 de/6t59eyj2BkgBUZHwFplJuWehYjEXBJgiI5nHFxrHRUCNO1ASviqUBI4PpAvaCIHR6 AoXn5ZMdjm0OttG5KZ3ksvJjmUFh2c11DCi+jE3GZTV6NYn/EgalXtX1MgZsnQGNG7ks Pnsw== X-Forwarded-Encrypted: i=1; AJvYcCUoHBkvwrIDslplDNmTTmz7HqSV7VkfeJntCvoUq3AhNAXr5BEukNDY89L5xceuZ1W0uIbn9YvzMA==@kvack.org X-Gm-Message-State: AOJu0YywYCZDO3C9OC2hJxkzuI7Mr79bO+kVfq9l/c8627npHanvkDC3 BLd/yquDa9x7NcniNqPSKZ2p4u8nL2HeP13fl8Lm6SsiKqWu8Q+5 X-Gm-Gg: ASbGncviJ11vm96eJyVYf5Cf3oVQLej7OhX2Q+ErIiExJSUz7RriZVbIBhKcMIFsj4c Esjw+X+hg8j9lY/XAiuapUFDTD2GRr56Yc21gTE+pZFR5fnkgCFziVNCHGnK8WtGAResKp9a7ai bI8QGwgVIDIXmrt/Egv/cwjPi5nmNk/pdnEEBeqAYhCvqf5UBPglpSwaut0NOeDlluLFb6D8J7n BRScIN6uG68Wp864ooiKDBLPo8pt8VCnxxNZGy6GLO2Fi/N4u4auNBPpZTnzoLzpivEg7gAr5bR 3A2Ue4nV X-Google-Smtp-Source: AGHT+IEFmgpkoNWWnjwaaKDdY0kFEuoA/VYtTI9WUz5vYKzivTLXj4vvKroyeHsRxZ0DCUaCQZ66Tg== X-Received: by 2002:a17:90b:51cb:b0:2ee:863e:9fff with SMTP id 98e67ed59e1d1-2f548f2a4afmr34600926a91.10.1736821185517; Mon, 13 Jan 2025 18:19:45 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:4043]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-a317a07cccbsm7420136a12.11.2025.01.13.18.19.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 18:19:45 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v4 3/6] locking/local_lock: Introduce local_trylock_irqsave() Date: Mon, 13 Jan 2025 18:19:19 -0800 Message-Id: <20250114021922.92609-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250114021922.92609-1-alexei.starovoitov@gmail.com> References: <20250114021922.92609-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: CEC5D18000B X-Rspamd-Server: rspam12 X-Stat-Signature: 3s6ufr8xfn1y66hoa9ftp1upg6bj3jt8 X-Rspam-User: X-HE-Tag: 1736821186-812587 X-HE-Meta: U2FsdGVkX1/BXfm/HjlKbTZ3hDTDw/gtUC2VqJpiYDB3rJDqVD5F9RO+ekIvernalC+DBlQ2rSsKREe3g3Sje+juZx6C9vFmzjeeROH8ODWk3kd7AVUS9ksVGmnjMsnxf1x6Fbsri8CouVQdHadxmic6juUqmdzYNNB/onUko46d6J/l7dFYEL0KuCMTxH1094kJlWC/g8ec33Skj3tj2ZCL8mivqy9KZHsE5/p83iQgb1BqtUWqaxh+iZlSRgeAm/9+VtHLQPhVzhMLnONC8heicxVEJYzQf42Mw9YtUBP/5wzAn3PAxSc01MyqDocEYjmqwOrLecbQY5E9ZVS4Q/lxtrzOgjigDIdZeB98TJ61yj+D8MiHSRVsjqhLop2Uc7dGtuUy2qG/dG8NwTzJba10LvygWCAtGNpzWQJerXHQzJRLJ4ml7lyBcUk2k4wi2eHGXlbIPzjf045eCsmHSaLLEbe3N0IStzbIQUOQWElhIswNa2DYd5oQb+DvbrLwc/lL7lP81Tm1mbMoytlXc2hMFUhQtxTzIabOtaRBE8QZ/xJOpb/7tC/kdK5Y+Z4QN0o0fyd4sTuqpA5RF2y5ZhiAIS4v5hmbkgUOPnTrrCjdDloObES/g7qA41vPxd04CzpMg5lg7DJxh72m1uh60sbameJKMp/wHH+5zQXWTTM/rdIa+P2aSsNF9kex3+DPoLFWX04P55Rohd0Cy1vh+mOyob+ZCeYsFLRPZaQqBCLYpNZypUNd1gWv/NbfPf92IDCaDZw4X6QpQ76WFpnQvxEEUySIbyviWqINMpqVVimAPRQHqmEvYzuVVwB+Vf/ZAn0Z9WeomRoPF2jHH+L1bOcpE8XF2paPc9HLA78KcPc/i6Hn1qg2sSdf4DHRUjQhBQyyBC9ZLePb6Rs12iTI0GZl1vJpFRLlXdgG//YsQhLTRKVhmPDInlPiNg/23A1gvoyE7RT61g2BLbJxssT HnAuRPQd 3UO8Rhp46KAlTRdR1QVyRScwAF7643rAm0gBxCtFX7yJnAB7ayy7EVsHrW3w2oVnb6SuVh/7UscuUyy+mN6EIHcdCCLqXXLzLCueHxVQEYL9ZIQP+gqay/upb4ahhnLL0vz8F7Jy6F8L3qGu6PKiIuQfWYNrvfTlpfJyQpshzBkwAobKni4fPjZF9zNFKc+3oxAN3Gy9ImsmcFQswvNk1fauevZ48PHZjitM9zkESJaT02hOngkxkdrdsUVtOPSCdvQgNH/h71z3KO9BgWYgKS7IWiMqdUA8f12+bNvSlRkHl7WWLh5NdNT7GeVI/u8S6cG9cX1UZOt8XsbL8EBfFMyZxsnKkRBsnSpAVps6Y8mUUhf59+lRIGBjuE7j6fDibZg8uU9rTzdGjY+qbqL30vqqzBQuCXWImssBTBb3bJG/Lyv0dkxs+FDhuXdeMRZVNwmSr57OrSktCkWgiELAD6XxGG6nHYxu21zM6riqFLIY/om+JxkwmYo56lQ== 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: From: Alexei Starovoitov Similar to local_lock_irqsave() introduce local_trylock_irqsave(). This is inspired by 'struct local_tryirq_lock' in: https://lore.kernel.org/all/20241112-slub-percpu-caches-v1-5-ddc0bdc27e05@suse.cz/ Use spin_trylock in PREEMPT_RT when not in hard IRQ and not in NMI and fail instantly otherwise, since spin_trylock is not safe from IRQ due to PI issues. In !PREEMPT_RT use simple active flag to prevent IRQs or NMIs reentering locked region. Note there is no need to use local_inc for active flag. If IRQ handler grabs the same local_lock after READ_ONCE(lock->active) already completed it has to unlock it before returning. Similar with NMI handler. So there is a strict nesting of scopes. It's a per cpu lock. Multiple cpus do not access it in parallel. Signed-off-by: Alexei Starovoitov --- include/linux/local_lock.h | 9 ++++ include/linux/local_lock_internal.h | 76 ++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h index 091dc0b6bdfb..84ee560c4f51 100644 --- a/include/linux/local_lock.h +++ b/include/linux/local_lock.h @@ -30,6 +30,15 @@ #define local_lock_irqsave(lock, flags) \ __local_lock_irqsave(lock, flags) +/** + * local_trylock_irqsave - Try to acquire a per CPU local lock, save and disable + * interrupts. Always fails in RT when in_hardirq or NMI. + * @lock: The lock variable + * @flags: Storage for interrupt flags + */ +#define local_trylock_irqsave(lock, flags) \ + __local_trylock_irqsave(lock, flags) + /** * local_unlock - Release a per CPU local lock * @lock: The lock variable diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock_internal.h index 8dd71fbbb6d2..93672127c73d 100644 --- a/include/linux/local_lock_internal.h +++ b/include/linux/local_lock_internal.h @@ -9,6 +9,7 @@ #ifndef CONFIG_PREEMPT_RT typedef struct { + int active; #ifdef CONFIG_DEBUG_LOCK_ALLOC struct lockdep_map dep_map; struct task_struct *owner; @@ -22,7 +23,7 @@ typedef struct { .wait_type_inner = LD_WAIT_CONFIG, \ .lock_type = LD_LOCK_PERCPU, \ }, \ - .owner = NULL, + .owner = NULL, .active = 0 static inline void local_lock_acquire(local_lock_t *l) { @@ -31,6 +32,13 @@ static inline void local_lock_acquire(local_lock_t *l) l->owner = current; } +static inline void local_trylock_acquire(local_lock_t *l) +{ + lock_map_acquire_try(&l->dep_map); + DEBUG_LOCKS_WARN_ON(l->owner); + l->owner = current; +} + static inline void local_lock_release(local_lock_t *l) { DEBUG_LOCKS_WARN_ON(l->owner != current); @@ -45,6 +53,7 @@ static inline void local_lock_debug_init(local_lock_t *l) #else /* CONFIG_DEBUG_LOCK_ALLOC */ # define LOCAL_LOCK_DEBUG_INIT(lockname) static inline void local_lock_acquire(local_lock_t *l) { } +static inline void local_trylock_acquire(local_lock_t *l) { } static inline void local_lock_release(local_lock_t *l) { } static inline void local_lock_debug_init(local_lock_t *l) { } #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ @@ -60,6 +69,7 @@ do { \ 0, LD_WAIT_CONFIG, LD_WAIT_INV, \ LD_LOCK_PERCPU); \ local_lock_debug_init(lock); \ + (lock)->active = 0; \ } while (0) #define __spinlock_nested_bh_init(lock) \ @@ -75,37 +85,73 @@ do { \ #define __local_lock(lock) \ do { \ + local_lock_t *l; \ preempt_disable(); \ - local_lock_acquire(this_cpu_ptr(lock)); \ + l = this_cpu_ptr(lock); \ + lockdep_assert(l->active == 0); \ + WRITE_ONCE(l->active, 1); \ + local_lock_acquire(l); \ } while (0) #define __local_lock_irq(lock) \ do { \ + local_lock_t *l; \ local_irq_disable(); \ - local_lock_acquire(this_cpu_ptr(lock)); \ + l = this_cpu_ptr(lock); \ + lockdep_assert(l->active == 0); \ + WRITE_ONCE(l->active, 1); \ + local_lock_acquire(l); \ } while (0) #define __local_lock_irqsave(lock, flags) \ do { \ + local_lock_t *l; \ local_irq_save(flags); \ - local_lock_acquire(this_cpu_ptr(lock)); \ + l = this_cpu_ptr(lock); \ + lockdep_assert(l->active == 0); \ + WRITE_ONCE(l->active, 1); \ + local_lock_acquire(l); \ } while (0) +#define __local_trylock_irqsave(lock, flags) \ + ({ \ + local_lock_t *l; \ + local_irq_save(flags); \ + l = this_cpu_ptr(lock); \ + if (READ_ONCE(l->active) == 1) { \ + local_irq_restore(flags); \ + l = NULL; \ + } else { \ + WRITE_ONCE(l->active, 1); \ + local_trylock_acquire(l); \ + } \ + !!l; \ + }) + #define __local_unlock(lock) \ do { \ - local_lock_release(this_cpu_ptr(lock)); \ + local_lock_t *l = this_cpu_ptr(lock); \ + lockdep_assert(l->active == 1); \ + WRITE_ONCE(l->active, 0); \ + local_lock_release(l); \ preempt_enable(); \ } while (0) #define __local_unlock_irq(lock) \ do { \ - local_lock_release(this_cpu_ptr(lock)); \ + local_lock_t *l = this_cpu_ptr(lock); \ + lockdep_assert(l->active == 1); \ + WRITE_ONCE(l->active, 0); \ + local_lock_release(l); \ local_irq_enable(); \ } while (0) #define __local_unlock_irqrestore(lock, flags) \ do { \ - local_lock_release(this_cpu_ptr(lock)); \ + local_lock_t *l = this_cpu_ptr(lock); \ + lockdep_assert(l->active == 1); \ + WRITE_ONCE(l->active, 0); \ + local_lock_release(l); \ local_irq_restore(flags); \ } while (0) @@ -148,6 +194,22 @@ typedef spinlock_t local_lock_t; __local_lock(lock); \ } while (0) +#define __local_trylock_irqsave(lock, flags) \ + ({ \ + __label__ out; \ + int ret = 0; \ + typecheck(unsigned long, flags); \ + flags = 0; \ + if (in_nmi() || in_hardirq()) \ + goto out; \ + migrate_disable(); \ + ret = spin_trylock(this_cpu_ptr((lock))); \ + if (!ret) \ + migrate_enable(); \ + out: \ + ret; \ + }) + #define __local_unlock(__lock) \ do { \ spin_unlock(this_cpu_ptr((__lock))); \