Skip to contentSkip to conversation

drm/nouveau: avoid usage of list iterator after loop

PATCH New Jakob Koschel 1 participant 3 messages Mar 13, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover drm/nouveau: avoid usage of list iterator after loop

This patch set includes two instances where the list iterator variable 'pstate' is implicitly assumed to be valid after the iterator loop. While in pratice that is most likely the case (if 'pstatei'/'args->v0.state' is <= the elements in clk->states), we should explicitly only allow 'pstate' to always point to correct 'nvkm_pstate' structs.

That allows catching potential bugs with WARN_ON(!pstate) that otherwise would be completely undetectable.

It also helps the greater mission to hopefully move the list iterator variable into the iterating macro directly [1].

---
Changes in v2:
- convert BUG_ON() into WARN_ON()
- Link to v1: https://lore.kernel.org/r/20230301-drm-nouveau-avoid-iter-after-loop-v1-0-0702ec23f970@gmail.com

---
Jakob Koschel (2):
drm/nouveau/device: avoid usage of list iterator after loop drm/nouveau/clk: avoid usage of list iterator after loop

 drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 11 ++++++++---
 drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c    | 10 +++++++---
 2 files changed, 15 insertions(+), 6 deletions(-)

---
base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-drm-nouveau-avoid-iter-after-loop-4bff97166efa

Best regards,

Signature · 1 line
Jakob Koschel <[email protected]>
#1 lore raw

PATCH v2 1/2 drm/nouveau/device: avoid usage of list iterator after loop

If potentially no valid element is found, 'pstate' would contain an invalid pointer past the iterator loop. To ensure 'pstate' is always valid, we only set it if the correct element was found. That allows adding a WARN_ON() in case the code works incorrectly, exposing currently undetectable potential bugs.

Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the macro to avoid any potential misuse after the loop [1].

---
 drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +8 -3
index ce774579c89d..8ae14ab8f88e 100644
@@ -72,7 +72,7 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
 	} *args = data;
 	struct nvkm_clk *clk = ctrl->device->clk;
 	const struct nvkm_domain *domain;
-	struct nvkm_pstate *pstate;
+	struct nvkm_pstate *pstate = NULL, *iter;
 	struct nvkm_cstate *cstate;
 	int i = 0, j = -1;
 	u32 lo, hi;
@@ -103,11 +103,16 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
 		return -EINVAL;
 
 	if (args->v0.state != NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {
-		list_for_each_entry(pstate, &clk->states, head) {
-			if (i++ == args->v0.state)
+		list_for_each_entry(iter, &clk->states, head) {
+			if (i++ == args->v0.state) {
+				pstate = iter;
 				break;
+			}
 		}
 
+		if (WARN_ON_ONCE(!pstate))
+			return -EINVAL;
+
 		lo = pstate->base.domain[domain->name];
 		hi = lo;
 		list_for_each_entry(cstate, &pstate->list, head) {
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/2 drm/nouveau/clk: avoid usage of list iterator after loop

In reply to Jakob Koschel

If potentially no valid element is found, 'pstate' would contain an invalid pointer past the iterator loop. To ensure 'pstate' is always valid, we only set it if the correct element was found. That allows adding a WARN_ON() in case the code works incorrectly, exposing currently undetectable potential bugs.

Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the macro to avoid any potential misuse after the loop [1].

---
 drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +7 -3
index da07a2fbef06..d914cce6d0b8 100644
@@ -269,14 +269,18 @@ nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei)
 	struct nvkm_subdev *subdev = &clk->subdev;
 	struct nvkm_fb *fb = subdev->device->fb;
 	struct nvkm_pci *pci = subdev->device->pci;
-	struct nvkm_pstate *pstate;
+	struct nvkm_pstate *pstate = NULL, *iter;
 	int ret, idx = 0;
 
-	list_for_each_entry(pstate, &clk->states, head) {
-		if (idx++ == pstatei)
+	list_for_each_entry(iter, &clk->states, head) {
+		if (idx++ == pstatei) {
+			pstate = iter;
 			break;
+		}
 	}
 
+	if (WARN_ON(!pstate))
+		return -EINVAL;
 	nvkm_debug(subdev, "setting performance state %d\n", pstatei);
 	clk->pstate = pstatei;
 
Signature · 1 line
2.34.1