Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp778700ybb; Wed, 1 Apr 2020 09:27:16 -0700 (PDT) X-Google-Smtp-Source: ADFU+vttslHJKKCPldw+5qD0tOFDe9hEC6Om7iVXSVGKXAhZzdAiacT8XUHm1w6iILcIvzjgvbn2 X-Received: by 2002:a9d:a22:: with SMTP id 31mr16833985otg.349.1585758436341; Wed, 01 Apr 2020 09:27:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585758436; cv=none; d=google.com; s=arc-20160816; b=tzrZcaVjNuKE+Yh8tcMLHZKxfosuXYwWEoSZjC/oRnWzuL86fZz6+pJZkh8S6F3fXy JfiKFXv1VIh2/lFp+fo30l4Y76VS9bGBQb706T7CTKV3DVFDjAqLJSL0eud1kbe4QdrH dAYCEWLVuTLzQ4cZIg6kzl5KqzFZNcXCSeLlcEUAckBPacrYCa9k6ZoIhpDtKQ0oZP6g LglyWXmO6AOiJ83FfHIqxEETtqzJXQ6EOF7IXHGDycWX+iXDWfoddTJ0synFLXAVoHdl 7V/IxiR0IDgTYuYo2cNTOI9y2G8XAmmWp9sYxH1ABXRpVjwZTn0NGZrnmPdBTL7IArzd v9QA== 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=CDhsmJ5BghTrtTMhNWhj3jrAIEWbDlldAkc+78V9y84=; b=y+gFIYy3yPyLbcOHRA6qASEcZcFpA4c1k1JOQYQOLsRbh2M202UZAOW211VGR1BgnB ww050IuKPAEQpDL4G0ezKyO5ZpkFQVNrJl2zWB7/L/3upf7dFQxzsA68FL74hwB6QXgA pIyK8N+BlmoZKmOyMpm7EDFWorjBZD9V0mgtFrZ5ugIvrO96EAH0x03t9I3ITYtFE3Ls 4B9jEwoXHyK3VGX/1QsryTr0kjzBV2aJPMerXcEfnpQKprWg0iNLhqhkuHvg7suuA6qM jsLCfLcobavXAkFWnO0LteufBhWG6/9qq372nyWWX2NqAFc3/WUyyGAzI9jdz8lkLMIf nHcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=deClLUB8; 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 q197si1054129oic.262.2020.04.01.09.27.03; Wed, 01 Apr 2020 09:27:16 -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=deClLUB8; 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 S2387861AbgDAQZ7 (ORCPT + 99 others); Wed, 1 Apr 2020 12:25:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:50294 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387866AbgDAQZ4 (ORCPT ); Wed, 1 Apr 2020 12:25:56 -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 5946A20857; Wed, 1 Apr 2020 16:25:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1585758354; bh=3sWAja9yYmsMF6WwMs3QtzQdolyFgTh6jjQZPUjr8Io=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=deClLUB87Lhj8TYp58L3iHDkEyCdr1chwuMsMixeCnvkUeGjnJZlILDs/GP9rxeJq nB3HjHLRd2veAF7vEx8ElOJkwWajZdLDvytLZooZMk4k2XgxleqMy8Ate6vVeF5UiK hpi36p36w0DcqOnwbTJIH8p1MuFo6R5FtH0OpkAQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Edward Cree , Thomas Gleixner , Ben Hutchings Subject: [PATCH 4.19 064/116] genirq: Fix reference leaks on irq affinity notifiers Date: Wed, 1 Apr 2020 18:17:20 +0200 Message-Id: <20200401161551.657808364@linuxfoundation.org> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200401161542.669484650@linuxfoundation.org> References: <20200401161542.669484650@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: Edward Cree commit df81dfcfd6991d547653d46c051bac195cd182c1 upstream. The handling of notify->work did not properly maintain notify->kref in two cases: 1) where the work was already scheduled, another irq_set_affinity_locked() would get the ref and (no-op-ly) schedule the work. Thus when irq_affinity_notify() ran, it would drop the original ref but not the additional one. 2) when cancelling the (old) work in irq_set_affinity_notifier(), if there was outstanding work a ref had been got for it but was never put. Fix both by checking the return values of the work handling functions (schedule_work() for (1) and cancel_work_sync() for (2)) and put the extra ref if the return value indicates preexisting work. Fixes: cd7eab44e994 ("genirq: Add IRQ affinity notifiers") Fixes: 59c39840f5ab ("genirq: Prevent use-after-free and work list corruption") Signed-off-by: Edward Cree Signed-off-by: Thomas Gleixner Acked-by: Ben Hutchings Link: https://lkml.kernel.org/r/24f5983f-2ab5-e83a-44ee-a45b5f9300f5@solarflare.com Signed-off-by: Greg Kroah-Hartman --- kernel/irq/manage.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) --- a/kernel/irq/manage.c +++ b/kernel/irq/manage.c @@ -283,7 +283,11 @@ int irq_set_affinity_locked(struct irq_d if (desc->affinity_notify) { kref_get(&desc->affinity_notify->kref); - schedule_work(&desc->affinity_notify->work); + if (!schedule_work(&desc->affinity_notify->work)) { + /* Work was already scheduled, drop our extra ref */ + kref_put(&desc->affinity_notify->kref, + desc->affinity_notify->release); + } } irqd_set(data, IRQD_AFFINITY_SET); @@ -383,7 +387,10 @@ irq_set_affinity_notifier(unsigned int i raw_spin_unlock_irqrestore(&desc->lock, flags); if (old_notify) { - cancel_work_sync(&old_notify->work); + if (cancel_work_sync(&old_notify->work)) { + /* Pending work had a ref, put that one too */ + kref_put(&old_notify->kref, old_notify->release); + } kref_put(&old_notify->kref, old_notify->release); }