Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755239AbcCBUKX (ORCPT ); Wed, 2 Mar 2016 15:10:23 -0500 Received: from mail-db3on0083.outbound.protection.outlook.com ([157.55.234.83]:59744 "EHLO emea01-db3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755032AbcCBUKQ (ORCPT ); Wed, 2 Mar 2016 15:10:16 -0500 Authentication-Results: spf=fail (sender IP is 12.216.194.146) smtp.mailfrom=ezchip.com; ezchip.com; dkim=none (message not signed) header.d=none;ezchip.com; dmarc=none action=none header.from=ezchip.com; From: Chris Metcalf To: Gilad Ben Yossef , Steven Rostedt , Ingo Molnar , Peter Zijlstra , Andrew Morton , "Rik van Riel" , Tejun Heo , Frederic Weisbecker , Thomas Gleixner , "Paul E. McKenney" , Christoph Lameter , Viresh Kumar , Catalin Marinas , Will Deacon , Andy Lutomirski , , , CC: Chris Metcalf Subject: [PATCH v10 04/12] task_isolation: add initial support Date: Wed, 2 Mar 2016 15:09:28 -0500 Message-ID: <1456949376-4910-5-git-send-email-cmetcalf@ezchip.com> X-Mailer: git-send-email 2.1.2 In-Reply-To: <1456949376-4910-1-git-send-email-cmetcalf@ezchip.com> References: <1456949376-4910-1-git-send-email-cmetcalf@ezchip.com> X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CPI:12.216.194.146;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(2980300002)(1109001)(339900001)(189002)(199003)(19580405001)(105606002)(87936001)(1096002)(106466001)(5008740100001)(92566002)(6806005)(2201001)(189998001)(104016004)(85426001)(81156011)(1220700001)(19580395003)(5003940100001)(47776003)(4326007)(48376002)(5001970100001)(33646002)(36756003)(50226001)(5001770100001)(586003)(11100500001)(4001430100002)(50466002)(229853001)(2950100001)(50986999)(76176999)(42186005)(2906002)(107886002)(575784001)(921003)(21314002)(42882005)(2101003)(83996005)(1121003);DIR:OUT;SFP:1101;SCL:1;SRVR:VI1PR05MB1695;H:ld-1.internal.tilera.com;FPR:;SPF:Fail;MLV:sfv;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;AM1FFO11FD041;1:Yd8dY0YXk3XlgEgF7/w8A2zklQQ3jJUABEVJuFJJooBMhtAdJ4e7THcvkrFWuqnM0NPbd9TVyrB5sUPtr2zk+6Ib08J/7uy6e59Hs1ZYaN4GqEhnuWAsjWPLdrxUNYsWW4O97LP4fmn24Mcmj8ugRgFLYlzBG1xWIzWzk6t/wkNIUQEbpgS+AgQmJK9Zifm5TDx1BEr0YE4DN+Uf4XUGMu1qMDd/yp9XGH8PpgkEaXdgoF7/k0pJQrq8k2aG9qxyqBa6Wb1WbtaKydWsqXPA3GUYiMvAkIgG3xeorcXQDjxUmAf9kuhLur89BgFLmqB+H09erhW1eearVDXBmljL4Io+jti3l5lb91kWJsJBSs0dh3UhX8VSh142p662A5e/fOjPV4uq1coY2NJYI4mTtJXgWp0l+tBz5lV2iFsRx0wIa8UJbadewT7E6Ab9XH0HJUC+ODS6hrqyRytIWbwapc+cxD/I7nemeZw0b151nU003LB9iq3v0TLBKFiVxh7m MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: ed7fa230-e267-4695-f358-08d342d6a84b X-Microsoft-Exchange-Diagnostics: 1;VI1PR05MB1695;2:sQgv31GQ6cbnWYDOwnqO0DkUoU2zshL/oxOG7l7+2x0tCDQQ5HclaNW/J0zYaeJ2sETffyG53/bHW+G2oDWGboriKIZ6kDxgiV612UspTjLPJTyz1p4cbENvb8jqMbSPltjCj+ovTXNGJyugQcym42mAgBpu0pmuqvgDBwIIwHt12IT2jrmhQsa5dF6zAxLa;3:B1xWgkt/zmHVqzEJzlPaJEMuLcyWK1clxv6zfLmmDjmGvq9SOsYdvMWt59KY/kbSyrlDkgycbAHTpdQ28quVIEBbdi/tnmrl1VZqO14e/Y5ZH5ILKr5uPaoum5kbzL2xC/UZjc26d/kRHL5ErcZuDQmn4iVE43LTLj9fMQRtjfB5zOfTRzkxEEpFCrl71ALE;25:dIffy1cTRRnFoT9BooeZWSf2DbTs5BOKveNh7Bdgadvi/delOBawHK1S7NIhQHTR3xQIAQFwhvnzN1F6oS/Vl3SvIftzqvMSk5+JflvzF+pRscpGyfTUU6fd0athtdSq4MpMuYuc13etynglSumkNqj1xPNZdB/0mnPXKaUi+yZKi9aHlsi9eIrNFaRPemsO+3zOvEfqoIdfJ3gSDt88cu0tgWpjaaOdnd315XXYR/6i+1NtiO3QssscUQBvAMVBoIDkiHrIznykqO44vgr/+7xLLz5OVYDls2upISF4D+/2NRXRAEScIp32jOLgP0yg4tvDWeCDzRprNMtfG3OrLg== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:VI1PR05MB1695; X-MLNXRule-EZCH-Linux: Rule triggered X-MLNXRule-Acc-Dom-4: Rule triggered X-Microsoft-Exchange-Diagnostics: 1;VI1PR05MB1695;20:5KgIpXyrz3s+AtXGD6/eIpKHv20zYUE203MxkwJcYFOO23La5lt4LJv/7ekGDAZuiwY9v3H0d35CE8ADAkMVPb/6fnrGkGGoIlDSQb0XXAZc08GRmKELvC3lEA5hZpgol7wDgOr56TwzyUSWzmgzpMBaEqetxh5o2CnzRcQIMF6oUWdciFkWGUVRxZBtElx9IdCBb8YBAtI9hKRvYGhrHSQt1cO4oUHadhbzmO0GaFROipYYO4dHBxq3pe5PXlyAq84eE/pe6N1D5nqLSv9sHOOJuqjOqUTe0XNmqEHNFXPb5rY/kIy2GmnJtLOVsNhtA99FR6+geYGO4GrrGaU3rI4O3O4YjeSITOA04IC7mYs5ZManmqKzqglkupV7kC9R4uuEnBgV/g44Y1kr02+wwcDcCk4r79ZW6UhPC2nYkfdZifu9duAWFVx6kHvB0k5In3iCKTliGME+OWwtESB6DUC7UqQOUWw1eKd+aycexXe2CK+piSg0xmFl2JEF779KWZMArWiRoOXyHmjoWL4M92WvOzJr0/8LeP3YBgDuDrF4nPJzugTnKTrSPOjRF7zjJ+hMDPHQGS6FNXDJ7rRA5R/QvjQ/P3p89evNeK7g+zo= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(13015025)(13017025)(13023025)(13024025)(5005006)(8121501046)(13018025)(3002001)(10201501046);SRVR:VI1PR05MB1695;BCL:0;PCL:0;RULEID:;SRVR:VI1PR05MB1695; X-Microsoft-Exchange-Diagnostics: 1;VI1PR05MB1695;4:xdF0tELVUGdbd9yTqFrai7qFHk55NXeKTtJ6Kc9ey9a3wYALtST6xgR4vzRYeNScwXdjbelhHqxgd6lbzVNK3YmWjIH+whWqKGxOIkOqoVNSrqAyL5CbmF7AW8jRo6mjYli6P3HW+JdB0vMokpckuP88lkkqX2C4YuMLRl7yfJhEv2cZv4BZuAd/9iluQVWaFFaF7MpCCkz/9VaYJL9TInZOhI5sWacm17Pr6g5DcKAcawn7egHWO+5g6uP/FFFWr4BwsJLcDiXqqPdlHDQ0fb0UxHFmX/0FH/pb/PmaNqPZjVU3DuBH3OAp2ir+nH4wOoigcv3aCQjd/dArjHHC6aZzdWWRZkT6mDRMmbKReVWv5B0FZaEEXbLMISu414+MEt9Lv3kWv9rhX5HqC0R+zidnu2yZted+dKEC4sfe8mBirp3cPdBfpKDVYw55mR94vXuVArCxEmmAwYZHFORFiw== X-Forefront-PRVS: 086943A159 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;VI1PR05MB1695;23:vcdXxVNzdeCIoP0ygg/0AtoL+v3I9SFdwduZlfrjN?= =?us-ascii?Q?2KnQkmemMWZRxRLLSOzQBccmAfGOMtfzFYRU5S1EFLh7YDv2f3rRtu7mYkgv?= =?us-ascii?Q?RPKoGqqC7uWP0jMgXmiKcQQVuTk8/4Jas0g18m5PfYvRuUcj3OyBAlZ7UCS4?= =?us-ascii?Q?UFJ0jXV3gRe0QLe0+7T5YO9wxDOwDHe0Kw2n+fYfeYm/vtMU3eHtVqnHjDsg?= =?us-ascii?Q?dRnluERM+x7yL1N13AhpwQxYMj/EqPzVognvTbFTOrFbwG2s2KRh/Teu3Eon?= =?us-ascii?Q?TQ+uuch0ySkTAl+bS9oxDFD2uVd0sppbKl6SsijEH88IbKcHQzp69XDKBOZy?= =?us-ascii?Q?gr4oohYO0iyufG41/NX1vvcnBwaz+DtfYNut4urpMn0tyMQuxByu2g6uvtVb?= =?us-ascii?Q?Di8vrxzIP9izizLL5UdnDiuJo2do+oDTsXe3o6qPYcVh0/4olX0drmjcUMSz?= =?us-ascii?Q?jnmcUBe36+ZD90a2QNTi3fPPSBSGkpEeJ0k42psaaEBmUt/0BnoJ5H4RJn8c?= =?us-ascii?Q?KU7SM3FbjUVKfoGCeJo+ZuLIBEUIvk3/VN7FSloknuJyuENbjNJ3PdGU5Osw?= =?us-ascii?Q?bln6cUUZdEthKwR/4SdkbxgK1pzmVJRXgNeNGcozqKIKlUIRJr7nfqw6eC/D?= =?us-ascii?Q?54H4mK5VO1xr8wZi/NuPLFxqM85aU5so5+DrVcPvzxoY9S1R0EQIu725zE3q?= =?us-ascii?Q?p5+UhMHXRVoQVVn3Kshk0U03YKBfHL7seBTEi+azePH12+6eVNC6+2WUTs15?= =?us-ascii?Q?11RuXAoC7+TUpZNtuSsnbbejOQVSHCTbdQ0SVtRMHLFJgqSZB925ukLcvCWI?= =?us-ascii?Q?UeHMaEmKMFJRDOEKX+eRazpcBSXlNAvH5uw+Tmrc+m2CaSDTt93pKQYPDd26?= =?us-ascii?Q?gGDjCXH4mGGZo5pF7jEXYb5ozBdetdvrMVb2keneulDWV/BR68ALNE7+ONF4?= =?us-ascii?Q?GttT31XzBvuQV4JxJWznU4AD4RZJZ+50I9Eja/Jb6cAc9mVxpYdXUwFJ+dae?= =?us-ascii?Q?BCsquNNODwhdcf8yYNPZfurwks9IzZt+M8zMLSI2S8uO3gJOK2Hlv8vmIoLd?= =?us-ascii?Q?pKfyuRr07I9o6hX5CqgR4ijAkJRLAt6VS0mv772VmwunoPZCwjDXHRrpp8tI?= =?us-ascii?Q?eCCzpZD1fC5dNHmijNhqHUoWEn89T7yvje5gOG/bgEAIKsLPG4+jnxy6fxkC?= =?us-ascii?Q?RV3wI4nIs9i2lPaGdYGlvL0GSlFYibPIlNgjnTxz9ufQinK1P9uzLcNrT9mb?= =?us-ascii?Q?NZlu56+tCF4VU0TfLY=3D?= X-Microsoft-Exchange-Diagnostics: 1;VI1PR05MB1695;5:hHKikZF608ayygLlmCD3XzOVNmE165q/LMXT80q5CTgxUFwB2DgtEAJm0ofF/Oqdzf0bR5KNINLut7AriVYQF+51cJeE8bCkMDKGGDgWpDk3Ecw9gH3aq5o1f4kXmX1ZLbIxBUL5V2e++9+sZXQrWQ==;24:Nc7rySRVb2M2kY9mg8VBAfoDjjSpw9L8MYsk7ZOi26MPQZFPZ0q2RCS0cgo+h6DzbNd4mJ9t0vWhv+KZYUDJuin7W+mmUNq5PK0+E83fgto= X-OriginatorOrg: ezchip.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2016 20:10:09.8988 (UTC) X-MS-Exchange-CrossTenant-Id: a652971c-7d2e-4d9b-a6a4-d149256f461b X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=a652971c-7d2e-4d9b-a6a4-d149256f461b;Ip=[12.216.194.146];Helo=[ld-1.internal.tilera.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR05MB1695 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 16302 Lines: 499 The existing nohz_full mode is designed as a "soft" isolation mode that makes tradeoffs to minimize userspace interruptions while still attempting to avoid overheads in the kernel entry/exit path, to provide 100% kernel semantics, etc. However, some applications require a "hard" commitment from the kernel to avoid interruptions, in particular userspace device driver style applications, such as high-speed networking code. This change introduces a framework to allow applications to elect to have the "hard" semantics as needed, specifying prctl(PR_SET_TASK_ISOLATION, PR_TASK_ISOLATION_ENABLE) to do so. Subsequent commits will add additional flags and additional semantics. The kernel must be built with the new TASK_ISOLATION Kconfig flag to enable this mode, and the kernel booted with an appropriate task_isolation=CPULIST boot argument, which enables nohz_full and isolcpus as well. The "task_isolation" state is then indicated by setting a new task struct field, task_isolation_flag, to the value passed by prctl(). When the _ENABLE bit is set for a task, and it is returning to userspace on a task isolation core, it calls the new task_isolation_ready() / task_isolation_enter() routines to take additional actions to help the task avoid being interrupted in the future. The task_isolation_ready() call plays an equivalent role to the TIF_xxx flags when returning to userspace, and should be checked in the loop check of the prepare_exit_to_usermode() routine or its architecture equivalent. It is called with interrupts disabled and inspects the kernel state to determine if it is safe to return into an isolated state. In particular, if it sees that the scheduler tick is still enabled, it reports that it is not yet safe. Each time through the loop of TIF work to do, we call the new task_isolation_enter() routine, which takes any actions that might avoid a future interrupt to the core, such as a worker thread being scheduled that could be quiesced now (e.g. the vmstat worker) or a future IPI to the core to clean up some state that could be cleaned up now (e.g. the mm lru per-cpu cache). In addition, it requests rescheduling if the scheduler dyntick is still running. As a result of these tests on the "return to userspace" path, sys calls (and page faults, etc.) can be inordinately slow. However, this quiescing guarantees that no unexpected interrupts will occur, even if the application intentionally calls into the kernel. Separate patches that follow provide these changes for x86, arm64, and tile. Signed-off-by: Chris Metcalf --- Documentation/kernel-parameters.txt | 8 +++ drivers/base/cpu.c | 18 ++++++ include/linux/isolation.h | 53 ++++++++++++++++ include/linux/sched.h | 3 + include/linux/tick.h | 1 + include/uapi/linux/prctl.h | 5 ++ init/Kconfig | 20 ++++++ kernel/Makefile | 1 + kernel/fork.c | 3 + kernel/isolation.c | 118 ++++++++++++++++++++++++++++++++++++ kernel/sys.c | 9 +++ kernel/time/tick-sched.c | 31 ++++++---- 12 files changed, 257 insertions(+), 13 deletions(-) create mode 100644 include/linux/isolation.h create mode 100644 kernel/isolation.c diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index 9a53c929f017..c8d0b42d984a 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -3747,6 +3747,14 @@ bytes respectively. Such letter suffixes can also be entirely omitted. neutralize any effect of /proc/sys/kernel/sysrq. Useful for debugging. + task_isolation= [KNL] + In kernels built with CONFIG_TASK_ISOLATION=y, set + the specified list of CPUs where cpus will be able + to use prctl(PR_SET_TASK_ISOLATION) to set up task + isolation mode. Setting this boot flag implicitly + also sets up nohz_full and isolcpus mode for the + listed set of cpus. + tcpmhash_entries= [KNL,NET] Set the number of tcp_metrics_hash slots. Default value is 8192 or 16384 depending on total diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c index 691eeea2f19a..eaf40f4264ee 100644 --- a/drivers/base/cpu.c +++ b/drivers/base/cpu.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "base.h" @@ -290,6 +291,20 @@ static ssize_t print_cpus_nohz_full(struct device *dev, static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL); #endif +#ifdef CONFIG_TASK_ISOLATION +static ssize_t print_cpus_task_isolation(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int n = 0, len = PAGE_SIZE-2; + + n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(task_isolation_map)); + + return n; +} +static DEVICE_ATTR(task_isolation, 0444, print_cpus_task_isolation, NULL); +#endif + static void cpu_device_release(struct device *dev) { /* @@ -460,6 +475,9 @@ static struct attribute *cpu_root_attrs[] = { #ifdef CONFIG_NO_HZ_FULL &dev_attr_nohz_full.attr, #endif +#ifdef CONFIG_TASK_ISOLATION + &dev_attr_task_isolation.attr, +#endif #ifdef CONFIG_GENERIC_CPU_AUTOPROBE &dev_attr_modalias.attr, #endif diff --git a/include/linux/isolation.h b/include/linux/isolation.h new file mode 100644 index 000000000000..c564cf1886bb --- /dev/null +++ b/include/linux/isolation.h @@ -0,0 +1,53 @@ +/* + * Task isolation related global functions + */ +#ifndef _LINUX_ISOLATION_H +#define _LINUX_ISOLATION_H + +#include +#include + +#ifdef CONFIG_TASK_ISOLATION + +/* cpus that are configured to support task isolation */ +extern cpumask_var_t task_isolation_map; + +extern int task_isolation_init(void); + +static inline bool task_isolation_possible(int cpu) +{ + return tick_nohz_full_enabled() && + cpumask_test_cpu(cpu, task_isolation_map); +} + +extern int task_isolation_set(unsigned int flags); + +static inline bool task_isolation_enabled(void) +{ + return (current->task_isolation_flags & PR_TASK_ISOLATION_ENABLE) && + task_isolation_possible(raw_smp_processor_id()); +} + +extern bool _task_isolation_ready(void); +extern void _task_isolation_enter(void); + +static inline bool task_isolation_ready(void) +{ + return !task_isolation_enabled() || _task_isolation_ready(); +} + +static inline void task_isolation_enter(void) +{ + if (task_isolation_enabled()) + _task_isolation_enter(); +} + +#else +static inline void task_isolation_init(void) { } +static inline bool task_isolation_possible(int cpu) { return false; } +static inline bool task_isolation_enabled(void) { return false; } +static inline bool task_isolation_ready(void) { return true; } +static inline void task_isolation_enter(void) { } +#endif + +#endif diff --git a/include/linux/sched.h b/include/linux/sched.h index a10494a94cc3..df6eb22510c3 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1830,6 +1830,9 @@ struct task_struct { unsigned long task_state_change; #endif int pagefault_disabled; +#ifdef CONFIG_TASK_ISOLATION + unsigned int task_isolation_flags; +#endif /* CPU-specific state of this task */ struct thread_struct thread; /* diff --git a/include/linux/tick.h b/include/linux/tick.h index 97fd4e543846..b4c0f4dc909f 100644 --- a/include/linux/tick.h +++ b/include/linux/tick.h @@ -158,6 +158,7 @@ extern void tick_nohz_full_kick(void); extern void tick_nohz_full_kick_cpu(int cpu); extern void tick_nohz_full_kick_all(void); extern void __tick_nohz_task_switch(void); +extern void tick_nohz_full_add_cpus(const struct cpumask *mask); #else static inline int housekeeping_any_cpu(void) { diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index a8d0759a9e40..67224df4b559 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -197,4 +197,9 @@ struct prctl_mm_map { # define PR_CAP_AMBIENT_LOWER 3 # define PR_CAP_AMBIENT_CLEAR_ALL 4 +/* Enable/disable or query task_isolation mode for NO_HZ_FULL kernels. */ +#define PR_SET_TASK_ISOLATION 48 +#define PR_GET_TASK_ISOLATION 49 +# define PR_TASK_ISOLATION_ENABLE (1 << 0) + #endif /* _LINUX_PRCTL_H */ diff --git a/init/Kconfig b/init/Kconfig index 22320804fbaf..6cab348fe454 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -782,6 +782,26 @@ config RCU_EXPEDITE_BOOT endmenu # "RCU Subsystem" +config TASK_ISOLATION + bool "Provide hard CPU isolation from the kernel on demand" + depends on NO_HZ_FULL + help + Allow userspace processes to place themselves on task_isolation + cores and run prctl(PR_SET_TASK_ISOLATION) to "isolate" + themselves from the kernel. On return to userspace, + isolated tasks will first arrange that no future kernel + activity will interrupt the task while the task is running + in userspace. This "hard" isolation from the kernel is + required for userspace tasks that are running hard real-time + tasks in userspace, such as a 10 Gbit network driver in userspace. + + Without this option, but with NO_HZ_FULL enabled, the kernel + will make a best-faith, "soft" effort to shield a single userspace + process from interrupts, but makes no guarantees. + + You should say "N" unless you are intending to run a + high-performance userspace driver or similar task. + config BUILD_BIN2C bool default n diff --git a/kernel/Makefile b/kernel/Makefile index 53abf008ecb3..693a2ba35679 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -103,6 +103,7 @@ obj-$(CONFIG_TORTURE_TEST) += torture.o obj-$(CONFIG_MEMBARRIER) += membarrier.o obj-$(CONFIG_HAS_IOMEM) += memremap.o +obj-$(CONFIG_TASK_ISOLATION) += isolation.o $(obj)/configs.o: $(obj)/config_data.h diff --git a/kernel/fork.c b/kernel/fork.c index 2e391c754ae7..1890ea0dcd5b 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1413,6 +1413,9 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->sequential_io = 0; p->sequential_io_avg = 0; #endif +#ifdef CONFIG_TASK_ISOLATION + p->task_isolation_flags = 0; /* do not isolate */ +#endif /* Perform scheduler related setup. Assign this task to a CPU. */ retval = sched_fork(clone_flags, p); diff --git a/kernel/isolation.c b/kernel/isolation.c new file mode 100644 index 000000000000..e954afd8cce8 --- /dev/null +++ b/kernel/isolation.c @@ -0,0 +1,118 @@ +/* + * linux/kernel/isolation.c + * + * Implementation for task isolation. + * + * Distributed under GPLv2. + */ + +#include +#include +#include +#include +#include +#include "time/tick-sched.h" + +cpumask_var_t task_isolation_map; +static bool saw_boot_arg; + +/* + * Isolation requires both nohz and isolcpus support from the scheduler. + * We provide a boot flag that enables both for now, and which we can + * add other functionality to over time if needed. Note that just + * specifying "nohz_full=... isolcpus=..." does not enable task isolation. + */ +static int __init task_isolation_setup(char *str) +{ + saw_boot_arg = true; + + alloc_bootmem_cpumask_var(&task_isolation_map); + if (cpulist_parse(str, task_isolation_map) < 0) { + pr_warn("task_isolation: Incorrect cpumask '%s'\n", str); + return 1; + } + + return 1; +} +__setup("task_isolation=", task_isolation_setup); + +int __init task_isolation_init(void) +{ + /* For offstack cpumask, ensure we allocate an empty cpumask early. */ + if (!saw_boot_arg) { + zalloc_cpumask_var(&task_isolation_map, GFP_KERNEL); + return 0; + } + + /* + * Add our task_isolation cpus to nohz_full and isolcpus. Note + * that we are called relatively early in boot, from tick_init(); + * at this point neither nohz_full nor isolcpus has been used + * to configure the system, but isolcpus has been allocated + * already in sched_init(). + */ + tick_nohz_full_add_cpus(task_isolation_map); + cpumask_or(cpu_isolated_map, cpu_isolated_map, task_isolation_map); + + return 0; +} + +/* + * This routine controls whether we can enable task-isolation mode. + * The task must be affinitized to a single task_isolation core or we will + * return EINVAL. Although the application could later re-affinitize + * to a housekeeping core and lose task isolation semantics, this + * initial test should catch 99% of bugs with task placement prior to + * enabling task isolation. + */ +int task_isolation_set(unsigned int flags) +{ + if (cpumask_weight(tsk_cpus_allowed(current)) != 1 || + !task_isolation_possible(raw_smp_processor_id())) + return -EINVAL; + + current->task_isolation_flags = flags; + return 0; +} + +/* + * In task isolation mode we try to return to userspace only after + * attempting to make sure we won't be interrupted again. This test + * is run with interrupts disabled to test that everything we need + * to be true is true before we can return to userspace. + */ +bool _task_isolation_ready(void) +{ + WARN_ON_ONCE(!irqs_disabled()); + + return (!lru_add_drain_needed(smp_processor_id()) && + vmstat_idle() && + tick_nohz_tick_stopped()); +} + +/* + * Each time we try to prepare for return to userspace in a process + * with task isolation enabled, we run this code to quiesce whatever + * subsystems we can readily quiesce to avoid later interrupts. + */ +void _task_isolation_enter(void) +{ + WARN_ON_ONCE(irqs_disabled()); + + /* Drain the pagevecs to avoid unnecessary IPI flushes later. */ + lru_add_drain(); + + /* Quieten the vmstat worker so it won't interrupt us. */ + quiet_vmstat_sync(); + + /* + * Request rescheduling unless we are in full dynticks mode. + * We would eventually get pre-empted without this, and if there's + * another task waiting, it would run; but by explicitly requesting + * the reschedule, we reduce the latency. We could directly call + * schedule() here as well, but since our caller is the standard + * place where schedule() is called, we defer to the caller. + */ + if (!tick_nohz_tick_stopped()) + set_tsk_need_resched(current); +} diff --git a/kernel/sys.c b/kernel/sys.c index 78947de6f969..c1d621f1137e 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -2266,6 +2267,14 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_GET_FP_MODE: error = GET_FP_MODE(me); break; +#ifdef CONFIG_TASK_ISOLATION + case PR_SET_TASK_ISOLATION: + error = task_isolation_set(arg2); + break; + case PR_GET_TASK_ISOLATION: + error = me->task_isolation_flags; + break; +#endif default: error = -EINVAL; break; diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 0b17424349eb..14eb7dd06f26 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -311,30 +312,34 @@ static int tick_nohz_cpu_down_callback(struct notifier_block *nfb, return NOTIFY_OK; } -static int tick_nohz_init_all(void) +void tick_nohz_full_add_cpus(const struct cpumask *mask) { - int err = -1; + if (!cpumask_weight(mask)) + return; -#ifdef CONFIG_NO_HZ_FULL_ALL - if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) { + if (tick_nohz_full_mask == NULL && + !zalloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) { WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n"); - return err; + return; } - err = 0; - cpumask_setall(tick_nohz_full_mask); + + cpumask_or(tick_nohz_full_mask, tick_nohz_full_mask, mask); tick_nohz_full_running = true; -#endif - return err; } void __init tick_nohz_init(void) { int cpu; - if (!tick_nohz_full_running) { - if (tick_nohz_init_all() < 0) - return; - } + task_isolation_init(); + +#ifdef CONFIG_NO_HZ_FULL_ALL + if (!tick_nohz_full_running) + tick_nohz_full_add_cpus(cpu_possible_mask); +#endif + + if (!tick_nohz_full_running) + return; if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) { WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n"); -- 2.1.2