Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5948162ybp; Tue, 8 Oct 2019 10:35:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqzcpOAwq06suME6TTlc0PrvGdhcmaLEOiRN8IdLSNFROoQUvo35CUKsnMwdElTfRdEfto4g X-Received: by 2002:a50:8a21:: with SMTP id i30mr34455933edi.229.1570556138270; Tue, 08 Oct 2019 10:35:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570556138; cv=none; d=google.com; s=arc-20160816; b=oQ+T69WRurCYmNlWHMZFN39QwvuFsedhGVN2Ei9H52xi90KMLYTUbJRP8CSotoYPvb U4sPha9l9+U5Tc67Q82DtZQE4+Eaom/ciWUkjlOdMMh/Vrnop8/jUkqOgrcmdpKMaTiZ moXBRd+6lXPR/R3RqlozbN2O4n+qnyysTeVH7Upd2OF6HtMZjHV1yn+fr5LK7UQPLRLu 47XaTESWSn0nEkWjopGbJE7qTc8PKWihCukGQCPiqOueNasGkILYHB9zoT1HE+HnK2O5 3jjix5AioQrwlFzN9hTfARhA53f7h7JakF4qq1boxpzxvtW396WVHYU6OyAUBLxXOWeH AvzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=CPJ4WvmAJyyzE+ZwNMMLcNRFnx5t024x8AdCYAdjqqg=; b=DTvj871jpkH5mPOAeCVZz5zGn0hF9O3/uGmzzSxNqDGgVU4gTO27kqTbUSgPtbpDBr RLbtPXx/ZfNjxJCFnTQCid5M0J7PTLvuBa5HsSGI4Xs6QzH/MmUQGh/60JliSjftVPGy 0MJKuHkCJKlZXpCL+CFWC+IXRZrs94wL8COwy6/3q7pF7Htnb/J0TCN4DOJ8X2OhbIn6 EuCjNygOosXmfo1xZAc1HT1j4ckKFEbHtTyvtYQVcjLgffGUdalpCRvvWb7kDb3fhd5N 2SMYf4Ky3KEZqJJuU5m2DgdCpKOJGqiE+wSHQ2u/1VA8X+Aj9HN3HdUsyRGcpVNG+Nej Hl8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NDdyofzK; 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=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y7si9619885ejp.378.2019.10.08.10.35.13; Tue, 08 Oct 2019 10:35:38 -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=@google.com header.s=20161025 header.b=NDdyofzK; 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=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727688AbfJHRcJ (ORCPT + 99 others); Tue, 8 Oct 2019 13:32:09 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:43862 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726057AbfJHRcJ (ORCPT ); Tue, 8 Oct 2019 13:32:09 -0400 Received: by mail-pf1-f202.google.com with SMTP id i187so14233886pfc.10 for ; Tue, 08 Oct 2019 10:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=CPJ4WvmAJyyzE+ZwNMMLcNRFnx5t024x8AdCYAdjqqg=; b=NDdyofzKiCWppxENWKYuJsGPdFfx5Fmui0sZkF+fkb/xjlrCZqK+GE2DbvyD+XZAdl s49OKtPn9bO749Asu6LmhV0LEjQ+fMu1rFmr6jO5MoqfIaIY4G2XXFsnoWs4RBSnQtD1 /a2vsqdfwKlYrEeEkCm/0l/9kZJuRsCPcwamFYZepQEYR58yP4Tkg+Xw39oZuxkiipQ+ E1smu61ctzNJ2o3MJ/xiVurlrZoILTbDnwfbJwLXFytSUSp3sFP/OQHd5GdRw7kXV0Bv RFCOoTtp+k2ZZT6MzTThkKY6O6lRT2HvNTHwb0SCiljjHx7GP3r+TMHYbtQQw2U0uDj3 xEYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=CPJ4WvmAJyyzE+ZwNMMLcNRFnx5t024x8AdCYAdjqqg=; b=ffJvG1rw0j3OWnnIEkBXjMahwDkTTW+NbxlAg6dGu8mcCy9R5zDqZJhWGafcySLJBi nfDt94Y0R3bYjN8XUt0BTI4BPZxFr2aLF74owONNbPgKnuMRW7ee8ReeX9fJ0HIMjUjg fFtyRPwwrlfZBK0jvt7A/FtKu1CT+xfXIPgl8DimjBMhhDmpi5CawqXFAyZOCgO1vYbE AzEqPUIuB9zETS8K9PvfJbB6P2FsYCyYIw9eT2jZjxZaJsR3GsZUDqbA3n4kq7LSJn9s RxJMfWl6qDfBUDMc1qfyQl0Fzi6O1cQzRNf+Yg9dYpqo4WqyZroJrW0QpuSdG5Khzl0I EJcw== X-Gm-Message-State: APjAAAVxsX2x6/sKQid3D/sPRE6Q/Jfu1A9ePWjrzEcdk/qeLuPMOks6 DHGjqxiHgXu9NWViXiUHKwUDbjhaQZiCwA== X-Received: by 2002:a63:dd0c:: with SMTP id t12mr36376305pgg.82.1570555928123; Tue, 08 Oct 2019 10:32:08 -0700 (PDT) Date: Tue, 8 Oct 2019 10:32:04 -0700 Message-Id: <20191008173204.180879-1-edumazet@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.23.0.581.g78d2f28ef7-goog Subject: [PATCH] hrtimer: annotate lockless access to timer->base From: Eric Dumazet To: Thomas Gleixner Cc: linux-kernel , Eric Dumazet , Eric Dumazet , Julien Grall Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Followup to commit dd2261ed45aa ("hrtimer: Protect lockless access to timer->base") lock_hrtimer_base() fetches timer->base without lock exclusion. Compiler is allowed to read timer->base twice (even if considered dumb) and we could end up trying to lock migration_base and return &migration_base. base = timer->base; if (likely(base != &migration_base)) { /* compiler reads timer->base again, and now (base == &migration_base) raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); if (likely(base == timer->base)) return base; /* == &migration_base ! */ Similarly the write sides should use WRITE_ONCE() to avoid store tearing. Signed-off-by: Eric Dumazet Cc: Julien Grall Cc: Thomas Gleixner --- kernel/time/hrtimer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c index 0d4dc241c0fb498036c91a571e65cb00f5d19ba6..65605530ee349c9682690c4fccb43aa9284d4287 100644 --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -164,7 +164,7 @@ struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, struct hrtimer_clock_base *base; for (;;) { - base = timer->base; + base = READ_ONCE(timer->base); if (likely(base != &migration_base)) { raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); if (likely(base == timer->base)) @@ -244,7 +244,7 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, return base; /* See the comment in lock_hrtimer_base() */ - timer->base = &migration_base; + WRITE_ONCE(timer->base, &migration_base); raw_spin_unlock(&base->cpu_base->lock); raw_spin_lock(&new_base->cpu_base->lock); @@ -253,10 +253,10 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, raw_spin_unlock(&new_base->cpu_base->lock); raw_spin_lock(&base->cpu_base->lock); new_cpu_base = this_cpu_base; - timer->base = base; + WRITE_ONCE(timer->base, base); goto again; } - timer->base = new_base; + WRITE_ONCE(timer->base, new_base); } else { if (new_cpu_base != this_cpu_base && hrtimer_check_target(timer, new_base)) { -- 2.23.0.581.g78d2f28ef7-goog