Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1416675imm; Tue, 22 May 2018 03:52:10 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqrY0BV7317cAIHfZGM08pr2nh3vMXWoYyK9ehtYput2hruVNkyh+UF0L4jR8SrkF7ci3Y0 X-Received: by 2002:a65:578b:: with SMTP id b11-v6mr5377313pgr.57.1526986330084; Tue, 22 May 2018 03:52:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526986330; cv=none; d=google.com; s=arc-20160816; b=HzIyd8//rwYyVXxwnVrGhuUn/siJjoHjYpJQ+a8o3zwBhZPlx4NfdFvxbaNYYlo7Xr caCXA8voNOBgC95yg7T/XsWtshMCZAuSz+tsdki+h8krx3AsQLutko+vgWHToB7k6/zS zRYMMIeA4WuoLQFFqFL9GLkQ8Yd4UMwK2OyqN0D35NdLXtCnN+xzQLn3xQJauqigUGtk Lj82Y3Xq6yO6VA1o+skNqKF14Kpiqg7avey2KhN3NvNQDlzAcqY4BuT5KO6m1S9303wg BxDuDhmgVSlmeADiX0ebw95e4PVwnZzpxw6YgsXmvAvhlMqJWwqB+tBN84WfpAjbHKse rPzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=HcvmPGEiApQU8Lz4t09G1rHub9OFiPFmyf0XPgYAreE=; b=gKYRuqLkvXziNm9KPRV9FO01sJ1cKQe2g2aGcVHIsz4cQgB44Ntfnjl/ihd6EDg6iV QI38A6S2oDum4be6ndnT/zSzb/Cp/80Q/Ha3Zh80mzN8KSTO1QftPcFY+x5zJi2IbKQG tpCK99oxcCriz4hSYfN4KFDf3dik707/7SjyfU3yecm4xPPc15P9cGwdvI3VEsD5H3Bo ioKeoVBoO9jzR33FdIyBH+DlnclnhzFVjPlkp0plvST6/vucsLhiIg5czfmKbFblDRXS ku548dRNXrLRtXVKgiuBwJU84Mekxpi6pEpHwLmqOW8F/1yVReo/ZnnZppYA/ghniAFi s1Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TSrfQS85; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m2-v6si11178380pgc.588.2018.05.22.03.51.55; Tue, 22 May 2018 03:52:10 -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=@linaro.org header.s=google header.b=TSrfQS85; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751302AbeEVKuM (ORCPT + 99 others); Tue, 22 May 2018 06:50:12 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:40250 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751002AbeEVKuJ (ORCPT ); Tue, 22 May 2018 06:50:09 -0400 Received: by mail-pg0-f65.google.com with SMTP id l2-v6so7701764pgc.7 for ; Tue, 22 May 2018 03:50:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=HcvmPGEiApQU8Lz4t09G1rHub9OFiPFmyf0XPgYAreE=; b=TSrfQS85P4/2MSWByf4Td07ViQIHiJJZs5NVYh0+JcUH9kcqu9mYUxKbBdJw1c2d8s EHQaSygbokzcmo5GlKjEbxiKZw/YNlKKEcJLPGTOQ1cknoJXpCd87P2ioVzHkntnUbTL vu6t9xSF1ljLzw81bdbn+YUgwudd7NLpI68/k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=HcvmPGEiApQU8Lz4t09G1rHub9OFiPFmyf0XPgYAreE=; b=QDeAaWdQSG3IoodM3lxkVF7sS11sDShx6UrHuQgBEkiOgewWZCoy1Jvhrz7nTfTcSW XqJhWYmwoJQ9ghoPFLg8Vs/tT1FZpuP6e7HZnGEWqSa6QJ6jIq/CrrRFxSpoES3CIGQ+ v9e1uDd+NC+UgFA3NzoBwplU2+pmys/xXz7JMoiqX2Guet5dhP7VWngQmVHYkFjtzdTD wUjjzgccm+TmC7Tnrk135fIHnMrxE5WgqVmzny+Rhv0fPhDyGpggR0jUpBgYK2Gu9g75 62tjGO44raf5IAWXYpb6rKocGrHm7nqhTUaCYC9foTN5kq1G91wTNLsw2vFpnSiWCdfv OAlA== X-Gm-Message-State: ALKqPweDt4LVS8xWJe/5cibtk0YBY+za765xRRxh6t5NfECfQ0qhF8Po 2F5/6eB7MlMHCtfPIkpF/BpTgA== X-Received: by 2002:a65:4805:: with SMTP id h5-v6mr18594663pgs.96.1526986208566; Tue, 22 May 2018 03:50:08 -0700 (PDT) Received: from localhost ([122.167.163.112]) by smtp.gmail.com with ESMTPSA id k193-v6sm20735458pgc.39.2018.05.22.03.50.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 May 2018 03:50:07 -0700 (PDT) Date: Tue, 22 May 2018 16:20:06 +0530 From: Viresh Kumar To: "Joel Fernandes (Google.)" Cc: linux-kernel@vger.kernel.org, "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: Re: [PATCH v2] schedutil: Allow cpufreq requests to be made even when kthread kicked Message-ID: <20180522105006.hvntwnhzzvhznij2@vireshk-i7> References: <20180518185501.173552-1-joel@joelfernandes.org> <20180522103415.cuutobi5kbhj4gcw@vireshk-i7> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180522103415.cuutobi5kbhj4gcw@vireshk-i7> User-Agent: NeoMutt/20180323-120-3dd1ac Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 22-05-18, 16:04, Viresh Kumar wrote: > Okay, me and Rafael were discussing this patch, locking and races around this. > > On 18-05-18, 11:55, Joel Fernandes (Google.) wrote: > > diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c > > index e13df951aca7..5c482ec38610 100644 > > --- a/kernel/sched/cpufreq_schedutil.c > > +++ b/kernel/sched/cpufreq_schedutil.c > > @@ -92,9 +92,6 @@ static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time) > > !cpufreq_can_do_remote_dvfs(sg_policy->policy)) > > return false; > > > > - if (sg_policy->work_in_progress) > > - return false; > > - > > if (unlikely(sg_policy->need_freq_update)) { > > sg_policy->need_freq_update = false; > > /* > > @@ -128,7 +125,7 @@ static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time, > > > > policy->cur = next_freq; > > trace_cpu_frequency(next_freq, smp_processor_id()); > > - } else { > > + } else if (!sg_policy->work_in_progress) { > > sg_policy->work_in_progress = true; > > irq_work_queue(&sg_policy->irq_work); > > } > > @@ -291,6 +288,13 @@ static void sugov_update_single(struct update_util_data *hook, u64 time, > > > > ignore_dl_rate_limit(sg_cpu, sg_policy); > > > > + /* > > + * For slow-switch systems, single policy requests can't run at the > > + * moment if update is in progress, unless we acquire update_lock. > > + */ > > + if (sg_policy->work_in_progress) > > + return; > > + > > if (!sugov_should_update_freq(sg_policy, time)) > > return; > > > > @@ -382,13 +386,27 @@ sugov_update_shared(struct update_util_data *hook, u64 time, unsigned int flags) > > static void sugov_work(struct kthread_work *work) > > { > > struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work); > > + unsigned int freq; > > + unsigned long flags; > > + > > + /* > > + * Hold sg_policy->update_lock shortly to handle the case where: > > + * incase sg_policy->next_freq is read here, and then updated by > > + * sugov_update_shared just before work_in_progress is set to false > > + * here, we may miss queueing the new update. > > + * > > + * Note: If a work was queued after the update_lock is released, > > + * sugov_work will just be called again by kthread_work code; and the > > + * request will be proceed before the sugov thread sleeps. > > + */ > > + raw_spin_lock_irqsave(&sg_policy->update_lock, flags); > > + freq = sg_policy->next_freq; > > + sg_policy->work_in_progress = false; > > + raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags); > > > > mutex_lock(&sg_policy->work_lock); > > - __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq, > > - CPUFREQ_RELATION_L); > > + __cpufreq_driver_target(sg_policy->policy, freq, CPUFREQ_RELATION_L); > > mutex_unlock(&sg_policy->work_lock); > > - > > - sg_policy->work_in_progress = false; > > } > > And I do see a race here for single policy systems doing slow switching. > > 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-next-freq; > unlock(); > > > > Is the above theory right or am I day dreaming ? :) And here comes the ugly fix: diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 35826f4ec43c..1665da31862e 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -283,6 +283,9 @@ static void sugov_update_single(struct update_util_data *hook, u64 time, ignore_dl_rate_limit(sg_cpu, sg_policy); + if (!policy->fast_switch_enabled) + raw_spin_lock(&sg_policy->update_lock); + /* * For slow-switch systems, single policy requests can't run at the * moment if update is in progress, unless we acquire update_lock. @@ -312,6 +315,9 @@ static void sugov_update_single(struct update_util_data *hook, u64 time, } sugov_update_commit(sg_policy, time, next_f); + + if (!policy->fast_switch_enabled) + raw_spin_unlock(&sg_policy->update_lock); } static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) -- viresh