Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758807AbcDERjV (ORCPT ); Tue, 5 Apr 2016 13:39:21 -0400 Received: from mail-db3on0098.outbound.protection.outlook.com ([157.55.234.98]:2931 "EHLO emea01-db3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1757920AbcDERjL (ORCPT ); Tue, 5 Apr 2016 13:39:11 -0400 Authentication-Results: spf=fail (sender IP is 12.216.194.146) smtp.mailfrom=ezchip.com; mellanox.com; dkim=none (message not signed) header.d=none;mellanox.com; dmarc=fail action=none header.from=mellanox.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 , Michal Hocko , , , , CC: Chris Metcalf Subject: [PATCH v12 04/13] task_isolation: add initial support Date: Tue, 5 Apr 2016 13:38:33 -0400 Message-ID: <1459877922-15512-5-git-send-email-cmetcalf@mellanox.com> X-Mailer: git-send-email 2.7.2 In-Reply-To: <1459877922-15512-1-git-send-email-cmetcalf@mellanox.com> References: <1459877922-15512-1-git-send-email-cmetcalf@mellanox.com> X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:12.216.194.146;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(2980300002)(1109001)(1110001)(339900001)(52314003)(199003)(189002)(104016004)(5008740100001)(106466001)(92566002)(48376002)(586003)(47776003)(87936001)(50226001)(2950100001)(5003940100001)(960300001)(11100500001)(2201001)(6806005)(85426001)(105606002)(19580405001)(19580395003)(33646002)(50466002)(2906002)(76176999)(42186005)(4326007)(86362001)(50986999)(189998001)(1096002)(1220700001)(575784001)(4001430100002)(229853001)(5001970100001)(107886002)(5001770100001)(36756003)(921003)(2101003)(83996005)(1121003);DIR:OUT;SFP:1101;SCL:1;SRVR:AM4PR05MB1682;H:ld-1.internal.tilera.com;FPR:;SPF:Fail;MLV:sfv;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;AM1FFO11FD029;1:j8gsxUx4pzDJXCvNEy5omAqq0eRfc7cCrLj7SM/GNtnKSIA8znNtVf0kCiWJfo4bkfJwkosjCfzG8rICCJmC5Jni9QPW5gk0bQyE5A287/z/L2BSifOMAmB6fM1cxxtDkNsMJNJpUnlnJfy8Nf47DflwOhHsTOvOi1pip/asWvWCRxbeIo5ilhNnQjm08W6owspalFMsI1GXyYJLLI7SzzuyclmyRuCd6HDFLqX7C/N3ExaYkqfobC8vtz6yPMWxwLft37RQw/ujGXa8mM2pqKl38BPLApAw+vi3Nwt/EUFbWEYHZI4e/peK4aIp2+DZkBmA0+ajVUwS26btppgNwQVVXROYQ4i8JjsYW33qRQcDUd2cElSx78//vPIyneWlM8Pj/Yu5lZRwypkvzffesNp4qPxrrfxTG6ZemUw6iulqmo/muLx8uVga/WOcc9StPVV0vpVCeic0U3zj+o3YRF5nZHPmvgenaY84Vj+PIa5w3RiCpDt2el9ClVkDhlgYxwDPUCEhnXAjsB3U3ZlTN3fveTJFauDJ9pwYBtbc1S713AyHG5uVyXgN6GYipfCLQ0MLD2egBy4SSKu1D8wEh3fLDdm91/kqbwI894ppV9uFEhr+hRlRX0ssqaH9BLnPN79Yv+UyuVVqzku2bMwQBg== MIME-Version: 1.0 Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: dbf0f016-cba7-4a63-9102-08d35d79302b X-Microsoft-Exchange-Diagnostics: 1;AM4PR05MB1682;2:69r5/o/pSJrPwRzq7xy3tK7p0G1Zx9VFEsU8VX9VdTJqVMs3GF1SewLfqbE+QIZgkmI3zuOotcREM20fNpqpv3oR1X/fAKSvtsb5VCa87w9JB3htNrdgIMGbsk18eotChfqCeIY8XhBBSOt81RNBPuruojHH5A/I3nBPJ/6ChMwsI9qM2rz1fRjgdkR6PwHB;3:U3p9ntScij3jKIb5Ty59p3Ur9hgQOQwCFpyFaji4IOd3rlJ9/fCJX8GfOov8pO2LXceHvbJ4VehojpXlCKeM9iVH+GGQhWqJeBJvDqJwAMykWhJlXOcFgdj9I6l2ZWrisd5m00Be34dNfVZMyhza+wHVnt2CM6g377VKPNyTOR8UMR0OO6DJhEaqNIlrN67dR7yEVxalBmL329OWmdjVuRbmdKq1gpHmqVguQ2WtUkg= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:AM4PR05MB1682; X-Microsoft-Exchange-Diagnostics: 1;AM4PR05MB1682;25:7fX3lvavJ8rKu7s2jwXt3ejbUw1DufXFFC0l9jttBh0/CwTwAxobj+M7L/iXAKxk/pla74ZN10Pa6apfd41xM076g+lJM0/zEb6hxmPuKYfx5JuUuZUraaZkZJ8RKMnzFx9EZOKJjoiwBQLb5y7rRpP+UuMPKEEW343cWfgLbbzBORei4DbVVcEsNIWPGxGiRwsQNMfM3U0ov4wRWXBdq0UiJqDA1R0N6A9f/nA0YYLvsBkfTMayprFExU5DlaLKPuEf9e5MvlwAfHalKHlufzy682w4D6q/UECUOvrKvIrCihrJJabnj5WtI3cOJvJfxmB172kTbMf01G5Hb4jMHOWJBBUnjMrVYs6WcbvSTjE+YIVNytNt7gMF4v3FpCi+Z871h0ACxFJyGBTFZiNN9bGet8+qF7nxBcdGS6EMxKvtFQn3t+CkTlMqkAcJVIjeBzP0MXUUz11GyPl68SrBqB1Ptnk6z/b/CvyDjN4szJx2yPGQlx11gqDbFR/ZYjnV2emtbkAoEXYrLzMJ3GkQFRcNRLqoLKNBHT4eaJFHQfs1TAZtA6k9Fezz7Wd8A2daU+Tmf0XzRUpjW2d4OhW0bj1y1fgamZi0VEmuwpy1fWY0x4C+qAAZewULoNw9o8GWgxLyPqR1gA8+kdDGvlJPLH8wnWedcj2PJ+AVFRS89ghdH7lLBZCf9050YfxjsGJZ X-Microsoft-Exchange-Diagnostics: 1;AM4PR05MB1682;20:x2l5LgiprnNwUmigYpZvuCqnEGQ8dsMUMY6WDPc5aJn2b0JbXLe/OEQUU7nsu9051Q3xYRhcTwkWsDcD2NQ6VFrGaPrWGUyqsGR9oRVtBTs+hO5dOnMUkOoYafe1kzsm43XXktZR+zolgQi5XhrmAefQjeIsQRtStHSH5F4HqqsMBAlmYO3q79bBp6ovW49TwUY8FtGxojvz39vM3Leo+Bh3+tdvzq1hjU6F/psuXfa5EIqg9is081cSIL8xIBQXtNtxkf3HqxNttGnKFEmzgcAFIfekmG2eZfTsL+eMolCbyppoX+kQpkSQaIg3T+v2D+3KQJ07GFbq445mBf2EhF+htbZuDp6SpRGIAp8JSPU9e3RnNnsPPfF9XnBqAtkxy+0RVtdrFtVLa0P3AZe1YpsABXSx5HOSzvM9+wS0YIt363T01czPGesXGuHZO9CTKrI/SJSLwU0RCpNNiM+oAmmg868u6xTRecdcds7sCsNvRURLuPKE23HpBIawpLwl X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(13024025)(5005006)(8121501046)(13023025)(13018025)(13017025)(13015025)(10201501046)(3002001);SRVR:AM4PR05MB1682;BCL:0;PCL:0;RULEID:;SRVR:AM4PR05MB1682; X-Microsoft-Exchange-Diagnostics: 1;AM4PR05MB1682;4:aCq7wFJdb0l3bzAiJdki7T/Z060gb2vRrc/ZwJudbW3qxzBZStIGZUPQaNPY8ZGYGx+aFQynJ/O14cljSBOLa1SRoCXY1xhMltu6ZJNPHNfIKpLlt03NtSMUDVSVFmGkl1RnWbcHZcT3ybyjf1vYSIdJOZhgIWcBU3GL+EV5isUY6mmHpZ9RHVRd/k0ZnEIUac1LK8MbpgmMInBb+zGIhTCAU5xb5CeS4i32JHEI1r9AJ6AxqpE309QRBO1/c5MwlaW/uW5LoMlNQg/2xOuxadrVRfI+rJDoduLVOID/k1lBjNNVn7ej3BQlXi2bror0NhCdKKy1M2GBCR0CIgQUCg1PXrRPPSqsOQWv01JleNU8rzSfnqXa1+KD5T1oc84QrWeRM0gwGwiLeA67hAYNsmauBJIYD3IYGkt9H/tpLoIjuCHnW+fITvVQ1vlHo0wt+LPRN2eb0cU/DPeHM5k7YA== X-Forefront-PRVS: 0903DD1D85 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;AM4PR05MB1682;23:EGzoG2LU+RSJfCVcOvz6r0sHk+fIIwdHyBlRDUCIz?= =?us-ascii?Q?IQz49Q1tveDj80A/Y0sMKWOmFHS9cRjGql44OligMmwHMk2tjRpXGhsNdASX?= =?us-ascii?Q?mVRPFKo0++pyEphyIznGbpj9LQ+0vBSHeivNa3EF42RYK5IjwbMSQIf8q8/p?= =?us-ascii?Q?SNp5XMYjRYuA0Xegk5ihUpFrYiMqTj4OLFDoiQIE8dcT1kW1go2877kfbBPO?= =?us-ascii?Q?EqcLlVQYHzTgEdGKx4P0C1JrpKH6s6bfJBDyM2rhdi59hUaV67oakN269bMa?= =?us-ascii?Q?6d1tmxiSb/z7vTvCSYKoY0VQ1Fa5/FMF8iuoL3ATUzOyKEfsJ90O5uil1WAV?= =?us-ascii?Q?SwR3+NU9eLXDJhbSuLkzRGwskkaLicvDXCgVTpAEJW7JFkRc7AUPNmys3Lfo?= =?us-ascii?Q?8RebhORH/YiLsCzKNlrTXrxC3kJhk+3cV55idcb8Id6TwIrcuB64N+kUg3Go?= =?us-ascii?Q?MNT8mqgm5PtSlM7z+Wg4p1CruaMcQn+3otKSKeD0FDdwQ86AxjWTWJNyiaht?= =?us-ascii?Q?bOmOIPdR8ZEOOSMBq64LCKTf8OMZECAAAl+rYXtzWqlk+D2NXaUMEvQbgg/a?= =?us-ascii?Q?XaevrpNincrrMopZMCJPK9jF+nTTEBnu6pEJjGzk9uDtH0JUcGvscbcUGsXW?= =?us-ascii?Q?XIcWxK1b/Hc3qbUpb0lEmXVnnhbkRBs3cgN0GLgMBsn2e5JD2zZQfMAsF03z?= =?us-ascii?Q?q0idA26fW0hQ1jLJXClJI1HAJalxkhISqMEHE2swZ053kh7Dwd97XyMz9dAI?= =?us-ascii?Q?WbB/8yyiCoOuXrlbe1IzjPo68RiUrV9AephWOFEeOjLVvvhWybyWRCbzf8g7?= =?us-ascii?Q?c41pLzjScQSRXpcB6nZ5CdepiL/i2lPsc9CCWpAPstSLSU9r6eRvec0+07ZA?= =?us-ascii?Q?GmSvASHDU4FDyXGpV7xN1jQfEkxSb/RCMJuAhh/be5yf/HR6hS2MwBdqAW0E?= =?us-ascii?Q?AkDK+c/ZKYQefeIde8a8LsuKogXLcy827Izviefh1eqzQa7jhY4bkjeuUlcq?= =?us-ascii?Q?Qb6CmkZC/a6WtCleA/VNp3xfGN0fzM9btPv9B3xg5CvE8G5YYOvnkDl64c9c?= =?us-ascii?Q?Gc0CQkdtt5qmEldgj3xtqkCZscOebGkbSXFLca15YhjVfD7S1q2Fb8n6kg0B?= =?us-ascii?Q?M3SfgrXCq86BPXXgfkZV+d5R/guTSsmx6+2+6jLLz3ccwCOhFttkCMYgZcrb?= =?us-ascii?Q?3Ebcw+D/rCapBRViYGD7gshJGWuv9Xjvrso0X4XbdEf4JwEtpoOI/womf1K6?= =?us-ascii?Q?BBNyUBjK4zb1SlSEEAEJftDfMEDuIww8cfiNaN4?= X-Microsoft-Exchange-Diagnostics: 1;AM4PR05MB1682;5:cPAW+nSvZmT4CBmNdyVaZVlol07z6vJ8xT7uN7dtCRhSA1VYqWZOrNODztyKyGROTVuJwW7C4k4c0P8sclBuYVVaeCkGA4eK+9Alf04bvqIDD77l3NaJBvlpDHIgL6zSCKtuSdvHGiK/fbGMzBPq+A==;24:qpgpgAjQ8LadUknDAguveaiqbIji+oCNo3FCsjrtj68R3m0LCOKEzsjheAPnKs+SAGSo5SHd6EKcJrLwX4qntuB6bSgTCf4f/xoAvlZIKv4= X-OriginatorOrg: Mellanox.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Apr 2016 17:39:06.5747 (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: AM4PR05MB1682 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 18921 Lines: 580 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(), and also setting a TIF_TASK_ISOLATION bit in thread_info flags. When task isolation is enabled 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. A new /sys/devices/system/cpu/task_isolation pseudo-file is added, parallel to the comparable nohz_full file. The task_isolation_ready() call is invoked when TIF_TASK_ISOLATION is set in prepare_exit_to_usermode() or its architectural equivalent, and forces the loop to retry if the system is not ready. 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, if TIF_TASK_ISOLATION is set, we call the new task_isolation_enter() routine. This 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 reqeusts 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, tile, and arm64. Signed-off-by: Chris Metcalf --- Documentation/kernel-parameters.txt | 8 ++ drivers/base/cpu.c | 18 +++++ include/linux/isolation.h | 48 +++++++++++ include/linux/sched.h | 3 + include/linux/tick.h | 2 + include/uapi/linux/prctl.h | 5 ++ init/Kconfig | 23 ++++++ kernel/Makefile | 1 + kernel/fork.c | 3 + kernel/isolation.c | 153 ++++++++++++++++++++++++++++++++++++ kernel/signal.c | 4 + kernel/sys.c | 9 +++ kernel/time/tick-sched.c | 36 ++++++--- 13 files changed, 300 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 ecc74fa4bfde..9bd5e91357b1 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -3808,6 +3808,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..99b909462e64 --- /dev/null +++ b/include/linux/isolation.h @@ -0,0 +1,48 @@ +/* + * 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 task_isolation_map != NULL && + cpumask_test_cpu(cpu, task_isolation_map); +} + +extern int task_isolation_set(unsigned int flags); + +extern bool task_isolation_ready(void); +extern void task_isolation_enter(void); + +static inline void task_isolation_set_flags(struct task_struct *p, + unsigned int flags) +{ + p->task_isolation_flags = flags; + + if (flags & PR_TASK_ISOLATION_ENABLE) + set_tsk_thread_flag(p, TIF_TASK_ISOLATION); + else + clear_tsk_thread_flag(p, TIF_TASK_ISOLATION); +} + +#else +static inline void task_isolation_init(void) { } +static inline bool task_isolation_possible(int cpu) { return false; } +static inline bool task_isolation_ready(void) { return true; } +static inline void task_isolation_enter(void) { } +extern inline void task_isolation_set_flags(struct task_struct *p, + unsigned int flags) { } +#endif + +#endif diff --git a/include/linux/sched.h b/include/linux/sched.h index 60bba7e032dc..90f6856493bb 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1852,6 +1852,9 @@ struct task_struct { #ifdef CONFIG_MMU struct task_struct *oom_reaper_list; #endif +#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 62be0786d6d0..fbd81e322860 100644 --- a/include/linux/tick.h +++ b/include/linux/tick.h @@ -235,6 +235,8 @@ static inline void tick_dep_clear_signal(struct signal_struct *signal, extern void tick_nohz_full_kick_cpu(int cpu); extern void __tick_nohz_task_switch(void); +extern void tick_nohz_full_add_cpus(const struct cpumask *mask); +extern bool can_stop_my_full_tick(void); #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 e0d26162432e..767f37bc3391 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -782,6 +782,29 @@ config RCU_EXPEDITE_BOOT endmenu # "RCU Subsystem" +config HAVE_ARCH_TASK_ISOLATION + bool + +config TASK_ISOLATION + bool "Provide hard CPU isolation from the kernel on demand" + depends on NO_HZ_FULL && HAVE_ARCH_TASK_ISOLATION + 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 f0c40bf49d9f..5281b866b0a1 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -114,6 +114,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 d277e83ed3e0..8541b7ee231c 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -76,6 +76,7 @@ #include #include #include +#include #include #include @@ -1507,6 +1508,8 @@ static struct task_struct *copy_process(unsigned long clone_flags, #endif clear_all_latency_tracing(p); + task_isolation_set_flags(p, 0); + /* ok, now we should be set up.. */ p->pid = pid_nr(pid); if (clone_flags & CLONE_THREAD) { diff --git a/kernel/isolation.c b/kernel/isolation.c new file mode 100644 index 000000000000..282a34ecb22a --- /dev/null +++ b/kernel/isolation.c @@ -0,0 +1,153 @@ +/* + * 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; +} + +/* + * Get a snapshot of whether, at this moment, it would be possible to + * stop the tick. This test normally requires interrupts disabled since + * the condition can change if an interrupt is delivered. However, in + * this case we are using it in an advisory capacity to see if there + * is anything obviously indicating that the task isolation + * preconditions have not been met, so it's OK that in principle it + * might not still be true later in the prctl() syscall path. + */ +static bool can_stop_my_full_tick_now(void) +{ + bool ret; + + local_irq_disable(); + ret = can_stop_my_full_tick(); + local_irq_enable(); + return ret; +} + +/* + * This routine controls whether we can enable task-isolation mode. + * The task must be affinitized to a single task_isolation core, or + * else we return EINVAL. And, it must be at least statically able to + * stop the nohz_full tick (e.g., no other schedulable tasks currently + * running, no POSIX cpu timers currently set up, etc.); if not, we + * return EAGAIN. + * + * Although the application could later re-affinitize to a + * housekeeping core and lose task isolation semantics, or other tasks + * could be forcibly scheduled onto this core to restart preemptive + * scheduling, etc., this initial test should catch 99% of bugs with + * task placement prior to enabling task isolation. + */ +int task_isolation_set(unsigned int flags) +{ + if (flags != 0) { + if (cpumask_weight(tsk_cpus_allowed(current)) != 1 || + !task_isolation_possible(raw_smp_processor_id())) + return -EINVAL; + if (!can_stop_my_full_tick_now()) + return -EAGAIN; + } + + task_isolation_set_flags(current, 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 may 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. + * + * A more substantive approach here would be to use a struct + * completion here explicitly, and complete it when we shut + * down dynticks, but since we presumably have nothing better + * to do on this core anyway, just spinning seems plausible. + */ + if (!tick_nohz_tick_stopped()) + set_tsk_need_resched(current); +} diff --git a/kernel/signal.c b/kernel/signal.c index aa9bf00749c1..53e4e62f2778 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -34,6 +34,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -2213,6 +2214,9 @@ relock: /* Trace actually delivered signals. */ trace_signal_deliver(signr, &ksig->info, ka); + /* Disable task isolation when delivering a signal. */ + task_isolation_set_flags(current, 0); + if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */ continue; if (ka->sa.sa_handler != SIG_DFL) { diff --git a/kernel/sys.c b/kernel/sys.c index cf8ba545c7d3..6d5b87273fcc 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -2269,6 +2270,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 084b79f5917e..04e77e562ea1 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -207,6 +208,11 @@ static bool can_stop_full_tick(struct tick_sched *ts) return true; } +bool can_stop_my_full_tick(void) +{ + return can_stop_full_tick(this_cpu_ptr(&tick_cpu_sched)); +} + static void nohz_full_kick_func(struct irq_work *work) { /* Empty, the tick restart happens on tick_nohz_irq_exit() */ @@ -408,30 +414,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.7.2