Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1413489imm; Thu, 5 Jul 2018 22:56:32 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcWaHSj+xozrtAMZqcbXND/8dxGW4gVt9sSZZr93wA6KKhoTBOzW4hDCTCfBcJXfOXvcA+P X-Received: by 2002:a62:8b87:: with SMTP id e7-v6mr9318176pfl.133.1530856592319; Thu, 05 Jul 2018 22:56:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530856592; cv=none; d=google.com; s=arc-20160816; b=P8mkj8tZfZbMD4Awyg2TtTMFFZWCZBj+5fjX/4OGuHI6WpovG6eOsn8Y3t0koLGgqq QBoDS2hwi5wdwGfFeWIuwecl2i2zXUEej8rnkrYd8LdZZQFUpygBC1lRni4AOC674hk2 d6fRw4ISFUyKDeDmEKoE9BHj//eleFS52XWq0AAH0VMC8h1qMQrkRhBhep1zPwOta4KO iCc2TvHRFLGsH9LNP1nRNuUkuZiJpffxxQFRepUFoGPVuTQlrrfXLn7Ckwar1RyXs9n8 UNyj0FrpbrCSdicteZ0fJY9v/wzFnbepkMUFfq9RDjArNfhDpQj2SD2u9j4lFJD1C/tA IkFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=7dfs+QRFvdJcWUKndM40rILeO/rDbC9INrwrS1yrp70=; b=yQbg3JAEN6Tu8RkTtexQXl6YRzJeMi39DIkyJC/VKOfe5Ecrfyi5sHaLVKjORGAywZ bzK1FIrWJ7HLf6IcUcG3iTCipZdRvKc7sXsMRf4sYPNKR7K0A8gAwDpkSiOoyuj06rES u8FoFXmSFjq8rHy1SZXrXOy93NWrdZze2aGChioz0x7a8bPRLxkG/D4nI5avJcR5ADth LbOcYlClQrnXDu6LsUa7chWTg6f5YfOJDzn8tSwBJ7C+tjK7ywRjyYgTxuYAuh4hSlxl LZNeNd5gksmGuM66/ac8sXIZZzDKPk1zxMcyHQBGs1XUtDokp36C8H9RcuYS64OW7tRo fw/g== 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 o23-v6si7263078pgm.170.2018.07.05.22.56.18; Thu, 05 Jul 2018 22:56:32 -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 S934587AbeGFFw0 (ORCPT + 99 others); Fri, 6 Jul 2018 01:52:26 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:33828 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934554AbeGFFwX (ORCPT ); Fri, 6 Jul 2018 01:52:23 -0400 Received: from localhost (D57D388D.static.ziggozakelijk.nl [213.125.56.141]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 34274B14; Fri, 6 Jul 2018 05:52:22 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Linus Torvalds , "Paul E. McKenney" , Steven Rostedt , Tejun Heo , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 4.14 57/61] sched/core: Fix rules for running on online && !active CPUs Date: Fri, 6 Jul 2018 07:47:21 +0200 Message-Id: <20180706054714.517481938@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180706054712.332416244@linuxfoundation.org> References: <20180706054712.332416244@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Peter Zijlstra [ Upstream commit 175f0e25abeaa2218d431141ce19cf1de70fa82d ] As already enforced by the WARN() in __set_cpus_allowed_ptr(), the rules for running on an online && !active CPU are stricter than just being a kthread, you need to be a per-cpu kthread. If you're not strictly per-CPU, you have better CPUs to run on and don't need the partially booted one to get your work done. The exception is to allow smpboot threads to bootstrap the CPU itself and get kernel 'services' initialized before we allow userspace on it. Signed-off-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Paul E. McKenney Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Tejun Heo Cc: Thomas Gleixner Fixes: 955dbdf4ce87 ("sched: Allow migrating kthreads into online but inactive CPUs") Link: http://lkml.kernel.org/r/20170725165821.cejhb7v2s3kecems@hirez.programming.kicks-ass.net Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/sched/core.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -894,6 +894,33 @@ void check_preempt_curr(struct rq *rq, s } #ifdef CONFIG_SMP + +static inline bool is_per_cpu_kthread(struct task_struct *p) +{ + if (!(p->flags & PF_KTHREAD)) + return false; + + if (p->nr_cpus_allowed != 1) + return false; + + return true; +} + +/* + * Per-CPU kthreads are allowed to run on !actie && online CPUs, see + * __set_cpus_allowed_ptr() and select_fallback_rq(). + */ +static inline bool is_cpu_allowed(struct task_struct *p, int cpu) +{ + if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) + return false; + + if (is_per_cpu_kthread(p)) + return cpu_online(cpu); + + return cpu_active(cpu); +} + /* * This is how migration works: * @@ -951,16 +978,8 @@ struct migration_arg { static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf, struct task_struct *p, int dest_cpu) { - if (p->flags & PF_KTHREAD) { - if (unlikely(!cpu_online(dest_cpu))) - return rq; - } else { - if (unlikely(!cpu_active(dest_cpu))) - return rq; - } - /* Affinity changed (again). */ - if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) + if (!is_cpu_allowed(p, dest_cpu)) return rq; update_rq_clock(rq); @@ -1489,10 +1508,9 @@ static int select_fallback_rq(int cpu, s for (;;) { /* Any allowed, online CPU? */ for_each_cpu(dest_cpu, &p->cpus_allowed) { - if (!(p->flags & PF_KTHREAD) && !cpu_active(dest_cpu)) - continue; - if (!cpu_online(dest_cpu)) + if (!is_cpu_allowed(p, dest_cpu)) continue; + goto out; }