Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4083667pxb; Mon, 21 Feb 2022 11:47:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJya9T2cWz+GmWFI1rND6PqaPIh/JgOBpcb/+ek5GjizAWgwuj23lEwCPcDu5YbQzEkDB7/2 X-Received: by 2002:aa7:cd81:0:b0:410:d64e:aa31 with SMTP id x1-20020aa7cd81000000b00410d64eaa31mr23124262edv.167.1645472824704; Mon, 21 Feb 2022 11:47:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645472824; cv=none; d=google.com; s=arc-20160816; b=GA6GoGx8iHfhoAMgD5Vf3R2TciNeotYC/RxuDjSyG5/OJmPJGgPKzb0scaJGiS4+la DiiGae0lkEb4l7Xglm3dgrByHg3IOH5NJv9oeKAdmF2SQuKO7x/gMWsNePiZMnR2sSGO VnVe8fII1hyfiapuewwLGrW7XamGVqcdh/uKDrX7qqQgx3FncDY8j0rEXUPBbPmBHbNH /fHstOnNtMXiNbApeX8A2+/u1n968UrAjDZWGmyURNS/zQhphoK44VR18NhQAKHBWjf6 zgxWq4VkOn1ezHQ3mpsNnyhGc75aISMgYV866j58qXL2/P8I1hep6l509Icf0YAKRczx kv/Q== 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=sErvjwhnFanwCSVuMOBPSIzkIIrvQ3qaCrZlm0Mtssc=; b=XhS2PevyEUwZBxZ5DlPvUS439Vs7d6BRroD8JYmCx1FUAVunVC6p5dpBcxV44cywzb DVHDgib7+lwzis2gC4B1GtPQmJbnZcorCDQmZVCe6jf03jSsacYMUPflxaHxZVbqqhQo 5EtXnAw4OOLmF+V1lAq9doFTVfucsFBMKhKtMlkuy/ZFvEf1+vuAwJPfJ5FPc/e63Rad QZIfbEnCYBrT9U8P6+X8V5jMuhWWpyHHEqm00ijALrxtZfzGs3gviT5gg5H3KXvzsJee L0xhAJWmC6taGypSM1wBvvyxeX1Lz9n4WH+Xjf58ZsTahgRS5Lb3LFHhXf95mZDsy3Ub MQCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ee9XbjTf; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lj7si8775531ejb.914.2022.02.21.11.46.41; Mon, 21 Feb 2022 11:47:04 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=ee9XbjTf; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235139AbiBUJAP (ORCPT + 99 others); Mon, 21 Feb 2022 04:00:15 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:57964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346541AbiBUI7p (ORCPT ); Mon, 21 Feb 2022 03:59:45 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAE2426543; Mon, 21 Feb 2022 00:55:13 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5F0C961132; Mon, 21 Feb 2022 08:55:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40D3FC340E9; Mon, 21 Feb 2022 08:55:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645433712; bh=DxiS2imM2kRlhNNZ2tvupf0VGWXlv4zHDpnN/oeZKQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ee9XbjTfGhfUcuDDNvkVeXL4KpVlCHF3VSSe04SkN/y8rT6iYwvnDrfTbLX5NbhO8 czMD96BfhxLqIstWJy343lIlGcjiGj3gHnoekSkIWNw4ZEdthCwfy1IyuzPLv1A6qT uY3jxYKxqY3DMls/Rkf6NwyGGMbDD4Z0wOjyxwAk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Igor Pylypiv , Changyuan Lyu , Luis Chamberlain , Tejun Heo , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 16/58] Revert "module, async: async_synchronize_full() on module init iff async is used" Date: Mon, 21 Feb 2022 09:49:09 +0100 Message-Id: <20220221084912.413500007@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084911.895146879@linuxfoundation.org> References: <20220221084911.895146879@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Igor Pylypiv [ Upstream commit 67d6212afda218d564890d1674bab28e8612170f ] This reverts commit 774a1221e862b343388347bac9b318767336b20b. We need to finish all async code before the module init sequence is done. In the reverted commit the PF_USED_ASYNC flag was added to mark a thread that called async_schedule(). Then the PF_USED_ASYNC flag was used to determine whether or not async_synchronize_full() needs to be invoked. This works when modprobe thread is calling async_schedule(), but it does not work if module dispatches init code to a worker thread which then calls async_schedule(). For example, PCI driver probing is invoked from a worker thread based on a node where device is attached: if (cpu < nr_cpu_ids) error = work_on_cpu(cpu, local_pci_probe, &ddi); else error = local_pci_probe(&ddi); We end up in a situation where a worker thread gets the PF_USED_ASYNC flag set instead of the modprobe thread. As a result, async_synchronize_full() is not invoked and modprobe completes without waiting for the async code to finish. The issue was discovered while loading the pm80xx driver: (scsi_mod.scan=async) modprobe pm80xx worker ... do_init_module() ... pci_call_probe() work_on_cpu(local_pci_probe) local_pci_probe() pm8001_pci_probe() scsi_scan_host() async_schedule() worker->flags |= PF_USED_ASYNC; ... < return from worker > ... if (current->flags & PF_USED_ASYNC) <--- false async_synchronize_full(); Commit 21c3c5d28007 ("block: don't request module during elevator init") fixed the deadlock issue which the reverted commit 774a1221e862 ("module, async: async_synchronize_full() on module init iff async is used") tried to fix. Since commit 0fdff3ec6d87 ("async, kmod: warn on synchronous request_module() from async workers") synchronous module loading from async is not allowed. Given that the original deadlock issue is fixed and it is no longer allowed to call synchronous request_module() from async we can remove PF_USED_ASYNC flag to make module init consistently invoke async_synchronize_full() unless async module probe is requested. Signed-off-by: Igor Pylypiv Reviewed-by: Changyuan Lyu Reviewed-by: Luis Chamberlain Acked-by: Tejun Heo Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- include/linux/sched.h | 1 - kernel/async.c | 3 --- kernel/module.c | 25 +++++-------------------- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 761d0f85c4a50..f92d5ae6d04e7 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1389,7 +1389,6 @@ extern struct pid *cad_pid; #define PF_MEMALLOC 0x00000800 /* Allocating memory */ #define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */ #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */ -#define PF_USED_ASYNC 0x00004000 /* Used async_schedule*(), used by module init */ #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */ #define PF_FROZEN 0x00010000 /* Frozen for system suspend */ #define PF_KSWAPD 0x00020000 /* I am kswapd */ diff --git a/kernel/async.c b/kernel/async.c index a893d6170944f..4bf1b00a28d86 100644 --- a/kernel/async.c +++ b/kernel/async.c @@ -191,9 +191,6 @@ static async_cookie_t __async_schedule(async_func_t func, void *data, struct asy atomic_inc(&entry_count); spin_unlock_irqrestore(&async_lock, flags); - /* mark that this task has queued an async job, used by module init */ - current->flags |= PF_USED_ASYNC; - /* schedule for execution */ queue_work(system_unbound_wq, &entry->work); diff --git a/kernel/module.c b/kernel/module.c index 68637e661d75c..42a604401c4dd 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -3526,12 +3526,6 @@ static noinline int do_init_module(struct module *mod) } freeinit->module_init = mod->init_layout.base; - /* - * We want to find out whether @mod uses async during init. Clear - * PF_USED_ASYNC. async_schedule*() will set it. - */ - current->flags &= ~PF_USED_ASYNC; - do_mod_ctors(mod); /* Start the module */ if (mod->init != NULL) @@ -3557,22 +3551,13 @@ static noinline int do_init_module(struct module *mod) /* * We need to finish all async code before the module init sequence - * is done. This has potential to deadlock. For example, a newly - * detected block device can trigger request_module() of the - * default iosched from async probing task. Once userland helper - * reaches here, async_synchronize_full() will wait on the async - * task waiting on request_module() and deadlock. - * - * This deadlock is avoided by perfomring async_synchronize_full() - * iff module init queued any async jobs. This isn't a full - * solution as it will deadlock the same if module loading from - * async jobs nests more than once; however, due to the various - * constraints, this hack seems to be the best option for now. - * Please refer to the following thread for details. + * is done. This has potential to deadlock if synchronous module + * loading is requested from async (which is not allowed!). * - * http://thread.gmane.org/gmane.linux.kernel/1420814 + * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous + * request_module() from async workers") for more details. */ - if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC)) + if (!mod->async_probe_requested) async_synchronize_full(); ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base + -- 2.34.1