Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1195526ybn; Wed, 2 Oct 2019 12:15:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqyILeYNUXTdjcg4w6Fctbv6X5AocRAlPVrFXe3R0JsL0v52DBrUzfx54L+cmOFpwDX/H8V/ X-Received: by 2002:a50:ac0a:: with SMTP id v10mr5537383edc.83.1570043745465; Wed, 02 Oct 2019 12:15:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570043745; cv=none; d=google.com; s=arc-20160816; b=da9IgKleTyPRLHASiYEutCHIElWQBZTuGD/m6rGqNqVolk9mqTepdx6Hp4L3ltdcrl 7moodvRXDPCynhEPms9/5+ROESBe3ifkyp5Fac0z+vGL+MG8IhFNCfh6XEnZYFqVsp9w F2vLr12spRWulobpPx5b/tqc61uUNsjaf8qqrOAwdj0v8xcVY7b60pBJSDFfqHcMEzTE o0YqcSD/L86qKLzxX0JIzvfmYnFMmi9wR3Tw9yfjpCru9hMt/wgp9EQCHkHGf6D/4aQ/ oWlb/J3U9bn+KllYCyXoHGVmoBdeWc/jvuq5TTqEuinTlZmHhg1nflJ1GOyUOu/EJ8p8 zBKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=pYwXUupIw60YCcK9aQV4sSi8DJxNAbcyDv3ND6x3Tks=; b=zGGH3zOTYZmVAx3iEZMBfGymCVZyezIZ3B+KyXhJ3AzNSwSwyYABNrOHIgE9/zj2+S mjhieCojwClMzuSlTTesW4VXAbDccw63ykZppJCezm7o+t4GRa7rfhN8kZY43kUjPnq0 By1ZeKr2PjMbgkhmNFlMpvKEOmKu/5lOoY91Nf3/4zhm5R9z5bn/d/48KxYjBXfuWXsl 1i394Jg35vi+Ur+09FS+EuWMNzLlGF5pKxXVLy8PFZD+klMLPHbABQ0PjOfinLxJmSUk vYuyH5wO1rSX/km9Aia+yxJ1FsSzXYunKXUgZXbz11NHinixgID/yRxq32vB/17clGzT TGZQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rn4si6177ejb.251.2019.10.02.12.15.21; Wed, 02 Oct 2019 12:15:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730051AbfJBTME (ORCPT + 99 others); Wed, 2 Oct 2019 15:12:04 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35584 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729197AbfJBTIM (ORCPT ); Wed, 2 Oct 2019 15:08:12 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iFjyr-00035s-Bw; Wed, 02 Oct 2019 20:08:09 +0100 Received: from ben by deadeye with local (Exim 4.92.1) (envelope-from ) id 1iFjyo-0003di-Fv; Wed, 02 Oct 2019 20:08:06 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "David S. Miller" , "Paolo Abeni" Date: Wed, 02 Oct 2019 20:06:51 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 48/87] pktgen: do not sleep with the thread lock held. In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.75-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Paolo Abeni commit 720f1de4021f09898b8c8443f3b3e995991b6e3a upstream. Currently, the process issuing a "start" command on the pktgen procfs interface, acquires the pktgen thread lock and never release it, until all pktgen threads are completed. The above can blocks indefinitely any other pktgen command and any (even unrelated) netdevice removal - as the pktgen netdev notifier acquires the same lock. The issue is demonstrated by the following script, reported by Matteo: ip -b - <<'EOF' link add type dummy link add type veth link set dummy0 up EOF modprobe pktgen echo reset >/proc/net/pktgen/pgctrl { echo rem_device_all echo add_device dummy0 } >/proc/net/pktgen/kpktgend_0 echo count 0 >/proc/net/pktgen/dummy0 echo start >/proc/net/pktgen/pgctrl & sleep 1 rmmod veth Fix the above releasing the thread lock around the sleep call. Additionally we must prevent racing with forcefull rmmod - as the thread lock no more protects from them. Instead, acquire a self-reference before waiting for any thread. As a side effect, running rmmod pktgen while some thread is running now fails with "module in use" error, before this patch such command hanged indefinitely. Note: the issue predates the commit reported in the fixes tag, but this fix can't be applied before the mentioned commit. v1 -> v2: - no need to check for thread existence after flipping the lock, pktgen threads are freed only at net exit time - Fixes: 6146e6a43b35 ("[PKTGEN]: Removes thread_{un,}lock() macros.") Reported-and-tested-by: Matteo Croce Signed-off-by: Paolo Abeni Signed-off-by: David S. Miller Signed-off-by: Ben Hutchings --- net/core/pktgen.c | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -3055,7 +3055,13 @@ static int pktgen_wait_thread_run(struct if_unlock(t); + /* note: 't' will still be around even after the unlock/lock + * cycle because pktgen_thread threads are only cleared at + * net exit + */ + mutex_unlock(&pktgen_thread_lock); msleep_interruptible(100); + mutex_lock(&pktgen_thread_lock); if (signal_pending(current)) goto signal; @@ -3072,6 +3078,10 @@ static int pktgen_wait_all_threads_run(s struct pktgen_thread *t; int sig = 1; + /* prevent from racing with rmmod */ + if (!try_module_get(THIS_MODULE)) + return sig; + mutex_lock(&pktgen_thread_lock); list_for_each_entry(t, &pn->pktgen_threads, th_list) { @@ -3085,6 +3095,7 @@ static int pktgen_wait_all_threads_run(s t->control |= (T_STOP); mutex_unlock(&pktgen_thread_lock); + module_put(THIS_MODULE); return sig; }