From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3E22C43333 for ; Sat, 16 Jan 2021 17:10:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9FBAB2388E for ; Sat, 16 Jan 2021 17:10:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728245AbhAPRKZ (ORCPT ); Sat, 16 Jan 2021 12:10:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728079AbhAPRKW (ORCPT ); Sat, 16 Jan 2021 12:10:22 -0500 Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD79EC0617BD for ; Sat, 16 Jan 2021 06:45:15 -0800 (PST) Received: by mail-io1-xd2e.google.com with SMTP id y19so23995904iov.2 for ; Sat, 16 Jan 2021 06:45:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=EHihIcKDoV0+JHPY4VGxSXXMUYFdCoryZeO5QPeUbJc=; b=GnaGfjbkXKtzD9cX08SDfUJtODWmWyYUR89tTrLQgOD1I5QxSiHgjQMO/m8swKC3sN h7LVVYetoA/fYq+IkgN+Mf2t5mARxHNvhMCTFwAxuEvtbrwIlhUfXqDQ0n4pSJqbXJl/ cAb0OBHLuLB4qEZ07mNIMgroKOHO7L20gfJ5NN3RBBt10D1hxW2Z5bKa0t7Wp4pAMqgR KbKZPoq4IpZGK2PF7QIx+dVs0GzExH4THCt+WnltChaT73PRi7GXa9RejQI6b08j3qng 2BLcL3zFCFWibRL4K4PCe8zJW+Fvk20zx+egLzNccgJQh7eVUYG56EGgIoOqzfwIXmWt aJfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=EHihIcKDoV0+JHPY4VGxSXXMUYFdCoryZeO5QPeUbJc=; b=b4cKX2NbnNadvS6tF36M1jnaGYYmUa/yy7dhvinsSntrsULqsH8ICNVk3ohUcLIIBb Rr43Xxb9KndM2glYnO0hZguoQmFBbteA3UvU8a+huKP3rLnIHRGe92RgtWvaL65ytxFo 7UBk6QobBfW8cClCFdeb1Fly3iPrDu90fBRN2OR5tTswXxFgT2kmo+aEs8lZl+o6oQxW G7LMZr3uKaNGvE6KdZQWdmbYs43kGZ2oq6qYly8+vDLqo2UioUkyNlZ+erE6xUzDqIDV wbaJZY41e/CILZQFl2SjZGZG8u04waClMyBoDv2O+Vle/bSfSfoo10VHBOoZ9B9BTm7p 6NCQ== X-Gm-Message-State: AOAM533FKdc6B2W2UZePdNJkQuNPCNlnONAKjoJmUJitfQA35jaQBx7o 3kTzBo3NBGutKC4p3JLoxvmUm+cMsb5K303Rtik= X-Google-Smtp-Source: ABdhPJw6eRSI1V+vyLoF3FgZnrQIknsQXZyGjEmAtVMeI5M2g92vnTxIkF8bY4HVhiFXlEgph4zZyGmp02wdxjDFvI0= X-Received: by 2002:a5d:8c85:: with SMTP id g5mr12002223ion.154.1610808315233; Sat, 16 Jan 2021 06:45:15 -0800 (PST) MIME-Version: 1.0 References: <20210112144344.850850975@infradead.org> <20210112144843.849135905@infradead.org> In-Reply-To: From: Lai Jiangshan Date: Sat, 16 Jan 2021 22:45:04 +0800 Message-ID: Subject: Re: [PATCH 3/4] workqueue: Tag bound workers with KTHREAD_IS_PER_CPU To: Peter Zijlstra Cc: Valentin Schneider , Ingo Molnar , Thomas Gleixner , LKML , Qian Cai , Vincent Donnefort , Dexuan Cui , "Paul E. McKenney" , Vincent Guittot , Steven Rostedt , Tejun Heo Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jan 16, 2021 at 8:45 PM Peter Zijlstra wrote: > > On Sat, Jan 16, 2021 at 02:27:09PM +0800, Lai Jiangshan wrote: > > On Thu, Jan 14, 2021 at 11:35 PM Peter Zijlstra wrote: > > > > > > > > -void kthread_set_per_cpu(struct task_struct *k, bool set) > > > +void kthread_set_per_cpu(struct task_struct *k, int cpu) > > > { > > > struct kthread *kthread = to_kthread(k); > > > if (!kthread) > > > return; > > > > > > - if (set) { > > > - WARN_ON_ONCE(!(k->flags & PF_NO_SETAFFINITY)); > > > - WARN_ON_ONCE(k->nr_cpus_allowed != 1); > > > - set_bit(KTHREAD_IS_PER_CPU, &kthread->flags); > > > - } else { > > > + 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); > > > } > > > > > > > I don't see the code to set the mask of the cpu to the task > > since set_cpus_allowed_ptr() is removed from rebind_worker(). > > > > Is it somewhere I missed? > > kthread_unpark(). > > > > @@ -4978,9 +4982,9 @@ static void rebind_workers(struct worker_pool *pool) > > > * from CPU_ONLINE, the following shouldn't fail. > > > */ > > > for_each_pool_worker(worker, pool) { > > > - WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, > > > - pool->attrs->cpumask) < 0); > > > - kthread_set_per_cpu(worker->task, true); > > > + WARN_ON_ONCE(kthread_park(worker->task) < 0); > > > + kthread_set_per_cpu(worker->task, pool->cpu); > > > + kthread_unpark(worker->task); > > > > I feel nervous to use kthread_park() here and kthread_parkme() in > > worker thread. And adding kthread_should_park() to the fast path > > also daunt me. > > Is that really such a hot path that an additional load is problematic? > > > How about using a new KTHREAD_XXXX instead of KTHREAD_IS_PER_CPU, > > so that we can set and clear KTHREAD_XXXX freely, especially before > > set_cpus_allowed_ptr(). > > KTHREAD_IS_PER_CPU is exactly what we need, why make another flag? > > The above sequence is nice in that it restores both the > KTHREAD_IS_PER_CPU flag and affinity while the task is frozen, so there > are no races where one is observed and not the other. > > It is also the exact sequence normal per-cpu threads (smpboot) use to > preserve affinity. Other per-cpu threads normally do short-live works. wq's work can be lengthy, cpu-intensive, heavy-lock-acquiring or even call get_online_cpus() which might result in a deadlock with kthread_park().