Skip to contentSkip to conversation

drm/dp_mst: Track allocated_pbn from ALLOCATE_PAYLOAD down-reply

PATCH Superseded Xiao Lu 2 participants 4 messages Sep 8 – Sep 9

Subsystems (2)
lore
Share & tools
lore

Xiao Lu

author
#0 lore raw

PATCH 1/1 drm/dp_mst: Track allocated_pbn from ALLOCATE_PAYLOAD down-reply

The DP MST ALLOCATE_PAYLOAD sideband message returns the payload bandwidth actually allocated by the branch in the allocated_pbn field of the down-reply. That value may differ from the requested PBN when an intermediate branch applies its own constraints, so the source should keep track of it instead of only relying on the local request.

Store the allocated_pbn returned by the ALLOCATE_PAYLOAD down-reply sideband message and use the larger of requested PBN and allocated PBN for the MST bandwidth limit checks. Also carry the value across dependent atomic commits and clear it when releasing the payload, matching the lifetime of the remote allocation state.

---
 drivers/gpu/drm/display/drm_dp_mst_topology.c | 17 ++++++++++++-----
 include/drm/display/drm_dp_mst_helper.h       | 10 ++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
drivers/gpu/drm/display/drm_dp_mst_topology.c +12 -5
index 7ce9e2127..86a42c5e2 100644
@@ -3120,6 +3120,7 @@ drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
 
 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
 				   struct drm_dp_mst_port *port,
+				   struct drm_dp_mst_atomic_payload *payload,
 				   int id,
 				   int pbn)
 {
@@ -3166,10 +3167,14 @@ static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
 	 */
 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
 	if (ret > 0) {
-		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
+		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
 			ret = -EINVAL;
-		else
+		} else {
+			if (payload)
+				payload->allocated_pbn =
+					txmsg->reply.u.allocate_payload.allocated_pbn;
 			ret = 0;
+		}
 	}
 	kfree(txmsg);
 fail_put:
@@ -3285,7 +3290,7 @@ static int drm_dp_create_payload_to_remote(struct drm_dp_mst_topology_mgr *mgr,
 	if (!port)
 		return -EIO;
 
-	ret = drm_dp_payload_send_msg(mgr, port, payload->vcpi, payload->pbn);
+	ret = drm_dp_payload_send_msg(mgr, port, payload, payload->vcpi, payload->pbn);
 	drm_dp_mst_topology_put_port(port);
 	return ret;
 }
@@ -3298,7 +3303,7 @@ static void drm_dp_destroy_payload_at_remote_and_dfp(struct drm_dp_mst_topology_
 
 	/* it's okay for these to fail */
 	if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE) {
-		drm_dp_payload_send_msg(mgr, payload->port, payload->vcpi, 0);
+		drm_dp_payload_send_msg(mgr, payload->port, NULL, payload->vcpi, 0);
 		payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP;
 	}
 
@@ -4575,6 +4580,7 @@ int drm_dp_atomic_release_time_slots(struct drm_atomic_commit *state,
 	drm_dbg_atomic(mgr->dev, "[MST PORT:%p] TU %d -> 0\n", port, payload->time_slots);
 	if (!payload->delete) {
 		payload->pbn = 0;
+		payload->allocated_pbn = 0;
 		payload->delete = true;
 		if (payload->vcpi > 0)
 			topology_state->payload_mask &= ~BIT(payload->vcpi - 1);
@@ -4670,6 +4676,7 @@ void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_commit *state)
 			new_payload = drm_atomic_get_mst_payload_state(new_mst_state,
 								       old_payload->port);
 			new_payload->vc_start_slot = old_payload->vc_start_slot;
+			new_payload->allocated_pbn = old_payload->allocated_pbn;
 			new_payload->payload_allocation_status =
 							old_payload->payload_allocation_status;
 		}
@@ -5352,7 +5359,7 @@ drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
 			return -EINVAL;
 		}
 
