Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1005968imm; Sun, 2 Sep 2018 06:38:15 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdav6A+rbcQXaTO5p/YfUSOOUfNPDZ02/RvMgk9bCORvbX0OYzolup9MWgnaYtox0a4ZDCHs X-Received: by 2002:a63:1c61:: with SMTP id c33-v6mr9199520pgm.109.1535895495505; Sun, 02 Sep 2018 06:38:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535895495; cv=none; d=google.com; s=arc-20160816; b=XJfHiF/P63qezaHWc1+8fdx5qkjDidr4es3Gq5AVq+Dmcnx0HA7Dx4PDXFiEWnZw0G hhzipYOUXHBytfpq5+pkjxnEjrVgG2wN9byZaROervemG7ZBDtEB++VH7mayOuo2I3Be MhhZrmYCSYNrrT8M8Z9uK4o/UdUtDptZCe3HyVTPT7NdLztGOPc1dzraDw6koZkTgtVZ 7JqFE5tUVj0tUazSxcK24JB7OdnA4P+smY3GEYUYkHGUB9aWm6D6UUwm0eH0eVX4Q1Sd uGSF9w+KnDx7dx4cbWrSyW+IYolKxSeSVCu+UFuD8b5u8cwAuufZWIONl6r7761isIVf F6pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=i4jHx47NtKnncj6ZzjqnZNWwIcVcppMyU2J0QBHMmlc=; b=Em7nqJWSIrnimXqG9aenvNSat94PAOmbSoJ28vKS7GkOU5BpFZomu1JaT8exyHwzZR W0bt4lRigqoSS1p+9zsxpal9hGPUjRc3TL7fX4fgVe+OfXTvNW+3i0pAFMJjfMktMmlb 6iYDjZALreFOFcGr4E4vnG0AXWNy+uHjCY0zT7vL3x1qAavlZQIGKjPQjZApJ5vNILIB o6OZcdo/GYwqJ69qBe7oG5lzB5fWZAaPl0SoNczRYh7ETulVfpn8oaUOWUgXK1UNGeov pdWmn5AotDmaUrxBx/Jkmuq10Td+E0I+m/hgq2MkIWMpI5WZazhk2Hgi1FG3i4ALCnd0 CqBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b="A9/p7kQS"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r3-v6si14323897plb.221.2018.09.02.06.38.01; Sun, 02 Sep 2018 06:38:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b="A9/p7kQS"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727608AbeIBRTg (ORCPT + 99 others); Sun, 2 Sep 2018 13:19:36 -0400 Received: from mail-cys01nam02on0094.outbound.protection.outlook.com ([104.47.37.94]:40784 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727573AbeIBRTf (ORCPT ); Sun, 2 Sep 2018 13:19:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=i4jHx47NtKnncj6ZzjqnZNWwIcVcppMyU2J0QBHMmlc=; b=A9/p7kQScdq9opmJUjEWmi1lx/MLbspUgDzEiG/qN4bcHWcBiT/ZaJasAJD8FE3ZOtItwjgdHNUDCkvwT1WGT8hy20E2f8d50aABLRxvd1eEyVvcLLsG2iDG4eDN9bnQ5vaWOm8bUFGGB2tdUw6PdlE+0WTqFMEyTkab3i3OoGg= Received: from CY4PR21MB0776.namprd21.prod.outlook.com (10.173.192.22) by CY4PR21MB0150.namprd21.prod.outlook.com (10.173.189.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1122.4; Sun, 2 Sep 2018 13:03:30 +0000 Received: from CY4PR21MB0776.namprd21.prod.outlook.com ([fe80::7c3a:eea8:1391:1611]) by CY4PR21MB0776.namprd21.prod.outlook.com ([fe80::7c3a:eea8:1391:1611%7]) with mapi id 15.20.1143.000; Sun, 2 Sep 2018 13:03:30 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Gaurav Kohli , Thomas Gleixner , "john.stultz@linaro.org" , "sboyd@kernel.org" , "linux-arm-msm@vger.kernel.org" , Sasha Levin Subject: [PATCH AUTOSEL 4.18 025/131] timers: Clear timer_base::must_forward_clk with timer_base::lock held Thread-Topic: [PATCH AUTOSEL 4.18 025/131] timers: Clear timer_base::must_forward_clk with timer_base::lock held Thread-Index: AQHUQr1YB2oGADKO80emED/U7OSKqA== Date: Sun, 2 Sep 2018 13:03:30 +0000 Message-ID: <20180902064601.183036-25-alexander.levin@microsoft.com> References: <20180902064601.183036-1-alexander.levin@microsoft.com> In-Reply-To: <20180902064601.183036-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;CY4PR21MB0150;6:K7OMZBuxzY8//HQqglyh8OQL5lNnyr8Kl6QIx5RIp71u9nxcoXlimuP6XVlgaQC+j4xoQ/8zCkSoSuHO6yHszw3Zvx7yqzJnPG5QYBf1BLpjKALc4YmTaWwQVAtMtOhr6qfIeVBKok8Ith42Rog01KKAKXWniIgJkAKFXMwppQeQJgWL3fTcpWe/o1zVsmYoXB3/4XdVhAdF70MS6d6usPfa9jWWo3bPImPOBz5oqmJgN9JEnIVZ9YS/8l1w78UzVAadi6b2BV5yZURy7ETevkWaMrUStiPpeItmf+i/MlRSfh/yPOli+D3UtbvBdGyKSvUA/OGikM1qlEMmFYMKzKw14GvT3ZidZtfsI9dVNdnOi7AzqxG+abquIhbcJcpalbg7/LLcxv7WHTx0M7lQYjOZG9lU9V3M2MPOxPL4W1dfZb/cgbMr/15Nja4FCyHgu5U6DzmEoPT+iVMvJG6cHg==;5:UOPYk1NHbRGQZWFKuQDdmmaGd2QVBzCxRcoClD/kWKJLVU3xS6hQqkLcqXJLCeMNm3YiSSBxm0PWa3CgaG4D5wihEyFFzThpw/RzKpP8DBY8b3dcmgkYyZix+kOo9FW4xkOs/q253Wr+I+xu8tFyTZW5spQ3iygXjMJYWiWK4uo=;7:/Z3rA8NYazmmcsUArnu4LvLA3B+O43LIYjVfhr0A/+6F5zgcSFBZjN4meHRw78eSg5A+iz8flK50+SKROT2n4WQrSmdGhina0wjrZZemmpyh3rU90V95pIFeED4NwGhYszKfnfw5SCGZx5iSSHBfKy+eKHRk8BZeNY+jpTh76hA2Pi5zaUSVjDccJojmcWogvyjff7pBNMIS6V+GBx3knWNk6krGiNBOb0WUoc40BkHTN0h32iSZWuqrv0IL6gRz x-ms-office365-filtering-correlation-id: 4c703536-d458-4c68-c81c-08d610d47b14 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(7020095)(4652040)(4534165)(4627221)(201703031133081)(201702281549075)(5600074)(711020)(4618075)(2017052603328)(7193020);SRVR:CY4PR21MB0150; x-ms-traffictypediagnostic: CY4PR21MB0150: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(9452136761055)(42068640409301); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(10201501046)(3002001)(93006095)(93001095)(3231340)(944501410)(52105095)(2018427008)(6055026)(149027)(150027)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123560045)(20161123564045)(20161123562045)(201708071742011)(7699049)(76991033);SRVR:CY4PR21MB0150;BCL:0;PCL:0;RULEID:;SRVR:CY4PR21MB0150; x-forefront-prvs: 078310077C x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(366004)(39860400002)(376002)(346002)(136003)(199004)(189003)(256004)(22452003)(2906002)(217873002)(3846002)(68736007)(10090500001)(6506007)(6116002)(1076002)(6436002)(102836004)(486006)(53936002)(76176011)(106356001)(105586002)(14444005)(7736002)(305945005)(8676002)(316002)(5660300001)(6306002)(6512007)(99286004)(36756003)(4326008)(97736004)(5250100002)(2616005)(476003)(2900100001)(11346002)(186003)(2501003)(575784001)(81166006)(26005)(8936002)(81156014)(110136005)(54906003)(107886003)(66066001)(478600001)(86612001)(14454004)(6346003)(6486002)(10290500003)(86362001)(72206003)(966005)(25786009)(446003);DIR:OUT;SFP:1102;SCL:1;SRVR:CY4PR21MB0150;H:CY4PR21MB0776.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: ny7mTKksamCT5aqNoafMRswRnZLrnY0/D1O36DCA9ypeY14/LweNN8FNy2S7q76EIVxDO+ADUPegH0I2X+dvM4iSHrI+rAk4g3MsPRKqs/+66tji/oyaI50SUh3eM9wegKv7xXvKXOisSjxF+6TUguyCWpxL/Wl726FJe6xAcgYs9v7wwaJkpq/oeLRZ75APKwZYd9aOCWuraTGCb40vlG7/K7YcmE7zpFxnm8K1V0Rjrvr4Su1DAf+xCmcWqUtlHC4LAbQrm2mw2qmnc2SSoz82XnR1hmUOgCs2IKR+C5uwcCCtCjl/SlrufL9B4Jm7s4rTsDzTLY7x3JDMrsTMolRttsF7dntHOW9gfuxGfzE= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4c703536-d458-4c68-c81c-08d610d47b14 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Sep 2018 13:03:30.1632 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR21MB0150 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Gaurav Kohli [ Upstream commit 363e934d8811d799c88faffc5bfca782fd728334 ] timer_base::must_forward_clock is indicating that the base clock might be stale due to a long idle sleep. The forwarding of the base clock takes place in the timer softirq or when a timer is enqueued to a base which is idle. If the enqueue of timer to an idle base happens from a remote CPU, then the following race can happen: CPU0 CPU1 run_timer_softirq mod_timer base =3D lock_timer_base(timer); base->must_forward_clk =3D false if (base->must_forward_clk) forward(base); -> skipped enqueue_timer(base, timer, idx); -> idx is calculated high due to stale base unlock_timer_base(timer); base =3D lock_timer_base(timer); forward(base); The root cause is that timer_base::must_forward_clk is cleared outside the timer_base::lock held region, so the remote queuing CPU observes it as cleared, but the base clock is still stale. This can cause large granularity values for timers, i.e. the accuracy of the expiry time suffers. Prevent this by clearing the flag with timer_base::lock held, so that the forwarding takes place before the cleared flag is observable by a remote CPU. Signed-off-by: Gaurav Kohli Signed-off-by: Thomas Gleixner Cc: john.stultz@linaro.org Cc: sboyd@kernel.org Cc: linux-arm-msm@vger.kernel.org Link: https://lkml.kernel.org/r/1533199863-22748-1-git-send-email-gkohli@co= deaurora.org Signed-off-by: Sasha Levin --- kernel/time/timer.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index cc2d23e6ff61..786f8c014e7e 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1657,6 +1657,22 @@ static inline void __run_timers(struct timer_base *b= ase) =20 raw_spin_lock_irq(&base->lock); =20 + /* + * timer_base::must_forward_clk must be cleared before running + * timers so that any timer functions that call mod_timer() will + * not try to forward the base. Idle tracking / clock forwarding + * logic is only used with BASE_STD timers. + * + * The must_forward_clk flag is cleared unconditionally also for + * the deferrable base. The deferrable base is not affected by idle + * tracking and never forwarded, so clearing the flag is a NOOP. + * + * The fact that the deferrable base is never forwarded can cause + * large variations in granularity for deferrable timers, but they + * can be deferred for long periods due to idle anyway. + */ + base->must_forward_clk =3D false; + while (time_after_eq(jiffies, base->clk)) { =20 levels =3D collect_expired_timers(base, heads); @@ -1676,19 +1692,6 @@ static __latent_entropy void run_timer_softirq(struc= t softirq_action *h) { struct timer_base *base =3D this_cpu_ptr(&timer_bases[BASE_STD]); =20 - /* - * must_forward_clk must be cleared before running timers so that any - * timer functions that call mod_timer will not try to forward the - * base. idle trcking / clock forwarding logic is only used with - * BASE_STD timers. - * - * The deferrable base does not do idle tracking at all, so we do - * not forward it. This can result in very large variations in - * granularity for deferrable timers, but they can be deferred for - * long periods due to idle. - */ - base->must_forward_clk =3D false; - __run_timers(base); if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); --=20 2.17.1