From patchwork Wed Jan 15 02:17:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13939760 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 A99D7C02183 for ; Wed, 15 Jan 2025 02:18:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40AF36B0092; Tue, 14 Jan 2025 21:18:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BA3D6B0093; Tue, 14 Jan 2025 21:18:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25AED6B0095; Tue, 14 Jan 2025 21:18:11 -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 070046B0092 for ; Tue, 14 Jan 2025 21:18:11 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7F7D98072D for ; Wed, 15 Jan 2025 02:18:10 +0000 (UTC) X-FDA: 83008076340.11.034C4E0 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf28.hostedemail.com (Postfix) with ESMTP id 9F42BC0015 for ; Wed, 15 Jan 2025 02:18:08 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UibYau5G; spf=pass (imf28.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.51 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=1736907488; 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=ya64AUDwnxg51Ko7qINtXyIraw3La1yxHRcto22WozgCwCRrt1+hvyQL4cDqoB1eo+I7Zc ah3CZfKdO7tDDpo+I87Iv5/vtE646Kziq8NeVLN0wApTf9h4EiOJEpUzBjIdA4N8E8CfBE 9vZsRFk2Pxcwkl8uggre2vavL2WSwvM= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UibYau5G; spf=pass (imf28.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.51 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=1736907488; a=rsa-sha256; cv=none; b=VRKcFrOxdZc419kIEAbFZ28MA4sHLRrYdpKsJTQtCCa4GeF0PpgJjZUIMRIlp8KRk41DS+ don1gYX9uK8nFRV0CjDpcE/Mk3SL0BnegT0sLSBfCIvbrcxeJrmDFRAvqVP3KdbXUkc5hH 5guAYJKn6bs1+BtSaGAvBEajQ74KFgg= Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2f441904a42so10670909a91.1 for ; Tue, 14 Jan 2025 18:18:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736907487; x=1737512287; 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=UibYau5GQHLvXKW8hRyUo9f01jJGrC0nrKJulEGUAJi5MNHCVtocvJ29iA+dZ8HU1c uLjb//Nep62t3a3ALLCR6sQAr5TP7Se59eiXmU+XRS/BUhC2e8Q4lNMl2Z5hFvf4R5hc 7eeVtDhxCO2Enl72HBDn4PeO14CY6mtPotQPX7vyZQyPEWGrzbk7VfXgMIfbgZSqUDMx j7UYdV/vjeBEGq2/B06t5RdJjah7sKbhMFN39BwxDyelMtDqwnwjCL0EqCakNBfTVZN1 zc+gKpiMzlK+wyfol5rcbUvPJUZ65rhXAiVgq9UWFIz2fMImpAeHFFFci2bIz9fw1vhr tq3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736907487; x=1737512287; 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=DMDg92xDYPYzUl9vuuKjZqgz028KolD9Jmb1N/VAr9Z2dIUR2HgN4DWN0B33oIdWxh YRXXjkd4fOdW3BG0GnVvhCW42qsHvpzU2LVPZzwvsLDnyz9NFYid2TYbvs7B4Qht1O2b 69VlRxMHSdKtfjYl3Dja34HE/p6drkpIgAMoR6PCywp/ajjK38IdiVvAmfT0L+u9WPyW CI+QbMq6BD20Npe5wVs7IfX9YGaaVtsA+Og2g577WXpEHI7fK+FztNqNwfaTKFvWfWuE 1zH4QI/SVTFiLA++SvtDuCq53m71/XiySA0MTQl5Br+bzVFYR5f3q1iKbGfUbjJW3EgK rQDQ== X-Forwarded-Encrypted: i=1; AJvYcCX+BGyEa5l5JX+BJ86BARP9/2oJvlddLh/Tl+pP496aXDFw+iCKg6PMUrOQWE2gXAcYdtYEofpbbA==@kvack.org X-Gm-Message-State: AOJu0YwOoRlAAh3qsyAJU8B4VNoJ6OI5fgbHJBV/oLonpsW/ueXsLBBo f/RGqWoHuX4r8nHJ/yfUVy4kY3jDc47z13Rc0XpXlYTUHkwws6x8 X-Gm-Gg: ASbGnctlA8LXip+3kqQhtFWyoczClX9V+xrHaOHNMzHGX8pxGmAupCdQqkNYu7FFWps Q3YEXi115sXSMFcTiBEJfPywf0WlNaGPDuN92dnzuA6WEIAdU2vw0Z35sPS6L7ZiiUKfVmwOUFq 2Dg76dLSdIk+wdpFJvSXzAIlRNpYUH4dlTrhU4VlZCNE1AMZUwGrCkAnrjOldjcuPdiNiHQYjGk KLqcMr96n5pWkq8FEY/sxyRPBvxFxnkzWq1qaIvEgPdcG9/+10Lhi5H64vv2x4+xiYyBv190fjy LTdv4Bqb X-Google-Smtp-Source: AGHT+IG/cFjjNteRnggOPcJd7ml3IweEri0dZgwymeHUFWzv/5oMKi4cBo8NV/fu9YuNibkf/qV0PQ== X-Received: by 2002:a05:6a00:1942:b0:71e:6b8:2f4a with SMTP id d2e1a72fcca58-72d21f7bcccmr39594907b3a.12.1736907487230; Tue, 14 Jan 2025 18:18:07 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:4043]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d406a4dddsm8016029b3a.155.2025.01.14.18.18.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 14 Jan 2025 18:18:06 -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 v5 3/7] locking/local_lock: Introduce local_trylock_irqsave() Date: Tue, 14 Jan 2025 18:17:42 -0800 Message-Id: <20250115021746.34691-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250115021746.34691-1-alexei.starovoitov@gmail.com> References: <20250115021746.34691-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Stat-Signature: 5s7ekij1brbuq431pef9aj1nnz7n1js8 X-Rspamd-Queue-Id: 9F42BC0015 X-Rspam-User: X-HE-Tag: 1736907488-352266 X-HE-Meta: U2FsdGVkX19j8J1eFvpzD4RgYGCWA4tZQEzH5E/pMaAX+kMNsrgJIFxTASXWmXIc21zhRzM1vPD6DjzHKl0HTpS7YmLuEbFo9Cg979XUDQPW4IdqA18AsO3XHwwPoEqhKon1PL9K+LNIOJdVdCk9ZZntfwjlw9iOrE/3WX8P4qs//a3SBd2EUIZeRqIlbtNy3cbxdm1we/5tWbGmFgS07w9K65ZqvpXDpPnXzfYe7G7Inx0eFi+C6anxjVyGavh688j/yMx7TpLiW/XS7a5dF24SWpqP1VXypakh3GHgfnVvXoiUEz969FB5/ZyJa3XglyDNJYNN6RW51GjOFVp6CBUV8VhStOaKZEY/inxmZVbbS9lm/u+4O/nXn+cB2bj+Ihef7APxqYlxfW5bn8vJRXLKmxvzBX/ZfLQ6l4wxyTWZlAfrzZmJF3J6jcohUU2UeTvofUw4C1YWSevEPwsVvcLuzsEKZnOuB5muZMzOkoaHR3kYn1ifmzqxVMm3q5PvkQZA10NdVGXVJrsHs7xvlT1AjcIBrjyb0ycuiYdUeYko9S2GtnRL98GTsXHWnCs+GAoJpVq3J135pGBzi7kW5G7OzPn5NHzzDCcF67Bw0fQCkQSrlS6VCsY94AKJ536ISQNdHo2KzXRHlUGQLSeIYVtSfAftJNZ2V4sO8BK9iuxJ5F3JIbjbhidnXrUMJXay4cegBSS+FOX+kcHII1D7pnU36rcczsflpzEOF7HWLPQ1/3TrZg+a9mul4ntI9VYUOpjvEMv4uyt27r6juyc2K52sVZlNVYuKQ2iWgZxYtuk7HRKGj/ynygExf1fAormvZfztWV9wThjjFSyD/Sx/neAsmOQ3Z8F/UrlFcliscZztAe+GOSEZeZzkubfKG/UOKcr/4dmAiTZTL1etDQL9K39lVGIGVOEXNcFafDV25SULVcsxRS1EA6eMaKTsY38hbllANulRloztggmiYAv hRzNF9Nr 72jrrWWPrdcEBFXejbPub5wnNwzwxWGUR04Ppw0X2XkDfE8OIJ3L0KHtRkCBHQUY5O8sYm9zESBYq1bc/h1Esozid2F3T9zfUonuNy1+QTlJ6OuTSpSNNgXNmdOcx1fgo7YfNa8U/FtYVyWxQEoYY/BzIBqx965qgySwpHPM8F64iA4Jq4INEpvt5O1W9GTTkH9OHTyKkpsDdLYa4rXqLANPtBpiS7YXFveM9n+k2MLelyP7cO7vRHrHmIOX09bi+E2QKp5ttIQT2CadunsCzN3pgvJpskpI/c2SGVKmDtTZlNoVuk0GurzPXVg7igNS+u14zrZaRVm11cIxVFUCTtqHF8Yex5+SSP6uRQT+E5cYz2gzX3EPGxpK/E88C/UrCZZrXFLFeJfqeLZyR0uKQYU91MUI0W8cpzwDMC6Y2QVs3TZsQC/TGc2TsdDHJoNF573wsYvI2sITOrQxsQ54Cx/N+a9r+dFU8gMiuEhdtRxz8UQ+TFzEoa8YYSA== 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))); \