From patchwork Wed Dec 18 00:01:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912729 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 5211C44360 for ; Wed, 18 Dec 2024 00:02:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480145; cv=none; b=USr31PpstmcnqkUwcdnujSH0tUbBkHN1VJ3LZjK5tvr3Lbh9bzeIOHo/AHsKIjIBznfjpbU94ChxdNCrddtlVsQIXoZLIVqHZJCvM+dOMmVk4KkQWs8PTewfwWSWYdDDjchhiVUJlw2MlGJIkYDug6BTrplnSGGrYozT5GJ5Y18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480145; c=relaxed/simple; bh=e/2xP3qFb869WanTNBYe+qJB5MKHi9usxQIzrOvob5c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sS1ycGGIjkyqRgy//kN5vPkCrJYqzGlLWLdFHU0ksReSfG/6R9jkEBOm10jq2ax/J7wRZSATxlRy0CertYZAPce+5R/uEYf4JJAzwdu2ijOhEkcDj9XRJZoiaIIHiRYgdMI9KzIGEo7gIry6xW82yWdLxnSQsALgGR0K+9kBZnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=j2gHB8N1; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="j2gHB8N1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480143; bh=Eh2nL80zh7HWftk0msMSgPR2NrIlsrV6kCVdoP2/U/c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=j2gHB8N1VE0sxCA9hDQao6S2E87uQsywhx05ScK5a3wtAkwMybSkor4qm9Lfwd47c a70rf330pQ+EPQpXULXgxibd1jfBsxgohiGbtO4pMdKJZKfyiD0+mcBhhK8LXMem8V FHDEDuySGBbOtWh6A62dL4ln6GjouLm+OBaVhIBUjCT5EBRHlEIT41KWZsz6bk81td NMdm96c7/Lv82Ceky8Ijd/WMHS4c2NrCG5NvVy7vW4XCNT/XzEJ03D1k5xd5ARiU20 H1YaCvGuYOiuIii4uRwBvaIFij6eqQQJDloXqhp4Jo//Nj8YgrpWrgRIr7ZMWvAE6E 4WUCwKVLPwfIQ== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id 772CB96018F; Wed, 18 Dec 2024 00:02:16 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:31 +0800 Subject: [PATCH v4 1/8] driver core: class: Fix wild pointer dereferences in API class_dev_iter_next() Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-1-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: KgSYcnwTObkWkgn5Hgdu4e-1op-hHu0H X-Proofpoint-GUID: KgSYcnwTObkWkgn5Hgdu4e-1op-hHu0H X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu There are a potential wild pointer dereferences issue regarding APIs class_dev_iter_(init|next|exit)(), as explained by below typical usage: // All members of @iter are wild pointers. struct class_dev_iter iter; // class_dev_iter_init(@iter, @class, ...) checks parameter @class for // potential class_to_subsys() error, and it returns void type and does // not initialize its output parameter @iter, so caller can not detect // the error and continues to invoke class_dev_iter_next(@iter) even if // @iter still contains wild pointers. class_dev_iter_init(&iter, ...); // Dereference these wild pointers in @iter here once suffer the error. while (dev = class_dev_iter_next(&iter)) { ... }; // Also dereference these wild pointers here. class_dev_iter_exit(&iter); Actually, all callers of these APIs have such usage pattern in kernel tree. Fix by: - Initialize output parameter @iter by memset() in class_dev_iter_init() and give callers prompt by pr_crit() for the error. - Check if @iter is valid in class_dev_iter_next(). Fixes: 7b884b7f24b4 ("driver core: class.c: convert to only use class_to_subsys") Signed-off-by: Zijun Hu --- Alternative fix solutions ever thought about: 1) Use BUG_ON(!sp) instead of error return in class_dev_iter_init(). 2) Change class_dev_iter_init()'s type to int, lots of jobs to do. This issue is APIs themself issues, and regardless of how various API users use them, and silent wild pointer dereferences are not what API users expect for the error absolutely. --- drivers/base/class.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/base/class.c b/drivers/base/class.c index 582b5a02a5c410113326601fe00eb6d7231f988f..d57f277978dc9033fba3484b4620bcf884a4029f 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -323,8 +323,12 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class, struct subsys_private *sp = class_to_subsys(class); struct klist_node *start_knode = NULL; - if (!sp) + memset(iter, 0, sizeof(*iter)); + if (!sp) { + pr_crit("%s: class %p was not registered yet\n", + __func__, class); return; + } if (start) start_knode = &start->p->knode_class; @@ -351,6 +355,9 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter) struct klist_node *knode; struct device *dev; + if (!iter->sp) + return NULL; + while (1) { knode = klist_next(&iter->ki); if (!knode) From patchwork Wed Dec 18 00:01:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912730 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 354F63DBB6 for ; Wed, 18 Dec 2024 00:02:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480153; cv=none; b=cnN0vD1hHk1PcfjAHyQESc4hl+3J+Eo/A3/jrCLDfgNZEfwA2N8emRKoYN+iKWhcgZxeGPkDlp9jUFuf5hPXYn4YqmsgoSe258XLGvT/hAfD6kOMZC9GXRJe6K71tqMYOPCQAjxMIkoTciRGWBRAHr5BeSxxiAkXtUX9OpzyqM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480153; c=relaxed/simple; bh=v2UDBGjNX/TBVEEyZ+l09JjXrifkZ0al8qUGlpvaJ5I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LtpyJhWuUiOdt4BTo0DBHPkt9bTKhsCq+K2hk2w3GEqbvAj1pIv6hojiKEsaazz9ZihYUrpKN8s3QNz8dSSWwadu9mx/rMyOyT21IfX1e3AXGbSP0zrYbQhN2XDS0yxPAaQMo/HWftbUk5VXWkChkJx2qgesWTYpVC1F3xxyNrY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=pY3ZflW9; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="pY3ZflW9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480151; bh=/xL/Yze+0QjxeSoIHE+CFVDh+nPc165CBlyFJrejgkI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=pY3ZflW9atnEjrr9EIzDbuL9ZSgDGxe828AAJGy9cs9HGiCB5xKrQcpBGka6yldc8 68k2kzxpFFozC3+D9tWJ2MUVixANz8MK1wRR8HUlmympWp5MGsvh7VU5zUJhvwlDmM yc6DEZkySYFqhjGDGBLq/HDPKEdODAuzaBKQ123AvVa8+J0atiRLLOIZ4BsxCRCFAi 2rXntuKzFXIwFJmA0KTN/l5MQKmZEm4R2zFKQqsGjgf4FC32YPMFEUJIkXZopIyWQy ex9xev3bIrtW0SHcFQSfSAxMzcGSD198TNg2yqx1DmFqHQQOdkWICpqiXUd9l7LfFm pDOoCYJ7mKF0A== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id CBD979602BB; Wed, 18 Dec 2024 00:02:24 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:32 +0800 Subject: [PATCH v4 2/8] blk-cgroup: Fix class @block_class's subsystem refcount leakage Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-2-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu , =?utf-8?q?M?= =?utf-8?q?ichal_Koutn=C3=BD?= , stable@vger.kernel.org X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: OO8FJ9GKUHIVtIi_MadGQBRzJ2KI06yY X-Proofpoint-GUID: OO8FJ9GKUHIVtIi_MadGQBRzJ2KI06yY X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu blkcg_fill_root_iostats() iterates over @block_class's devices by class_dev_iter_(init|next)(), but does not end iterating with class_dev_iter_exit(), so causes the class's subsystem refcount leakage. Fix by ending the iterating with class_dev_iter_exit(). Fixes: ef45fe470e1e ("blk-cgroup: show global disk stats in root cgroup io.stat") Reviewed-by: Michal Koutný Cc: Greg Kroah-Hartman Cc: stable@vger.kernel.org Signed-off-by: Zijun Hu --- block/blk-cgroup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index e68c725cf8d975f53703ecf6e6c50594076204c8..fb9858efdfe9443704cb9a239def0e08addf2518 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1138,6 +1138,7 @@ static void blkcg_fill_root_iostats(void) blkg_iostat_set(&blkg->iostat.cur, &tmp); u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags); } + class_dev_iter_exit(&iter); } static void blkcg_print_one_stat(struct blkcg_gq *blkg, struct seq_file *s) From patchwork Wed Dec 18 00:01:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912731 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 6910F14B06A for ; Wed, 18 Dec 2024 00:02:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480160; cv=none; b=bBTIf0fS5XlB/BI8u7tpAW+DA5Z3hbtUjrMNgTknHDnI9H/cyDHVjF9iBI0m3x3LLa/tNwgUBsmNiildxyPn+I157enTIKrjKSuM9ktO1dyqGu4DZWkCO4cGX31+7L4pD9OUAp8qurD9IAJEhcSfwjL5B4ilNQ65KuCafAKkRhw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480160; c=relaxed/simple; bh=G49zBKYjNI4Lr2O92y6LMZppduNJ4fYsvssXA+rD8DU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jL6W5YORT+MB89oOG4j+ixOI7uxrR/qlCqXqpG2VypQ8T4JP5iu8fQsR8PvngQ/qVf3brFrWrayHWbM+sLjYjHqzr9adFj3gci2380cIcLf/W24OamsBpKf6iDWKUE3tU0PE1r55zZKR1Q0v27kFSTr246O2ZIKGAi9k47yxx9A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=UUS7809b; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="UUS7809b" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480159; bh=cOPHgek+7txVXNgAZKPNrM641zXSkIkTh3Riom+d1rc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=UUS7809bi9IRKOia8eeN3oT/j6sb15b2Vi7Enta+6kp4t2V/5dZxUjS1kqTvSSJgf 2cZjz9wXoOL09iuJjZjp8+g6jF/TabqAOvQRnHBTR0WsB9aheiJh5E0VsDVyx6hgE8 XxrvNZa/NHnD+eQX5OAY3jJtr1P9HM5jalMIXyhz6f0jykggvUJ+bfBtlYfHtlvCCN uBRVoWCVVF9O2U0U3QpRs+t5kYhVXeEPqaY6nPQqiNGTl8s0nzNa0VaswDwghGp0ZQ hPyygVZqVJtBkLJ7YnyoXWAVolS6MtxRN3ORxfvOowRm4o1mctO6IEqYUEOdFNXUqw kJF12GbrrvZFA== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id 3EE7996020B; Wed, 18 Dec 2024 00:02:31 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:33 +0800 Subject: [PATCH v4 3/8] driver core: Move true expression out of if condition in 3 device finding APIs Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-3-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu , Fan Ni , Jonathan Cameron X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: CDH0732C4YUMOuSX6q14K8VHFeZp09I8 X-Proofpoint-GUID: CDH0732C4YUMOuSX6q14K8VHFeZp09I8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu For bus_find_device(), driver_find_device(), and device_find_child(), all of their function body have pattern below: { struct klist_iter i; struct device *dev; ... while ((dev = next_device(&i))) if (match(dev, data) && get_device(dev)) break; ... } The expression 'get_device(dev)' in the if condition always returns true since @dev != NULL. Move the expression to if body to make logic of these APIs more clearer. Reviewed-by: Fan Ni Reviewed-by: Jonathan Cameron Signed-off-by: Zijun Hu --- drivers/base/bus.c | 7 +++++-- drivers/base/core.c | 7 +++++-- drivers/base/driver.c | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 657c93c38b0dc2a2247e5f482fadd3a9376a58e8..73a56f376d3a05962ce0931a2fe8b4d8839157f2 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -402,9 +402,12 @@ struct device *bus_find_device(const struct bus_type *bus, klist_iter_init_node(&sp->klist_devices, &i, (start ? &start->p->knode_bus : NULL)); - while ((dev = next_device(&i))) - if (match(dev, data) && get_device(dev)) + while ((dev = next_device(&i))) { + if (match(dev, data)) { + get_device(dev); break; + } + } klist_iter_exit(&i); subsys_put(sp); return dev; diff --git a/drivers/base/core.c b/drivers/base/core.c index 8bdbc9e657e832a063542391426f570ccb5c18b9..69bb6bf4bd12395226ee3c99e2f63d15c7e342a5 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -4089,9 +4089,12 @@ struct device *device_find_child(struct device *parent, const void *data, return NULL; klist_iter_init(&parent->p->klist_children, &i); - while ((child = next_device(&i))) - if (match(child, data) && get_device(child)) + while ((child = next_device(&i))) { + if (match(child, data)) { + get_device(child); break; + } + } klist_iter_exit(&i); return child; } diff --git a/drivers/base/driver.c b/drivers/base/driver.c index b4eb5b89c4ee7bc35458fc75730b16a6d1e804d3..6f033a741aa7ce6138d1c61e49e72b2a3eb85e06 100644 --- a/drivers/base/driver.c +++ b/drivers/base/driver.c @@ -160,9 +160,12 @@ struct device *driver_find_device(const struct device_driver *drv, klist_iter_init_node(&drv->p->klist_devices, &i, (start ? &start->p->knode_driver : NULL)); - while ((dev = next_device(&i))) - if (match(dev, data) && get_device(dev)) + while ((dev = next_device(&i))) { + if (match(dev, data)) { + get_device(dev); break; + } + } klist_iter_exit(&i); return dev; } From patchwork Wed Dec 18 00:01:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912732 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 CC883157A5A for ; Wed, 18 Dec 2024 00:02:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480168; cv=none; b=ePJR7B2U2yLt+zhnMSwBiLq9BjgDfbODs9ik7y7TR+VxYUz90Mdx90BqHniSez6CzzK1iYgqvcDeWKpMa3AhM6AqVSGHM4ZjW8nOz2MOo1V2ClRjIbAihsnoDbQ8R86vNzYF0jo6CeEN3lHTwNPoNxDyHPoUzVzT3i+qL/zT19w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480168; c=relaxed/simple; bh=r1myTHTt6RqQBq+qIcrBHUm5l/c3S1IeRV2Ch3Esx2E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uFHd9JSbG03Xn7vAhZN9mNS2YnkgxDk5F9sLyAsI6t3nKq9/L1V3cvZUbpkZ9+sm7wLBqp1DEZdNB8UQMcgCL/bRM+AUAIfqN+TwDeWTI6obvPc9hqmmnL6FKp5QmTLOPuGCUxhpxDpPgqKjkiDFIqJmfs29W7jafGmGNzeHHHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=xy/6u9uL; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="xy/6u9uL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480166; bh=bpdPHGVvsNjPbtVeiARlh6UhJFtwCfTalqRar5MzOl4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=xy/6u9uLKuAxe1VdLiHgYm4XXVsoBQt2J//8hxcoCn7v/oJlO7gY2NU7kEbIejMkg F0E0duVeZhFvdyyXtOGTKqkeXLJgSODBcsWEUypnS9Ce5ZBKFpK+7VFIy3NSOIAizn 9ml2Gwx/PDNablLOtG7drfn7Ow0Qika7dTsI4qslvn/UQ9VuBkBL47cGnkfnZ2kbJ9 LLBZ4+auHNB2CgLP3Vy33XBtps1DG0JSfCitgh42bGbKKNxZbstkszos+ABoB0+vyX gy8eeOADm/Or3SoCSSaAxXfj7kdDK5o8Ju6ZAL7ZnpxNdZH5sTGN3cDnlxBZpIjJ55 2XEicBXj77vGQ== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id ADBA0960497; Wed, 18 Dec 2024 00:02:39 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:34 +0800 Subject: [PATCH v4 4/8] driver core: Rename declaration parameter name for API device_find_child() cluster Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-4-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu , Fan Ni , Jonathan Cameron X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: YWGCb2xGChNivds01FUrxipNy657CFO1 X-Proofpoint-GUID: YWGCb2xGChNivds01FUrxipNy657CFO1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu For APIs: device_find_child() device_for_each_child() device_for_each_child_reverse() Their declaration has parameter name 'dev', but their defination changes the name to 'parent'. Rename declaration name to defination 'parent' to make both have the same name. Reviewed-by: Fan Ni Reviewed-by: Jonathan Cameron Signed-off-by: Zijun Hu --- include/linux/device.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/linux/device.h b/include/linux/device.h index 0e0bc9bfe0d15a8734bf3d34106300f4df6b5364..a9d928398895b062094b94f2c188cbe9951d7ac1 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1074,14 +1074,14 @@ void device_del(struct device *dev); DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T)) -int device_for_each_child(struct device *dev, void *data, +int device_for_each_child(struct device *parent, void *data, int (*fn)(struct device *dev, void *data)); -int device_for_each_child_reverse(struct device *dev, void *data, +int device_for_each_child_reverse(struct device *parent, void *data, int (*fn)(struct device *dev, void *data)); int device_for_each_child_reverse_from(struct device *parent, struct device *from, const void *data, int (*fn)(struct device *, const void *)); -struct device *device_find_child(struct device *dev, const void *data, +struct device *device_find_child(struct device *parent, const void *data, device_match_t match); struct device *device_find_child_by_name(struct device *parent, const char *name); From patchwork Wed Dec 18 00:01:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912733 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 8D2FA1607B7 for ; Wed, 18 Dec 2024 00:02:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480175; cv=none; b=kwRcaLLLSjT/S930wINDNCxL0scwZcD3a1Lo4UbM1v22SWszRS5XECFj3DOrLkNtIunYeSug1Zr+osaLZJqEG+LRRuoCP3kG3qPRSw72h/Kay9EGWa7yeZwgF6wJIfVg6ZaQxIgZUQMnD+FIC6qsKEFgZ8YW2rboUOecMAgBkek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480175; c=relaxed/simple; bh=9Y0rzZnBTx0M5MQSIP1RwELLmzfYczu0ijWLu1jrY5g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kT4An3NvqvreO+vOxUPqizLqO0jM4pNRtRa2vpv7i5sXEiKl7CUzX2Vn8LXbiElkimLzlD7geh1PFe4cD6YbisP3SXrwnv8npAewVkadGbJkwoX4ygDA1AVFZ+MCuy9LklTEN/XWx4zVdwS0kvQ1tcNgNzJQem7xtIfOzO/dmjk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=kmACy36Q; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="kmACy36Q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480173; bh=VKx9/BEbeYqmaBNZCu91NOLWQRLnn6ACylShMKN74W8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=kmACy36QOK4/r7uTgptRo1D+Y14AoMnQTPcWZmKDAMSIw0k/X1FxISAcG5YX4kiah HDl08eKYcQ1QDII7Lks43T0XnzIU6dwTzXGk2tLbKdFstL70TtXdh47IpAvdQwH5ZU lnpltLpyJIJHyrQLlWF4k3ZxN8M1Xp51JNWWYUqYxm9w3ql357ZjB6J6ZeeUEqNX81 sD/qtdRje1YrPHMERIYNqEwvnH1kIi0BL/ZuO7JpjBWYT1pN1VCCSlF/ph73z/Vyn6 3MpiNgdF0oap6Z4XBbtE92c++FbADkfjQ5tzOg1CcYtNdVGbGCfOWIzznJZil5U5Gv 9oA8GFtfQOY2A== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id EA902960384; Wed, 18 Dec 2024 00:02:46 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:35 +0800 Subject: [PATCH v4 5/8] driver core: Correct parameter check for API device_for_each_child_reverse_from() Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-5-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: KokUCULd9Vqgo7vGe7-HrA4X93kFzyS0 X-Proofpoint-GUID: KokUCULd9Vqgo7vGe7-HrA4X93kFzyS0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=882 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu device_for_each_child_reverse_from() checks (!parent->p) for its parameter @parent, and that is not consistent with other APIs of its cluster as shown below: device_for_each_child_reverse_from() // check (!parent->p) device_for_each_child_reverse() // check (!parent || !parent->p) device_for_each_child() // same above device_find_child() // same above Correct the API's parameter @parent check by (!parent || !parent->p). Signed-off-by: Zijun Hu --- drivers/base/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 69bb6bf4bd12395226ee3c99e2f63d15c7e342a5..34fb13f914b3db47e6a047fdabf3c9b18ecc08cc 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -4050,7 +4050,7 @@ int device_for_each_child_reverse_from(struct device *parent, struct device *child; int error = 0; - if (!parent->p) + if (!parent || !parent->p) return 0; klist_iter_init_node(&parent->p->klist_children, &i, From patchwork Wed Dec 18 00:01:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912734 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 3903C28EA for ; Wed, 18 Dec 2024 00:03:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480183; cv=none; b=BgQh1iQI+b5H65Y8ObelrJQtKyzq1gkPLi8SImnhIbxII59TWjW1kO5Sh9CPwvyIHv1cDELHI/c/iYPaurDCgelvxMbnX5pL5lDVTz6TU97ZB9jxy8Cy2RKccbpiqnjyQCYGeHgMIWd9aWi0mXh6YYXAJZzYegl6GOGpAonoOVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480183; c=relaxed/simple; bh=DwQV+1u1k+MMC8p20XB/ThrvufR22TR7sB4o/3ThFnI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hupEFxIXtOUTvwXCCAEfZZULEop7QdaCgjdb2FuPKKQH35DN00GA01dW3ML6cvmnca7BIYpelrEbZBzp4y4KCcMkyU1c3AlVClPkwAJMxG6QslWMhCBVt35RfAxMvyHX4L5IE3+4NmCbxNuzBwJAh2bfTkJipppj5LimNyaYWsI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=OR/Y9JsU; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="OR/Y9JsU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480179; bh=hSVqi/9cLAHS96j0Wuyjns8MrivpzIzzYpGb9TI00yE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=OR/Y9JsUHaDQlXISfxeOFaZpQRqD65DCPcmFSigBoE2A1rmUYmj3CqW1tLxZPHNsM 86dMbMhlOsnaDBsJXlx5VOkY/kx7I6fM73N4SPikJWQP9DID61gg5DUh42KsV2PrBv mFlA0954YXj/qxYad4GycDBF3MglAuEs1ax2APDQCeuYK4rssQWFYuDAGR/D4kzI2C sL4oPIDztzsGbqILvfFaqlzLaEE2TjXecWR+Rpy1tLyiqMrtvnkMHt3adX0uDTd+Xc bMChPkk9dd6htpdfpB/zmwG51CEZ6N3pl8ylw/xAPokSZ1ngdCuAr+0woK0l8qRnl6 A+pUT4aIWYJlg== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id 99D059602B1; Wed, 18 Dec 2024 00:02:53 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:36 +0800 Subject: [PATCH v4 6/8] driver core: Correct API device_for_each_child_reverse_from() prototype Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-6-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu , Jonathan Cameron X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: ciFWXYAp8bRX3d_gQtgIfkHBzsHCBVFL X-Proofpoint-GUID: ciFWXYAp8bRX3d_gQtgIfkHBzsHCBVFL X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=971 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu For API device_for_each_child_reverse_from(..., const void *data, int (*fn)(struct device *dev, const void *data)) - Type of @data is const pointer, and means caller's data @*data is not allowed to be modified, but that usually is not proper for such non finding device iterating API. - Types for both @data and @fn are not consistent with all other for_each device iterating APIs device_for_each_child(_reverse)(), bus_for_each_dev() and (driver|class)_for_each_device(). Correct its prototype by removing const from parameter types, then adapt for various existing usages. An dedicated typedef device_iter_t will be introduced as @fn() type for various for_each device interating APIs later. Reviewed-by: Jonathan Cameron Signed-off-by: Zijun Hu --- drivers/base/core.c | 4 ++-- drivers/cxl/core/hdm.c | 2 +- drivers/cxl/core/region.c | 2 +- include/linux/device.h | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 34fb13f914b3db47e6a047fdabf3c9b18ecc08cc..7be9c732bec1b060a477b362f555c3e87c8c7b91 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -4043,8 +4043,8 @@ EXPORT_SYMBOL_GPL(device_for_each_child_reverse); * device_for_each_child_reverse_from(); */ int device_for_each_child_reverse_from(struct device *parent, - struct device *from, const void *data, - int (*fn)(struct device *, const void *)) + struct device *from, void *data, + int (*fn)(struct device *, void *)) { struct klist_iter i; struct device *child; diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c index 28edd5822486851912393f066478252b20abc19d..50e6a45b30ba260c066a0781b9ed3a2f6f3462d7 100644 --- a/drivers/cxl/core/hdm.c +++ b/drivers/cxl/core/hdm.c @@ -703,7 +703,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) return 0; } -static int commit_reap(struct device *dev, const void *data) +static int commit_reap(struct device *dev, void *data) { struct cxl_port *port = to_cxl_port(dev->parent); struct cxl_decoder *cxld; diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index bfecd71040c2f4373645380b4c31327d8b42d095..a4cff4c266e7a7dd6a3feb1513bf14b7d3f9afa2 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -778,7 +778,7 @@ static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos) return rc; } -static int check_commit_order(struct device *dev, const void *data) +static int check_commit_order(struct device *dev, void *data) { struct cxl_decoder *cxld = to_cxl_decoder(dev); diff --git a/include/linux/device.h b/include/linux/device.h index a9d928398895b062094b94f2c188cbe9951d7ac1..025bac08fca7b2513acb1fbcb666be1dc64f03d1 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1079,8 +1079,8 @@ int device_for_each_child(struct device *parent, void *data, int device_for_each_child_reverse(struct device *parent, void *data, int (*fn)(struct device *dev, void *data)); int device_for_each_child_reverse_from(struct device *parent, - struct device *from, const void *data, - int (*fn)(struct device *, const void *)); + struct device *from, void *data, + int (*fn)(struct device *, void *)); struct device *device_find_child(struct device *parent, const void *data, device_match_t match); struct device *device_find_child_by_name(struct device *parent, From patchwork Wed Dec 18 00:01:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912735 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 8086D1990D8 for ; Wed, 18 Dec 2024 00:03:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480189; cv=none; b=iGxpTZ1ZQlarsYz6SyYlSSfl6Ik9S9U5fycZZUZRaR5YwwtoGD8dlLkvuaoGdfsUq5Jq0lbQ81I1FENp6wfU+ZjCTE+ZHnw5FT6YrCSp0CmI4vj0qSX6oYKyvIPuPtB3SVLHxkoRQdMuWHrNu3k/jMYSclQzeiaBtRXzicQ99Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480189; c=relaxed/simple; bh=uLh0iwbx+bHSkqf31NJfzOKrZYjwuqeeVtQOgp32dKo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qmPudQDMtjS/VTa4LlmXF1rudRjxNCQy+MaoJ69FqbSkrU2h1N0p8k4OMOlURgBvaRsO6EEeZuA4yhWzJ10cOraM+EXUhqSv1oEBBN8qqOOyT82OEU3rQdwwV/L7qCKEiC88RZVJF5H/CHqOn/Mnjtuz+vKmP6j5N6Zyhes6Kfg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=YnP7Ay91; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="YnP7Ay91" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480187; bh=D+LGXKD45T7pkrXMj16PMStlutJQBRuIDizc9g023YQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=YnP7Ay91kLubdbRfzxrkuP5cJSyhydYxHZoV1WXRLj2skxdBvSVM57cXFGPO42zrE bZExsU0Xuu8EbkcNkaqw9A9vUwMC1HavVudGrYWBjGy+o/+t0wW4YJoJwwzvGu9nJg 4U0nykDx3XEQbvNTkPsL9UK+7MdFYhXrloFz4od4Ias62Rsl03iNP1rxIpXSEhePW2 tisLhMDBrQIpJ08lwvKvTzjhagGb6c0i9X+QuU8p5EGM1BCcgwWKDasXqs81tujvnx tAe0YDrcMHBNrGMuVqde5/m7JqRWbuVNzZGupN4GzWhnayuBbOojyy55OzHjjYwGJU oZx2xpDSCtLpg== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id 7771D960369; Wed, 18 Dec 2024 00:03:00 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:37 +0800 Subject: [PATCH v4 7/8] driver core: Introduce device_iter_t for device iterating APIs Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-7-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu , Jonathan Cameron X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: op-Uu6fWZ2at7V2gq1P2oHGG5qp6-LJB X-Proofpoint-GUID: op-Uu6fWZ2at7V2gq1P2oHGG5qp6-LJB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu There are several for_each APIs which has parameter with type below: int (*fn)(struct device *dev, void *data) They iterate over various device lists and call @fn() for each device with caller provided data @*data, and they usually need to modify @*data. Give the type an dedicated typedef with advantages shown below: typedef int (*device_iter_t)(struct device *dev, void *data) - Shorter API declarations and definitions - Prevent further for_each APIs from using bad parameter type So introduce device_iter_t and apply it to various existing APIs below: bus_for_each_dev() (class|driver)_for_each_device() device_for_each_child(_reverse|_reverse_from)(). Reviewed-by: Jonathan Cameron Signed-off-by: Zijun Hu --- drivers/base/bus.c | 2 +- drivers/base/class.c | 2 +- drivers/base/core.c | 6 +++--- drivers/base/driver.c | 2 +- include/linux/device.h | 6 +++--- include/linux/device/bus.h | 7 +++++-- include/linux/device/class.h | 4 ++-- include/linux/device/driver.h | 2 +- 8 files changed, 17 insertions(+), 14 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 73a56f376d3a05962ce0931a2fe8b4d8839157f2..6b9e65a42cd2e12046ddabf2d8dfb209d513f7da 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -354,7 +354,7 @@ static struct device *next_device(struct klist_iter *i) * count in the supplied callback. */ int bus_for_each_dev(const struct bus_type *bus, struct device *start, - void *data, int (*fn)(struct device *, void *)) + void *data, device_iter_t fn) { struct subsys_private *sp = bus_to_subsys(bus); struct klist_iter i; diff --git a/drivers/base/class.c b/drivers/base/class.c index d57f277978dc9033fba3484b4620bcf884a4029f..70ee6a7ba5a3746b5a182c6101b5c085b424d01d 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -402,7 +402,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_exit); * code. There's no locking restriction. */ int class_for_each_device(const struct class *class, const struct device *start, - void *data, int (*fn)(struct device *, void *)) + void *data, device_iter_t fn) { struct subsys_private *sp = class_to_subsys(class); struct class_dev_iter iter; diff --git a/drivers/base/core.c b/drivers/base/core.c index 7be9c732bec1b060a477b362f555c3e87c8c7b91..930e43a970952b20cd1c71856bdef889698f51b4 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3980,7 +3980,7 @@ const char *device_get_devnode(const struct device *dev, * other than 0, we break out and return that value. */ int device_for_each_child(struct device *parent, void *data, - int (*fn)(struct device *dev, void *data)) + device_iter_t fn) { struct klist_iter i; struct device *child; @@ -4010,7 +4010,7 @@ EXPORT_SYMBOL_GPL(device_for_each_child); * other than 0, we break out and return that value. */ int device_for_each_child_reverse(struct device *parent, void *data, - int (*fn)(struct device *dev, void *data)) + device_iter_t fn) { struct klist_iter i; struct device *child; @@ -4044,7 +4044,7 @@ EXPORT_SYMBOL_GPL(device_for_each_child_reverse); */ int device_for_each_child_reverse_from(struct device *parent, struct device *from, void *data, - int (*fn)(struct device *, void *)) + device_iter_t fn) { struct klist_iter i; struct device *child; diff --git a/drivers/base/driver.c b/drivers/base/driver.c index 6f033a741aa7ce6138d1c61e49e72b2a3eb85e06..8ab010ddf709a2b173cfd0c18610a122e58a2f4c 100644 --- a/drivers/base/driver.c +++ b/drivers/base/driver.c @@ -115,7 +115,7 @@ EXPORT_SYMBOL_GPL(driver_set_override); * Iterate over the @drv's list of devices calling @fn for each one. */ int driver_for_each_device(struct device_driver *drv, struct device *start, - void *data, int (*fn)(struct device *, void *)) + void *data, device_iter_t fn) { struct klist_iter i; struct device *dev; diff --git a/include/linux/device.h b/include/linux/device.h index 025bac08fca7b2513acb1fbcb666be1dc64f03d1..36d1a1607712f5a6b0668ac02a6cf6b2d0651a2d 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1075,12 +1075,12 @@ void device_del(struct device *dev); DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T)) int device_for_each_child(struct device *parent, void *data, - int (*fn)(struct device *dev, void *data)); + device_iter_t fn); int device_for_each_child_reverse(struct device *parent, void *data, - int (*fn)(struct device *dev, void *data)); + device_iter_t fn); int device_for_each_child_reverse_from(struct device *parent, struct device *from, void *data, - int (*fn)(struct device *, void *)); + device_iter_t fn); struct device *device_find_child(struct device *parent, const void *data, device_match_t match); struct device *device_find_child_by_name(struct device *parent, diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index bc3fd74bb763e6d2d862859bd2ec3f0d443f2d7a..3d3517da41a141aeadc8f219a44fd360cfd931ff 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -139,9 +139,12 @@ int device_match_acpi_dev(struct device *dev, const void *adev); int device_match_acpi_handle(struct device *dev, const void *handle); int device_match_any(struct device *dev, const void *unused); +/* Device iterating function type for various driver core for_each APIs */ +typedef int (*device_iter_t)(struct device *dev, void *data); + /* iterator helpers for buses */ -int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, - int (*fn)(struct device *dev, void *data)); +int bus_for_each_dev(const struct bus_type *bus, struct device *start, + void *data, device_iter_t fn); struct device *bus_find_device(const struct bus_type *bus, struct device *start, const void *data, device_match_t match); /** diff --git a/include/linux/device/class.h b/include/linux/device/class.h index 518c9c83d64bdf85bb5607cea6ea640884ec3460..aa67d473681612f24a4569bf82fe5f91237d5a50 100644 --- a/include/linux/device/class.h +++ b/include/linux/device/class.h @@ -92,8 +92,8 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class, struct device *class_dev_iter_next(struct class_dev_iter *iter); void class_dev_iter_exit(struct class_dev_iter *iter); -int class_for_each_device(const struct class *class, const struct device *start, void *data, - int (*fn)(struct device *dev, void *data)); +int class_for_each_device(const struct class *class, const struct device *start, + void *data, device_iter_t fn); struct device *class_find_device(const struct class *class, const struct device *start, const void *data, device_match_t match); diff --git a/include/linux/device/driver.h b/include/linux/device/driver.h index 5c04b8e3833b995f9fd4d65b8732b3dfce2eba7e..cd8e0f0a634be9ea63ff22e89d66ada3b1a9eaf2 100644 --- a/include/linux/device/driver.h +++ b/include/linux/device/driver.h @@ -154,7 +154,7 @@ void driver_remove_file(const struct device_driver *driver, int driver_set_override(struct device *dev, const char **override, const char *s, size_t len); int __must_check driver_for_each_device(struct device_driver *drv, struct device *start, - void *data, int (*fn)(struct device *dev, void *)); + void *data, device_iter_t fn); struct device *driver_find_device(const struct device_driver *drv, struct device *start, const void *data, device_match_t match); From patchwork Wed Dec 18 00:01:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zijun Hu X-Patchwork-Id: 13912736 Received: from mr85p00im-ztdg06011201.me.com (mr85p00im-ztdg06011201.me.com [17.58.23.181]) (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 A61184317E for ; Wed, 18 Dec 2024 00:03:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.23.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480196; cv=none; b=pfkbGugXe5PCEEiJQx8t2kq0STgD03yWVIrcso7bq10439JANEVjplpecTBn6LAGn880HANiG3HMtfwQL5LxKReU7oT0Km80WVk74naf21vFpKdxlnRQ8sOlZnRYDRkuKpeL4hK/l5fTnf+SsctwOwRefpSOPFTFeO+3VFIqDD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734480196; c=relaxed/simple; bh=7c0hWaDRO0lYYPUw3fSJwZidpoLlvEvnQjd/RkahQfY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MBfI5h9TKjhhghIfW8+O22udPhE9srde5iKK+eUNioPEIr7RLH122PlwG83nkqM+il982DlpYx5ifG7OhQhVBVEO3/P74TpL4CK2Rh6deTNigsSRv3Aaf+PbUgs5WnXVJgU5T4rtqSzpfUfybKZ61GqTbrG16d3XHeTOLRbHwP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=MRPctL5d; arc=none smtp.client-ip=17.58.23.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="MRPctL5d" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1734480194; bh=dqP9qynvc8yZXa+bnipKSS5T9b0BbFcof3abx8Gc0z8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To: x-icloud-hme; b=MRPctL5dFoYQ3LiwnmvxriiGc1QwXzo77zQ8JdWaoYlb27NF2n6sH3ScmReaHbiLq 7WCQ76VcTlQU8AccwKGQYO5H8qogJ/e43Ko0yRgPEZrvKGw+U1/YZ2DCsoB/mbCNqL rABYg5d9XKnu58OLaNEooQ5le6l9CCwkZkN4sUgu9alT56VobjIuOgznGWiqVwJvNO camcNBZ03VQC2wyFoHcHtCj47iEZQC9lEcz28hc7HjVPzde7souSaOxe7yKSWGy/G3 /ov97PmMYTfEu6rEN3UvCXey+DvbSVGSGph7dSzK0qutYFeRTG/DdpMh8XllvhriSM cJbLQV4/fqwIw== Received: from [192.168.1.26] (mr38p00im-dlb-asmtp-mailmevip.me.com [17.57.152.18]) by mr85p00im-ztdg06011201.me.com (Postfix) with ESMTPSA id C0DA896019F; Wed, 18 Dec 2024 00:03:07 +0000 (UTC) From: Zijun Hu Date: Wed, 18 Dec 2024 08:01:38 +0800 Subject: [PATCH v4 8/8] driver core: Move 2 one line device finding APIs to header Precedence: bulk X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241218-class_fix-v4-8-3c40f098356b@quicinc.com> References: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> In-Reply-To: <20241218-class_fix-v4-0-3c40f098356b@quicinc.com> To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Tejun Heo , Josef Bacik , Jens Axboe , Boris Burkov , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Dan Williams Cc: Zijun Hu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-cxl@vger.kernel.org, Zijun Hu X-Mailer: b4 0.14.2 X-Proofpoint-ORIG-GUID: iQ8-mg4aEfHHSs5wuAPGnOfDbU92wXlr X-Proofpoint-GUID: iQ8-mg4aEfHHSs5wuAPGnOfDbU92wXlr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-17_12,2024-12-17_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 clxscore=1015 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2412170183 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 From: Zijun Hu The following device finding APIs only have one line code in function body device_find_child_by_name() device_find_any_child() Move them to header as static inline function. Signed-off-by: Zijun Hu --- drivers/base/core.c | 32 -------------------------------- include/linux/device.h | 14 +++++++++++--- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 930e43a970952b20cd1c71856bdef889698f51b4..3f37a2aecb1d11561f4edd72e973a1c43368de04 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -4100,38 +4100,6 @@ struct device *device_find_child(struct device *parent, const void *data, } EXPORT_SYMBOL_GPL(device_find_child); -/** - * device_find_child_by_name - device iterator for locating a child device. - * @parent: parent struct device - * @name: name of the child device - * - * This is similar to the device_find_child() function above, but it - * returns a reference to a device that has the name @name. - * - * NOTE: you will need to drop the reference with put_device() after use. - */ -struct device *device_find_child_by_name(struct device *parent, - const char *name) -{ - return device_find_child(parent, name, device_match_name); -} -EXPORT_SYMBOL_GPL(device_find_child_by_name); - -/** - * device_find_any_child - device iterator for locating a child device, if any. - * @parent: parent struct device - * - * This is similar to the device_find_child() function above, but it - * returns a reference to a child device, if any. - * - * NOTE: you will need to drop the reference with put_device() after use. - */ -struct device *device_find_any_child(struct device *parent) -{ - return device_find_child(parent, NULL, device_match_any); -} -EXPORT_SYMBOL_GPL(device_find_any_child); - int __init devices_init(void) { devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); diff --git a/include/linux/device.h b/include/linux/device.h index 36d1a1607712f5a6b0668ac02a6cf6b2d0651a2d..d1871a764be62e6857595bc10b9e54862c99dfa2 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1083,9 +1083,17 @@ int device_for_each_child_reverse_from(struct device *parent, device_iter_t fn); struct device *device_find_child(struct device *parent, const void *data, device_match_t match); -struct device *device_find_child_by_name(struct device *parent, - const char *name); -struct device *device_find_any_child(struct device *parent); + +static inline struct device *device_find_child_by_name(struct device *parent, + const char *name) +{ + return device_find_child(parent, name, device_match_name); +} + +static inline struct device *device_find_any_child(struct device *parent) +{ + return device_find_child(parent, NULL, device_match_any); +} int device_rename(struct device *dev, const char *new_name); int device_move(struct device *dev, struct device *new_parent,