Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2074990imm; Mon, 16 Jul 2018 01:31:33 -0700 (PDT) X-Google-Smtp-Source: AAOMgpflu+aYKcGAWY3wnaRtXghEpG4fvduCOhWCf78eyCpMQMjYQne//Ti6qquNdsVKW2qrQ6M4 X-Received: by 2002:a65:6688:: with SMTP id b8-v6mr14970020pgw.24.1531729893549; Mon, 16 Jul 2018 01:31:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531729893; cv=none; d=google.com; s=arc-20160816; b=ylCuSX5ObZURRDtYE3mGuoawce3w6Y+Pr7BS1+au8PpopT089dyJLkXq6bRpH0vfiI SZpnbq5wJddkvvixPrEDSV5SEPKq1ldZcgBGeG1XDeTcI1DBCHkdwJOGMGCHs5jYPnTn V/m3lBDTZhSGbub28JZC0DIfEAbAvbeDHuk6aWfuKDlJRkEsw1wmLCQX7IAS41pddS/p rG7VfRa13j8k7L1OQMmC9HbXb83QUqHX+HNC126S81cEubGVDY4x4dU71+8c9vbJ73le 4FZIYPGPEY/OOSUUal2cZ9XipUcYiT4RBYPn793tva9/xSPEqSJOPNDShNf/8bDm3N5H Qc5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=Y53bkGNAlN/XER6Agzi2YZscrg8Jrt6InVVaNBH9EJs=; b=ZkIxO4Sn1erwIu6xy9SNuh9/Uz04T51Sxfw8CDuZpwAatUROLvW5MHkR/4NKz2PTdR tcaM6xL+qaeLQkp2T1svcalMOKLUvVapphYwIn9x+xxH0c5O5RKshLViGIIE1rLVVrEV k3RcRfYvtb+IDIGeBVk4aWp5Fj6QvrXyhM5CsyIpOpqIi/IjMjTk6duC+G39ohrxrsfD 9pEtV/9or5i47nVKG4vboA+uKAl3/wuM0jnWrcmC8fY6liUxe0PyF1bP6vfFKYx9HeTx awT4n68nsgF0SMoDeYsofh6C4v0Qb7Xh3NV45XGUfgzTYZXH4uMpHMNyWRAbnytX44kw xK8g== 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 p64-v6si28601776pga.25.2018.07.16.01.31.19; Mon, 16 Jul 2018 01:31:33 -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 S1731296AbeGPIzz (ORCPT + 99 others); Mon, 16 Jul 2018 04:55:55 -0400 Received: from foss.arm.com ([217.140.101.70]:54352 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731256AbeGPIzz (ORCPT ); Mon, 16 Jul 2018 04:55:55 -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 EC07780D; Mon, 16 Jul 2018 01:29:39 -0700 (PDT) Received: from e110439-lin.cambridge.arm.com (e110439-lin.cambridge.arm.com [10.1.210.68]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2B8DE3F5A0; Mon, 16 Jul 2018 01:29:37 -0700 (PDT) From: Patrick Bellasi To: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Cc: Ingo Molnar , Peter Zijlstra , Tejun Heo , "Rafael J . Wysocki" , Viresh Kumar , Vincent Guittot , Paul Turner , Dietmar Eggemann , Morten Rasmussen , Juri Lelli , Todd Kjos , Joel Fernandes , Steve Muckle , Suren Baghdasaryan Subject: [PATCH v2 04/12] sched/core: uclamp: update CPU's refcount on clamp changes Date: Mon, 16 Jul 2018 09:28:58 +0100 Message-Id: <20180716082906.6061-5-patrick.bellasi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180716082906.6061-1-patrick.bellasi@arm.com> References: <20180716082906.6061-1-patrick.bellasi@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Utilization clamp values enforced on a CPU by a task can be updated at run-time, for example via a sched_setattr syscall, while a task is currently RUNNABLE on that CPU. In these cases, the task can be already refcounting a clamp group for its CPU and thus we need to update this reference to ensure the new constraints are immediately enforced. Since a clamp value change always implies a clamp group refcount update, this patch hooks into the clamp group refcount getter to trigger a CPU refcount syncup. Such a syncup is required only by currently RUNNABLE tasks which are also referencing at least one valid clamp group. Signed-off-by: Patrick Bellasi Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Paul Turner Cc: Todd Kjos Cc: Joel Fernandes Cc: Juri Lelli Cc: Dietmar Eggemann Cc: Morten Rasmussen Cc: linux-kernel@vger.kernel.org Cc: linux-pm@vger.kernel.org --- kernel/sched/core.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index d1969931fea6..b2424eea7990 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1082,6 +1082,52 @@ static inline void uclamp_cpu_put(struct rq *rq, struct task_struct *p) uclamp_cpu_put_id(p, rq, clamp_id); } +/** + * uclamp_task_update_active: update the clamp group of a RUNNABLE task + * @p: the task which clamp groups must be updated + * @clamp_id: the clamp index to consider + * @group_id: the clamp group to update + * + * Each time the clamp value of a task group is changed, the old and new clamp + * groups have to be updated for each CPU containing a RUNNABLE task belonging + * to this tasks group. Sleeping tasks are not updated since they will be + * enqueued with the proper clamp group index at their next activation. + */ +static inline void +uclamp_task_update_active(struct task_struct *p, int clamp_id, int group_id) +{ + struct rq_flags rf; + struct rq *rq; + + /* + * Lock the task and the CPU where the task is (or was) queued. + * + * We might lock the (previous) RQ of a !RUNNABLE task, but that's the + * price to pay to safely serialize util_{min,max} updates with + * enqueues, dequeues and migration operations. + * This is the same locking schema used by __set_cpus_allowed_ptr(). + */ + rq = task_rq_lock(p, &rf); + + /* + * The setting of the clamp group is serialized by task_rq_lock(). + * Thus, if the task's task_struct is not referencing a valid group + * index, then that task is not yet RUNNABLE and it's going to be + * enqueued with the proper clamp group value. + */ + if (!uclamp_task_active(p)) + goto done; + + /* Release p's currently referenced clamp group */ + uclamp_cpu_put_id(p, rq, clamp_id); + + /* Get p's new clamp group */ + uclamp_cpu_get_id(p, rq, clamp_id); + +done: + task_rq_unlock(rq, p, &rf); +} + /** * uclamp_group_put: decrease the reference count for a clamp group * @clamp_id: the clamp index which was affected by a task group @@ -1150,6 +1196,9 @@ static inline int uclamp_group_get(struct task_struct *p, uc_map[next_group_id].se_count += 1; raw_spin_unlock_irqrestore(&uc_map[next_group_id].se_lock, flags); + /* Update CPU's clamp group refcounts of RUNNABLE task */ + uclamp_task_update_active(p, clamp_id, next_group_id); + /* Release the previous clamp group */ uclamp_group_put(clamp_id, prev_group_id); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 65bf9ebacd83..b471d2222410 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2199,6 +2199,51 @@ static inline bool uclamp_group_active(struct uclamp_group *uc_grp, { return uc_grp[group_id].tasks > 0; } + +/** + * uclamp_task_affects: check if a task affects a utilization clamp + * @p: the task to consider + * @clamp_id: the utilization clamp to check + * + * A task affects a clamp index if: + * - it's currently enqueued on a CPU + * - it references a valid clamp group index for the specified clamp index + * + * Return: true if p currently affects the specified clamp_id + */ +static inline bool uclamp_task_affects(struct task_struct *p, int clamp_id) +{ + return (p->uclamp[clamp_id].group_id != UCLAMP_NONE); +} + +/** + * uclamp_task_active: check if a task is currently clamping a CPU + * @p: the task to check + * + * A task actively affects the utilization clamp of a CPU if: + * - it's currently enqueued or running on that CPU + * - it's refcounted in at least one clamp group of that CPU + * + * Return: true if p is currently clamping the utilization of its CPU. + */ +static inline bool uclamp_task_active(struct task_struct *p) +{ + struct rq *rq = task_rq(p); + int clamp_id; + + lockdep_assert_held(&p->pi_lock); + lockdep_assert_held(&rq->lock); + + if (!task_on_rq_queued(p) && !p->on_cpu) + return false; + + for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) { + if (uclamp_task_affects(p, clamp_id)) + return true; + } + + return false; +} #endif /* CONFIG_UCLAMP_TASK */ #ifdef CONFIG_CPU_FREQ -- 2.17.1