Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3307099imc; Wed, 13 Mar 2019 14:24:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqwCaBPjMzNbMkqajubAri6daxNdjgyvdyMSPVkC1gViKqX6DUOPkHBnih9KT94JA/w8PZk5 X-Received: by 2002:a63:69c2:: with SMTP id e185mr41865996pgc.4.1552512264278; Wed, 13 Mar 2019 14:24:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552512264; cv=none; d=google.com; s=arc-20160816; b=QQ6hwD4nbdawA18kGWl3Esv7qDvtDMfuFTi0blIb9ctR3YZ0y9rmlnHw9RhESsw9vG H+y4qwjme8uOm/G33XQAhwsLhHOR70BiAYW8xktrCKA1tKw9Bwl77CgT15ukh7C3oPwa mewcq3aS7+zqDl1tbKfyrdVSsqCrC5sHB7D2h//cVGQxT4iM+wv6CvQdUOrUJ4NSEwKn aapWSK36aNHRJzdXX8iprvZVZKcLmvK5qELnnZNKgEbX7bmLvwqzVyibwnpcKdB9Y5mr pc3l3bCHnGIXCpjNR4fobiu2rwSSmTv+jM0CPFeYyOU+dBqkgAfJqDRWp10nTPmGet/H x9VA== 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 :in-reply-to:references:mime-version:dkim-signature; bh=d80SjBQPI372fPrNSR2OW/xZJywruVGwSVEcb7iHozw=; b=MFXtupnXuaUoZ243ahvDhFKGAr/VrzbwGsIQVdozXFo+goM7bPIMwohYKjdtKcpsck 64ELFZ3f1ZYIhWPt97zntmMRIFMan4o6nFcrmhOThBzs5iJjgMnouIeIFqSSSVux331y peyW7aZypAGIJwgG3Uz6MbvFnTch2zuN3nvsTMcCz+ot8rxX8dKxUgKh0cqFiMvUNKen rCeWpqhJE5vZbapTL97DQG7IDpXTM8uLVEiXllViaQP7wOmk08vSO/3U6YC4r0vOzVom UJQN08uakrRDClgWq0njMGNIbp2RmSXUH1aq4wYnQd4cjnaxFNwLqXTCdhGsZYEYVKFq MTRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tuRV1Zu7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y19si11813141pfc.229.2019.03.13.14.24.08; Wed, 13 Mar 2019 14:24:24 -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=@google.com header.s=20161025 header.b=tuRV1Zu7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727439AbfCMVX1 (ORCPT + 99 others); Wed, 13 Mar 2019 17:23:27 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:53824 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727357AbfCMVX1 (ORCPT ); Wed, 13 Mar 2019 17:23:27 -0400 Received: by mail-wm1-f65.google.com with SMTP id e74so763591wmg.3 for ; Wed, 13 Mar 2019 14:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=d80SjBQPI372fPrNSR2OW/xZJywruVGwSVEcb7iHozw=; b=tuRV1Zu7sfoxA4yP2I/0oN8wA9yG3pZK97eA8vRB1jhJwn6Gk/QLm6dhBX/PBVbZZ0 qAyDIMjrmE2LvMxayNrJiYfj0RLVMxzhCWmQiK9XAa2tWN+gmcTymxbjXQ6W1QT0rtWf 7Y0jpKpUxi51iXNjsfQ4WpRp5fpbTaixyKB50NmnXehVmaNcPqYe+plOX3a1FaqzjE/A HU8D4LzWXu/Z3fJUnjy2UYLf6vnsRA+SjmR5tomDI4U74gM1Yu6p5WIBlZnKTAvsn3CL b86oSTiOAcB5aJ4/JCQODN7VM1ok/wTj2cR9M47hmofAdnJLOv93MTR3blOdu4ceZzKS 9Mww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=d80SjBQPI372fPrNSR2OW/xZJywruVGwSVEcb7iHozw=; b=ZYGItSS+ur5vY31t0Mn77++rkeB1MX7WFQOZ+viz0nGEuhVCAHm5BcCZN+U816bogV AL47+J8BmYWKsdzpz4mE7nE7f+t6Tok0qLigPVRwgZnG2zv8aixQ/eGWGHEbU7BOO60x 9eyBzWSwAnIgox4QQAT56dnPV/tyWWX85w4eG0ylaUOg3/ebshDRLcOIqd7kVzR7MtiJ 3sBPyN6ATJEQriAd1wWhfoQl2VQzARg4WzmVzdWrDeFUFjO1iq/ch6fyZVi4zuJD5zXM VKUKuwY+FMGlxqbZK/vbeIUNMlDoZDfQSe2xpy5dg9u7nWFrLhzR/Yzuhb7T4t6+lxRZ T6NQ== X-Gm-Message-State: APjAAAWtZU8OGs6sjH/4XGL7HqTICenfiFUkkLS2CP5LMcSMvkF2p898 BupjRSN8ixdJoiSczA/TCDkRso6eECja8Ve1PpvEqg== X-Received: by 2002:a1c:f61a:: with SMTP id w26mr166566wmc.70.1552512204479; Wed, 13 Mar 2019 14:23:24 -0700 (PDT) MIME-Version: 1.0 References: <20190208100554.32196-1-patrick.bellasi@arm.com> <20190208100554.32196-2-patrick.bellasi@arm.com> <20190313135238.GC5922@hirez.programming.kicks-ass.net> In-Reply-To: <20190313135238.GC5922@hirez.programming.kicks-ass.net> From: Suren Baghdasaryan Date: Wed, 13 Mar 2019 14:23:13 -0700 Message-ID: Subject: Re: [PATCH v7 01/15] sched/core: uclamp: Add CPU's clamp buckets refcounting To: Peter Zijlstra Cc: Patrick Bellasi , LKML , linux-pm@vger.kernel.org, linux-api@vger.kernel.org, Ingo Molnar , Tejun Heo , "Rafael J . Wysocki" , Vincent Guittot , Viresh Kumar , Paul Turner , Quentin Perret , Dietmar Eggemann , Morten Rasmussen , Juri Lelli , Todd Kjos , Joel Fernandes , Steve Muckle 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, Mar 13, 2019 at 6:52 AM Peter Zijlstra wrote: > > On Fri, Feb 08, 2019 at 10:05:40AM +0000, Patrick Bellasi wrote: > > +/* > > + * When a task is enqueued on a rq, the clamp bucket currently defined by the > > + * task's uclamp::bucket_id is reference counted on that rq. This also > > + * immediately updates the rq's clamp value if required. > > + * > > + * Since tasks know their specific value requested from user-space, we track > > + * within each bucket the maximum value for tasks refcounted in that bucket. > > + * This provide a further aggregation (local clamping) which allows to track > > + * within each bucket the exact "requested" clamp value whenever all tasks > > + * RUNNABLE in that bucket require the same clamp. > > + */ > > +static inline void uclamp_rq_inc_id(struct task_struct *p, struct rq *rq, > > + unsigned int clamp_id) > > +{ > > + unsigned int bucket_id = p->uclamp[clamp_id].bucket_id; > > + unsigned int rq_clamp, bkt_clamp, tsk_clamp; > > + > > + rq->uclamp[clamp_id].bucket[bucket_id].tasks++; > > + > > + /* > > + * Local clamping: rq's buckets always track the max "requested" > > + * clamp value from all RUNNABLE tasks in that bucket. > > + */ > > + tsk_clamp = p->uclamp[clamp_id].value; > > + bkt_clamp = rq->uclamp[clamp_id].bucket[bucket_id].value; > > + rq->uclamp[clamp_id].bucket[bucket_id].value = max(bkt_clamp, tsk_clamp); > > So, if I read this correct: > > - here we track a max value in a bucket, > > > + rq_clamp = READ_ONCE(rq->uclamp[clamp_id].value); > > + WRITE_ONCE(rq->uclamp[clamp_id].value, max(rq_clamp, tsk_clamp)); > > +} > > + > > +/* > > + * When a task is dequeued from a rq, the clamp bucket reference counted by > > + * the task is released. If this is the last task reference counting the rq's > > + * max active clamp value, then the rq's clamp value is updated. > > + * Both the tasks reference counter and the rq's cached clamp values are > > + * expected to be always valid, if we detect they are not we skip the updates, > > + * enforce a consistent state and warn. > > + */ > > +static inline void uclamp_rq_dec_id(struct task_struct *p, struct rq *rq, > > + unsigned int clamp_id) > > +{ > > + unsigned int bucket_id = p->uclamp[clamp_id].bucket_id; > > + unsigned int rq_clamp, bkt_clamp; > > + > > + SCHED_WARN_ON(!rq->uclamp[clamp_id].bucket[bucket_id].tasks); > > + if (likely(rq->uclamp[clamp_id].bucket[bucket_id].tasks)) > > + rq->uclamp[clamp_id].bucket[bucket_id].tasks--; > > + > > + /* > > + * Keep "local clamping" simple and accept to (possibly) overboost > > + * still RUNNABLE tasks in the same bucket. > > + */ > > + if (likely(rq->uclamp[clamp_id].bucket[bucket_id].tasks)) > > + return; > > (Oh man, I hope that generates semi sane code; long live CSE passes I > suppose) > > But we never decrement that bkt_clamp value on dequeue. > > > + bkt_clamp = rq->uclamp[clamp_id].bucket[bucket_id].value; > > + > > + /* The rq's clamp value is expected to always track the max */ > > + rq_clamp = READ_ONCE(rq->uclamp[clamp_id].value); > > + SCHED_WARN_ON(bkt_clamp > rq_clamp); > > + if (bkt_clamp >= rq_clamp) { > > head hurts, this reads ==, how can this ever not be so? > > > + /* > > + * Reset rq's clamp bucket value to its nominal value whenever > > + * there are anymore RUNNABLE tasks refcounting it. > > -ENOPARSE > > > + */ > > + rq->uclamp[clamp_id].bucket[bucket_id].value = > > + uclamp_bucket_value(rq_clamp); > > But basically you decrement the bucket value to the nominal value. > > > + uclamp_rq_update(rq, clamp_id); > > + } > > +} > > Given all that, what is to stop the bucket value to climbing to > uclamp_bucket_value(+1)-1 and staying there (provided there's someone > runnable)? > > Why are we doing this... ? I agree with Peter, this part of the patch was the hardest to read. SCHED_WARN_ON line makes sense to me. The condition that follows and the following comment are a little baffling. Condition seems to indicate that the code that follows should be executed only if we are in the top-most occupied bucket (the bucket which has tasks and has the highest uclamp value). So this bucket just lost its last task and we should update rq->uclamp[clamp_id].value. However that's not exactly what the code does... It also resets rq->uclamp[clamp_id].bucket[bucket_id].value. So if I understand correctly, unless the bucket that just lost its last task is the top-most one its value will not be reset to nominal value. That looks like a bug to me. Am I missing something? Side note: some more explanation would be very helpful.