Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1401531imm; Tue, 22 May 2018 03:34:50 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoxhYKKJetKqfSgR+4vxE51HyFYbrioqkH9t4Kv7I3yyvrxlwkPHEILB+2Lnxgxkne7sP/Q X-Received: by 2002:a65:4c8d:: with SMTP id m13-v6mr18934826pgt.310.1526985290582; Tue, 22 May 2018 03:34:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526985290; cv=none; d=google.com; s=arc-20160816; b=jjb46sMT3e7FDWLEtaRkHXovIIz8qh5ZqOabTgW/96v+lRsZIf0S01K0FlDggOXpL0 40kJzJRhF3mZgehqW2pt4rlmkifwggqCqlHNCpA82pJ6lO1kuVG1FV+mgP2stfbhxhTs OHqR/do3Xw4yOTmF6JjMBXszxe1YV5VULmIY3gnUNFBwjf9YGJGCSc9yZRt5BfFpQ2mp j/gdq3VfeclijjRN8quur1UFhwFjJivjFeG3xIijOIYSuL/9ZEqb6m6+EeYJRKwFNKTE AoQ+MQdnRsZsemRM6/HdeBkZwJ0NjS/6BbOcmLDPzXIH4z6v6JPYKW1tZtBqxu6gGrnn +r0Q== 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=WY3dCp80EJbMR23GVPQg1zhzjh/IWAyEEkHMTG9Iw8c=; b=HqAWD67++sTx0o1YjPoOwEpNmYL9ZNv6x4Y5ez9wCLH9W+maekf1qZcCcqsjAA0MtK dPEaKJUojq+BqKwr9qp4CYJLeN8mW+gy9EjxCultI131ATNJUYO5tdwbQ6rPkZNRFoUA b4uFeiqhHXwM+/xIvRQxK+6XKv2coWv41+ARgcz+HaB5n2JoE39af0YUz2K69/p/rSSr R3u5N2rPU2ogcJ1XSlmuQ358zd13Bstjjximgxktu8UcvlJzSTo2iF7g9VROo5cM4/qp UU4slOwl2km8YgpG7xLdckBosVpKcAv4FG0ts08ybmmsawOgFkkxldLZJZUeXNTvWvhn zfEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SKCaKMmC; 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 e191-v6si12782557pgc.233.2018.05.22.03.34.33; Tue, 22 May 2018 03:34:50 -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=SKCaKMmC; 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 S1751301AbeEVKeW (ORCPT + 99 others); Tue, 22 May 2018 06:34:22 -0400 Received: from mail-pl0-f67.google.com ([209.85.160.67]:41119 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750733AbeEVKeT (ORCPT ); Tue, 22 May 2018 06:34:19 -0400 Received: by mail-pl0-f67.google.com with SMTP id az12-v6so10650428plb.8 for ; Tue, 22 May 2018 03:34:18 -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=WY3dCp80EJbMR23GVPQg1zhzjh/IWAyEEkHMTG9Iw8c=; b=SKCaKMmChJxHpeKwDxkO8+zHw7vWH7yrt3e1N3qIjI2lm56h4yUnX11Rx4O96qUPtY JKzQgiWhtIFlAGmbV/bAjq8uqZvBjZGwRl/h4XzdS/Bms8QUuIMxV8cApRocOhw3etDq ikYT9IHE9qYjAKVzOEbm13yyNbDuRJfAjFBEU= 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=WY3dCp80EJbMR23GVPQg1zhzjh/IWAyEEkHMTG9Iw8c=; b=od5MXar5xrTvPr3Y6f9BZyAzn5RJqqlVMWHlYHI+XAlIng+Y524RChshVkQNNg7skx ci9PwZxY5qamd0XWt4yyLRUUQTF0U+ztSkRRG4VkjISHVAf3pfEtFI0Wd/qyCY6xyQJt paaHRtsI/hX5iMZvvC/sCe2D67M4b7Psk18vWdBXEgL+4vepiW0cdHKkJD9/iEfd9G2a 7y9ODYNSR1p7rbgbcqxccLfcPCTLqlhydd3hkoo/zIiLQzvDwBOOB9X/GWkoFR2CDnMI 4JU6Ymo+DM/LEcIGsnysUnsP3jezOy2Ox+TPabOrMhS89NSGJpfdpvCZY554rmLqHcTd VjZQ== X-Gm-Message-State: ALKqPwf41ArDFoAKb5o4HkPaedl2+orYylB7UojiseUgUPs55GU2Yuh4 FW7rH10m5abpf9pyn0ir/vkUIw== X-Received: by 2002:a17:902:7c18:: with SMTP id x24-v6mr24705148pll.173.1526985258353; Tue, 22 May 2018 03:34:18 -0700 (PDT) Received: from localhost ([122.167.163.112]) by smtp.gmail.com with ESMTPSA id a18-v6sm25501882pfn.117.2018.05.22.03.34.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 May 2018 03:34:17 -0700 (PDT) Date: Tue, 22 May 2018 16:04:15 +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: <20180522103415.cuutobi5kbhj4gcw@vireshk-i7> References: <20180518185501.173552-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180518185501.173552-1-joel@joelfernandes.org> 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 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 ? :) -- viresh