Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1572719rwr; Fri, 5 May 2023 16:51:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Xc2fw6d8/NNSdJU17F/y6myHstw1IsKLo/VcwxzONaU1zXxtdgXNdMop2HGrt8cgyjxQI X-Received: by 2002:a05:6a20:3d94:b0:d9:6660:8746 with SMTP id s20-20020a056a203d9400b000d966608746mr5280697pzi.18.1683330695496; Fri, 05 May 2023 16:51:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683330695; cv=none; d=google.com; s=arc-20160816; b=otH8Y/Mw43950O3cYFagbNmcHjspdZhXuHUKwgerCNQgGENeBKWOe51R6E2B3tAidp fopt2Mus24wN4CwyLaWRwCLsVU4UREkTk3S+BzgeQBr3q39GMZB6uxM67a2zk3oGY2aq 6B1JodfKkz4/TEMl8H049b9DKD7IOO1Qbwfb0wi5q7M7eG3om2dMJwHcHL+ksB7EMw4B B5DTJICqEoeCxTdz0bxxhq8zSoXw+v6bBgB/uTFWNyw+Vk0rCthMzh+03FUhzy/8nkUW BVG04RrxzsWqcmzXZASdHKFBS1Rj1svGir6CfHqZt+4rd0edmng/3XTyufMV71aFNQOs J+wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=6sxb0Huk9DeXydwe6cpIDkL7SV2wlpTgDqHSYs1vaAs=; b=IxmVeMOJPtqgeJ3351ESczrkdHoyUkVRnUEgA0D0M5a9hG/QtAfI0PMgCNSP4y3mn0 XhuGON57XkfxmV29N4lX25roIZY2xTtttLpTrX2HGNld0MyGJAwfwKsDIlVOyLdRiNYO rmfyLVy0MMAANgAS8jyyC1EVj/VyGnAbU2/BKuzItjsV0xKR/EXx23o1hAnb4EXDNys3 pToZROCbaGomsID3OjTyfgKDtVMAvW7NN2kGTccDyVgrwqEk3WaaAP+qHYNHtE6M7CTg aXHnDxMqg4ozVdJI5lvSfXw6ahenholL/SK6UognUU8JIsGVHeZj2Hqo+5q7K9fJIT1J RR+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wXfT696K; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=47+4+aKd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 82-20020a630055000000b0052c6564773esi3190069pga.622.2023.05.05.16.51.21; Fri, 05 May 2023 16:51:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wXfT696K; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=47+4+aKd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232277AbjEEW7G (ORCPT + 99 others); Fri, 5 May 2023 18:59:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230471AbjEEW7F (ORCPT ); Fri, 5 May 2023 18:59:05 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 720B1618B for ; Fri, 5 May 2023 15:59:01 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1683327536; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6sxb0Huk9DeXydwe6cpIDkL7SV2wlpTgDqHSYs1vaAs=; b=wXfT696KaUYaGEuot+1Gy+WTVD+vu5j5EFC2LDzX+0ILhP/GjZeAhRSNqHPhj7V735MCNF TNSaKjBop7cinpFB+Lq8P90+v37I/HD8LBqz0U0ehJouMf2x2FLx5L/mmZd1oW3Ub1okkr J1yeIfb+uZautH79EEFukMh6jn1nSo6hXX7utU+HM0T+/+hIGp1es/KnnTYbmYCdhpTuwu s6+OtQdXemng/eqJw7gIHF7JqM3pBZhtc5bOUFzejmc4FjnOP9oAG8kJOi3nukiceoHL9A FK4y/JSajK37lhsSQFxYJDtIwQuElg3V4kIKEJ4ZdpnTE04aarNabweHtfuetw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1683327536; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6sxb0Huk9DeXydwe6cpIDkL7SV2wlpTgDqHSYs1vaAs=; b=47+4+aKd4VWzrC4aVBxdK1WldAEyyCODSxsUrh5aqMLO9HvHrV2tgM9Vy+XeoGmBGbCi7a 4XQvR+P3vBB4MADg== To: Frederic Weisbecker Cc: LKML , Anna-Maria Behnsen , Peter Zijlstra , syzbot+5c54bd3eb218bb595aa9@syzkaller.appspotmail.com, Dmitry Vyukov , Sebastian Siewior , Michael Kerrisk Subject: Re: [patch 02/20] posix-timers: Ensure timer ID search-loop limit is valid In-Reply-To: References: <20230425181827.219128101@linutronix.de> <20230425183312.932345089@linutronix.de> Date: Sat, 06 May 2023 00:58:56 +0200 Message-ID: <87zg6i2xn3.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 05 2023 at 16:50, Frederic Weisbecker wrote: > On Tue, Apr 25, 2023 at 08:48:58PM +0200, Thomas Gleixner wrote: >> >> - do { >> + /* Can be written by a different task concurrently in the loop below */ >> + start = READ_ONCE(sig->next_posix_timer_id); >> + >> + for (id = ~start; start != id; id++) { >> spin_lock(&hash_lock); >> - head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)]; >> - if (!__posix_timers_find(head, sig, sig->posix_timer_id)) { >> + id = sig->next_posix_timer_id; >> + >> + /* Write the next ID back. Clamp it to the positive space */ >> + WRITE_ONCE(sig->next_posix_timer_id, (id + 1) & INT_MAX); > > Isn't that looping forever? No. The loop breaks when @id reaches the locklessly read out @start value again. I admit that the 'id = ~start' part in the for() expression is confusing without a comment. That initial @id value is in the invalid space to make sure that the termination condition 'start != id' does not trigger right away. But that value gets immediately overwritten after acquiring hash_lock by the real sig->next_posix_timer_id value. The clamp to the positive space has nothing to do with that. That's required because the ID must be positive as a negative value would be an error when returned, right? So the whole thing works like this: start = READ_LOCKLESS(sig->next_id); // Enfore that id and start are different to not terminate right away id = ~start; loop: if (id == start) goto fail; lock() id = sig->next_id; <-- stable readout sig->next_id = (id + 1) & INT_MAX; <-- prevent going negative if (unused_id(id)) { add_timer_to_hash(timer, id); unlock(); return id; } id++; unlock(); goto loop; As the initial lockless readout is guaranteed to be in the positive space, how is that supposed to be looping forever? Admittedly this can be written less obscure, but not tonight :) Thanks, tglx