Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2659543pxj; Mon, 31 May 2021 07:37:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmL2M7wMJa2IdwGTBiPm7DiUXqasZXYlZz4esKTP5zG399NCEshgrPud/uN+WeFVWXmWlY X-Received: by 2002:a17:907:33d0:: with SMTP id zk16mr11287535ejb.144.1622471844305; Mon, 31 May 2021 07:37:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622471844; cv=none; d=google.com; s=arc-20160816; b=PyAZPuzcR4jSNPITEUkbA+LIC2IAsVhtzzsHtOfL+O6AdyCUcEtcEkCe2Ph9emGc0m R1sXvuPfl9bAZdTbtEkJpGHxmKMBmviITYgnLQU5pVZSPTRfetGUYyuILTvn+yU6RJQv fQ313qtmQTT2dpJ1sBrODhba8vYS3fYYjFaTXLEpxa7Sr6TqK1M6Pj2wm7qj5nYH26+k kjfKFqggTMdOQLCIOiMHvkHqv05zA9x5eSTl4pGENr6SMlzW+pF8GTh+jdV9u68H2paU GlUzl0p9eqe+nyir1r7oHPECgYc59KEdMfzSOh3q5fiaA3ZEMPq12FbnXDY5fzk/XlgL mCbw== 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=hxhwYMHGpXgbVlwId2aJ8Ox8ZRC+VDPit4KA4T5FcHo=; b=gewHPVr04aSE96PFmOFnsXvGoHZj4Crp0gO6UH0Il6ZjgtaltTsib6RMWBlGsQ9CXI SGT/nI3BHV4YRktPy9ApVhK84YAGQMy+suVAe2hSaHut0pwvXkIky2DDMlitOi2i4pVW hCx0tz5Eg9mDqQd8jAQXfTcfc8rji7m6gTMo4/Hv8+NX38mljK3K1vyjOT7Ysz1CylUZ 06uUhL0tj09NZjp9hbQxXP7DgRyvmVDT4s58uxbWXon5QmH3LLOmpkQ0KMrIbpsrftMt pnxajt/fDCZedXog6yVxhQ69IocQB06g31600M2O7/nHW/ETOQXfD7ZH8UVtn1oL2XQP UhcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=p44UR1mL; 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 yh21si16212048ejb.150.2021.05.31.07.36.59; Mon, 31 May 2021 07:37:24 -0700 (PDT) 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=p44UR1mL; 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 S232866AbhEaOhi (ORCPT + 99 others); Mon, 31 May 2021 10:37:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:60840 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232656AbhEaN42 (ORCPT ); Mon, 31 May 2021 09:56:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 191E861929; Mon, 31 May 2021 13:34:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622468081; bh=RqXk2yi0BUOZJ/yOhOEQKEeAozBKF0mcnoPjQEu93TE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p44UR1mLIKpWPxYDSjAwWU/qhi35Qkoj1+0VRHud6zE4yygKSOX4wEIb2cpxs4WEb kpZtemQWsOzw1uilmjj/F5Z34NIAA7bIJIw27Z9KOHbqVHEvcTZdBVbyIfVHem/rvl E81AOIpxd5jNZ3y6KOGiii4Ea+BbjjFqi/qt4EbY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Shuang Li , Xin Long , Jon Maloy , "David S. Miller" Subject: [PATCH 5.10 108/252] tipc: wait and exit until all work queues are done Date: Mon, 31 May 2021 15:12:53 +0200 Message-Id: <20210531130701.656863490@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130657.971257589@linuxfoundation.org> References: <20210531130657.971257589@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: Xin Long commit 04c26faa51d1e2fe71cf13c45791f5174c37f986 upstream. On some host, a crash could be triggered simply by repeating these commands several times: # modprobe tipc # tipc bearer enable media udp name UDP1 localip 127.0.0.1 # rmmod tipc [] BUG: unable to handle kernel paging request at ffffffffc096bb00 [] Workqueue: events 0xffffffffc096bb00 [] Call Trace: [] ? process_one_work+0x1a7/0x360 [] ? worker_thread+0x30/0x390 [] ? create_worker+0x1a0/0x1a0 [] ? kthread+0x116/0x130 [] ? kthread_flush_work_fn+0x10/0x10 [] ? ret_from_fork+0x35/0x40 When removing the TIPC module, the UDP tunnel sock will be delayed to release in a work queue as sock_release() can't be done in rtnl_lock(). If the work queue is schedule to run after the TIPC module is removed, kernel will crash as the work queue function cleanup_beareri() code no longer exists when trying to invoke it. To fix it, this patch introduce a member wq_count in tipc_net to track the numbers of work queues in schedule, and wait and exit until all work queues are done in tipc_exit_net(). Fixes: d0f91938bede ("tipc: add ip/udp media type") Reported-by: Shuang Li Signed-off-by: Xin Long Acked-by: Jon Maloy Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/tipc/core.c | 2 ++ net/tipc/core.h | 2 ++ net/tipc/udp_media.c | 2 ++ 3 files changed, 6 insertions(+) --- a/net/tipc/core.c +++ b/net/tipc/core.c @@ -121,6 +121,8 @@ static void __net_exit tipc_exit_net(str #ifdef CONFIG_TIPC_CRYPTO tipc_crypto_stop(&tipc_net(net)->crypto_tx); #endif + while (atomic_read(&tn->wq_count)) + cond_resched(); } static void __net_exit tipc_pernet_pre_exit(struct net *net) --- a/net/tipc/core.h +++ b/net/tipc/core.h @@ -151,6 +151,8 @@ struct tipc_net { #endif /* Work item for net finalize */ struct tipc_net_work final_work; + /* The numbers of work queues in schedule */ + atomic_t wq_count; }; static inline struct tipc_net *tipc_net(struct net *net) --- a/net/tipc/udp_media.c +++ b/net/tipc/udp_media.c @@ -806,6 +806,7 @@ static void cleanup_bearer(struct work_s kfree_rcu(rcast, rcu); } + atomic_dec(&tipc_net(sock_net(ub->ubsock->sk))->wq_count); dst_cache_destroy(&ub->rcast.dst_cache); udp_tunnel_sock_release(ub->ubsock); synchronize_net(); @@ -826,6 +827,7 @@ static void tipc_udp_disable(struct tipc RCU_INIT_POINTER(ub->bearer, NULL); /* sock_release need to be done outside of rtnl lock */ + atomic_inc(&tipc_net(sock_net(ub->ubsock->sk))->wq_count); INIT_WORK(&ub->work, cleanup_bearer); schedule_work(&ub->work); }