-		pbn_used = payload->pbn;
+		pbn_used = max(payload->pbn, payload->allocated_pbn);
 	} else {
 		pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb,
 								 state,
include/drm/display/drm_dp_mst_helper.h +10 -0
index 27658bfb5..1a9430a62 100644
@@ -572,6 +572,16 @@ struct drm_dp_mst_atomic_payload {
 	int time_slots;
 	/** @pbn: The payload bandwidth for this payload */
 	int pbn;
+	/**
+	 * @allocated_pbn: The payload bandwidth actually confirmed by the
+	 * branch device in the ALLOCATE_PAYLOAD reply. Each MST branch along
+	 * the path overrides this field to the maximum of the requested and
+	 * its own constrained value, so the value received by the source
+	 * reflects the tightest bottleneck along the entire path.
+	 * Used instead of @pbn for bandwidth limit checks so that the source
+	 * accounts for any per-hop adjustments made by intermediate branches.
+	 */
+	int allocated_pbn;
 
 	/** @delete: Whether or not we intend to delete this payload during this atomic commit */
 	bool delete : 1;
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Data race and TOCTOU flaw on payload->allocated_pbn. - [Medium] Stale allocated_pbn constraint…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Data race and TOCTOU flaw on payload->allocated_pbn.
- [Medium] Stale allocated_pbn constraint leads to spurious -ENOSPC errors when reducing bandwidth. --

commit 51837e7219143c6b9b0e48d938f61a3258bf06cd
Author: Xiao Lu <[email protected]>

drm/dp_mst: Track allocated_pbn from ALLOCATE_PAYLOAD down-reply

The DP MST ALLOCATE_PAYLOAD sideband message returns the payload bandwidth actually allocated by the branch in the allocated_pbn field of the down-reply. That value may differ from the requested PBN when an intermediate branch applies its own constraints, so the source should keep track of it instead of only relying on the local request.

Quoted 4 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +0 -0
index 7ce9e212770ad..86a42c5e2debb 100644

[ ... ]

drivers/gpu/drm/display/drm_dp_mst_topology.c static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr, · line 3175

Quoted 12 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +5 -2
@@ -3166,10 +3167,14 @@ static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
 	 */
 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
 	if (ret > 0) {
-		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
+		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
 			ret = -EINVAL;
-		else
+		} else {
+			if (payload)
+				payload->allocated_pbn =
+					txmsg->reply.u.allocate_payload.allocated_pbn;

[Severity: High]
Can this write to payload->allocated_pbn cause a data race with concurrent atomic checks?

When a non-blocking mode commit runs atomic_commit_tail in a workqueue, it writes to payload->allocated_pbn here. Concurrently, if userspace initiates a new atomic check, it calls drm_dp_mst_duplicate_state(), which synchronously copies the active payload object:

drivers/gpu/drm/display/drm_dp_mst_topology.c:drm_dp_mst_duplicate_state() {
    ...
    list_for_each_entry(pos, &old_state->payloads, next) {
        ...
        payload = kmemdup(pos, sizeof(*payload), GFP_KERNEL);
    ...
}

This unsynchronized kmemdup() could result in a torn read of payload->allocated_pbn, which is later evaluated during the atomic check in drm_dp_mst_atomic_check_port_bw_limit().

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +1 -0
 			ret = 0;
+		}
 	}
 	kfree(txmsg);
 fail_put:

[ ... ]

drivers/gpu/drm/display/drm_dp_mst_topology.c drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port, · line 5362

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +1 -1
@@ -5352,7 +5359,7 @@ drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
 			return -EINVAL;
 		}
 
-		pbn_used = payload->pbn;
+		pbn_used = max(payload->pbn, payload->allocated_pbn);

[Severity: Medium]
Will this stale payload->allocated_pbn constraint lead to spurious -ENOSPC errors when reducing bandwidth?

If userspace initially sets a high resolution, payload->allocated_pbn is populated with a high bandwidth value. Later, if userspace requests a lower resolution, drm_dp_atomic_find_time_slots() updates payload->pbn but leaves payload->allocated_pbn unchanged:

drivers/gpu/drm/display/drm_dp_mst_topology.c:drm_dp_atomic_find_time_slots() {
    ...
    payload->time_slots = req_slots;
    payload->pbn = pbn;
    return req_slots;
}

Because payload->allocated_pbn is not cleared or updated, max(payload->pbn, payload->allocated_pbn) will resolve to the old, larger bandwidth. This could falsely fail the bandwidth limit check and prevent userspace from reclaiming bandwidth for other displays.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

PATCH v2 1/1 drm/dp_mst: Track allocated_pbn from ALLOCATE_PAYLOAD down-reply

In reply to Xiao Lu

The DP MST ALLOCATE_PAYLOAD sideband message returns the payload bandwidth actually allocated by the branch in the allocated_pbn field of the down-reply. That value may differ from the requested PBN when an intermediate branch applies its own constraints, so the source should keep track of it instead of only relying on the local request.

Store the allocated_pbn returned by the ALLOCATE_PAYLOAD down-reply sideband message and use the larger of requested PBN and allocated PBN for the MST bandwidth limit checks. Also carry the value across dependent atomic commits and clear it when releasing the payload, matching the lifetime of the remote allocation state.

---
v2: Clear allocated_pbn in drm_dp_atomic_find_time_slots() so that
a subsequent bandwidth reduction does not retain a stale high value from the previous allocation, which could cause spurious -ENOSPC in the bandwidth limit check.

---
 drivers/gpu/drm/display/drm_dp_mst_topology.c | 19 ++++++++++++++-----
 include/drm/display/drm_dp_mst_helper.h       | 10 ++++++++++
 2 files changed, 24 insertions(+), 5 deletions(-)
drivers/gpu/drm/display/drm_dp_mst_topology.c +14 -5
index 7ce9e2127..b0e4cf845 100644
@@ -3120,6 +3120,7 @@ drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
 
 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
 				   struct drm_dp_mst_port *port,
+				   struct drm_dp_mst_atomic_payload *payload,
 				   int id,
 				   int pbn)
 {
@@ -3166,10 +3167,14 @@ static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
 	 */
 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
 	if (ret > 0) {
-		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
+		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
 			ret = -EINVAL;
-		else
+		} else {
+			if (payload)
+				payload->allocated_pbn =
+					txmsg->reply.u.allocate_payload.allocated_pbn;
 			ret = 0;
+		}
 	}
 	kfree(txmsg);
 fail_put:
