From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from YT3PR01CU008.outbound.protection.outlook.com (mail-canadacentralazon11020141.outbound.protection.outlook.com [52.101.189.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AF1833D4FB for ; Thu, 9 Jul 2026 23:22:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.189.141 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783639346; cv=fail; b=m1TTVsEfKFLMyP5wEYJ34T4lfwlBzCC3PW7WzpsTFs2tJGLakoKmclO/o3YDNbyiTWhG24uo87gCPLxvfsf2XxwqDkPPv2wn7DYmDq3CU3zlgDHDOHICkVBQfMD9ndtuDxUaONfTRcn+OhQHRJbuiQEOJjj+SHHpYAEuQt7Q8eQ= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783639346; c=relaxed/simple; bh=Xc7FSNLYIjYHHo7vhSZkHxdSz7NmNvZo/VfSWeNEef8=; h=Message-ID:Date:Subject:To:Cc:References:From:In-Reply-To: Content-Type:MIME-Version; b=QNiS4AOW7wNr2eBxWh+UwlecKfzwpMjDOSioExxSLg2xO6xIfll7h/D/4hRw1GpEfh2MVkr+lMx+SltogxqI/gn7X2FycJmw3jGoPs3smEE035vGtztm8+5dSon+51wVp9G6QgBI1aps8PBmzsI7l4XoUfrqMnKGZs51hDV7/gI= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b=bWNH/npV; arc=fail smtp.client-ip=52.101.189.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b="bWNH/npV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Tzx1AJovGXy5Muhb85vpsfcOR3+v1Du8dXZAJC6wFFGGzWSXPABjxdBJxPWyKEuzU/u8f03dcdjh9juz8pjV4Kj9VPr1wqaeFpiXzRwPapJuYq3hUxM/LNMjX1xHuKn5Wg3eNPQczSAC0E8y4p8sGP0Tzh2D+hY95uMDkEa+OaJnTFdyLW3JX7iIV+HM42az4PJ3cdV1Y1x0shX+0mfcnASbdRde6fhbLjzKQSPrw8kgdFMtBq98Jic2gMcaLGd07QJcoxhdIPpf1yVBilQjx3IjcNAHGhTnbqkKXK/06JXduR4uHQc+1rvtmVEm0GVGr531vQxX5sv9MLV2BPtimw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=531AgE9OwWrCQRSWBNR3agGvOrzN3W34NaaN/qFkxnE=; b=gqyVZxRV3tmvWG+OokVnu/oMpbGDLcIihmVtYwKPx5IuDYWKUIH6EtDzC+cl867SgwEgrPPt5/8gAtu76tx36r4fe+3qLQdGUoMQg+dEURESk0Cza2IdwIM76MRbgE/x18tf90w41BUqjp2xcXL0ctB+bkg8qwhNDnkkTq1y1IYjrFCTyFtoy9giX1wxS0ffAkE4XYM7cg2XO72zXBjn6HiQlllgcXHXrw9hQe4Xv1I/h43MM9zGcnPBz1yrytgU4WSV/ahSKsZbmyIf0vEpjKD0bYlYHocmMTjtf61+E8JzkNJHcvfJgwhsi5e8na+wgWlfVuld/I9CJSqRABzaig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=efficios.com; dmarc=pass action=none header.from=efficios.com; dkim=pass header.d=efficios.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=531AgE9OwWrCQRSWBNR3agGvOrzN3W34NaaN/qFkxnE=; b=bWNH/npV08xFIvqbRVq+0uQdYunADSuOdWYZKK2z+gRl+Iei1ocU8MWczUXbiq3kdW2fmi8ej/8ET3t5acL/zIEodhDOuqNWMRV+Au3iClJ/u5bn2cjjhZhY7z/5xJ2pb6JHyvIabNYJE2PDFxih/07xyrAB9iDoX5Z+nWdIakdEgNBRhWtFGRb39514+SpXiGc1Ip88M0q5RPeZFYVYaHJUsogdFvkOljl2xde56FKtS3b40clp/SvxMKk63y6eFghj7MZMFj3KDdHBziglZp7nKtXb5jCNQC1Lo9l9bhxVOKflj9gVwcB+ZED2uiAhZtWTxrIqzNOR005vF1Ji6g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=efficios.com; Received: from YT2PR01MB9175.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:be::5) by YT1PPF626F6579C.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b08::541) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.18; Thu, 9 Jul 2026 23:22:20 +0000 Received: from YT2PR01MB9175.CANPRD01.PROD.OUTLOOK.COM ([fe80::6004:a862:d45d:90c1]) by YT2PR01MB9175.CANPRD01.PROD.OUTLOOK.COM ([fe80::6004:a862:d45d:90c1%4]) with mapi id 15.21.0181.017; Thu, 9 Jul 2026 23:22:20 +0000 Message-ID: <2d801522-6a77-47ad-8daf-d23cc85cbb7a@efficios.com> Date: Thu, 9 Jul 2026 19:22:18 -0400 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] hazptr: Introduce CONFIG_HAZPTR_DEBUG misuse detection To: paulmck@kernel.org Cc: Boqun Feng , linux-kernel@vger.kernel.org References: <20260709183009.6814-1-mathieu.desnoyers@efficios.com> <20260709183009.6814-2-mathieu.desnoyers@efficios.com> <2402b90a-7f26-4ef8-b5ff-b3c92d1be9d0@paulmck-laptop> <55079bd3-b049-4e0f-9225-ad41e3562500@efficios.com> <222ca62f-a39f-4b6e-b5b4-7e40bb5ceb36@paulmck-laptop> <2c7f97fa-8429-4fbb-bb33-53e015629f01@paulmck-laptop> From: Mathieu Desnoyers Content-Language: en-US In-Reply-To: <2c7f97fa-8429-4fbb-bb33-53e015629f01@paulmck-laptop> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: YT4PR01CA0022.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d1::25) To YT2PR01MB9175.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:be::5) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: YT2PR01MB9175:EE_|YT1PPF626F6579C:EE_ X-MS-Office365-Filtering-Correlation-Id: dfab31de-1c06-4201-9cc0-08dede10ec41 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|10070799003|376014|23010399003|18002099003|22082099003|5023799004|4143699003|56012099006; X-Microsoft-Antispam-Message-Info: G9AsoP4xWkS10aypaF7eYFI+aGMEmPkgYURJxY6D89N6+2GPzgTXQ7d1q6WH06Rad8wQKPvN8SH9yPTf1JCF/CKnBt96p6Ruhn1JkjN8GMcH3GRJUxvhzf2dgS2u5UYfIAuUSYx9QfdYmB8YywsKuCJ3T2RLuLi/JhaXeRHj4lej3ys2/rSxkhd5vmK32RIbKdk0SOOJB9Hrn9mGDicVzyBfZx7GHq+7AkSB/FprqedYZxyyHrNV6QAM1t0bKFURIG4pYsWlT8sD9T4tpJQft2eEdPxFn00B6jxpjfdYaMJjRMEJ7gs0WIyyWPGskRZm5I6g6tXRe480kwTVQuayFntnIX25HoKVvOZ8R/+NWwmdSKKYAejqx8Fz2LKFbRJ4tgLcZLpZq0FXc/UvVH1hajtHaSvikQ3f1Gle7Q2Zh5uqEIeB4FHazlOv33UB5XFEFoJ7FaPjAGkz2/DPg7oGjAu4MznWBaLlJ4rXMgzZ6skdZhLSxy9mJm98W7ofCmcIoPDMLWnbFcD3ZnYqCgwHAeBxBzpSWNpLVKfVyC9KCrtQc9H+ovvgEif9i/kKRQ9f+7EbCqOaOb+m8uhJ+jsoDksZ2nVELh9GOYayaZjMVyY= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:YT2PR01MB9175.CANPRD01.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(10070799003)(376014)(23010399003)(18002099003)(22082099003)(5023799004)(4143699003)(56012099006);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?dXpHRkVuSmt0ZUNpV2pGSDRzeSszM2M2ZFJFU0pyeEsyOEdNeVlLUHZtVWRm?= =?utf-8?B?MDRXajNjRlY3N1ZoV0k5V1pKTjloVThIeFJoanBHMy80Y1plQlh0OHZSQkVs?= =?utf-8?B?SXBvcDZhWU5VZ0FlVEpQdElLRlJ1b0M0V09GTStjQkZxMFAzb201MFp6OGdU?= =?utf-8?B?ajZtOEFZVitDN1lTMDlZaXc0S1JhR2g1L0ExT2ZZR1dkbG1UcUhCVGU5azF0?= =?utf-8?B?VVJaMVN0ZitwSWJFWER1ZVUzSjhhRDJZQTNkRUlhL0IzNTlIQUtlS2hSOFdy?= =?utf-8?B?aGFtK2YzVkp2TjcySDVGQVhVdllEbW9Xc3lzNWhSY0I3TElC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lpVWplUnJUQ0NNSzdEdVNjeGpsR0xuTkp0S0xnTE1DODVabjB1?= =?utf-8?B?YjA2SzYyT0tPYmNhVzU2QU9WcUVvSG1CQ3JFUVpvY0c0SHZIZ3BkWG1wa0cy?= =?utf-8?B?N2VWMXFidXgwZlFITFdUMVd6M2lyL0tRRXlOWVZNQk1HelFZOXRXNjNCZDZo?= =?utf-8?B?Mm9oeXFsT09QNWN0czZzWDhwd1pkODBWdXhaaXA5UW9DRkhHUDkxUDlNaHRV?= =?utf-8?Q?8laRRxLDAFSYyK9bnJRRFdKeW9p6bwHxcqzB2fJcdsQxH?= X-MS-Exchange-AntiSpam-MessageData-1: onOWVEXBWgiRnLjBzF9XygIwJ9UBhlqZG0G4qKuAA1SothRSpDr9y2US X-OriginatorOrg: efficios.com X-MS-Exchange-CrossTenant-Network-Message-Id: dfab31de-1c06-4201-9cc0-08dede10ec41 X-MS-Exchange-CrossTenant-AuthSource: YT2PR01MB9175.CANPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jul 2026 23:22:20.1589 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4f278736-4ab6-415c-957e-1f55336bd31e X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QeDDyO8fqNAvu+nCrhq3aU4wcHOGuPASzZ8XKKBW6jQC62jbBWZWsmWOx1WJiORXrlG3oZjS+MokuQyt7YV0VbbyMEvkaup7Nr+GoILngPE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: YT1PPF626F6579C On 2026-07-09 19:05, Paul E. McKenney wrote: > On Thu, Jul 09, 2026 at 05:47:00PM -0400, Mathieu Desnoyers wrote: [...] >>> + */ >>> static inline >>> void hazptr_detach_from_task(struct hazptr_ctx *ctx) >>> { >>> @@ -160,17 +178,29 @@ void hazptr_note_context_switch(void) >>> } >>> } >>> -/* >>> - * hazptr_acquire: Load pointer at address and protect with hazard pointer. >>> +/** >>> + * hazptr_acquire - Load pointer at address and protect with hazard pointer. >>> + * >>> + * @ctx: The hazard-pointer context to be passed to hazptr_release(). >>> + * @addr_p: Pointer to the pointer that is to be hazard-pointer protected. >>> * >>> * Load @addr_p, and protect the loaded pointer with hazard pointer. >>> - * When using hazptr_acquire from interrupt handlers, the acquired slots >>> - * need to be released before returning from the interrupt handler. >> >> I see that you removed wording of a major constraint here which allowed >> use of hazptr locally in a interrupt handler: the need to pair the >> acquire/release within the handler. > > I did indeed remove that wording. You could do something like this: > > Task Context IRQ Handler Interrupts Task > ------------ --------------------------- > preempt_disable(); > ihp = __this_cpu_read(irq_hc); ihp = __this_cpu_read(irq_hc); > p = hazptr_acquire(ihp, &gp); > lp = xchg(p, NULL); > if (lp) { > do_something(lp); > hazptr_release(ihp, lp); > } > preempt_enable(); > > If I understand the rules correctly (ha!), this is perfectly legal > and does not require a hazptr_detach_from_task(). > I am concerned about it, because I knowingly just used preempt disable to protect hazptr_acquire from the scheduler, but not from interrupt handlers, because it's faster than irqoff. I am concerned that this use of hazptr_acquire could confuse the thread-level hazptr_acquire, let's dig: void *hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p) { struct hazptr_percpu_slots *percpu_slots; struct hazptr_slot_item *slot_item; struct hazptr_slot *slot; void *addr; guard(preempt)(); percpu_slots = this_cpu_ptr(&hazptr_percpu_slots); slot_item = &percpu_slots->items[0]; slot = &slot_item->slot; [...] from here -------------------- if (unlikely(slot->addr)) return __hazptr_acquire(ctx, addr_p); WRITE_ONCE(slot->addr, HAZPTR_WILDCARD); /* Store B */ /* Memory ordering: Store B before Load A. */ smp_mb(); /* * Load @addr_p after storing wildcard to the hazard pointer slot. */ addr = READ_ONCE(*addr_p); /* Load A */ /* * We don't care about ordering of Store C. It will simply * replace the wildcard by a more specific address. If addr is * NULL, we simply store NULL into the slot. */ WRITE_ONCE(slot->addr, addr); /* Store C */ to here ---------------------------- I designed hazptr_acquire so a _nested_ interrupt which _brings back_ the slot addr to its original state will work. Now let's see if that's still OK if the interrupt handler leaves the slot->addr populated with an address. And no. If the interrupt happens right before "Store B", its reserved slot address is overwritten by the thread. That's incorrect. So as it is today, the irq handler needs to vacate the slot before it returns, either through a hazptr release or a detach. That being said, this is the code as it is today. If someone finds a clever way to support this use-case and keep it fast and not too complex, I'm all ears! :) One possible way to make it work would be to install the HAZPTR_WILDCARD with a local-cmpxchg expecting a NULL slot->addr. This would close this race window. But it comes at a non-null overhead price. Another alternative on x86 would be to use a lock prefixed cmpxchg, which has an implied smp_mb on success, which may be in the same ballpark as the sequence of WRITE_ONCE+ explicit smp_mb(). Thanks, Mathieu -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com