Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1418043imm; Tue, 22 May 2018 03:53:43 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoIFohAeZAWAoqcEcTyVnds0JJSp06fzmqdWt7YRIn1rG811IBPukBRrQIAqOelZkdjZK5q X-Received: by 2002:a65:53c9:: with SMTP id z9-v6mr18611323pgr.356.1526986422974; Tue, 22 May 2018 03:53:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526986422; cv=none; d=google.com; s=arc-20160816; b=jXab4ZYfoSEpoIcWdWBZ5yo7HYq+V9BV+9qVWF0/6BWm5JFT1krnc940QU4Mno9dRt QpKIeDEyqN7rRLv54l8pE39ttv5pmoHVkNV1GitxoVozoVc0a/pH8j+IUDbRAPX8KEYV nhYMqnnJvCbitiR6XjCHoo6yW1ZGL4kfaS42rTsRFFbliT0pj9KpJrvAlEPE+S+DlTh3 fl0tiQvzzpcrGQN7eE8Lxx+hdLXUBL5sHfVzemc7y1a17ogd+r36PCpBmNHtesIrE0kl oxr1bThtQinlZc5wjJhYAh060y/lqc5+/dLss593sqff4+A3X/OG/SxdfIOmw03YKi4t qCgA== 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:arc-authentication-results; bh=7GDsOFTR7LcoL1yo+V4N5bxOWoBd4uS1u5R/PrvGO/Q=; b=Ezrigy6Jyu3UVUD0wcCjtfCxN0SCzX+rikxAPZ/2IsUnJzRJkrhX5/bfo2O8b3JrpM Y1+m3GjospTPNF1/wZFIxwcvZ719DEntTdPSCH7bA/OAF5/jPqqO5W9oyNHMlJYiGwxk aaUMVZex+cThONKn+k0zO3A1tPizxb5JFRhDusNnNAarNzv5xcddTnqzO+KreO/UKZf0 no2389UKIMnJ/qWS+gipXpeRKjWl7JDtel9e8BISrjDoi7QgQYRAkZvVO9hC5mQXqbhP u0OPJ2Xexuv1c5MdQmRneK2uhz9jUPxpUj6j0oNLr1H60zr2mQarE+vhnGlVtKFPZtUB cg7g== ARC-Authentication-Results: i=1; mx.google.com; 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 v1-v6si138235pgf.75.2018.05.22.03.53.28; Tue, 22 May 2018 03:53:42 -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; 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 S1751415AbeEVKwD (ORCPT + 99 others); Tue, 22 May 2018 06:52:03 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:34730 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751106AbeEVKwC (ORCPT ); Tue, 22 May 2018 06:52:02 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2DD7B1596; Tue, 22 May 2018 03:52:02 -0700 (PDT) Received: from e110439-lin (e110439-lin.cambridge.arm.com [10.1.210.68]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D5E303F24A; Tue, 22 May 2018 03:51:59 -0700 (PDT) Date: Tue, 22 May 2018 11:51:57 +0100 From: Patrick Bellasi To: Viresh Kumar Cc: "Joel Fernandes (Google.)" , linux-kernel@vger.kernel.org, "Joel Fernandes (Google)" , "Rafael J . Wysocki" , Peter Zijlstra , Ingo Molnar , 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: <20180522105157.GX30654@e110439-lin> 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.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 22-May 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: > > @@ -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 ? :) It could happen, but using: raw_spin_lock_irqsave(&sg_policy->update_lock, flags); freq = READ_ONCE(sg_policy->next_freq) WRITE_ONCE(sg_policy->work_in_progress, false); raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags); if (!READ_ONCE(sg_policy->work_in_progress)) { WRITE_ONCE(sg_policy->work_in_progress, true); irq_work_queue(&sg_policy->irq_work); } should fix it by enforcing the ordering as well as documenting the concurrent access. However, in the "sched update" side, where do we have the sequence: sg_policy->next_freq = 0; sg_policy->next_freq = real-next-freq; AFAICS we always use locals for next_freq and do one single assignment in sugov_update_commit(), isn't it? -- #include Patrick Bellasi