From patchwork Tue May 22 23:50:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 10419763 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2E7016032A for ; Tue, 22 May 2018 23:50:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CA5428CE1 for ; Tue, 22 May 2018 23:50:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0FC0528CC2; Tue, 22 May 2018 23:50:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D996128CC2 for ; Tue, 22 May 2018 23:50:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753337AbeEVXuk (ORCPT ); Tue, 22 May 2018 19:50:40 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:44599 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753319AbeEVXuj (ORCPT ); Tue, 22 May 2018 19:50:39 -0400 Received: by mail-pl0-f65.google.com with SMTP id m24-v6so455477pls.11 for ; Tue, 22 May 2018 16:50:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=+q/5UDzYXBuzuH9NpCs0k/Acyefn6/pzwKkePpJir+s=; b=DUD2wf79zWPJO4VglfQE++XaW5F5ebD7uwJV3fA28jWBRoJoV4HlcDrfSikdI27c5I E9p+0DWHHvPia5GZ9SvHZ5MlyGXxQoF1Sit6I6tqUG5u0dI04nc5KLN9VQZeBMQBH6O9 sNBx51la8X6fAFmzgjPtFIkk96VHUkpg5hxOT92uMjEedXiJ/7zt3SqtLlaK0rQgxY5W S172HRmbs6HfZcYO8eRGJFNITC9Pa0J9Rdqtob5HNADEDQ864F8qKiuKVO6XENKMIMaj AiWE4faj+/EzpaPY/vEQcOz/E1kl4YZODflOmYIYs6mukGMciYZfDohqfLYuCdhStzcO IpcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+q/5UDzYXBuzuH9NpCs0k/Acyefn6/pzwKkePpJir+s=; b=H28cfS8I3xtJkt4xmtI97tn2MXGD7Q4cEP8Lg+nCgAyhIXrxK41rOvFOAqinmmRWq/ 65Jz2o2NeZ2ntFC5nnjYsETKw/eJMZ+pxn5Aq8/rzvsKwOZsayBROLm9oAilBg1LJOvT O55j7cdecleTSk/GGehfjOuzjE5qbk2L+pZzfJVhwE/ORJrxjjHrxz5XxW6V7tm/nDCB PJ+0kyLxo6oTW2AgVNPIWFLvW6z0nQiXig+5xClxPXnGNri5d7K2mwdP8suUVOUxJwOD bnpXwKiGUxVI224k15YopFqn2pX2bfDj6uMHFWDWpscknAnOvXcwnEYLXE9LF0QTCS0a FY6Q== X-Gm-Message-State: ALKqPwe6iyD8HWv2WlupQApKhN1t9ri+yzAVj5D8wlXO6rVPDwZ2m9s2 54bU66Gg3PB6wL5nbMNcBhvo0g== X-Google-Smtp-Source: AB8JxZpCPpYRBcnLh28T5cJYsPJfG2KdQHJbKuafSm/fbhsOgNe4j/3UtXaEKXfbNKFEnPgUULHL/w== X-Received: by 2002:a17:902:1025:: with SMTP id b34-v6mr506601pla.207.1527033038916; Tue, 22 May 2018 16:50:38 -0700 (PDT) Received: from joelaf.mtv.corp.google.com ([2620:0:1000:1600:3122:ea9c:d178:eb]) by smtp.gmail.com with ESMTPSA id w134-v6sm29460366pfd.187.2018.05.22.16.50.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 May 2018 16:50:38 -0700 (PDT) From: "Joel Fernandes (Google)" X-Google-Original-From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , "Rafael J . Wysocki" , Peter Zijlstra , Ingo Molnar , Patrick Bellasi , Juri Lelli , Luca Abeni , Todd Kjos , claudio@evidence.eu.com, kernel-team@android.com, linux-pm@vger.kernel.org Subject: [PATCH RFC] schedutil: Address the r/w ordering race in kthread Date: Tue, 22 May 2018 16:50:28 -0700 Message-Id: <20180522235028.80564-1-joel@joelfernandes.org> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently there is a race in schedutil code for slow-switch single-CPU systems. Fix it by enforcing ordering the write to work_in_progress to happen before the read of next_freq. Kthread Sched update sugov_work() sugov_update_single() lock(); // The CPU is free to rearrange below // two in any order, so it may clear // the flag first and then read next // freq. Lets assume it does. work_in_progress = false if (work_in_progress) return; sg_policy->next_freq = 0; freq = sg_policy->next_freq; sg_policy->next_freq = real-freq; unlock(); Reported-by: Viresh Kumar CC: Rafael J. Wysocki CC: Peter Zijlstra CC: Ingo Molnar CC: Patrick Bellasi CC: Juri Lelli Cc: Luca Abeni CC: Todd Kjos CC: claudio@evidence.eu.com CC: kernel-team@android.com CC: linux-pm@vger.kernel.org Signed-off-by: Joel Fernandes (Google) --- I split this into separate patch, because this race can also happen in mainline. kernel/sched/cpufreq_schedutil.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 5c482ec38610..ce7749da7a44 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -401,6 +401,13 @@ static void sugov_work(struct kthread_work *work) */ raw_spin_lock_irqsave(&sg_policy->update_lock, flags); freq = sg_policy->next_freq; + + /* + * sugov_update_single can access work_in_progress without update_lock, + * make sure next_freq is read before work_in_progress is set. + */ + smp_mb(); + sg_policy->work_in_progress = false; raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);