Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2141666pxb; Fri, 5 Feb 2021 09:53:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJxg/Q1sStQ1jyml2J07wtFqgKNar2Qb/qanJMaIZTPW3uNfCxI6Dgv4EbgHQg+HwW5tCU5T X-Received: by 2002:a05:6402:d09:: with SMTP id eb9mr4698949edb.285.1612547626437; Fri, 05 Feb 2021 09:53:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612547626; cv=none; d=google.com; s=arc-20160816; b=ndUGoeW1Q5uXLcu+PcUz7wx+2TfX2M/IQAUAMUuoHfE0Uy5YP9DxyLvElcRkocVnXn 60pd9VAcVB/JFx2TiCnJdwPuRv9TnnUN2+b4v+usXbrdtAWMvf3fherIi5LBVb/jMVTA sr1y+GWnXXHQRWfwmqOnI788iV3oej1/beFonVZCsukZbTJUbOxdBYMlzAOCv+jmwZuU amOrgWZF8gdNdm29PTgnBjvYltM8WnCXlmbEUTwoMqB+TcFBSIvJzdMAQuobggWbt7gh KgChDwYc02xqUViB9oXqq0UbM8fh7+RuH6ZUxke4VA9qyrkeLnaGVIjZt6mCGdkCJ6ZW CnrQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BtGYqh0CGxj6jiLQzTtlQVtlXnsn4q1L5Y5o3020faA=; b=sus+cqz+kR0fYcIo6r/YKjaSVv6BUzJNMts7XiBUTKQnBdcAoYs44H+6ZeYQUJ8wKl 30wRwKALglSN271FCkMR+eh86VPCrr+v2O3Sh3U++bOtCnhroO6hUwvAwgD2+SY5E+ZL 2FaP+NFRufbeJBgjJegtYOo1q7jhiDUjhopr45fPsN2/p4UubQ+aSnlnp0rFWBTkJd7s v8R0AixLwNx6HCC+s3Y6/+0ZpbdNAJtYFYpa0pZIpqwInesF9dFTYqDBo1Y9qcJgCYjx loB5Mm5/Mdra3FPALD6YdGVkOjsxhJybf2PtWCMJPiqpjCg0lcpihZpZfxUZaEGl3l7G mn+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0Kr89kBo; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j21si3799198ejn.713.2021.02.05.09.53.20; Fri, 05 Feb 2021 09:53:46 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=0Kr89kBo; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232591AbhBEQKI (ORCPT + 99 others); Fri, 5 Feb 2021 11:10:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:54900 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233538AbhBEQC0 (ORCPT ); Fri, 5 Feb 2021 11:02:26 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C0A9665070; Fri, 5 Feb 2021 14:12:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612534380; bh=pTxpyCFqUKZ3YRONb+u0vMa8oBSPHdGX8W0v+HTaRJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0Kr89kBov0aJaSegZYuNTi66aZrKiC2jgSZlFefvFh8OawLoQcHgzQwS4cAAXUJYQ Gtqj8WcBCYGpCvpBcOc4Z87/oYSyRI7SVtLLLPvL7SMua7tUc4NWS1R7OBoCiSkqGH jtD24RO0LOoui3XYWyuIN25DOVBfPuE+DNSl7yzk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Valentin Schneider , Sasha Levin Subject: [PATCH 5.4 31/32] kthread: Extract KTHREAD_IS_PER_CPU Date: Fri, 5 Feb 2021 15:07:46 +0100 Message-Id: <20210205140653.666133800@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205140652.348864025@linuxfoundation.org> References: <20210205140652.348864025@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra [ Upstream commit ac687e6e8c26181a33270efd1a2e2241377924b0 ] There is a need to distinguish geniune per-cpu kthreads from kthreads that happen to have a single CPU affinity. Geniune per-cpu kthreads are kthreads that are CPU affine for correctness, these will obviously have PF_KTHREAD set, but must also have PF_NO_SETAFFINITY set, lest userspace modify their affinity and ruins things. However, these two things are not sufficient, PF_NO_SETAFFINITY is also set on other tasks that have their affinities controlled through other means, like for instance workqueues. Therefore another bit is needed; it turns out kthread_create_per_cpu() already has such a bit: KTHREAD_IS_PER_CPU, which is used to make kthread_park()/kthread_unpark() work correctly. Expose this flag and remove the implicit setting of it from kthread_create_on_cpu(); the io_uring usage of it seems dubious at best. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Valentin Schneider Tested-by: Valentin Schneider Link: https://lkml.kernel.org/r/20210121103506.557620262@infradead.org Signed-off-by: Sasha Levin --- include/linux/kthread.h | 3 +++ kernel/kthread.c | 27 ++++++++++++++++++++++++++- kernel/smpboot.c | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 0f9da966934e2..c7108ce5a051c 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -31,6 +31,9 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), unsigned int cpu, const char *namefmt); +void kthread_set_per_cpu(struct task_struct *k, int cpu); +bool kthread_is_per_cpu(struct task_struct *k); + /** * kthread_run - create and wake a thread. * @threadfn: the function to run until signal_pending(current). diff --git a/kernel/kthread.c b/kernel/kthread.c index e51f0006057df..1d4c98a19043f 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -469,11 +469,36 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), return p; kthread_bind(p, cpu); /* CPU hotplug need to bind once again when unparking the thread. */ - set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags); to_kthread(p)->cpu = cpu; return p; } +void kthread_set_per_cpu(struct task_struct *k, int cpu) +{ + struct kthread *kthread = to_kthread(k); + if (!kthread) + return; + + WARN_ON_ONCE(!(k->flags & PF_NO_SETAFFINITY)); + + if (cpu < 0) { + clear_bit(KTHREAD_IS_PER_CPU, &kthread->flags); + return; + } + + kthread->cpu = cpu; + set_bit(KTHREAD_IS_PER_CPU, &kthread->flags); +} + +bool kthread_is_per_cpu(struct task_struct *k) +{ + struct kthread *kthread = to_kthread(k); + if (!kthread) + return false; + + return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags); +} + /** * kthread_unpark - unpark a thread created by kthread_create(). * @k: thread created by kthread_create(). diff --git a/kernel/smpboot.c b/kernel/smpboot.c index 2efe1e206167c..f25208e8df836 100644 --- a/kernel/smpboot.c +++ b/kernel/smpboot.c @@ -188,6 +188,7 @@ __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu) kfree(td); return PTR_ERR(tsk); } + kthread_set_per_cpu(tsk, cpu); /* * Park the thread so that it could start right on the CPU * when it is available. -- 2.27.0