Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp1195885rwb; Thu, 4 Aug 2022 19:49:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR7l8hYEdmDEgIBm7lqN6Odj4+cbeOCbRGalAtbFjfaYUR3BjCV7R0L1fdnSO0ePN5kvnEUA X-Received: by 2002:a17:907:6e88:b0:730:e16e:b760 with SMTP id sh8-20020a1709076e8800b00730e16eb760mr2226045ejc.5.1659667775381; Thu, 04 Aug 2022 19:49:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659667775; cv=none; d=google.com; s=arc-20160816; b=q4lKeYZb19UIMfKkGHQqhTkCHOBgNE3LP5knZpV92R+/35UNCxxebUmlqp95RgzLi/ g9x9jwhOmS0ZtszgByAvcVRdiC3akfQ5vFYNt+ULa1btbZn4iJnxPwAaZ5CNUY/eYiJR Y2K/Vdu5RpGbaLF/ymd9INqDv8i9/tuANspE1TZvuP+m0kednTgfrUXk3kAaQ2IGOCMU wIQKqgdU219xBjBcMj8eVvNa1Pt7TidcOP4Le0afzovivc/Y+wiBz6wBIM/OmJSblI7a z0rrhBoGHNxRnrPCFFkqoikcvz5aSyDf+FSe+NOSur6k1YZ0OKN4pnrDtN1AMFk4b0c+ CeUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=f2BJAmcvvAY58PqydeJ3rqlXHLmAQBj3QTr5P4Kw3c0=; b=ZaAZvjKj5ObxuI9lcBalwSgx8NsSC74tYPKM8Ksyi8c3NACirx+ee6fj6QUPLo1vgP ZnYhBD/aGBPd6GkJxXn0/3KAPXhJyhDxJGG7QspHCZmPLIU2RqXq8S2HoL6h7lAzeygc y/CXJI2AmY87Iki9sDABV2WMnyledBDHMqPCQ97bL78FYHBntJBABAdZOg62sDgN/M8Z 0IzGESOC8R5HQNPLH682vljOiXN0sh1xYYdL9jvScBIbq2D8zgSU5QrvD3rigaYZda41 15TX4s/FX1jAIyIZKmkSCsKpTYCbfvf75A9VjPorhXJNrcOX9b1YnBFALZA6Qt+6h2Gx h3EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BCWgPMUi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dn22-20020a17090794d600b007303cde561csi1009508ejc.947.2022.08.04.19.49.10; Thu, 04 Aug 2022 19:49:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BCWgPMUi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233049AbiHECa1 (ORCPT + 99 others); Thu, 4 Aug 2022 22:30:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231175AbiHECaZ (ORCPT ); Thu, 4 Aug 2022 22:30:25 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5E4D2648 for ; Thu, 4 Aug 2022 19:30:23 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id a11so769000wmq.3 for ; Thu, 04 Aug 2022 19:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=f2BJAmcvvAY58PqydeJ3rqlXHLmAQBj3QTr5P4Kw3c0=; b=BCWgPMUiqsf6voBMKS3E1sVghsxihJNJYf6OZNBIW1EBgukFXdjKdpXkknYwboJZmV Oqg80QJnNRE9vRnhZrYEE6FN4M8zsYFhNRd87LrpXvgxVp8qO43AaJjaXBa5iwfOQPPa tw+vo0v2ZRV+Vh+ewQct2iJyX0WkhWWL3QVKiRwe1zLaZkLJjMbe6BY7qqb3qG83QcoG mA2Bdy+AaF8GIYwiIzNJsCbB4/XmlsTLrpKxAMW/r+ux6DKmla4N7synfrp3WyzfHslQ Hac9bUR4u4M6yF+OjPnj/60AKZ+lox08y6pAEGGbISYv9LBpsiVVoZNU7uzF9B57SMOx PNPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=f2BJAmcvvAY58PqydeJ3rqlXHLmAQBj3QTr5P4Kw3c0=; b=mQ8ctsCcZq179Oed9yEgReOnbijtgHAsY5AHvZyMtdFrUJLI306DsEyIsNbi22k9Ch YqdEZV9Ga7a6YbYDjhgwjJbn/SK8JbbFADRXSdKtXNI4UxJt6tpHopLXsDKlP4qteyBA JEE+ygEudpL4q14NjLfInXoOiA6IHXFVZUG7QXWUIQ+Yr2IAYjbt1TmR2jjSFsPXDJ9O YT8ZLuNGOokcbgQ075Jxt7TMgEgvnfZmCNtmSz1DuyF+V7GIEjhXa9Q5N/KK881mSRJu S16VJxfTaP0bEU3sQHaLtn0LUZOdETm2/2AiPDKvPgQRfJNFu1AiXi6BfeTlwJsAIjEu xTNw== X-Gm-Message-State: ACgBeo1PdvT9cGmiWSJ6PI5Caeh8S4vZHJr6YdCldv9GaQ6M6vywyINV XgsMI65MF43ZHlMbjxS5J6mTjE/Fz5wrEHU19Bi+OI6To4o= X-Received: by 2002:a7b:cb55:0:b0:3a5:41a:829c with SMTP id v21-20020a7bcb55000000b003a5041a829cmr5524554wmj.153.1659666622409; Thu, 04 Aug 2022 19:30:22 -0700 (PDT) MIME-Version: 1.0 References: <20220804084135.92425-1-jiangshanlai@gmail.com> <20220804084135.92425-3-jiangshanlai@gmail.com> <20220804123520.1660-1-hdanton@sina.com> In-Reply-To: <20220804123520.1660-1-hdanton@sina.com> From: Lai Jiangshan Date: Fri, 5 Aug 2022 10:30:10 +0800 Message-ID: Subject: Re: [RFC PATCH 2/8] workqueue: Make create_worker() safe against prematurely wakeups To: Hillf Danton Cc: LKML , linux-mm@kvack.org, Petr Mladek , Peter Zijlstra Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org i On Thu, Aug 4, 2022 at 8:35 PM Hillf Danton wrote: > > On Thu, 4 Aug 2022 16:41:29 +0800 Lai Jiangshan wrote: > > > > @@ -1942,6 +1943,7 @@ static struct worker *create_worker(struct worker_pool *pool) > > goto fail; > > > > worker->id = id; > > + worker->pool = pool; > > > > if (pool->cpu >= 0) > > snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, > > @@ -1949,6 +1951,7 @@ static struct worker *create_worker(struct worker_pool *pool) > > else > > snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); > > > > + reinit_completion(&pool->created); > > worker->task = kthread_create_on_node(worker_thread, worker, pool->node, > > "kworker/%s", id_buf); > > if (IS_ERR(worker->task)) > > @@ -1957,15 +1960,9 @@ static struct worker *create_worker(struct worker_pool *pool) > > set_user_nice(worker->task, pool->attrs->nice); > > kthread_bind_mask(worker->task, pool->attrs->cpumask); > > > > - /* successful, attach the worker to the pool */ > > - worker_attach_to_pool(worker, pool); > > - > > /* start the newly created worker */ > > - raw_spin_lock_irq(&pool->lock); > > - worker->pool->nr_workers++; > > - worker_enter_idle(worker); > > wake_up_process(worker->task); > > - raw_spin_unlock_irq(&pool->lock); > > + wait_for_completion(&pool->created); > > > > return worker; > > cpu0 cpu1 cpu2 > === === === > complete > > reinit_completion > wait_for_completion reinit_completion() and wait_for_completion() are both in create_worker(). create_worker() itself is mutually exclusive which means no two create_worker()s running at the same time for the same pool. No work item can be added before the first initial create_worker() returns for a new or first-online per-cpu pool, so there would be no manager for the pool during the first initial create_worker(). The manager is the only worker who can call create_worker() for a pool except the first initial create_worker(). And there is always only one manager after the first initial create_worker(). The document style in some of workqueue code is: "/* locking rule: what it is */" For example: struct list_head worklist; /* L: list of pending works */ which means it is protected by pool->lock. And for struct completion created; /* create_worker(): worker created */ it means it is protected by the exclusive create_worker(). > > Any chance for race above?