@@ -3285,7 +3290,7 @@ static int drm_dp_create_payload_to_remote(struct drm_dp_mst_topology_mgr *mgr,
 	if (!port)
 		return -EIO;
 
-	ret = drm_dp_payload_send_msg(mgr, port, payload->vcpi, payload->pbn);
+	ret = drm_dp_payload_send_msg(mgr, port, payload, payload->vcpi, payload->pbn);
 	drm_dp_mst_topology_put_port(port);
 	return ret;
 }
@@ -3298,7 +3303,7 @@ static void drm_dp_destroy_payload_at_remote_and_dfp(struct drm_dp_mst_topology_
 
 	/* it's okay for these to fail */
 	if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE) {
-		drm_dp_payload_send_msg(mgr, payload->port, payload->vcpi, 0);
+		drm_dp_payload_send_msg(mgr, payload->port, NULL, payload->vcpi, 0);
 		payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP;
 	}
 
@@ -4495,6 +4500,8 @@ int drm_dp_atomic_find_time_slots(struct drm_atomic_commit *state,
 	}
 	payload->time_slots = req_slots;
 	payload->pbn = pbn;
+	/* Clear allocated_pbn so it is re-populated from the next ALLOCATE_PAYLOAD reply. */
+	payload->allocated_pbn = 0;
 
 	return req_slots;
 }
@@ -4575,6 +4582,7 @@ int drm_dp_atomic_release_time_slots(struct drm_atomic_commit *state,
 	drm_dbg_atomic(mgr->dev, "[MST PORT:%p] TU %d -> 0\n", port, payload->time_slots);
 	if (!payload->delete) {
 		payload->pbn = 0;
+		payload->allocated_pbn = 0;
 		payload->delete = true;
 		if (payload->vcpi > 0)
 			topology_state->payload_mask &= ~BIT(payload->vcpi - 1);
@@ -4670,6 +4678,7 @@ void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_commit *state)
 			new_payload = drm_atomic_get_mst_payload_state(new_mst_state,
 								       old_payload->port);
 			new_payload->vc_start_slot = old_payload->vc_start_slot;
+			new_payload->allocated_pbn = old_payload->allocated_pbn;
 			new_payload->payload_allocation_status =
 							old_payload->payload_allocation_status;
 		}
@@ -5352,7 +5361,7 @@ drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
 			return -EINVAL;
 		}
 
