Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2302280pxb; Fri, 5 Feb 2021 14:23:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJyOIKOCvTGym7mDAkcdEwr3JxY0nrMBnT8PXNA9y5IcOpxuDAPR7C020uVSKY/0dzr9KbaM X-Received: by 2002:a17:906:c34d:: with SMTP id ci13mr6108823ejb.333.1612563788317; Fri, 05 Feb 2021 14:23:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612563788; cv=none; d=google.com; s=arc-20160816; b=JU4Ig/PjyMWGH3G2D3XKp+lSm+4THTxSVr/7xBsbttDzItiQVslqDtyuVAdOVyy2ZW w1EfSKxTd/ya0alvGNmBx3O8fR9OYIPRaM2/+WavGmn3Wlw0bAMq+JfejmeETvtBeH7W bHEbXB9NIGz7IAGXw3OuntLjOMUUYtqMP7Yyla8rcuaRbfvPL0aBU6UcnYCW2q7X/9xh 6NBZdAjU6L/DG/LWjL34n7mfMkUoQ5erqJO5jUyQyG+4rjcMoz6R2sAzxCvHw1pgow8E aTAwAaUOI7qvW7mIoCKrWpgbUSrSkXF65gv/m/7gNUZVrDGSbKC/8aAKnr8vCE2F7B79 pOVg== 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=37/gaLuUoJdBZKKGbDoxQwQcTjmsMWIeI82cRs/XLJg=; b=hKSzBfVdqQD49vupNmexVbb+UeSvK3bR4Z8GW4t4wNXOKJKf0q19i8hEKjioMR6mcm yNgS6eP3gx2Wek0wcKKKWUwzdDiiDfNdRIcb2M2kuclr7durvPTrvb2UgsO6t+RKbb1D rwBRmAcwF1FhtUU31a24ttSimeFerG9weZW2pZAen6mmYDY+wHUDrp9tPUDAPI9nATS8 oT1paF7yfByQPiL97aEIv6k2FaORumRVcOyrnCnBiX5TuOg6c73BvnlL+f7nw4iYDhSy X+tK8u1GrwwbywhXhsgjIjrDOEh7oFBcF19ymEIlnmq5f+ieUq+0cDDlocRnN0hgfJ/+ YfZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="KNe+/toG"; 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 l19si6225954ejb.324.2021.02.05.14.22.43; Fri, 05 Feb 2021 14:23:08 -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="KNe+/toG"; 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 S232931AbhBEWUY (ORCPT + 99 others); Fri, 5 Feb 2021 17:20:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:44974 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232927AbhBEO5J (ORCPT ); Fri, 5 Feb 2021 09:57:09 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6614865094; Fri, 5 Feb 2021 14:13:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612534431; bh=CV+N50vR0wXZUYWyuvQ9Ue+mTRaVX7Jh7HwUsT/Z5q4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KNe+/toGw/KvN4gjny33+HcXPUpAJBb3Y9M+Tr1eJKokwsWlABrtczBRXfeRVsV6x W2XQWFiEGH0+1ielUtPrXcoCGqpysKnFDGIitf/jCe/eOvxJE8RYTbgFLPyJtXJ65L ct6//0HAjmeVzoznrpJh2edrHAXxHGzq3nq1HNZs= 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 4.19 16/17] kthread: Extract KTHREAD_IS_PER_CPU Date: Fri, 5 Feb 2021 15:08:10 +0100 Message-Id: <20210205140650.464297049@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205140649.825180779@linuxfoundation.org> References: <20210205140649.825180779@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 c1961761311db..72308c38e06c4 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -32,6 +32,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 2eed853ab9cc5..81abfac351272 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -460,11 +460,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 c230c2dd48e19..84c16654d8598 100644 --- a/kernel/smpboot.c +++ b/kernel/smpboot.c @@ -187,6 +187,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