Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3548105ybi; Tue, 2 Jul 2019 09:30:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqz8QBEx+utBOerOAt0Xiyba/NqJ1dwrpRZWUx97Dc9J3GJ1c/wp3QSw9VCuj54ygJMJc/AV X-Received: by 2002:a63:e53:: with SMTP id 19mr31197456pgo.137.1562085023614; Tue, 02 Jul 2019 09:30:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562085023; cv=none; d=google.com; s=arc-20160816; b=wDzCFvY8jHNRdJrRahjcf3qLS5cX1+X0mS/83exc6kxYVbiU08L7oKS5ibR8gVt+z3 LemHq9XxW7RqIcl0hF2j/9oeVhtEI6iw8pmuqMZ7uM5yfnMVm+is+xncqw50droDNw1n Bvbn16kUi/mo5N25+6dcBhJsHHYDUiY6RQtBjIGzBTPx0FzIrz+qPcXtAUhfhl6q8rOZ iAWNagSFEm18YMe+aoRimOK6hcXclOVpL7gOMP6SHVI7qEusV7jlUXui64d+3ZHISmaf 0AUVmQGQMuLW/lLkWzli4F6zTkt+3NTvpCma4XUAdtLCdma9DatUjElLBAvyg/3MMGXc KATQ== 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; bh=i3U3+SH43F3XPOPGdtgZpSBrZbLIY/EEkJSSfHm9egY=; b=b7dNvHH3g5iBwonII5BxG1aUHRHeduLQb7DlSup6F16PNzwfb+hPLqTcjayJE2NY72 VQR7liJqjVBl/bHzHvcGo0P0yfPrmPKOdSzbJXSijuJTAU2U2SkKu15MFlSja3lG4g/4 XHjnsLLtTLQeeC5JgEDORsD+s/fXkbjRrw238HKuq3xI5oGjl+St9XfMXgjvSGAOYY+r jqp+WM+FkD9kumi/OzUftcRc8K80zYvnEx6JcqoL/ZvdHoKwKMF2F84cI1RWVRjUGMyL ubNVbsLHM11V9wFsUvFXQqdDCMbFFWHXd15VgULjajTvCVwIv9AQltV+0FyhyX5R2MwQ cgcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=JmqRM9vA; 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 w16si13227699plp.329.2019.07.02.09.30.06; Tue, 02 Jul 2019 09:30:23 -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=@infradead.org header.s=merlin.20170209 header.b=JmqRM9vA; 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 S1726358AbfGBQ3p (ORCPT + 99 others); Tue, 2 Jul 2019 12:29:45 -0400 Received: from merlin.infradead.org ([205.233.59.134]:50168 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726150AbfGBQ3p (ORCPT ); Tue, 2 Jul 2019 12:29:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=i3U3+SH43F3XPOPGdtgZpSBrZbLIY/EEkJSSfHm9egY=; b=JmqRM9vAEAnCpHNKTnMEyFDmv /Lg912H/YMUBUzQGySA2pVAwuzv8pvKtZ009UYIeUxYgRAy4S8pLASBf/ThV27EW5A5+5VTVwTgwL Qgr+KHaQ7Tt5ZfaIu17aICHChTKUoASSKtBs6WoLUI0iTSCrGc7YcA5FMdQrnYQA68eUHZ+m1ygcJ 7+mSHfDoBza6bJC2z7UTAlFVsD5Zg9uKou8RsUaBjAjPN/EJw9xqQgT+XOO6A45MBGzqQu41xg0PK qrj9CD7/ND8SccTcqERmtG50WSPL4iv5jSDPxE90ug4gpnqQk0HBgWGbWSmgcTl1dhrBABS8+RvSU k9omqdFLg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1hiLeo-00083w-L5; Tue, 02 Jul 2019 16:29:27 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 13879203C694A; Tue, 2 Jul 2019 18:29:25 +0200 (CEST) Date: Tue, 2 Jul 2019 18:29:25 +0200 From: Peter Zijlstra To: subhra mazumdar Cc: linux-kernel@vger.kernel.org, mingo@redhat.com, tglx@linutronix.de, prakash.sangappa@oracle.com, dhaval.giani@oracle.com, daniel.lezcano@linaro.org, vincent.guittot@linaro.org, viresh.kumar@linaro.org, tim.c.chen@linux.intel.com, mgorman@techsingularity.net Subject: Re: [RFC PATCH 1/3] sched: Introduce new interface for scheduler soft affinity Message-ID: <20190702162925.GZ3436@hirez.programming.kicks-ass.net> References: <20190626224718.21973-1-subhra.mazumdar@oracle.com> <20190626224718.21973-2-subhra.mazumdar@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190626224718.21973-2-subhra.mazumdar@oracle.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 26, 2019 at 03:47:16PM -0700, subhra mazumdar wrote: > @@ -1082,6 +1088,37 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) > put_prev_task(rq, p); > > p->sched_class->set_cpus_allowed(p, new_mask); > + set_cpus_preferred_common(p, new_mask); > + > + if (queued) > + enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); > + if (running) > + set_curr_task(rq, p); > +} > + > +void do_set_cpus_preferred(struct task_struct *p, > + const struct cpumask *new_mask) > +{ > + struct rq *rq = task_rq(p); > + bool queued, running; > + > + lockdep_assert_held(&p->pi_lock); > + > + queued = task_on_rq_queued(p); > + running = task_current(rq, p); > + > + if (queued) { > + /* > + * Because __kthread_bind() calls this on blocked tasks without > + * holding rq->lock. > + */ > + lockdep_assert_held(&rq->lock); > + dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); > + } > + if (running) > + put_prev_task(rq, p); > + > + set_cpus_preferred_common(p, new_mask); > > if (queued) > enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); > @@ -1170,6 +1207,41 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, > return ret; > } > > +static int > +__set_cpus_preferred_ptr(struct task_struct *p, const struct cpumask *new_mask) > +{ > + const struct cpumask *cpu_valid_mask = cpu_active_mask; > + unsigned int dest_cpu; > + struct rq_flags rf; > + struct rq *rq; > + int ret = 0; > + > + rq = task_rq_lock(p, &rf); > + update_rq_clock(rq); > + > + if (p->flags & PF_KTHREAD) { > + /* > + * Kernel threads are allowed on online && !active CPUs > + */ > + cpu_valid_mask = cpu_online_mask; > + } > + > + if (cpumask_equal(&p->cpus_preferred, new_mask)) > + goto out; > + > + if (!cpumask_intersects(new_mask, cpu_valid_mask)) { > + ret = -EINVAL; > + goto out; > + } > + > + do_set_cpus_preferred(p, new_mask); > + > +out: > + task_rq_unlock(rq, p, &rf); > + > + return ret; > +} > + > int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) > { > return __set_cpus_allowed_ptr(p, new_mask, false); > @@ -4724,7 +4796,7 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, > return retval; > } > > -long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > +long sched_setaffinity(pid_t pid, const struct cpumask *in_mask, int flags) > { > cpumask_var_t cpus_allowed, new_mask; > struct task_struct *p; > @@ -4742,6 +4814,11 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > get_task_struct(p); > rcu_read_unlock(); > > + if (flags == SCHED_SOFT_AFFINITY && > + p->sched_class != &fair_sched_class) { > + retval = -EINVAL; > + goto out_put_task; > + } > if (p->flags & PF_NO_SETAFFINITY) { > retval = -EINVAL; > goto out_put_task; > @@ -4790,18 +4867,37 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > } > #endif > again: > - retval = __set_cpus_allowed_ptr(p, new_mask, true); > - > - if (!retval) { > - cpuset_cpus_allowed(p, cpus_allowed); > - if (!cpumask_subset(new_mask, cpus_allowed)) { > - /* > - * We must have raced with a concurrent cpuset > - * update. Just reset the cpus_allowed to the > - * cpuset's cpus_allowed > - */ > - cpumask_copy(new_mask, cpus_allowed); > - goto again; > + if (flags == SCHED_HARD_AFFINITY) { > + retval = __set_cpus_allowed_ptr(p, new_mask, true); > + > + if (!retval) { > + cpuset_cpus_allowed(p, cpus_allowed); > + if (!cpumask_subset(new_mask, cpus_allowed)) { > + /* > + * We must have raced with a concurrent cpuset > + * update. Just reset the cpus_allowed to the > + * cpuset's cpus_allowed > + */ > + cpumask_copy(new_mask, cpus_allowed); > + goto again; > + } > + p->affinity_unequal = false; > + } > + } else if (flags == SCHED_SOFT_AFFINITY) { > + retval = __set_cpus_preferred_ptr(p, new_mask); > + if (!retval) { > + cpuset_cpus_allowed(p, cpus_allowed); > + if (!cpumask_subset(new_mask, cpus_allowed)) { > + /* > + * We must have raced with a concurrent cpuset > + * update. > + */ > + cpumask_and(new_mask, new_mask, cpus_allowed); > + goto again; > + } > + if (!cpumask_equal(&p->cpus_allowed, > + &p->cpus_preferred)) > + p->affinity_unequal = true; > } > } > out_free_new_mask: This seems like a terrible lot of pointless duplication; don't you get a much smaller diff by passing the hard/soft thing into __set_cpus_allowed_ptr() and only branching where it matters?