Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp118761imm; Tue, 22 May 2018 15:10:26 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq6+/mM93U3Bh7//LelkfZIbto4+FhA6Yg3h9UOta6V5Mr1jII9AlZhbK99CCtdc2hvK9ge X-Received: by 2002:a63:6301:: with SMTP id x1-v6mr220463pgb.124.1527027026099; Tue, 22 May 2018 15:10:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527027026; cv=none; d=google.com; s=arc-20160816; b=rV6NUL6srqCxL3qlT3o3rY1JR+NgQ5ljJXkpNXzJPY6/JqxqY0OZHfuyg2JS5gKSXy AxaK0VJlCY6T5ArtVQgtDAPeATA4h3uk+6GnMAjIx0RaiUUOBDTP/JmH4Np52YNmk6KD DiG6CYlq2zJPoSMx6AjPrYWvzLO3O2q8VSUXbf/azHEtxovINUkS1iK4A1bqTglHJPWE VJPJSOLaddd5r0v4IjvnhrdmTtMR88Ti2899jLy3LcU9mmvbh2qedOBQBEY3untWSR0W tBZv4alZYN2MMX1Pdh9cYel+HG2EurT8EqbeYA+kSTlVHd8M3MKr8YTUEXxnjZWRVQMX IS5g== 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=CshSV63yYlBmpWGBkfPg97A2X0Amfymo3ubUKy5+dqM=; b=SuVssEbqDUsQ/Oehm3FG6uE0jvd/zYxg4tR8sbQOsyipxTDTTFkA9Hlij42+KcuNVq 4sSJRqRyD0H/2rcRkoIic8ZyJ65Qz+mD3xPmayop47+GXz91IijmZCBPlvbQG+jpdWq2 uK4X+KxrPVu8cN678tDnDcAyfUB6eFisReKxp/6BwjPN13Z5HYi6HEExraMna8RJEH/9 IeqV2M/EseXcU0tZtwC+hinvUhHQQqE1phnZ1c2qMbxOXaQkJl6QSclX+DBSq4GH0Yt8 /N4BuhZUc6PesYa5zRKMEwZJxqzpcV9sFjrV+eNSP94zgcUEHOLX3j2D0v4RJahTCVZb et0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=v+GZhJIg; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j9-v6si1996549pgn.86.2018.05.22.15.10.11; Tue, 22 May 2018 15:10:26 -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=@joelfernandes.org header.s=google header.b=v+GZhJIg; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753299AbeEVWJ5 (ORCPT + 99 others); Tue, 22 May 2018 18:09:57 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:43113 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753175AbeEVWJ4 (ORCPT ); Tue, 22 May 2018 18:09:56 -0400 Received: by mail-pf0-f193.google.com with SMTP id j20-v6so9426829pff.10 for ; Tue, 22 May 2018 15:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=CshSV63yYlBmpWGBkfPg97A2X0Amfymo3ubUKy5+dqM=; b=v+GZhJIg4fMu7GiWzY4vmyiTErbAWROZ1ipl/SYSqoujKatJRDKfucqEP7al/QIYcY zS4387MbxXk4dK8sqVsx6E/zkPDHL4MFE4KqsCa2hgdNTuIrtt2bnlH+rjr6NJViKnyS SqUycxcEFXth6mZxK0pygda6pvkxzaLTlaO7w= 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=CshSV63yYlBmpWGBkfPg97A2X0Amfymo3ubUKy5+dqM=; b=kzQD6zu272aRO0jU20CYDsnTUcVqg1SLQ/pi5PhAafyirqC8S3twRa8Nip7mEM+KDO TUZR0p9XH7wZ6X1taEk5HHcQper8A05GyR/Ii2Hijaan+bvORYDBjTz6WiVcbzsk1MVn Rmt+h3OkDmZHMZnWYmb+3xbnnxYg0FaNSJOGu0KmuWerkd56s+g6hIqhT1wRN3NbxywX oO2CNQyaW7QoPcOYhP3YBJW7bJ0upF2XBiujoSZqQRlCxEgiNJ0niT9GQjfoPcxq8zb/ 2VWgsX6rVJ3TDAgYnb/Xg29FCnv3vC8nZgyazhHzBgbVfWb+sGllUtUnz3iHK50iUsMZ 4TAg== X-Gm-Message-State: ALKqPwcqFNQ/2clffD+ov/sBe/KN7fK2BbqqQkSXDBBWX9c/pbPxIa6X 23QlUVPeVTxnU1ecSLNkoCjIJQ== X-Received: by 2002:a62:14c3:: with SMTP id 186-v6mr264466pfu.92.1527026995451; Tue, 22 May 2018 15:09:55 -0700 (PDT) Received: from localhost ([100.96.59.14]) by smtp.gmail.com with ESMTPSA id a195-v6sm36351588pfa.143.2018.05.22.15.09.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 22 May 2018 15:09:53 -0700 (PDT) Date: Tue, 22 May 2018 15:09:53 -0700 From: Joel Fernandes To: Viresh Kumar Cc: "Joel Fernandes (Google.)" , linux-kernel@vger.kernel.org, "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: <20180522220953.GB40506@joelaf.mtv.corp.google.com> 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: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. I'll send out a v3 with Acks for the original patch, and the send out the smp_mb() as a separate patch if that's Ok. thanks, - Joel