Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp921817pxa; Wed, 19 Aug 2020 20:01:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJynlbwCY5LPQ7L67anYJLVSaaKREWcNwgvzY/SruVvcC0kypGB5KtaPrjGkVX8ZvfcZR42q X-Received: by 2002:a17:906:8608:: with SMTP id o8mr1372575ejx.156.1597892470452; Wed, 19 Aug 2020 20:01:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597892470; cv=none; d=google.com; s=arc-20160816; b=L+yyE5ZX+Bm5qHOKCpRye9SBbGVaoxpqVlqEtSlSPmJTy4Nxfw43kROfPwJVakZaaI UKVr+ZoB6Hqv7I9anC3pghCS7uZBxwt1R3ezbsO/o4wEmJAGqSm4RR2oCZJb9b1CWE2M +ocSeqos/0ZqEhHVirxgqhHXHFmeVNxjtXVMS3qd69m5tjQDkRN7cUHQF/w57Pys4FyT WmCiPRLR1ILloP6TyY4CI0Zd5bhZ2qR/hsIr3sHUxDyGRpHyQ9D6ng5NcbBwI3Eu7Msq b20+M3cMLTTI+pIb0lmQ12WbLOeriXmb2rxfxELZBMAD1nhI2w7BqRh/4HVz4WNXLIY0 +GmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:feedback-id:message-id:date:subject:cc:to :from; bh=UpIncUECbPHj0TGmawl8hbsqOTQbIhF9hP3h77uksVA=; b=w2+3yLxULuJd/HfzRPfggqkOhKqWsEdr5Ckt7AgMYWzaZSZBZMZf/q6mTTSYkvLzU1 BZN2l0Z6/wlY6tcWTm9DsByQ+NMBeuCw3N4Ky6/nGbXqX95OMxBiQ6ziOR84hlIrVJ9l KI13xfB+BcvppPJeV9HidTPHaj1g6czQz44NpdFihvPEwy7z39bI0Bw6QmfSjmg4h69y ERvJec7dfYb0PuYUh+YFqVbqFNGj56gNRCmozazFDmMPUafZcGswHJhv8JkuUHACTfnA MxFxQXwwz9MMoeWdKhBSaqh2xVX1gZGJoWHt82gg/47UiqVLzChXj6lJLrY5kfv6cbRM DZYg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e11si377125ejc.49.2020.08.19.20.00.46; Wed, 19 Aug 2020 20:01:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726745AbgHTC7w (ORCPT + 99 others); Wed, 19 Aug 2020 22:59:52 -0400 Received: from smtpbgsg2.qq.com ([54.254.200.128]:49141 "EHLO smtpbgsg2.qq.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726435AbgHTC7v (ORCPT ); Wed, 19 Aug 2020 22:59:51 -0400 X-QQ-mid: bizesmtp20t1597892367tn1degxx Received: from localhost.localdomain.info (unknown [103.37.140.45]) by esmtp6.qq.com (ESMTP) with id ; Thu, 20 Aug 2020 10:59:22 +0800 (CST) X-QQ-SSF: 01100000002000E0Z000B00A0001000 X-QQ-FEAT: 9W4TTyWj0GPg03owh0ocBhZJGDp9froOm3u11QDwX/8s26Qa2LOKslI420QkJ tiIPItLxJfLGJd8H/qu6WvfqP+nGNpFWW4z5K80n8ERPeYIhKSxMxruXEqr9lZe8SGfvUZ+ YiSnOsoW3NuIkh17v+0jQlYbIt8fV6w1k6wgxaLd8Lac17fceav6Ss53De2cXHWXaKXvwG+ PcmLujj1JcYVym7KRYKcgd6LXmSFVwLcc181uQu+HL2bH79fJ/j/8IG4texemuS/YEZ06tf 9MGySk+OWmNnokjs2N0A2mnaBaM3yNFxO9K1Lypt1NP1ekspAvAq/DU0tU+MijDyblOgjfL J/exmieKDIM9zLDOOI= X-QQ-GoodBg: 0 From: Wang Long To: tglx@linutronix.de Cc: john.stultz@linaro.org, sboyd@kernel.org, linux-kernel@vger.kernel.org, w@laoqinren.net Subject: [PATCH] timer: use raw_spin_unlock_irqrestore and raw_spin_lock_irqsave instead of raw_spin_{lock|unlock} Date: Thu, 20 Aug 2020 10:59:17 +0800 Message-Id: <1597892357-1349-1-git-send-email-w@laoqinren.net> X-Mailer: git-send-email 1.8.3.1 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:laoqinren.net:qybgforeign:qybgforeign6 X-QQ-Bgrelay: 1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The current code in function __mod_timer(https://github.com/torvalds/linux/blob/master/kernel/time/timer.c#L961): 994 base = lock_timer_base(timer, &flags); ----------------------(1) forward_timer_base(base); if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) && time_before_eq(timer->expires, expires)) { ret = 1; goto out_unlock; } clk = base->clk; idx = calc_wheel_index(expires, clk, &bucket_expiry); /* * Retrieve and compare the array index of the pending * timer. If it matches set the expiry to the new value so a * subsequent call will exit in the expires check above. */ if (idx == timer_get_idx(timer)) { if (!(options & MOD_TIMER_REDUCE)) timer->expires = expires; else if (time_after(timer->expires, expires)) timer->expires = expires; ret = 1; goto out_unlock; } } else { 1021 base = lock_timer_base(timer, &flags); ------------------------(2) forward_timer_base(base); } ret = detach_if_pending(timer, base, false); if (!ret && (options & MOD_TIMER_PENDING_ONLY)) goto out_unlock; new_base = get_target_base(base, timer->flags); if (base != new_base) { /* * We are trying to schedule the timer on the new base. * However we can't change timer's base while it is running, * otherwise del_timer_sync() can't detect that the timer's * handler yet has not finished. This also guarantees that the * timer is serialized wrt itself. */ if (likely(base->running_timer != timer)) { /* See the comment in lock_timer_base() */ timer->flags |= TIMER_MIGRATING; 1042 raw_spin_unlock(&base->lock); -----------------------(3) base = new_base; raw_spin_lock(&base->lock); -------------------------(4) WRITE_ONCE(timer->flags, (timer->flags & ~TIMER_BASEMASK) | base->cpu); forward_timer_base(base); } } debug_timer_activate(timer); timer->expires = expires; /* * If 'idx' was calculated above and the base time did not advance * between calculating 'idx' and possibly switching the base, only * enqueue_timer() is required. Otherwise we need to (re)calculate * the wheel index via internal_add_timer(). */ if (idx != UINT_MAX && clk == base->clk) enqueue_timer(base, timer, idx, bucket_expiry); else internal_add_timer(base, timer); out_unlock: 1066 raw_spin_unlock_irqrestore(&base->lock, flags); ---------------------(5) return ret; } The code in (1)(2) lock the base with raw_spin_lock_irqsave(&base->lock, flag), if base != new_base, the code in (3) unlock the old base, the code in (4) lock the new base. at the end of the function(5), use raw_spin_unlock_irqrestore(&base->lock, flags); to unlock the new_base. Consider the following situation: CPU0 CPU1 base = lock_timer_base(timer, &flags); (1)(2) raw_spin_unlock(&base->lock); (3) base = new_base; raw_spin_lock(&base->lock); (4) raw_spin_unlock_irqrestore(&base->lock, flags); (5) The flags save from CPU0, and restore to CPU1. Is this wrong? we encountered a kernel panic, and we suspect that it is the problem. How about the following patch to fix. Signed-off-by: Wang Long --- kernel/time/timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index a16764b..4153766 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1039,9 +1039,9 @@ static struct timer_base *lock_timer_base(struct timer_list *timer, /* See the comment in lock_timer_base() */ timer->flags |= TIMER_MIGRATING; - raw_spin_unlock(&base->lock); + raw_spin_unlock_irqrestore(&base->lock, flags); base = new_base; - raw_spin_lock(&base->lock); + raw_spin_lock_irqsave(&base->lock, flags); WRITE_ONCE(timer->flags, (timer->flags & ~TIMER_BASEMASK) | base->cpu); forward_timer_base(base); -- 1.8.3.1