From patchwork Tue Jun 22 23:41:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 465660 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=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham 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 5D53CC48BDF for ; Tue, 22 Jun 2021 23:42:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40DBC611CA for ; Tue, 22 Jun 2021 23:42:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229853AbhFVXoV (ORCPT ); Tue, 22 Jun 2021 19:44:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:52536 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229747AbhFVXoV (ORCPT ); Tue, 22 Jun 2021 19:44:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 69FAE61003; Tue, 22 Jun 2021 23:42:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624405325; bh=g4E/vdYh2QVf8t933gHLgeNZUvOXHmnn6xmrpWfhGlg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=APtIlBGBprpAh3N1sTE4nJF6xqWCbAJPKeWDs+FZUdRYU8Cy9ATCMHKVfsN8eQVjB pK+739hYzMIKv1E01/8k76+UlbSDMQB/Nv3reKUih0ZOvVluVIduoyTbKER2SClBWy dTt03/z2a1AKeU222AUaz9zhA16va/Nhd2odZtK0g1GRXjhy/EPFqE9b2szSIH5uk5 EK6JF4ezksMloHCeic8BZWvNpLJWqNy2wjphohvTdlzH+T5vRSjJ5vdWknzx5tErc+ Yvdy63pRe4LY+qhpzLjz9RnYHtPlCNP9KsWywDKOlP3meX8mzatF7sb44AUtfIn/j4 /fvAKfkW6MxmQ== From: Frederic Weisbecker To: Thomas Gleixner , Peter Zijlstra Cc: LKML , Frederic Weisbecker , "Eric W . Biederman" , Oleg Nesterov , stable@vger.kernel.org, Ingo Molnar Subject: [PATCH 1/7] posix-cpu-timers: Fix rearm racing against process tick Date: Wed, 23 Jun 2021 01:41:49 +0200 Message-Id: <20210622234155.119685-2-frederic@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210622234155.119685-1-frederic@kernel.org> References: <20210622234155.119685-1-frederic@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org 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 3bb96a8b49c9..aa52fc85dbcb 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. */