Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2384156ybi; Sun, 9 Jun 2019 10:22:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqxMeLbDq18Z66eYMQagxiA0ciavhTBh1QFAld0lWGIBa0vaa+sBa8vYonb9N6DTXk1tsZog X-Received: by 2002:a63:6c83:: with SMTP id h125mr11781040pgc.86.1560100976454; Sun, 09 Jun 2019 10:22:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560100976; cv=none; d=google.com; s=arc-20160816; b=jyR4zTnNzD1cwHUEzGWWxKucVCxyeWBD3OrjcU7z7UxchDSs7EWjva1nj2D7EfInSk 2UFpVxc+9iA0VuILz0XwHiwsf1JELAc4LJQmuhuHFqL8NWSP0t0kVmloTPkE1Rqra/KT hZ5GHSA8JWan4fDjFQurzAEKfkdMZZ0Girra+zS7kVF4uRvlQYgwieCg4z+l6KTHJEgb QyaMtCNh0muC+ugWPtBXO6XubrD+qsBMDI09FyBKIQgXPEF8f3w7mKgIV73L54NiAgs5 RkdjSWiYArBM/p3hDNokc7dn7Q43VKfemVAeMS6ckT0b1OVSvw8Fgg1ow5jVhYO3GCzx E65A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2HDyRQt7D9RVY2bPasKHpNMgUAPuPlaOE2O+cjVL03E=; b=030yKsV8VM7w+LPJjQubc425mzWRBfBjNwPspMoiRb+Yv6qOL32caVbQxSMzFpTk1r frGZ94pVIu9euG4eQWRfQiZj34LrnNUcSC92f7V72FbvDwJgJTqw8odVP3zNGkG0RRZh V/yko9wCaGq246J8UvIvazv74F8gunEbTEef+nIjz5XFdaxndxx/t7APgoEf+5o7QHpS 4Tjcec/+DRpfyJst5Pye2w8SLUUT1tJNImfPz1iFXn4LJgEhZMOYesGIGFU8H7k3/4uR 8huutmyryfSIYFtecd4lU34KOIEgSl3CZPMOwLWr7Hp7FAZp74gS66mcPU93+QVW4MD1 2phw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b6tlbs39; 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 b142si8005711pfb.245.2019.06.09.10.22.40; Sun, 09 Jun 2019 10:22:56 -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; dkim=pass header.i=@kernel.org header.s=default header.b=b6tlbs39; 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 S1730931AbfFIQrl (ORCPT + 99 others); Sun, 9 Jun 2019 12:47:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:46276 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730881AbfFIQrf (ORCPT ); Sun, 9 Jun 2019 12:47:35 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7A3D42145D; Sun, 9 Jun 2019 16:47:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560098854; bh=9lxHmmF1tLB8UR7q4NKCCLTiVUByphdknOQMJDU8YxQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b6tlbs39f/BDkiB9NPD4Z5GRO5wK59337+l9ZW+m//5TuSKghPbbyTcWrtsapM2PO 25oqDIFI7jZU5yP8AX5KiWBSfz/FzOOpwEhKfcYFIcNCDWBKegL7G0QIdMaKe8k2/Z 9ldmNiBpGdgOCFAucSMzgFXxIvgtKBMvjtvUM1PA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paolo Abeni , "David S. Miller" , Matteo Croce Subject: [PATCH 4.19 13/51] pktgen: do not sleep with the thread lock held. Date: Sun, 9 Jun 2019 18:41:54 +0200 Message-Id: <20190609164127.874060734@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190609164127.123076536@linuxfoundation.org> References: <20190609164127.123076536@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paolo Abeni [ Upstream commit 720f1de4021f09898b8c8443f3b3e995991b6e3a ] 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: Greg Kroah-Hartman --- net/core/pktgen.c | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/net/core/pktgen.c +++ b/net/core/pktgen.c @@ -3065,7 +3065,13 @@ static int pktgen_wait_thread_run(struct { while (thread_is_running(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; @@ -3080,6 +3086,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) { @@ -3093,6 +3103,7 @@ static int pktgen_wait_all_threads_run(s t->control |= (T_STOP); mutex_unlock(&pktgen_thread_lock); + module_put(THIS_MODULE); return sig; }