Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp550082imm; Wed, 23 May 2018 01:19:17 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpcuNiyludtHQv2CEAqKxhJFM8LVjGhvTajRbne2G9WSs281pbDNAiXldqxhe3R5ie84bAg X-Received: by 2002:a62:c898:: with SMTP id i24-v6mr1921499pfk.35.1527063557346; Wed, 23 May 2018 01:19:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527063557; cv=none; d=google.com; s=arc-20160816; b=oJZuEsO+Fl3QJMCo2t8SkWqyrHgg9Xcr3Je34mqgWXP9jM5oR5Iy6svG3zBihgDoUT vwCEpEl1w+7NAFBLN+gWxAH2zusMlUzv8kYyB1RTjK6E3N4P21ebPwNRkSPx/n2yBYmp SjXBjBrkWFuFRh/FPAQHyasYdabU+K2NyzcZpD5YgtTrLzfBl7SkRTic0XxpjXT0/ES3 mm5cF7QL1GdoJEuH6K/Gt9kCce2ZBqkf9bnSRha4aAyvKNj8we8QY2C2tU46SsOTSgUN w7Ig+8F9nsJaOGWtQNqfUGHTxYL0i2JQvkWx24SM3rUk57acDLva521sIBacGLvDVrEi sO+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=zCrcb7h4Ajn/yttANcU+Kh2GVEqH5jDjiY8yvNXwHJ8=; b=lwnfFFBNmPKq5KroYI/U42L2eYvSwV9CrrAHdvO5qVe0B3uPIFZsU0ScuQWxj69Ezt 3nahUyWScnQ2yq7WlWzy+AlKyBgF54mdEogjwuepBCKByDi/BtrhWaSlZYOPrzPMu+Qa vRajlFJG5SDEmCAtRtldwvuy6U03jKFPNiw4jEmLzvBmCTf7bkg8RtC8sQ/Juaevoidx s0iSpjnBot3FV9q9DsF/YQCKTa0Q4Q1A4azPEqnbBBrBcKxKrAmHmzI1HEAEIDma6zu6 pmPizmORH1jK7vAY9k97Vln4zRtNdQVsEv80Kqh5AKQ8lDys/e7guzrb7hsUvnsvq8qc 9cig== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=q4y2cxHh; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r39-v6si18721620pld.249.2018.05.23.01.19.02; Wed, 23 May 2018 01:19:17 -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=fail header.i=@gmail.com header.s=20161025 header.b=q4y2cxHh; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932121AbeEWISa (ORCPT + 99 others); Wed, 23 May 2018 04:18:30 -0400 Received: from mail-oi0-f66.google.com ([209.85.218.66]:44564 "EHLO mail-oi0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932105AbeEWIS0 (ORCPT ); Wed, 23 May 2018 04:18:26 -0400 Received: by mail-oi0-f66.google.com with SMTP id e80-v6so18700577oig.11; Wed, 23 May 2018 01:18:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=zCrcb7h4Ajn/yttANcU+Kh2GVEqH5jDjiY8yvNXwHJ8=; b=q4y2cxHhBRXIcYGJeVUyT1IMIbN69jOogCEY+UeUi/znlofRzkDustjrcxmba+QWGp toy9eTvLViSGButGxa02uN25/A9RYtD7Jir+Djm0ltOBVx2ZhUP1iw48gFXZpoS9ySi1 vz/UJl9ueCWptLgfMBBeBUWwCbyLA547jYzcEaS6Dgrqdb3VdAIXuuHAM/rasxBhN/MM RzZksFzlNAjVJYQ8MddL2RqoKRmvsn6hagLlyfItYn2bnoKJ4nne81lS7xyozD90d0nD ZdASUvLGx6qiMmmdge7wOlRnawambQyCPTIsE0kMdRWxDWcGn28vUdEvdb5CMX2OpYBW hkUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=zCrcb7h4Ajn/yttANcU+Kh2GVEqH5jDjiY8yvNXwHJ8=; b=Mot7D8x2XUUoCeXn5JvlqxSTItqGND7FqSaiAwtXlQusMbwxkCm8wuViNg9LU7fGXi SUl/8KWUVQ16lxkpIr64NtJ61HKhCsD4JRCCNBmkzAL/de9H0vMHsTGTKXVopL0yEoh1 zW1AC5clzMV35ZTblliXzoEBs0tgrQKEPVK5KUvQRp52PlSHSwxouW+sTpOpc2jMAwiR GjckiL84VtLXOXQ3sFa3Q7TRMYXoO6pEk3+g7KYgr1HfgV5EUXf8vFW1mnI5FAHd4UxQ k0byOuE9Eqly3XgJl/+KFzOYV5YfggHZiHFRtCRXXxOYW+1uI3o74cHm+Jk1THXKBnL/ f3OA== X-Gm-Message-State: ALKqPwdONWMttOUk5L9SEPZz23ER5ILfO9zUmBGHDwL1UO2OOyDgdhnY BBY+Coexn3NEHAyh4N4ZeBCh9zwFea/qNLhmY88= X-Received: by 2002:aca:681:: with SMTP id 123-v6mr899108oig.358.1527063506142; Wed, 23 May 2018 01:18:26 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a9d:1468:0:0:0:0:0 with HTTP; Wed, 23 May 2018 01:18:25 -0700 (PDT) In-Reply-To: <20180522220953.GB40506@joelaf.mtv.corp.google.com> References: <20180518185501.173552-1-joel@joelfernandes.org> <20180522103415.cuutobi5kbhj4gcw@vireshk-i7> <20180522220953.GB40506@joelaf.mtv.corp.google.com> From: "Rafael J. Wysocki" Date: Wed, 23 May 2018 10:18:25 +0200 X-Google-Sender-Auth: tJZLhCV7EO_18_UxPOSMGAvrM_M Message-ID: Subject: Re: [PATCH v2] schedutil: Allow cpufreq requests to be made even when kthread kicked To: Joel Fernandes Cc: Viresh Kumar , "Joel Fernandes (Google.)" , Linux Kernel Mailing List , "Rafael J . Wysocki" , Peter Zijlstra , Ingo Molnar , Patrick Bellasi , Juri Lelli , Luca Abeni , Todd Kjos , Claudio Scordino , kernel-team@android.com, Linux PM 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 On Wed, May 23, 2018 at 12:09 AM, Joel Fernandes wrote: > On Tue, May 22, 2018 at 04:04:15PM +0530, 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(); >> > > I agree with the race you describe for single policy slow-switch. Good find :) > > The mainline sugov_work could also do such reordering in sugov_work, I think. Even > with the mutex_unlock in mainline's sugov_work, that work_in_progress write could > be reordered by the CPU to happen before the read of next_freq. AIUI, > mutex_unlock is expected to be only a release-barrier. > > Although to be safe, I could just put an smp_mb() there. I believe with that, > no locking would be needed for such case. Yes, but leaving the work_in_progress check in sugov_update_single() means that the original problem is still there in the one-CPU policy case. Namely, utilization updates coming in between setting work_in_progress in sugov_update_commit() and clearing it in sugov_work() will be discarded in the one-CPU policy case, but not in the shared policy case. > I'll send out a v3 with Acks for the original patch, OK > and the send out the smp_mb() as a separate patch if that's Ok. I would prefer to use a spinlock in the one-CPU policy non-fast-switch case and remove the work_in_progress check from sugov_update_single(). I can do a patch on top of yours for that. In fact, I've done that already. :-) Thanks, Rafael