Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3597388pxb; Mon, 24 Jan 2022 13:09:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJwyazAh+yX/Go4HmKgVJiF5zhXVBpjJcxLnWiCi1Mgnb7o/20/x+zwMVJUOi4A65/NkR44j X-Received: by 2002:a17:90a:d584:: with SMTP id v4mr98365pju.139.1643058555497; Mon, 24 Jan 2022 13:09:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643058555; cv=none; d=google.com; s=arc-20160816; b=D1V8QJ2VQYuv1Ap84dMIILlnjZLxEaODhLPVyRoWU+me3I+XEZRVNooW0vbvmnV2dF P5bbhNP1cuaUIDT/00MsF1rDru8dtzq6S5dvXdDgfTc9FV3JdYLHg8lsZxgpmS1uQCbo c4p/wDOIsAtjcxC8Hu6DajYLersuN339ziQnC49Gv2QNExM7RKAluxTBlXP0lxOcXq7m /AW8N9bg20JZLt1qaANtDKMNktxDJC5T4U7PQSTHLZfbyH7u6Qmk/TufWKg8nyHZFq6a FPpn+Jp8X7qoTrbob0aaIhM1eCoZ7wORHZUXZ1EpY0MXBPFLNM8Slv1l1Z63U0GDzF/2 AOHA== 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=C4waS00I7O8nEBETsqSsTfNpAZekKnrW6JV3oj90dKc=; b=isIi4OpFsVE6eh42QeOfv8J78hxhAW7C+37XYL3yNiiGxTBAepb9pmsGbjVS1wZFV1 n63suNowKVb1bbbCKoDN+AOmGkJ7IuDIBSLnh4B/VQon4nnVm/WpfGhTnMzEJ2aZxTcX kKqfGcVYTku2vuK/dVkZMKG/ZTdP6qWLy+IOF1RBv6y/to5VL/PirXpYZNC7MS20T/Xz 5QpJE7vWYE2WjpqbmD4qKWZZibFQZHdNotUB7RNfFJWdplwjnK4DUaKF2oj7gWXhQfOu wMncEI5Kq3tpkXYdIv+Atc48zrL7gAOE34L30LefpAQQWAdb+NqEkPaNXWesfYEj4fU5 /Vcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iWUahNJU; 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 m5si15547036pfk.99.2022.01.24.13.09.03; Mon, 24 Jan 2022 13:09:15 -0800 (PST) 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=iWUahNJU; 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 S1386045AbiAXUe6 (ORCPT + 99 others); Mon, 24 Jan 2022 15:34:58 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:37168 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244247AbiAXUMn (ORCPT ); Mon, 24 Jan 2022 15:12:43 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8B30E6131F; Mon, 24 Jan 2022 20:12:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68848C340E5; Mon, 24 Jan 2022 20:12:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643055163; bh=n1nbNAay+RH+Ky8IQRliasEUD9HvunJnAqUvWTiXY/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iWUahNJUkcXPaYy9W5jQoGXU32zi4fPmYXesepNz29AziG3LdZQaaK299v0/DmE/p twA0RUuHo74Qg0AdIDm9JWK4Am8YhvpgsiduQJg24mTFE9ULm7qVlcUuWyz5pEWIT5 hqdeytSdzj7u/Mg/SxNs5Yqni0MKDpNiIOxDVVpw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jonathan Cameron , Ben Widawsky , Dan Williams Subject: [PATCH 5.15 061/846] cxl/pmem: Fix reference counting for delayed work Date: Mon, 24 Jan 2022 19:32:58 +0100 Message-Id: <20220124184103.076682307@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@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: Dan Williams commit 08b9e0ab8af48895337192e683de44ab1e1b7427 upstream. There is a potential race between queue_work() returning and the queued-work running that could result in put_device() running before get_device(). Introduce the cxl_nvdimm_bridge_state_work() helper that takes the reference unconditionally, but drops it if no new work was queued, to keep the references balanced. Fixes: 8fdcb1704f61 ("cxl/pmem: Add initial infrastructure for pmem support") Cc: Reviewed-by: Jonathan Cameron Reviewed-by: Ben Widawsky Link: https://lore.kernel.org/r/163553734757.2509761.3305231863616785470.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Dan Williams Signed-off-by: Greg Kroah-Hartman --- drivers/cxl/pmem.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) --- a/drivers/cxl/pmem.c +++ b/drivers/cxl/pmem.c @@ -149,14 +149,24 @@ static void cxl_nvb_update_state(struct put_device(&cxl_nvb->dev); } +static void cxl_nvdimm_bridge_state_work(struct cxl_nvdimm_bridge *cxl_nvb) +{ + /* + * Take a reference that the workqueue will drop if new work + * gets queued. + */ + get_device(&cxl_nvb->dev); + if (!queue_work(cxl_pmem_wq, &cxl_nvb->state_work)) + put_device(&cxl_nvb->dev); +} + static void cxl_nvdimm_bridge_remove(struct device *dev) { struct cxl_nvdimm_bridge *cxl_nvb = to_cxl_nvdimm_bridge(dev); if (cxl_nvb->state == CXL_NVB_ONLINE) cxl_nvb->state = CXL_NVB_OFFLINE; - if (queue_work(cxl_pmem_wq, &cxl_nvb->state_work)) - get_device(&cxl_nvb->dev); + cxl_nvdimm_bridge_state_work(cxl_nvb); } static int cxl_nvdimm_bridge_probe(struct device *dev) @@ -177,8 +187,7 @@ static int cxl_nvdimm_bridge_probe(struc } cxl_nvb->state = CXL_NVB_ONLINE; - if (queue_work(cxl_pmem_wq, &cxl_nvb->state_work)) - get_device(&cxl_nvb->dev); + cxl_nvdimm_bridge_state_work(cxl_nvb); return 0; }