From patchwork Fri Feb 14 16:27:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13975224 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 1869EC021A4 for ; Fri, 14 Feb 2025 16:28:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D20B280001; Fri, 14 Feb 2025 11:27:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 95B7A6B008A; Fri, 14 Feb 2025 11:27:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7FA8A280001; Fri, 14 Feb 2025 11:27:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5F47A6B0089 for ; Fri, 14 Feb 2025 11:27:59 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0667146F18 for ; Fri, 14 Feb 2025 16:27:59 +0000 (UTC) X-FDA: 83119081878.20.BB38853 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf16.hostedemail.com (Postfix) with ESMTP id C6727180010 for ; Fri, 14 Feb 2025 16:27:56 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=FpUnZ8Jw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=FybShcH+; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=FpUnZ8Jw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=FybShcH+; spf=pass (imf16.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739550477; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GaCYe+OEwMyM/ciPR9cdhcr1JY5HT+u6CwMm73JcQ7Y=; b=DBr/ZXZOj+51MY50t+i8tq3tvkagKbK5lmS0K2Tir78bcywXlTaAee+nNbgUmmJjgmrWoE PfrLklbWhSYVUIs5tzq2I/vAMamftGcJdMJvZ/18xGQ6CYYe+/hY0v9GHNnaVMKCRabe96 yH1OafUgJPFzrBvSd3FgdjUIE2hfVPA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=FpUnZ8Jw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=FybShcH+; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=FpUnZ8Jw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=FybShcH+; spf=pass (imf16.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739550477; a=rsa-sha256; cv=none; b=QKBI+OVk/vESnEJ3IT8VU1aSefGlO+zQfXhHQxCawgArH6JfcNIzzHLsfNp4hvU/zVYdAZ LSjqj0r1P8sE96iE1I9JCbZJwZwP+qwRbMfoK02emeQH5YkL1MVNFvw9Xqve6rtq1yTr2c qQwQL2tKHTvPbL9zKTuRef1wOzidjyM= Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id B92C42117D; Fri, 14 Feb 2025 16:27:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1739550462; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GaCYe+OEwMyM/ciPR9cdhcr1JY5HT+u6CwMm73JcQ7Y=; b=FpUnZ8JwEuFAZsbEuF7Z15Lr/OVP5f2ut541wcsVGpno2oeEvbGcEQrP1ZFch3lFU2CKZN L0Dvd36wbSPnlnSWri3Wnkan1yOspbXOirKfsMLtGIurXlsidC8+D+CpG+4zBLF7skeEJk 7aEdxB0yipiR/E6/5tWe/nz3U+2S0RY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1739550462; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GaCYe+OEwMyM/ciPR9cdhcr1JY5HT+u6CwMm73JcQ7Y=; b=FybShcH+TSd/qpYk8NdgYVWZwt++GnRT4WuQEnujxAyxs2Yhru2NcJqhWpW/zbYAWCQRhm oyVUltV/HBnNk9DA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1739550462; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GaCYe+OEwMyM/ciPR9cdhcr1JY5HT+u6CwMm73JcQ7Y=; b=FpUnZ8JwEuFAZsbEuF7Z15Lr/OVP5f2ut541wcsVGpno2oeEvbGcEQrP1ZFch3lFU2CKZN L0Dvd36wbSPnlnSWri3Wnkan1yOspbXOirKfsMLtGIurXlsidC8+D+CpG+4zBLF7skeEJk 7aEdxB0yipiR/E6/5tWe/nz3U+2S0RY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1739550462; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GaCYe+OEwMyM/ciPR9cdhcr1JY5HT+u6CwMm73JcQ7Y=; b=FybShcH+TSd/qpYk8NdgYVWZwt++GnRT4WuQEnujxAyxs2Yhru2NcJqhWpW/zbYAWCQRhm oyVUltV/HBnNk9DA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A488913285; Fri, 14 Feb 2025 16:27:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id QBvhJ/5ur2eHSAAAD6G6ig (envelope-from ); Fri, 14 Feb 2025 16:27:42 +0000 From: Vlastimil Babka Date: Fri, 14 Feb 2025 17:27:40 +0100 Subject: [PATCH RFC v2 04/10] locking/local_lock: add localtry_trylock() MIME-Version: 1.0 Message-Id: <20250214-slub-percpu-caches-v2-4-88592ee0966a@suse.cz> References: <20250214-slub-percpu-caches-v2-0-88592ee0966a@suse.cz> In-Reply-To: <20250214-slub-percpu-caches-v2-0-88592ee0966a@suse.cz> To: Suren Baghdasaryan , "Liam R. Howlett" , Christoph Lameter , David Rientjes Cc: Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Uladzislau Rezki , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org, maple-tree@lists.infradead.org, Vlastimil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7voiiRCd1+8OVuMnByzQ/XxbOrc6CLsImmGj+jp73YkTFo nGByZegN s9zRL2MXzOfrmJWXkXdqTDmU2ZaGZagNG7uSjgZDTiR1AEHz0BUbYsR+7OKBlftguzfjFQCHiAB7saNpvZOlorq5d28aiWNAwDrPehw9opvo52nNWJCbXRgAIza+HcUbFtqBD6mn1RlIr1+psgwhUablcOLeli1fVRnxQnSzJy0qCKLIPpEjSgIcKwtuI9YrfbPrHsyPjisyo/cS6U9eK//ZXZXsi58JqmGk9vQb8H28R3sw8Vczwc/RCU0ySnzshCo6AAbK6M8keLsQsQJ3hk91bmnC/qXe9MZVJCc0QUPSC70ymkRM7iOHt6e+KQfF5sWFv4ggrufYiBS1Yn6tSbomx8FN5JDIMu5JTUcpm4Y/0lHWJl8K03tYF0Q== 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 a localtry_trylock() variant without _irqsave that will be used in slab sheaves implementation. Thanks to only disabling preemption and not irqs, it has a lower overhead. It's not necessary to disable irqs to avoid a deadlock if the irq context uses trylock and can handle failures. Also make the comment of localtry_trylock() more clear, and fix a compilation failure in localtry_lock_init(). Signed-off-by: Vlastimil Babka --- include/linux/local_lock.h | 13 ++++++++++++- include/linux/local_lock_internal.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h index 05c254a5d7d3e6db64d7f81a3a4a10f5a942c29e..1a0bc35839e360d5c8170105849c3883463852f8 100644 --- a/include/linux/local_lock.h +++ b/include/linux/local_lock.h @@ -77,6 +77,16 @@ #define localtry_lock_irqsave(lock, flags) \ __localtry_lock_irqsave(lock, flags) +/** + * localtry_trylock - Try to acquire a per CPU local lock. + * @lock: The lock variable + * + * The function can be used in any context such as NMI or HARDIRQ. Due to + * locking constrains it will _always_ fail to acquire the lock in NMI or + * HARDIRQ context on PREEMPT_RT. + */ +#define localtry_trylock(lock) __localtry_trylock(lock) + /** * localtry_trylock_irqsave - Try to acquire a per CPU local lock, save and disable * interrupts if acquired @@ -84,7 +94,8 @@ * @flags: Storage for interrupt flags * * The function can be used in any context such as NMI or HARDIRQ. Due to - * locking constrains it will _always_ fail to acquire the lock on PREEMPT_RT. + * locking constrains it will _always_ fail to acquire the lock in NMI or + * HARDIRQ context on PREEMPT_RT. */ #define localtry_trylock_irqsave(lock, flags) \ __localtry_trylock_irqsave(lock, flags) diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock_internal.h index c1369b300777d3ff3700cfd8bd4de8186124f036..67bd13d142fac39bc0f8b2c05eaf81717ff480f9 100644 --- a/include/linux/local_lock_internal.h +++ b/include/linux/local_lock_internal.h @@ -137,7 +137,7 @@ do { \ #define __localtry_lock_init(lock) \ do { \ __local_lock_init(&(lock)->llock); \ - WRITE_ONCE(&(lock)->acquired, 0); \ + WRITE_ONCE((lock)->acquired, 0); \ } while (0) #define __localtry_lock(lock) \ @@ -167,6 +167,24 @@ do { \ WRITE_ONCE(lt->acquired, 1); \ } while (0) +#define __localtry_trylock(lock) \ + ({ \ + localtry_lock_t *lt; \ + bool _ret; \ + \ + preempt_disable(); \ + lt = this_cpu_ptr(lock); \ + if (!READ_ONCE(lt->acquired)) { \ + WRITE_ONCE(lt->acquired, 1); \ + local_trylock_acquire(<->llock); \ + _ret = true; \ + } else { \ + _ret = false; \ + preempt_enable(); \ + } \ + _ret; \ + }) + #define __localtry_trylock_irqsave(lock, flags) \ ({ \ localtry_lock_t *lt; \ @@ -275,12 +293,10 @@ do { \ #define __localtry_unlock_irq(lock) __local_unlock(lock) #define __localtry_unlock_irqrestore(lock, flags) __local_unlock_irqrestore(lock, flags) -#define __localtry_trylock_irqsave(lock, flags) \ +#define __localtry_trylock(lock) \ ({ \ int __locked; \ \ - typecheck(unsigned long, flags); \ - flags = 0; \ if (in_nmi() | in_hardirq()) { \ __locked = 0; \ } else { \ @@ -292,4 +308,11 @@ do { \ __locked; \ }) +#define __localtry_trylock_irqsave(lock, flags) \ + ({ \ + typecheck(unsigned long, flags); \ + flags = 0; \ + __localtry_trylock(lock); \ + }) + #endif /* CONFIG_PREEMPT_RT */