Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2738934pxj; Mon, 31 May 2021 09:30:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSJ14rIAJB5sDAydPc97sbZNnji3iCg0ZUvliozcV9hk+w+TKXO03xEmYTdMrFnRLIRECC X-Received: by 2002:a17:906:abcc:: with SMTP id kq12mr24535540ejb.97.1622478656554; Mon, 31 May 2021 09:30:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622478656; cv=none; d=google.com; s=arc-20160816; b=a0HErwAABCe6MaRSPAZvw+C0j8FTOKL0nO4FVoZrBad4XH8rA7FpFgrBwILTFf3UVi 4F1SO0kQ0+BFciXpCz+5PnCq86kmvFXpRobUfLTstBRIuqXaxZLxXBsaOiWY6gnQwqMs BvTksmgzVsiAcDrv1szPrmwxSjfFbuWTzWYRVLmIRxqGcU7LzYiJFIG+H9wwVbV/J7SE cLk3lE+aIcfdnL4RRGbcnXqGBBnShsWy76xnILnEoNwnH0PhGXekCIZ/dvsA1uNXuUeV JVGZFK1ntAcyYD+5U0otSMJNNH0v0++mUECUZdwv6G7DgkX7++ytjCMGyyX6dJsjJmxx a0ZA== 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=eeYuIkmLGoxPC9UwyL8ElVAWbYczI8+U7qFQXGMfSZY=; b=zkEv2ObV3LUOKzpJ4S4yV+VzN3wRTKB27Dx/UjzlJhwUG7lr8JeXttuIiX/lXCN3vO KrPVp1LHGPx6gFd1Bw/GdNyhy4JVuxmqagOCxgLB3cufty7v8YHZNt1X9TK2HjInsbmC 6Ipai/+fvzGeJTGnS72L2Dn313Uk2Z455QdnqIwba62CjV+robuPgnyGDJRgjDtaGad9 yhShSzPBViisi6G1IA+05lTPVD2yu2MLCKWcce8fw7fEyVB59AeNjyymRgALkercXY0G ciYKBMFsAoCWwSP6KkXB27ie8C3dqWrBkPsw7KdtVWE7T513MH1UyAe58wx5flA5k8Qs Maxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=f8ZINK5z; 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 v24si13203576ejc.165.2021.05.31.09.30.29; Mon, 31 May 2021 09:30:56 -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=f8ZINK5z; 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 S234008AbhEaQam (ORCPT + 99 others); Mon, 31 May 2021 12:30:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:40824 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233691AbhEaOpm (ORCPT ); Mon, 31 May 2021 10:45:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 71BB261C89; Mon, 31 May 2021 13:55:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469342; bh=kdjzop78knCLkR4VrgYLtpxAmxAhLQBeZYKw52Pzjhc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f8ZINK5z+w1mL8SzhPn/9XIgxwdrLnUcOYTHNDNaCt9PMX8uH0aU/+u9AaeUFMNRy kR0mf8g3InGoqJ8/s0WG73kqpJTMzTMd6XLDV868gMJ90+uIqIWmrC+U3MJH/yyqkQ L/lPpfYw4mVdQGy7s+MSDzvKZsd0acDRjXk2FsKw= 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.12 134/296] tipc: wait and exit until all work queues are done Date: Mon, 31 May 2021 15:13:09 +0200 Message-Id: <20210531130708.397745526@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@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 @@ -119,6 +119,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 @@ -149,6 +149,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 @@ -812,6 +812,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(); @@ -832,6 +833,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); }