Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3299171ybb; Tue, 31 Mar 2020 02:23:28 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsJChoAsLNnH3UNdtLtWgygu575zf0fAivCbZ8FCNzhrMFdmG5toTMIcYzznc+Qx9wwwcAz X-Received: by 2002:a9d:525:: with SMTP id 34mr2364242otw.80.1585646607880; Tue, 31 Mar 2020 02:23:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585646607; cv=none; d=google.com; s=arc-20160816; b=Jv39AEL/lEVBW86BHdj0q/q6EENvg9lnqznfsKHdVN4aXKEnK2sBDhGDEF3q5UhAUr 3smrBcDXhM95ap/ZDqdY+MOS+ev+shdrIuCc2aH6EZ4IjhQNUZq5dlzHgMUdFvswW3oo Zbvq+jK6herw2IePXM8LG4MJWubBSqP2SRNtZ2p9bH1DpxF1VHHjLolXkzqjuk326vxl ipFJHCFXWBs7v3536dKDrlrPdDtWHVsaqn0qUTIYK+F7jH9Xid++/MPxc32ueQBSqqn5 nhd8xLb6MvcmHjgkMTqfY/A9HoQbQ629Z5V3K4WPmXSW07mjTQw2YPiOOjRGpzfhyOrP hqVw== 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=PVCKQxZ4kHiIM0T1H9PIm7lgs3RCMFTTXGN77DfG0Zk=; b=IcSSLPmBQCi4lk3R6yRJ+ISpkIyuX5mWFmTPAjQ2+OvWmE0ePXjpzHdOtBi0Osh49y 6mnsMqTTcOB/F7huZaEEMWk3ds8QJhpNT+lneYd+FK3rwATVPTKSQw7V/O05RiPP7sOH b3eprTZKHB6zk+aeZqomDwb+dsMChPtbA1pNa6JJI6YtF2MxMpBr3XpaC0e10ofU0Nlq 8KfLFP7CLSsnQ783mx82FM21t7aouTxaWyW+4YncVoZvczVnhOkoJWtlIe3xQ8JrT8ai KVBYJx3xU/9tgiypKVDTZrJJY0alpnfsm/Ri7/koZalkqJFxutLDp9kv0ySGnNgGecCI 7tfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AiAEzBYS; 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 g203si6582324oif.38.2020.03.31.02.23.15; Tue, 31 Mar 2020 02:23:27 -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=AiAEzBYS; 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 S1730754AbgCaJWV (ORCPT + 99 others); Tue, 31 Mar 2020 05:22:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:49354 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730815AbgCaJHc (ORCPT ); Tue, 31 Mar 2020 05:07:32 -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 EC0FE20675; Tue, 31 Mar 2020 09:07:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1585645651; bh=WGcRl9tUeiwf5FbfD7zo7anXQ38rjxdvVZUihUxkEjk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AiAEzBYSMyN9cmnmw/hD7DZ2BzSNKyQPCmrz/neHmhnG3nMWadOM2dEwbG+yqGSEe wfgEH/WW/9+zQWtCjCpM3GVPxyUSgU3aZbQzm5IATPyB2jZ3eZwfyW/1fXLVho461J xz45KSPnMeVh7kfg2x93iBTSpUaeYaaVCIQQBf/4= 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 5.5 121/170] genirq: Fix reference leaks on irq affinity notifiers Date: Tue, 31 Mar 2020 10:58:55 +0200 Message-Id: <20200331085436.904980426@linuxfoundation.org> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200331085423.990189598@linuxfoundation.org> References: <20200331085423.990189598@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 @@ -284,7 +284,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); @@ -384,7 +388,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); }