-		pbn_used = payload->pbn;
+		pbn_used = max(payload->pbn, payload->allocated_pbn);
 	} else {
 		pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb,
 								 state,
include/drm/display/drm_dp_mst_helper.h +10 -0
index 27658bfb5..1a9430a62 100644
@@ -572,6 +572,16 @@ struct drm_dp_mst_atomic_payload {
 	int time_slots;
 	/** @pbn: The payload bandwidth for this payload */
 	int pbn;
+	/**
+	 * @allocated_pbn: The payload bandwidth actually confirmed by the
+	 * branch device in the ALLOCATE_PAYLOAD reply. Each MST branch along
+	 * the path overrides this field to the maximum of the requested and
+	 * its own constrained value, so the value received by the source
+	 * reflects the tightest bottleneck along the entire path.
+	 * Used instead of @pbn for bandwidth limit checks so that the source
+	 * accounts for any per-hop adjustments made by intermediate branches.
+	 */
+	int allocated_pbn;
 
 	/** @delete: Whether or not we intend to delete this payload during this atomic commit */
 	bool delete : 1;
Signature · 1 line
2.43.0
#3 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Data race and stale state usage for `allocated_pbn` between asynchronous commit tail and…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Data race and stale state usage for allocated_pbn between asynchronous commit tail and synchronous atomic check. --

commit 60c9feaab7e5f91e4bacf61ee648e76157adea65
Author: Xiao Lu <[email protected]>
drm/dp_mst: Track allocated_pbn from ALLOCATE_PAYLOAD down-reply

This commit stores the allocated_pbn returned by the ALLOCATE_PAYLOAD down-reply sideband message and uses it to perform bandwidth limit checks based on the larger of the requested and allocated PBN.

Quoted 4 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +0 -0
index 7ce9e212770ad..b0e4cf8458eda 100644

[ ... ]

Quoted 7 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +1 -1
@@ -3166,10 +3167,14 @@ static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
 	 */
 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
 	if (ret > 0) {
-		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
+		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
 			ret = -EINVAL;

drivers/gpu/drm/display/drm_dp_mst_topology.c static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr, · line 3180

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +5 -1
-		else
+		} else {
+			if (payload)
+				payload->allocated_pbn =
+					txmsg->reply.u.allocate_payload.allocated_pbn;
 			ret = 0;
+		}
 	}
 	kfree(txmsg);
 fail_put:

[Severity: High]
Does modifying payload->allocated_pbn asynchronously here in drm_dp_payload_send_msg() during the commit tail introduce a data race?

When a non-blocking atomic commit updates the topology, its asynchronous commit tail waits for the ALLOCATE_PAYLOAD reply. If a subsequent atomic commit ioctl triggers the atomic check phase concurrently, that check phase duplicates the MST state without locks, which could capture a partially updated or zeroed allocated_pbn.

[ ... ]

drivers/gpu/drm/display/drm_dp_mst_topology.c void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_commit *state) · line 4684

Quoted 8 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +1 -0
@@ -4670,6 +4678,7 @@ void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_commit *state)
 			new_payload = drm_atomic_get_mst_payload_state(new_mst_state,
 								       old_payload->port);
 			new_payload->vc_start_slot = old_payload->vc_start_slot;
+			new_payload->allocated_pbn = old_payload->allocated_pbn;
 			new_payload->payload_allocation_status =
 							old_payload->payload_allocation_status;
 		}

[Severity: High]
Since drm_dp_mst_atomic_wait_for_dependencies() is executed in the commit tail after dependencies resolve, does copying allocated_pbn here mean the check phase of the current commit has already executed?

If so, the check phase would have already run using the un-updated, stale value of allocated_pbn from the previous commit state before this synchronization occurs.

[ ... ]

drivers/gpu/drm/display/drm_dp_mst_topology.c drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port, · line 5367

Quoted 9 lines
drivers/gpu/drm/display/drm_dp_mst_topology.c +1 -1
@@ -5352,7 +5361,7 @@ drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
 			return -EINVAL;
 		}
 
-		pbn_used = payload->pbn;
+		pbn_used = max(payload->pbn, payload->allocated_pbn);
 	} else {
 		pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb,
 								 state,

[Severity: High]
Does reading payload->allocated_pbn here in drm_dp_mst_atomic_check_port_bw_limit() risk bypassing bandwidth validation if the previous hardware reply hasn't arrived yet?

If unprivileged userspace (like a Wayland compositor) submits rapid back-to-back atomic commits, the check phase here could evaluate max(payload->pbn, payload->allocated_pbn) using a stale or zeroed value. This might allow configurations that exceed link bandwidth because intermediate branch constraints would be silently ignored.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1