From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC094C07E95 for ; Tue, 20 Jul 2021 11:02:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9EE5061107 for ; Tue, 20 Jul 2021 11:02:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237990AbhGTKVa (ORCPT ); Tue, 20 Jul 2021 06:21:30 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:40024 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238099AbhGTKOu (ORCPT ); Tue, 20 Jul 2021 06:14:50 -0400 Date: Tue, 20 Jul 2021 10:55:22 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1626778523; 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; bh=8lPvVTbfOrGyEFM7yYBrV80a7op6U6jWP3hzLmgYm+s=; b=zQUB1sj4GIj19ZOx/gxtR850gJ+PwFmi7U5/hXgeLDkTf45lyJPU1XsGZZyJ4oFBSTo1Q9 g3sRTzcqrNL9De2nFRHoAdFC8YtiwJeODmlWWd559aKoqdQKbXr9Zjy6IVGi7e/BhLkTib laXjr0mGczzNFNzKJ0yIJAOPoNvRB3+mIXEnLSbnagZ0V5ceR3pvJbU7PEo4t/ZE4RfdCt cEpgQGq05ReZKkoUobMRsD9i8HwMGVv4wfC2dkp60B9CFjNfk4Nu0GvFiNDYbCbTNKkXN3 XfaMNwC3sXc5v0xvaLWMM+i+Cupcj2SXwTxcptBMhCL2ErW0yDw4kSohQzWQPQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1626778523; 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; bh=8lPvVTbfOrGyEFM7yYBrV80a7op6U6jWP3hzLmgYm+s=; b=JiK0WDNi0VXxHudpdLy+iu6vAS7IiLoJVG5YYjg0ghVNWp/i8NNamR2Bs06v2felms8V1C TkYOgRGO909LoCBw== From: "tip-bot2 for Frederic Weisbecker" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/urgent] posix-cpu-timers: Fix rearm racing against process tick Cc: "Peter Zijlstra (Intel)" , Frederic Weisbecker , stable@vger.kernel.org, Oleg Nesterov , Thomas Gleixner , Ingo Molnar , "Eric W. Biederman" , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <162677852264.395.4295146742807519075.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the timers/urgent branch of tip: Commit-ID: 1a3402d93c73bf6bb4df6d7c2aac35abfc3c50e2 Gitweb: https://git.kernel.org/tip/1a3402d93c73bf6bb4df6d7c2aac35abfc3c50e2 Author: Frederic Weisbecker AuthorDate: Thu, 03 Jun 2021 01:15:59 +02:00 Committer: Frederic Weisbecker CommitterDate: Thu, 15 Jul 2021 01:20:10 +02:00 posix-cpu-timers: Fix rearm racing against process tick Since the process wide cputime counter is started locklessly from posix_cpu_timer_rearm(), it can be concurrently stopped by operations on other timers from the same thread group, such as in the following unlucky scenario: CPU 0 CPU 1 ----- ----- timer_settime(TIMER B) posix_cpu_timer_rearm(TIMER A) cpu_clock_sample_group() (pct->timers_active already true) handle_posix_cpu_timers() check_process_timers() stop_process_timers() pct->timers_active = false arm_timer(TIMER A) tick -> run_posix_cpu_timers() // sees !pct->timers_active, ignore // our TIMER A Fix this with simply locking process wide cputime counting start and timer arm in the same block. Acked-by: Peter Zijlstra (Intel) Signed-off-by: Frederic Weisbecker Fixes: 60f2ceaa8111 ("posix-cpu-timers: Remove unnecessary locking around cpu_clock_sample_group") Cc: stable@vger.kernel.org Cc: Oleg Nesterov Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Eric W. Biederman --- kernel/time/posix-cpu-timers.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c index 29a5e54..517be7f 100644 --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -991,6 +991,11 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer) if (!p) goto out; + /* Protect timer list r/w in arm_timer() */ + sighand = lock_task_sighand(p, &flags); + if (unlikely(sighand == NULL)) + goto out; + /* * Fetch the current sample and update the timer's expiry time. */ @@ -1001,11 +1006,6 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer) bump_cpu_timer(timer, now); - /* Protect timer list r/w in arm_timer() */ - sighand = lock_task_sighand(p, &flags); - if (unlikely(sighand == NULL)) - goto out; - /* * Now re-arm for the new expiry time. */