Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1758505pxb; Thu, 4 Nov 2021 07:59:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxVjaKlHG1xzPpz6NYrkZM5NJPPtYnIfFMndohL52VUeQkTwVIDEHygrDNhq6aAdRKaBbq X-Received: by 2002:a05:6602:1695:: with SMTP id s21mr26378099iow.10.1636037957861; Thu, 04 Nov 2021 07:59:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636037957; cv=none; d=google.com; s=arc-20160816; b=uXxB9BFgpm84UUl93rNld1yQTowNbeXVy1VlKXcGe0HWqj4Euwo4Kz1HcJLt7AALtv GCMIgjuHw8QeuQhm7S/I4SvTWiHbr0qU7ywFRF7f4Oxv5r6m4TDlmxL0UJ68gO2RF7P/ PlLjaFPi/gh+qhW1YJlZ74Gbq7nPeT2xkLWLSVvQPNMgDVcYn2EYhKhMHzOwxS3P8qMU OG71ovdo5HrNo9oQxZwAseJg66oorX1re9E6trreTcmgD0E1VmCsOQ5NJXyFpVO6ajK8 cr3kgPbZxMqjEdCIxNhkuzldwJhhsd2ibDWjatYrrbq4qVotGu4pL6HKCUepGYXaCmlZ Aj+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Q+ZCnISwtxsUAsqH9KMGNvXPhNAreIquwqkrQpOb+zc=; b=LnHKB6Ja90G6GKuR/sR7OHaaozqdmFzc5v7QVrUJ88Xg19lg7Ds2xd7BZ7bQeBKYYf 3RcPqgU5gdb04jilFyGBPYLgO817TJcJ0ZASKnCSxPV+MmM6RhwKxl4MXgINnzLj3X/O tfhQbMLeQYqWPGUs6AKp34Ivv6PbgWH6S96Uio8XrnX+9vwvTaHpIJc9p/6BCTpR/xq2 +KfNdEG6nr6yCDJMM9o111Zp4l57KmLAwSsjTweYMibq3YyrhqObLeB0JaRiH3Swfc7a 33sNh98ogaLB8YtCEZuBlEKn/wUCWWn/3K/3u0ZTOR+9jIlA8l60d08LFwJdBzL3uiaz /HDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=OhgZAejs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a4si7869995ilt.68.2021.11.04.07.59.02; Thu, 04 Nov 2021 07:59:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=OhgZAejs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231418AbhKDPAP (ORCPT + 99 others); Thu, 4 Nov 2021 11:00:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231388AbhKDPAN (ORCPT ); Thu, 4 Nov 2021 11:00:13 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 143A7C061714 for ; Thu, 4 Nov 2021 07:57:35 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id b13so7664827plg.2 for ; Thu, 04 Nov 2021 07:57:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q+ZCnISwtxsUAsqH9KMGNvXPhNAreIquwqkrQpOb+zc=; b=OhgZAejsPZopBWPjEX25LJzSzCfTXiGe++Rd8tWt1kErPWS7SirsjlQLuKvrbWXbG1 lT494W3ef++NFte7EY7OduZo7sNql5Oe4a17lvOjSQfJ1J0VjyS2AWhJMHmHJ+o9xrgE p8mAxtgc0b00DE2+NYOUN/QA7c9f1INDjZ5orjLOa3cD5RShPURHdj8M8pudIKjrdD5O oS8UMMnZRvNzMGlFZ2RSwIUZXg3dLljVEQQjXMb1ZwQWg7VdqlmVMykFW52megeny162 tWg6UtA8MZhC0xgBSGi6/KJJ80X/qJO5T2QMEww9yVspEkUSQwyzSdBhPKtpXTfF6wBf 3aPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q+ZCnISwtxsUAsqH9KMGNvXPhNAreIquwqkrQpOb+zc=; b=rRgbYkF7ZGmxcIF+08qoLhLt/GLvXT+XinX2w/E79zlZkvu40JUUtonE58xnP9yK+Z /i2oZzJLKOVJRGOpWjigm8/3lJv2VP6O6Bs6GJIXGfXElCAboNs4Pdz2MTf/IWZzm6XB AuZqlp/5UxjIHgCgp+AxmNg/twBZcfkbVJ7rqJ7bWrH6xlLrVduSRA3vMLNLQQkFeluA EpkjTMApIHlunVb5sQyYrAHH6wotxF+4V3aiNl5lvLEbhOBH9aPERcqjLN7wXi+u7Zlf TJzpiQ6HVp0eMqy7zCptuNJJqUdSMCtaYy5ziY46/BTzGn6XotK2TROO2+PIgc3KtZe1 LFRw== X-Gm-Message-State: AOAM533xVjlj2VpyILvUvvjafGQQuAgcRW6JV/+A9zsuGlrbz5yA51xj sDTSqYlqwjPYcXq8YCst4JA= X-Received: by 2002:a17:902:9b8a:b0:13f:c286:a060 with SMTP id y10-20020a1709029b8a00b0013fc286a060mr45073995plp.66.1636037854652; Thu, 04 Nov 2021 07:57:34 -0700 (PDT) Received: from localhost.localdomain ([140.82.17.67]) by smtp.gmail.com with ESMTPSA id t8sm4328530pgk.66.2021.11.04.07.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Nov 2021 07:57:34 -0700 (PDT) From: Yafang Shao To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com Cc: linux-kernel@vger.kernel.org, Yafang Shao , Valentin Schneider Subject: [RFC PATCH 2/4] sched/fair: Introduce cfs_migration Date: Thu, 4 Nov 2021 14:57:11 +0000 Message-Id: <20211104145713.4419-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211104145713.4419-1-laoar.shao@gmail.com> References: <20211104145713.4419-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A new per-cpu kthread named "cfs_migration/N" is introduced to do cfs specific balance works. It is a FIFO task with priority FIFO-1, which means it can preempt any cfs tasks but can't preempt other FIFO tasks. The kthreads as follows, PID COMMAND 13 [cfs_migration/0] 20 [cfs_migration/1] 25 [cfs_migration/2] 32 [cfs_migration/3] 38 [cfs_migration/4] ... $ cat /proc/13/sched ... policy : 1 prio : 98 ... $ cat /proc/13/status ... Cpus_allowed: 0001 Cpus_allowed_list: 0 ... All the works need to be done will be queued into a singly linked list, in which the first queued will be first serviced. Signed-off-by: Yafang Shao Cc: Valentin Schneider Cc: Vincent Guittot Cc: Dietmar Eggemann --- kernel/sched/fair.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 87db481e8a56..56b3fa91828b 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -20,6 +20,8 @@ * Adaptive scheduling granularity, math enhancements by Peter Zijlstra * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra */ +#include +#include #include "sched.h" /* @@ -11915,3 +11917,94 @@ int sched_trace_rq_nr_running(struct rq *rq) return rq ? rq->nr_running : -1; } EXPORT_SYMBOL_GPL(sched_trace_rq_nr_running); + +#ifdef CONFIG_SMP +struct cfs_migrater { + struct task_struct *thread; + struct list_head works; + raw_spinlock_t lock; +}; + +DEFINE_PER_CPU(struct cfs_migrater, cfs_migrater); + +static int cfs_migration_should_run(unsigned int cpu) +{ + struct cfs_migrater *migrater = &per_cpu(cfs_migrater, cpu); + unsigned long flags; + int run; + + raw_spin_lock_irqsave(&migrater->lock, flags); + run = !list_empty(&migrater->works); + raw_spin_unlock_irqrestore(&migrater->lock, flags); + + return run; +} + +static void cfs_migration_setup(unsigned int cpu) +{ + /* cfs_migration should have a higher priority than normal tasks, + * but a lower priority than other FIFO tasks. + */ + sched_set_fifo_low(current); +} + +static void cfs_migrater_thread(unsigned int cpu) +{ + struct cfs_migrater *migrater = &per_cpu(cfs_migrater, cpu); + struct cpu_stop_work *work; + +repeat: + work = NULL; + raw_spin_lock_irq(&migrater->lock); + if (!list_empty(&migrater->works)) { + work = list_first_entry(&migrater->works, + struct cpu_stop_work, list); + list_del_init(&work->list); + } + raw_spin_unlock_irq(&migrater->lock); + + if (work) { + struct cpu_stop_done *done = work->done; + cpu_stop_fn_t fn = work->fn; + void *arg = work->arg; + int ret; + + preempt_count_inc(); + ret = fn(arg); + if (done) { + if (ret) + done->ret = ret; + cpu_stop_signal_done(done); + } + preempt_count_dec(); + goto repeat; + } +} + +static struct smp_hotplug_thread cfs_migration_threads = { + .store = &cfs_migrater.thread, + .setup = cfs_migration_setup, + .thread_fn = cfs_migrater_thread, + .thread_comm = "cfs_migration/%u", + .thread_should_run = cfs_migration_should_run, +}; + +static int __init cfs_migration_init(void) +{ + struct cfs_migrater *cm = &per_cpu(cfs_migrater, raw_smp_processor_id()); + unsigned int cpu; + + for_each_possible_cpu(cpu) { + struct cfs_migrater *migrater = &per_cpu(cfs_migrater, cpu); + + raw_spin_lock_init(&migrater->lock); + INIT_LIST_HEAD(&migrater->works); + } + + WARN_ON_ONCE(smpboot_register_percpu_thread(&cfs_migration_threads)); + kthread_unpark(cm->thread); + + return 0; +} +early_initcall(cfs_migration_init) +#endif -- 2.17.1