From patchwork Sun Jun 18 20:50:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 109675 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2636571vqr; Sun, 18 Jun 2023 13:58:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7rudjVq6kWILtobrZqNgA7N59YF1rs8EGguZ8Yww19M8vOYTV8BMeZejGfzUl9T6YTbPir X-Received: by 2002:a37:ad0d:0:b0:75e:2a27:2532 with SMTP id f13-20020a37ad0d000000b0075e2a272532mr7542877qkm.57.1687121927459; Sun, 18 Jun 2023 13:58:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687121927; cv=none; d=google.com; s=arc-20160816; b=eY0po4FJegVYzDNN01fz2cNJUeRYN+1z91OTEUqyu0W3XsPyw1dEjxpRQJP+lbUmpe 9DIG9qCCXkG1hw4QhlC+Kcs+hEkujRcghYWVFYni7SMFJ9M0U4gMatoM+7sSYLsnw4Uj p+eT+y9zn1/UTobFFT0GfzEzctiCCE0GzhkpTZmpAiPUZyUw9LwcPQTx35hi3ULFeBaf BntOHqNW+RnWTFpqWa3xe2WmyXq8qi3Odwb/z9F36EW8fzAFX1I6rSYzgnvFEMO+OImq ZEzIo6KUhnZAgA6iZJAeImMYnubDqiBUhzz5ftHQOMWHaRITM6/jrpNjyRsRDA8EbCQs MjNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=LVSIjk+yc6ILF9K42y1xy8dftnlzkecrXrQkd8YW1eY=; b=ZS2mZ6kD7n3k57hZaX+TNMdPofkzuj9u7wf2TJVZk2gpXswrMX7v/GG/pEGiZB0s2F TmOJwNY1T0+fXBZe5FF8qm/sxFMQhzcjr5qHqxKOiJnQeRf44ZNQErhUd9EYcpCc7Ma2 cq3GIh+R9i6a8QKmovc9S9NKNr+6FTDWJRKucNTld5DBG9Xp6HWz+RtNuht+PcaTtZfo 9wuG3Ao0RdxmoLYH+oXE6jcuS3Ep58HNrho/3Wz7Q8UkMWTZ6uDM8pSytRI51MsG+Ru/ ZdBDa8d4T0hDiKFrVSjpLzWg6BwRi4xk2whrfjH7QgM2K65Pu4a9apGBts8CzJdtXn8l 3GJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=H8kmFWiJ; dkim=neutral (no key) header.i=@linutronix.de; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j16-20020a170902759000b001b5553bf6aesi2273331pll.78.2023.06.18.13.58.35; Sun, 18 Jun 2023 13:58:47 -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=H8kmFWiJ; dkim=neutral (no key) header.i=@linutronix.de; 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 S229823AbjFRUvD (ORCPT + 99 others); Sun, 18 Jun 2023 16:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229659AbjFRUuK (ORCPT ); Sun, 18 Jun 2023 16:50:10 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB33AE49; Sun, 18 Jun 2023 13:50:08 -0700 (PDT) Date: Sun, 18 Jun 2023 20:50:06 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1687121407; h=from:from:sender:sender:reply-to: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; bh=LVSIjk+yc6ILF9K42y1xy8dftnlzkecrXrQkd8YW1eY=; b=H8kmFWiJMpxJwJ0SAfNQcvCxBgIHjNvbObLfOxpKonZMXi9R0hKxSYWxHp2f6bRW4KcATv DqAhjKlFrGflnSZ2a1oXO8TpXKer78jkn1R/hQxwjKn3tfDGFFhoUWczHOX57SA32L+kEv vUsnydk69/y9Qmnj5uk+u/Qm8XClKXpweMfhhqrCSFk0e88Zb6pivUVey3jNa3suk5M9lP miuXtaXxc7wuODuQRxIj7Pv3FVs9A1kk4JmA9YhvU3IkNd961HpCT545fb0hscyk/2Ex+a mT+L7rRQMPbGzBgi/n5G/PWVYIy7yUjvt8z1WZ+dRQnRCTXnNzOZKP/ovvEXWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1687121407; h=from:from:sender:sender:reply-to: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; bh=LVSIjk+yc6ILF9K42y1xy8dftnlzkecrXrQkd8YW1eY=; b=OvWLZeqo4kI6CgJpkcWB+vmfmc8XEoaeL67KHZ/luGO3SurGzqbLIVNqitVdIMcId7OuqT xJg1ogdI1RTXL6Aw== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] posix-timers: Add comments about timer lookup Cc: Thomas Gleixner , Frederic Weisbecker , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230425183313.091081515@linutronix.de> References: <20230425183313.091081515@linutronix.de> MIME-Version: 1.0 Message-ID: <168712140656.404.4260184240973018058.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764175265642152195?= X-GMAIL-MSGID: =?utf-8?q?1769075562432602435?= The following commit has been merged into the timers/core branch of tip: Commit-ID: ae88967d71f1b4ffb6e48043993d37a106da8109 Gitweb: https://git.kernel.org/tip/ae88967d71f1b4ffb6e48043993d37a106da8109 Author: Thomas Gleixner AuthorDate: Tue, 25 Apr 2023 20:49:03 +02:00 Committer: Thomas Gleixner CommitterDate: Sun, 18 Jun 2023 22:41:49 +02:00 posix-timers: Add comments about timer lookup Document how the timer ID validation in the hash table works. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Link: https://lore.kernel.org/r/20230425183313.091081515@linutronix.de --- kernel/time/posix-timers.c | 39 ++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 79909a2..d7890ac 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -506,6 +506,12 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event, return -EAGAIN; spin_lock_init(&new_timer->it_lock); + + /* + * Add the timer to the hash table. The timer is not yet valid + * because new_timer::it_signal is still NULL. The timer id is also + * not yet visible to user space. + */ new_timer_id = posix_timer_add(new_timer); if (new_timer_id < 0) { error = new_timer_id; @@ -551,6 +557,7 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event, goto out; spin_lock_irq(¤t->sighand->siglock); + /* This makes the timer valid in the hash table */ new_timer->it_signal = current->signal; list_add(&new_timer->list, ¤t->signal->posix_timers); spin_unlock_irq(¤t->sighand->siglock); @@ -597,13 +604,6 @@ COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock, } #endif -/* - * Locking issues: We need to protect the result of the id look up until - * we get the timer locked down so it is not deleted under us. The - * removal is done under the idr spinlock so we use that here to bridge - * the find to the timer lock. To avoid a dead lock, the timer id MUST - * be release with out holding the timer lock. - */ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) { struct k_itimer *timr; @@ -615,10 +615,35 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) if ((unsigned long long)timer_id > INT_MAX) return NULL; + /* + * The hash lookup and the timers are RCU protected. + * + * Timers are added to the hash in invalid state where + * timr::it_signal == NULL. timer::it_signal is only set after the + * rest of the initialization succeeded. + * + * Timer destruction happens in steps: + * 1) Set timr::it_signal to NULL with timr::it_lock held + * 2) Release timr::it_lock + * 3) Remove from the hash under hash_lock + * 4) Call RCU for removal after the grace period + * + * Holding rcu_read_lock() accross the lookup ensures that + * the timer cannot be freed. + * + * The lookup validates locklessly that timr::it_signal == + * current::it_signal and timr::it_id == @timer_id. timr::it_id + * can't change, but timr::it_signal becomes NULL during + * destruction. + */ rcu_read_lock(); timr = posix_timer_by_id(timer_id); if (timr) { spin_lock_irqsave(&timr->it_lock, *flags); + /* + * Validate under timr::it_lock that timr::it_signal is + * still valid. Pairs with #1 above. + */ if (timr->it_signal == current->signal) { rcu_read_unlock(); return timr;