From patchwork Mon Apr 29 01:31:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhijian Li (Fujitsu)" X-Patchwork-Id: 13646183 Received: from esa9.hc1455-7.c3s2.iphmx.com (esa9.hc1455-7.c3s2.iphmx.com [139.138.36.223]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FF627F; Mon, 29 Apr 2024 01:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.138.36.223 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714354397; cv=none; b=ok/goOB6IevYrDqfB3Rkv9wKTeC6Hdw4r79J/CR+I4qyOQrEl0GdEGCZOVA6R2P/QF/kBkvGL10n+8lB89uWn41wXFmj3Mz0kXR0Ymv1R/uluKksuDhl9/MA2WomwieHPXHI5A1kPS8YPiKO/vx0cNNnY9i3UlMIvCSBRwnnW+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714354397; c=relaxed/simple; bh=xYYPaO+3oD5S6eHCxslEejj4KvT9R5R/OeS0jsKc+28=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=nHKJy4Dh8o3joaA+TnbRM6tcYaeq1G9J0/49D0aNn/Y6J5ktKseohdmjcCm4uPMA/YN8UjkgsMzdw/XXE2QgpnXBMsDUxlb3eX7UzFd0EnLK0hwO/OUTXv6M9qPCuC0+Nysgr51B+IBZSyu4c/vUZDoo85kv+992P7clpCS9zLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fujitsu.com; spf=pass smtp.mailfrom=fujitsu.com; dkim=pass (2048-bit key) header.d=fujitsu.com header.i=@fujitsu.com header.b=o4xCp3nd; arc=none smtp.client-ip=139.138.36.223 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fujitsu.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fujitsu.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fujitsu.com header.i=@fujitsu.com header.b="o4xCp3nd" DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=fujitsu.com; i=@fujitsu.com; q=dns/txt; s=fj2; t=1714354394; x=1745890394; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=xYYPaO+3oD5S6eHCxslEejj4KvT9R5R/OeS0jsKc+28=; b=o4xCp3ndGn2LeG0UeENwu8HQ3SzwHHSVfKb3a8Vxfo6AErEiMoNdvdoE XcJRsWd3kFkDkrDlo+9ZoU8FQkKJH1X/aSlneQ/i6d/Pex4PAXtral4gk 2h5Euem0q1ccRpUAEfB4fn7K6ydLZ/bMD1k1JdWnKIv/FJiyPeDfFzyVq FgRpwjSdAZ5IC6/WcsRTsVLkcCha7B/PASDuy6vPTRIr9J8YfVVD90dms a4mjieim5KExWzlSqJf94DZ4vO3mJm3fmPd+uh84Hq8f1VDf3c+yNveGK E2vlc9vS78MDnjjYuQXOODIXd8fZ3YhzEUrxyf27ce2qQkVaPOiPed1Xt w==; X-IronPort-AV: E=McAfee;i="6600,9927,11057"; a="145456651" X-IronPort-AV: E=Sophos;i="6.07,238,1708354800"; d="scan'208";a="145456651" Received: from unknown (HELO yto-r1.gw.nic.fujitsu.com) ([218.44.52.217]) by esa9.hc1455-7.c3s2.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2024 10:32:02 +0900 Received: from yto-m4.gw.nic.fujitsu.com (yto-nat-yto-m4.gw.nic.fujitsu.com [192.168.83.67]) by yto-r1.gw.nic.fujitsu.com (Postfix) with ESMTP id 38684D6F63; Mon, 29 Apr 2024 10:32:00 +0900 (JST) Received: from kws-ab3.gw.nic.fujitsu.com (kws-ab3.gw.nic.fujitsu.com [192.51.206.21]) by yto-m4.gw.nic.fujitsu.com (Postfix) with ESMTP id 752776415F; Mon, 29 Apr 2024 10:31:59 +0900 (JST) Received: from edo.cn.fujitsu.com (edo.cn.fujitsu.com [10.167.33.5]) by kws-ab3.gw.nic.fujitsu.com (Postfix) with ESMTP id 03FC32008FF88; Mon, 29 Apr 2024 10:31:59 +0900 (JST) Received: from localhost.localdomain (unknown [10.167.226.45]) by edo.cn.fujitsu.com (Postfix) with ESMTP id 250CF1A0002; Mon, 29 Apr 2024 09:31:58 +0800 (CST) From: Li Zhijian To: dave@stgolabs.net, jonathan.cameron@huawei.com, dave.jiang@intel.com, alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, dan.j.williams@intel.com, linux-cxl@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Li Zhijian Subject: [PATCH 1/2] cxl/region: Fix potential invalid pointer dereference Date: Mon, 29 Apr 2024 09:31:53 +0800 Message-Id: <20240429013154.368118-1-lizhijian@fujitsu.com> X-Mailer: git-send-email 2.31.1 Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-TM-AS-Product-Ver: IMSS-9.1.0.1417-9.0.0.1002-28350.004 X-TM-AS-User-Approved-Sender: Yes X-TMASE-Version: IMSS-9.1.0.1417-9.0.1002-28350.004 X-TMASE-Result: 10--1.576700-10.000000 X-TMASE-MatchedRID: 3VliZ9l/ZIYx4g+7LKrJbIL5ja7E+OhyF4r8H5YrEqzAuQ0xDMaXkH4q tYI9sRE/1rH4Rg0AOT3cD62qv4l2/hBxp8+zDVac8Jb881FGn9l9LQinZ4QefCP/VFuTOXUTae6 hIZpj4MuOhzOa6g8KrTBgI4CUkwyDjkYeOjePVdvqjr57M0aj7YHbQeJzNVtSrjbGgk2bYvq/I2 a5OjosMHYCPDeb9md5Ta0JRSB0l2IiWX6BBdcrqBXBt/mUREyAj/ZFF9Wfm7hNy7ppG0IjcFQqk 0j7vLVUewMSBDreIdk= X-TMASE-SNAP-Result: 1.821001.0001-0-1-22:0,33:0,34:0-0 construct_region() could return a PTR_ERR() which cannot be derefernced. Moving the dereference behind the error checking to make sure the pointer is valid. Fixes: a32320b71f08 ("cxl/region: Add region autodiscovery") Signed-off-by: Li Zhijian Reviewed-by: Ira Weiny --- drivers/cxl/core/region.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 8535718a20f0..3c80aa263a65 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -3086,10 +3086,9 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled) mutex_lock(&cxlrd->range_lock); region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa, match_region_by_range); - if (!region_dev) { + if (!region_dev) cxlr = construct_region(cxlrd, cxled); - region_dev = &cxlr->dev; - } else + else cxlr = to_cxl_region(region_dev); mutex_unlock(&cxlrd->range_lock); @@ -3097,6 +3096,9 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled) if (rc) goto out; + if (!region_dev) + region_dev = &cxlr->dev; + attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE); down_read(&cxl_region_rwsem); From patchwork Mon Apr 29 01:31:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhijian Li (Fujitsu)" X-Patchwork-Id: 13646184 Received: from esa7.hc1455-7.c3s2.iphmx.com (esa7.hc1455-7.c3s2.iphmx.com [139.138.61.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 428EC39B; Mon, 29 Apr 2024 01:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.138.61.252 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714354398; cv=none; b=Zw/ixUFSRTA11uU1UVXuVFB6Asa3b/J/mJvMfySFFQ3ZeYIH1/Z6DyqOENQnHTAtUHvRXfb0aTH71op17zc7lfg+w1f+nHfaXEc6rPzh9Lxnk5OYSxyPHhPTItfCVy+YhwdM2PClMuyeI7Pwgiozcf8chMo7xRo4pbDnYyzxsGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714354398; c=relaxed/simple; bh=9C6faDAQEmad8o7+66WBO/zaYHnr/MM9Us0C56pm7/Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N+wYojX+hhItHM2dWOzB/xLbo+F2cYPign0FIwVslCXUPraNFSIUzRWHCYOVCs3Caa4iTmmsMIOqZvpIPfGspSjB8xFBO/IuLD7nbxua79D+rIw4qYr5K+ZZBXb+WJPQENmnPUTr8x576/5hZuFS889f46YPB0u0ubtv088XH70= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fujitsu.com; spf=pass smtp.mailfrom=fujitsu.com; dkim=pass (2048-bit key) header.d=fujitsu.com header.i=@fujitsu.com header.b=QPVjxbt5; arc=none smtp.client-ip=139.138.61.252 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fujitsu.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fujitsu.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fujitsu.com header.i=@fujitsu.com header.b="QPVjxbt5" DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=fujitsu.com; i=@fujitsu.com; q=dns/txt; s=fj2; t=1714354395; x=1745890395; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9C6faDAQEmad8o7+66WBO/zaYHnr/MM9Us0C56pm7/Q=; b=QPVjxbt5mlda/cM9bIJndu+d7vckclgKEcEy4p8FN6/Haf4823WGz4Re 1Y3wolz+P6efwj3SYJlL8O+1VYcKhBobBwHOm7a2+2z3YHsB5aNHxhFly q/cB7ioBgfI2jxtnR0w/ZOJa+iDkdOUmWyLcW450RcGvX2MTWYWVxVmme 3qiZ2KwqPmP+uI1amcpwO6xGYDjbA91vT+EgJcNiOP6O0YB231ExserZ2 QmrKoa3MUqD2mv4E781aWjnEECrQ2NDJsYMN3P34/uSzIhMahzcBqm3v1 EZgBzkp66bVpK1nP+OA5sRrPWLlqDjMICv9BTkxo8ii1ovei8qFZ7rw0L A==; X-IronPort-AV: E=McAfee;i="6600,9927,11057"; a="135810658" X-IronPort-AV: E=Sophos;i="6.07,238,1708354800"; d="scan'208";a="135810658" Received: from unknown (HELO yto-r3.gw.nic.fujitsu.com) ([218.44.52.219]) by esa7.hc1455-7.c3s2.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2024 10:32:02 +0900 Received: from yto-m1.gw.nic.fujitsu.com (yto-nat-yto-m1.gw.nic.fujitsu.com [192.168.83.64]) by yto-r3.gw.nic.fujitsu.com (Postfix) with ESMTP id A302AC2AC3; Mon, 29 Apr 2024 10:32:00 +0900 (JST) Received: from kws-ab4.gw.nic.fujitsu.com (kws-ab4.gw.nic.fujitsu.com [192.51.206.22]) by yto-m1.gw.nic.fujitsu.com (Postfix) with ESMTP id E0C0712F6E2; Mon, 29 Apr 2024 10:31:59 +0900 (JST) Received: from edo.cn.fujitsu.com (edo.cn.fujitsu.com [10.167.33.5]) by kws-ab4.gw.nic.fujitsu.com (Postfix) with ESMTP id 66196E206E; Mon, 29 Apr 2024 10:31:59 +0900 (JST) Received: from localhost.localdomain (unknown [10.167.226.45]) by edo.cn.fujitsu.com (Postfix) with ESMTP id CA99C1A000C; Mon, 29 Apr 2024 09:31:58 +0800 (CST) From: Li Zhijian To: dave@stgolabs.net, jonathan.cameron@huawei.com, dave.jiang@intel.com, alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, dan.j.williams@intel.com, linux-cxl@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Li Zhijian Subject: [PATCH 2/2] cxl/region: Fix missing put_device(region_dev) Date: Mon, 29 Apr 2024 09:31:54 +0800 Message-Id: <20240429013154.368118-2-lizhijian@fujitsu.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240429013154.368118-1-lizhijian@fujitsu.com> References: <20240429013154.368118-1-lizhijian@fujitsu.com> Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-TM-AS-Product-Ver: IMSS-9.1.0.1417-9.0.0.1002-28350.004 X-TM-AS-User-Approved-Sender: Yes X-TMASE-Version: IMSS-9.1.0.1417-9.0.1002-28350.004 X-TMASE-Result: 10-1.444300-10.000000 X-TMASE-MatchedRID: MqppGi75AUU8c+bQ4YQ9BhmCYUYerLHrTFQnI+epPIY0DNPwjqLncGsW ZV9YMDBb9GF0M6TbxPxoVBGpf4JXIy/7QU2czuUNA9lly13c/gF13MofE6YQy5soi2XrUn/J8m+ hzBStantdY+ZoWiLImydET58jp62SPXgxlenIvYLdnUkTfH1zvcYAUrvpAu24bUT0WFPJAlmhPe a+lnFpXh4wA+Ks4bAaZnUnCHe3cUAcpzLq5IeMIPVxvlzS0OquwGC8e6520fKw0PJt06oJaHpaQ l5xviY7wxgWdRvK9Un9g+oMf9KM6Q== X-TMASE-SNAP-Result: 1.821001.0001-0-1-22:0,33:0,34:0-0 > mutex_lock(&cxlrd->range_lock); > region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa, > match_region_by_range); > if (!region_dev) > cxlr = construct_region(cxlrd, cxled); > else > cxlr = to_cxl_region(region_dev); > mutex_unlock(&cxlrd->range_lock); > > rc = PTR_ERR_OR_ZERO(cxlr); > if (rc) > goto out; > > if (!region_dev) > region_dev = &cxlr->dev; When to_cxl_region(region_dev) fails, put_device(region_dev) should be called to decrease the reference count added by device_find_child(). Simply put_device(region_dev) if region_dev is valid in the error path. Fixes: a32320b71f08 ("cxl/region: Add region autodiscovery") Signed-off-by: Li Zhijian --- drivers/cxl/core/region.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 3c80aa263a65..75390865382f 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -3117,8 +3117,9 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled) p->res); } - put_device(region_dev); out: + if (region_dev) + put_device(region_dev); put_device(cxlrd_dev); return rc; }