Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7078634rwr; Wed, 10 May 2023 03:41:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ51Rjhf0IfS4BmregRsyQ+QOfxfeStzDhtjgQMqTzb+lI/vJNEeq6CiKtRB7PW1YoHBz3md X-Received: by 2002:a17:903:1105:b0:1ac:47fe:888 with SMTP id n5-20020a170903110500b001ac47fe0888mr22352899plh.28.1683715296524; Wed, 10 May 2023 03:41:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683715296; cv=none; d=google.com; s=arc-20160816; b=P8/LM7FUZYj8yvRmT32jWjIw8kg/g9oV9ljgKLs5aa1InJs9/D7JZi6jzpuXOHNhP2 op6caPq/oRdbAYjofQmCqzHCTnRmy+jDa+262t5XgcEDeGGSf/DjxiOlyT+XgzWwmd5b X28MDjeOLV0r+WN+3BJZnuW2eF6N4oIliOm63guW4kVo1LeVxx2HNH9WA6Q+KRrOtu0E zwEdQWp5rqT9WBF4prmSrq4GRt36kqeeKqvbk0tlS9GRkbf+fypUaUZ/8zWiK5r/8zf8 DZpJMgMEioGZYCkRKegEBOF0SJ7krCIyiaYd8JrEYq13mdvtpuLX8k1mz/8jdn5P6ij3 h+xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=qwaY/PEZHacJpi/R5gqtIjkunGug0Atoe6jWSrD1dw8=; b=G/inUBxetpCPTJtY+XHwTGnGK1bWLTElsOXzJze2cKwtpUoyHHuIYUc+TTw/bvqZ70 tiIz8JW6B5nfo1dyBcE3vQ776SZf91NbVifVnpLtSTQc+hcP+j7qhKH75a/sKY0x3rTP rKrvB/8tSE9XXhPs4+ttS5uuP+HMfz6t4iDEQessh2uH3JVKPvv5fdMQXXaTn8PLdcIf BnIouhSQlZ3//JtPIByUmjB3XpWhDY+sHgShPBFD6rfGVn5Wj6iXG3s5vrYMNzytIutg 9UTZCncpfbupY1m0oqYqCS2al//qm4C5cqWzbd3zn1twq8aFqWTH+LirRw/eN/hWnU6w zqgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SCzxlqyk; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ja7-20020a170902efc700b001a05a78f952si3584559plb.272.2023.05.10.03.41.21; Wed, 10 May 2023 03:41:36 -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=@kernel.org header.s=k20201202 header.b=SCzxlqyk; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236358AbjEJKdE (ORCPT + 99 others); Wed, 10 May 2023 06:33:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235609AbjEJKdC (ORCPT ); Wed, 10 May 2023 06:33:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A49EA3582 for ; Wed, 10 May 2023 03:32:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 17CCA632BA for ; Wed, 10 May 2023 10:32:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92CE7C433EF; Wed, 10 May 2023 10:32:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683714778; bh=pcHCLtOh0HdEkzYFuTfO5KZmLO01o2e11R0rl38Yxfc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=SCzxlqyk3KGBvY1ct5gmyTLYNl6DQvaCNm0HLXYwsnaysDKggGej3KDZlhkDL1dyc PIeHtcF3J91XhCmrey76COmJ0MrOO/MjElF2g9yciZh9hnuAri/mKFkeOBaZZPo5NU EU9bPHjpHYAe8X844D9ApbMLioV5z2aQm8R7G7tmuxMbiqLoFsM0cSCZs/0idvPA3J FCp7bI/TEFm8zH7Rh0QO4wBF7q50cCr3eMsa58JBEQ5659SUkI/I22dgNGeTq1RrVQ pkkwOsfiGiPSxXth4+Ey5YQd+cZhmqA7Y5GgoyPkzXjfLs4ISwBtcrZMIrhTe5vHhq 41KaB8D5Ce2hQ== Date: Wed, 10 May 2023 12:32:53 +0200 From: Frederic Weisbecker To: Anna-Maria Behnsen Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , John Stultz , Thomas Gleixner , Eric Dumazet , "Rafael J . Wysocki" , Arjan van de Ven , "Paul E . McKenney" , Frederic Weisbecker , Rik van Riel , Steven Rostedt , Sebastian Siewior , Giovanni Gherdovich , Lukasz Luba , "Gautham R . Shenoy" Subject: Re: [PATCH v6 19/21] timer: Implement the hierarchical pull model Message-ID: References: <20230510072817.116056-1-anna-maria@linutronix.de> <20230510072817.116056-20-anna-maria@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230510072817.116056-20-anna-maria@linutronix.de> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 Le Wed, May 10, 2023 at 09:28:15AM +0200, Anna-Maria Behnsen a ?crit : > +static u64 tmigr_handle_remote_cpu(unsigned int cpu, u64 now, > + unsigned long jif) > +{ > + struct timer_events tevt; > + struct tmigr_walk data; > + struct tmigr_cpu *tmc; > + u64 next = KTIME_MAX; > + > + tmc = per_cpu_ptr(&tmigr_cpu, cpu); > + > + raw_spin_lock_irq(&tmc->lock); > + /* > + * Remote CPU is offline or no longer idle or other cpu handles cpu > + * timers already or next event was already expired - return! > + */ > + if (!tmc->online || tmc->remote || tmc->cpuevt.ignore || > + now < tmc->cpuevt.nextevt.expires) { > + raw_spin_unlock_irq(&tmc->lock); > + return next; > + } > + > + tmc->remote = 1; > + > + /* Drop the lock to allow the remote CPU to exit idle */ > + raw_spin_unlock_irq(&tmc->lock); > + > + if (cpu != smp_processor_id()) > + timer_expire_remote(cpu); > + > + /* > + * Pretend that there is no timer pending if the cpu is offline. > + * Possible pending timers will be migrated later to an active cpu. > + */ > + if (cpu_is_offline(smp_processor_id())) { > + raw_spin_lock_irq(&tmc->lock); > + tevt.local = tevt.global = KTIME_MAX; > + } else { > + /* > + * Lock ordering needs to be preserved - timer_base locks > + * before tmigr related locks. During fetching the next > + * timer interrupt, also tmc->lock needs to be > + * held. Otherwise there is a possible race window against > + * the CPU itself when it comes out of idle, updates the > + * first timer and goes back to idle. > + */ > + timer_lock_remote_bases(cpu); So the return value is ignored here. In the case of !PREEMPT_RT, I suppose it's impossible for the target CPU to be offline. You checked above tmc->online and in-between the call to timer_lock_remote_bases(), the path is BH-disabled, this prevents stop_machine from running and from setting the CPU as offline. However in PREEMPT_RT, ksoftirqd (or timersd) is preemptible, so it seems that it could happen in theory. And that could create a locking imbalance. My suggestion would be to unconditionally lock the bases, you already checked if !tmc->online before. The remote CPU may have gone down since then because the tmc lock has been relaxed but it should be rare enough that you don't care about optimizing with a lockless check. So you can just lock the bases, lock the tmc and check again if tmc->online. If not then you can just ignore the tmigr_new_timer_up call and propagation. Thanks. > + raw_spin_lock(&tmc->lock); > + > + /* next event of cpu */ > + fetch_next_timer_interrupt_remote(jif, now, &tevt, cpu); > + } > + > + /* > + * Nothing more to do when CPU came out of idle in the meantime - needs > + * to be checked when holding the tmc lock to prevent race. > + */ > + if (!tmc->idle) > + goto unlock; > + > + data.evt = &tmc->cpuevt; > + data.nextexp = tevt.global; > + data.groupstate.state = atomic_read(&tmc->tmgroup->migr_state); > + data.remote = true; > + tmc->cpuevt.ignore = 0; > + > + walk_groups(&tmigr_new_timer_up, &data, tmc); > + > + next = data.nextexp; > + > +unlock: > + tmc->remote = 0; > + raw_spin_unlock_irq(&tmc->lock); > + > + return next; > +}