From patchwork Tue Dec 10 02:39:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13900696 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 95F47E7717D for ; Tue, 10 Dec 2024 02:39:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EFDB6B00DE; Mon, 9 Dec 2024 21:39:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19F2A6B00E0; Mon, 9 Dec 2024 21:39:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F34216B00E1; Mon, 9 Dec 2024 21:39:57 -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 D4BD36B00DE for ; Mon, 9 Dec 2024 21:39:57 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 87775140213 for ; Tue, 10 Dec 2024 02:39:57 +0000 (UTC) X-FDA: 82877493636.13.6D369CF Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf08.hostedemail.com (Postfix) with ESMTP id 7B29E160008 for ; Tue, 10 Dec 2024 02:39:41 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gnKNNsH2; spf=pass (imf08.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.180 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=1733798386; 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=MT/DPo97yF9oIvPtZgU314ePao/rByOm5QF7hTFkWYs=; b=ZGHf5fv2i/JBsYoNPjWZCTTbNWzxuCL+OWXt5rowtkG/6I2+h2PKl4mFoyDkRUBYmQnpLT ojYN2yDRiFlKwrUrS2jIC1oxcdJVEjrqXbfIzojw+FeF/aoOcevwGNdBL8z1T/9KC8cH1e T/PbHSDHuw9Wg1Kf8lKHSgn7Q2Nsr7I= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gnKNNsH2; spf=pass (imf08.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.180 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=1733798386; a=rsa-sha256; cv=none; b=09Uavi999/beXjMJOVDIriXYK4a5xU2IEBVrIzfiVW7spSt6dw/YqZCkGXcSST95ug+1tO POkjHoDM9sbBxvj4NbgGcTcHJl2be5A2peZsaXyk2f2cRBTv2ge4BLJOkDTftwwBP+YbVF hPcERxpQ+9d+CTd3YQgPbRmHXx5p+UE= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-216426b0865so16316805ad.0 for ; Mon, 09 Dec 2024 18:39:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733798394; x=1734403194; 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=MT/DPo97yF9oIvPtZgU314ePao/rByOm5QF7hTFkWYs=; b=gnKNNsH2feIM9aqysUokJ9gPzyzb5q4xl6bom1UaD7obxp4N29nYnRYPs6LNuqnLhY imcfcTFdhev07ffXtNIS6wBf3KfyIb61QgmKEOv3Rd0CH2tzXMETovIAY5pSOrEtGD62 uSIdAKjFQHIh/85ycVr2o+JpYhuDNn5acz7tBej5509uQtk+S4ywWWWtnwdYFho0F+ZY qqRoU1Jn4lqmOqjzmJO7yHoSAK9FNS+go/ZyYDsOTgFvkaSLYAGcDWwDZsnMYPtfdf5Z 6UzUkKwYX/8H8hvchmF2nYkRgTZsXoCLWI7PmJOxG5b07x8lsivVcANn8Fx+J8dBUgwb fLbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733798394; x=1734403194; 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=MT/DPo97yF9oIvPtZgU314ePao/rByOm5QF7hTFkWYs=; b=JvLI212TtDvkz+fgxLSXNMfFiPjTfnWRd94+LqEJ31zSbk9qo0YSZdQ+QUnhyr8Txx NdVaG+QYr3nMWaHRroHM5tTFc4suEhzSHWiUW/3AavcxB5BHmk2mow9EReZrYhRa29Cu ZmvHhX3GduOfwAhRFEOVr/NRwWZyKRLVWN/VbEmJkQmZT7I1/f8iMHyNyJzf4Wm9YPro JNyiEG0F+LHro5Zwe3WPOctM+GxxOrpiX8umpo7DDyYORmVWnwBQeHlO4SNAY8oyQAhh e4gkuzU2UQ72cmHorlim3d2gRGs6fudUjHG79XO+0wkg32EbXJIDqH2ZOA1S18U5w8As tevQ== X-Forwarded-Encrypted: i=1; AJvYcCUKpumESqJZJHg0Exh/PHG/DMosLTkyKItbISJIzkFKusy4D+FdhkIC0NyNdmyksXrm7YuPpKm4Wg==@kvack.org X-Gm-Message-State: AOJu0YwZOpq+G8c2Qqij8PnoU3vT/5Mq0Q7TlTik5GKFC0/vVFLAmvjB cv9GVUSwOHX4lEsNtYhLKaNnkfVqg4EYEmH3wLdZNLC2gHsbmu8K X-Gm-Gg: ASbGnct8MWesd5uP5W4iIObN4BL6oMyif29BshrEhOhSXr4ElOmnGIT6aO3lfXzpT9N x9XdPc+5QmM9RjKbVaylQ7xSrO6WOcurirFfqrOctDQXj+/tSj4uCZt3NAIj9MfNfsj0DDKMaCZ zxIC5SLUnAq4t+ENFvWoDnksPXIRinNHB6ATF+NnhYhoCxKpnhaTAjGHUuQEwHiDDjbUdMu6OGp SlVbHyfCL/eoW9Cz3u3HrkWyukppSnbsBcz2WJq2d6xMlauiJYiyEf9Q2UiMbicIYPC7WNJh8Pk I0fOvQ== X-Google-Smtp-Source: AGHT+IGKLmePoGcXe+HAKRjlAgf3YSGz9J3cPaKQKdSbicAELPOmItuHpzzqliawMg0yEsYXj3ExxQ== X-Received: by 2002:a17:902:ecc5:b0:215:4fbf:11da with SMTP id d9443c01a7336-21669fc28d6mr43610845ad.21.1733798394490; Mon, 09 Dec 2024 18:39:54 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:83b0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-215f8e5f031sm79221425ad.82.2024.12.09.18.39.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Dec 2024 18:39:54 -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, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v2 3/6] locking/local_lock: Introduce local_trylock_irqsave() Date: Mon, 9 Dec 2024 18:39:33 -0800 Message-Id: <20241210023936.46871-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20241210023936.46871-1-alexei.starovoitov@gmail.com> References: <20241210023936.46871-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7B29E160008 X-Stat-Signature: 59d4aup5s7zicbmrua7y81wz6c7sy4uw X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1733798381-999040 X-HE-Meta: U2FsdGVkX18vz2qQeWW00DnBAGpksClVaxq1Bwz7a9wYnQdGyia2PFKg98pq/qriqQz9+xbk8UiSY/p7MEFmHplgX+LknPpVv7i2dlAri6kql/q9koaiPMJ1axY7MB67ZNUaO3783FG/f+xcpXa85CX4YmPJRQyspJQwLQtC9BYM85rCVjAQtdKYAEn8eq1x3BmXcW0wqZ/7fQmRpJpL01WxAEQt3Fyc02CKz+RfxPxfJx28qrFddtN/hdM875Mx8z1zroYNlcTM1yVp47O7DnSLDLPrrEnfmHh6FHgNN/2tZPfRPsGvDxFoYtJS2lQEd0DLD0h6623nYkON4ZgNebEHLkL98Iq555oD6dvQff7nux499bFGa3364BNC2WSkDFD3esfiS5UHW2I0swMgU9f4T9eYoaFBVzWdfzecjoukmwl+3mgC+pyrftid7NxXVS+Dw7DrHZyIeY4Jc92oQbGWnnvrzKgCNsrFMwoeFW3Gk9aYvzMV8d3lJWkl4LEs8u/CL3GthDhvUTFWrZEzsWm26cCQwe4fO9DTyNzyVqB+UqyEE/KL7P2SWNJLTlMI+iNsK9Fl9BGA/16dlT2PeofaSZwJTbH0k/DKPe4qH5LkMfW/rKneXI2TxVJPLjyooxKSriXXhskbntPyNfAiSRHQAjfF8pNg0elKE8CWoxzMMnn5j3/gMUWVhGfap3V2afZoRaaRAg8OteoVIuiw7uyxwlUIccsJdPenvKsX4Y83sElKJU5+BbXa9bdSgAQF4+hZw3HTzrilWPo/FMngv+PS7F2c0Nlq+5Upc61Z2+XrJ71vndGxAxcsdptibiSe2ZmTh7UeLWK7Q2M0iD8s9WmuKmX+HIjJi6CKAWhodhp1zGOyvVeN4HukUd2J4eRiywkjjswhP+CYoFSfPU3WZVQWe/XbwD8D3kIGtfm6awDlpr+FkepOkyjtUlDx+7B1Fe+iTi2NEr4/1rHer24 JCO4id3j /VZyRanRdpy/8rhlAQx94E3M3nPKV0srfJApZLhs3BMPBUcZhLmPKqZ2jvjA/nMGFqQIjTzsZEMMlze/hhpNJ8dqxI1xWW7rH7Su6dVAIt8MsekbtgvqOfCwRzKk0WO5zcxDw+J3uk3Q9FgqoZH4syjg/adS+5lljrDTEiyfO7aBGkH5l07bL/dm0dl0kY/a9M/1fg1AfvXUDf4uY51KEWJfVKXRO6xkwdtFTTDJsHd9d8eF9WiBXNpHZUn+ytKxN0wN9g3oRG7vKB/mJDy7r9YhlFW9Y0hC+5WSuB0Om0HNWR40D9bjVI8S3+oNDx0PywcT7IWkPr9CwRiRRVlSKr+7u2dXpP+2F/IgXq6wz0roohxGLUZ8pxi0+UkF2eGJzrsli66FVGkmighpQV6JEQSaDc9VEc1eTiKTT2vfkjOonrX4MMm2TSLbUqimsS8zLdCjLiAr7lPVK9yzaqcAOMsioKnFe1VI9sTPAnFBNihfi52POl7fkbY+A/A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.064903, 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(). It uses spin_trylock in PREEMPT_RT and always succeeds when !RT. Signed-off-by: Alexei Starovoitov --- include/linux/local_lock.h | 9 +++++++++ include/linux/local_lock_internal.h | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h index 091dc0b6bdfb..6880c29b8b98 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 succeeds in !PREEMPT_RT. + * @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..2c0f8a49c2d0 100644 --- a/include/linux/local_lock_internal.h +++ b/include/linux/local_lock_internal.h @@ -31,6 +31,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 +52,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 */ @@ -91,6 +99,13 @@ do { \ local_lock_acquire(this_cpu_ptr(lock)); \ } while (0) +#define __local_trylock_irqsave(lock, flags) \ + ({ \ + local_irq_save(flags); \ + local_trylock_acquire(this_cpu_ptr(lock)); \ + 1; \ + }) + #define __local_unlock(lock) \ do { \ local_lock_release(this_cpu_ptr(lock)); \ @@ -148,6 +163,14 @@ typedef spinlock_t local_lock_t; __local_lock(lock); \ } while (0) +#define __local_trylock_irqsave(lock, flags) \ + ({ \ + typecheck(unsigned long, flags); \ + flags = 0; \ + migrate_disable(); \ + spin_trylock(this_cpu_ptr((__lock))); \ + }) + #define __local_unlock(__lock) \ do { \ spin_unlock(this_cpu_ptr((__lock))); \