Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Reproducer for the NULL dc_stream dereference in
- * amdgpu_dm_atomic_commit_tail()'s connector stream-update loop.
- *
- * Atomic DPMS (CRTC ACTIVE=0) removes the DC stream but leaves the connector
- * routed to an enabled CRTC. A later property-only commit does not require a
- * modeset, so it reaches the connector loop with dm_crtc_state->stream == NULL.
- *
- * rgb mode: "Broadcast RGB" change -> deref of
- * dm_old_crtc_state->stream->output_color_space
- * (amdgpu_dm.c, before the update bailout)
- * Needs HDMI-A or non-MST DisplayPort.
- *
- * hdr mode: HDR_OUTPUT_METADATA change -> dc_stream_get_status(NULL)
- * Needs three commits: amdgpu_dm_connector_atomic_check() forces a
- * modeset when *entering or exiting* HDR (old or new blob NULL),
- * so we must first install a blob while lit, then blank, then
- * change blob->blob.
- * Works on eDP.
- *
- * Build:
- * gcc -O2 -Wall -o dm-repro dm-streamless-conn-repro.c \
- * $(pkg-config --cflags --libs libdrm)
- *
- * Run as root from a bare VT with the display manager stopped, ideally
- * driven over SSH from another machine so you can read dmesg afterwards.
- */
- #define _GNU_SOURCE
- #include <errno.h>
- #include <fcntl.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <xf86drm.h>
- #include <xf86drmMode.h>
- #include <drm_mode.h>
- static uint32_t find_prop(int fd, uint32_t obj_id, uint32_t obj_type,
- const char *name, uint64_t *cur_val)
- {
- drmModeObjectProperties *props;
- uint32_t found = 0;
- props = drmModeObjectGetProperties(fd, obj_id, obj_type);
- if (!props)
- return 0;
- for (uint32_t i = 0; i < props->count_props && !found; i++) {
- drmModePropertyRes *p = drmModeGetProperty(fd, props->props[i]);
- if (!p)
- continue;
- if (!strcmp(p->name, name)) {
- found = p->prop_id;
- if (cur_val)
- *cur_val = props->prop_values[i];
- }
- drmModeFreeProperty(p);
- }
- drmModeFreeObjectProperties(props);
- return found;
- }
- /* Put the CRTC back on, so a run that does not oops leaves a usable screen. */
- static void unblank(int fd, uint32_t crtc_id, uint32_t p_active)
- {
- drmModeAtomicReq *req = drmModeAtomicAlloc();
- if (!req)
- return;
- drmModeAtomicAddProperty(req, crtc_id, p_active, 1);
- if (drmModeAtomicCommit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL))
- fprintf(stderr, "warning: could not unblank: %s\n",
- strerror(errno));
- else
- printf("restored CRTC ACTIVE = 1\n");
- drmModeAtomicFree(req);
- }
- static int do_commit(int fd, drmModeAtomicReq *req, uint32_t flags,
- const char *what)
- {
- int ret = drmModeAtomicCommit(fd, req, flags, NULL);
- printf(" %-44s %s\n", what, ret ? strerror(errno) : "ok");
- return ret;
- }
- /* Returns 0 on success. Fills *crtc_id with the CRTC currently driving conn. */
- static int connector_crtc(int fd, drmModeConnector *conn, uint32_t *crtc_id)
- {
- drmModeEncoder *enc;
- if (!conn->encoder_id)
- return -ENODEV;
- enc = drmModeGetEncoder(fd, conn->encoder_id);
- if (!enc)
- return -ENODEV;
- *crtc_id = enc->crtc_id;
- drmModeFreeEncoder(enc);
- return *crtc_id ? 0 : -ENODEV;
- }
- static const char *conn_type_name(uint32_t type)
- {
- switch (type) {
- case DRM_MODE_CONNECTOR_eDP: return "eDP";
- case DRM_MODE_CONNECTOR_HDMIA: return "HDMI-A";
- case DRM_MODE_CONNECTOR_DisplayPort: return "DP";
- case DRM_MODE_CONNECTOR_LVDS: return "LVDS";
- default: return "other";
- }
- }
- static void fill_hdr_blob(struct hdr_output_metadata *m, uint16_t max_cll)
- {
- memset(m, 0, sizeof(*m));
- m->metadata_type = 0; /* STATIC_METADATA_TYPE1 */
- m->hdmi_metadata_type1.eotf = 2; /* SMPTE ST 2084 */
- m->hdmi_metadata_type1.metadata_type = 0;
- m->hdmi_metadata_type1.max_display_mastering_luminance = 1000;
- m->hdmi_metadata_type1.min_display_mastering_luminance = 1;
- m->hdmi_metadata_type1.max_cll = max_cll;
- m->hdmi_metadata_type1.max_fall = 400;
- }
- static void usage(const char *argv0)
- {
- fprintf(stderr,
- "usage: %s [--card N] [--connector ID] [--mode rgb|hdr] [--dry-run]\n"
- "\n"
- " --card N use /dev/dri/cardN (default: probe 0..3)\n"
- " --connector ID use this connector id (default: first suitable)\n"
- " --mode rgb|hdr which property to poke (default: rgb if available)\n"
- " --dry-run stop before the final, crashing commit\n",
- argv0);
- }
- int main(int argc, char **argv)
- {
- int card = -1, fd = -1, ret;
- uint32_t want_conn = 0, crtc_id = 0;
- const char *mode = NULL;
- bool dry_run = false;
- drmModeRes *res = NULL;
- drmModeConnector *conn = NULL;
- uint32_t p_active, p_rgb = 0, p_hdr = 0;
- uint64_t cur_rgb = 0, cur_active = 0;
- drmModeAtomicReq *req;
- uint32_t blob_a = 0, blob_b = 0;
- struct hdr_output_metadata meta;
- char path[64];
- setvbuf(stdout, NULL, _IOLBF, 0);
- for (int i = 1; i < argc; i++) {
- if (!strcmp(argv[i], "--card") && i + 1 < argc)
- card = atoi(argv[++i]);
- else if (!strcmp(argv[i], "--connector") && i + 1 < argc)
- want_conn = strtoul(argv[++i], NULL, 0);
- else if (!strcmp(argv[i], "--mode") && i + 1 < argc)
- mode = argv[++i];
- else if (!strcmp(argv[i], "--dry-run"))
- dry_run = true;
- else {
- usage(argv[0]);
- return 2;
- }
- }
- if (mode && strcmp(mode, "rgb") && strcmp(mode, "hdr")) {
- usage(argv[0]);
- return 2;
- }
- /* Open the card. */
- for (int i = (card < 0 ? 0 : card); i <= (card < 0 ? 3 : card); i++) {
- drmVersion *ver;
- snprintf(path, sizeof(path), "/dev/dri/card%d", i);
- fd = open(path, O_RDWR | O_CLOEXEC);
- if (fd < 0)
- continue;
- ver = drmGetVersion(fd);
- if (ver && !strcmp(ver->name, "amdgpu")) {
- drmFreeVersion(ver);
- printf("device: %s (amdgpu)\n", path);
- break;
- }
- if (ver)
- drmFreeVersion(ver);
- close(fd);
- fd = -1;
- }
- if (fd < 0) {
- fprintf(stderr, "no amdgpu DRM device found (run as root?)\n");
- return 1;
- }
- if (drmSetMaster(fd)) {
- fprintf(stderr,
- "drmSetMaster: %s\n"
- " -> stop your display manager and run from a bare VT\n",
- strerror(errno));
- return 1;
- }
- if (drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1)) {
- fprintf(stderr, "atomic cap: %s\n", strerror(errno));
- return 1;
- }
- res = drmModeGetResources(fd);
- if (!res) {
- fprintf(stderr, "drmModeGetResources: %s\n", strerror(errno));
- return 1;
- }
- /* Pick a connected connector that is currently lit. */
- for (int i = 0; i < res->count_connectors; i++) {
- drmModeConnector *c = drmModeGetConnector(fd, res->connectors[i]);
- if (!c)
- continue;
- if (want_conn && c->connector_id != want_conn) {
- drmModeFreeConnector(c);
- continue;
- }
- if (c->connection == DRM_MODE_CONNECTED &&
- !connector_crtc(fd, c, &crtc_id)) {
- conn = c;
- break;
- }
- drmModeFreeConnector(c);
- }
- if (!conn) {
- fprintf(stderr,
- "no connected, actively-driven connector found.\n"
- " -> make sure the console is lit on the output you want\n");
- return 1;
- }
- printf("connector: %u (%s), crtc: %u\n", conn->connector_id,
- conn_type_name(conn->connector_type), crtc_id);
- p_active = find_prop(fd, crtc_id, DRM_MODE_OBJECT_CRTC, "ACTIVE",
- &cur_active);
- p_rgb = find_prop(fd, conn->connector_id, DRM_MODE_OBJECT_CONNECTOR,
- "Broadcast RGB", &cur_rgb);
- p_hdr = find_prop(fd, conn->connector_id, DRM_MODE_OBJECT_CONNECTOR,
- "HDR_OUTPUT_METADATA", NULL);
- if (!p_active) {
- fprintf(stderr, "CRTC has no ACTIVE property?\n");
- return 1;
- }
- if (!mode)
- mode = p_rgb ? "rgb" : "hdr";
- if (!strcmp(mode, "rgb") && !p_rgb) {
- fprintf(stderr,
- "connector has no \"Broadcast RGB\" (eDP?); try --mode hdr\n");
- return 1;
- }
- if (!strcmp(mode, "hdr") && !p_hdr) {
- fprintf(stderr, "connector has no HDR_OUTPUT_METADATA\n");
- return 1;
- }
- printf("mode: %s%s\n\n", mode, dry_run ? " (dry run)" : "");
- if (!strcmp(mode, "hdr")) {
- fill_hdr_blob(&meta, 1000);
- if (drmModeCreatePropertyBlob(fd, &meta, sizeof(meta), &blob_a)) {
- fprintf(stderr, "create blob A: %s\n", strerror(errno));
- return 1;
- }
- fill_hdr_blob(&meta, 500);
- if (drmModeCreatePropertyBlob(fd, &meta, sizeof(meta), &blob_b)) {
- fprintf(stderr, "create blob B: %s\n", strerror(errno));
- return 1;
- }
- /*
- * Step 1: enter HDR while the CRTC is still active. This one
- * legitimately forces a modeset (old blob is NULL).
- */
- printf("[1] install HDR metadata blob while lit\n");
- req = drmModeAtomicAlloc();
- drmModeAtomicAddProperty(req, conn->connector_id, p_hdr, blob_a);
- ret = do_commit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET,
- "set HDR_OUTPUT_METADATA = blob A");
- drmModeAtomicFree(req);
- if (ret)
- return 1;
- sleep(1);
- }
- /* Blank the CRTC: this is what drops the DC stream. */
- printf("[%c] blank the CRTC (ACTIVE=0) - screen goes dark here\n",
- !strcmp(mode, "hdr") ? '2' : '1');
- req = drmModeAtomicAlloc();
- drmModeAtomicAddProperty(req, crtc_id, p_active, 0);
- ret = do_commit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET,
- "set CRTC ACTIVE = 0");
- drmModeAtomicFree(req);
- if (ret)
- return 1;
- sleep(1);
- /*
- * Property-only commit against the now streamless CRTC. Deliberately
- * NOT passing ALLOW_MODESET: if the kernel decides this needs a modeset
- * the commit fails with EINVAL and we know the repro did not apply.
- */
- printf("[%c] property-only commit against the streamless CRTC\n",
- !strcmp(mode, "hdr") ? '3' : '2');
- req = drmModeAtomicAlloc();
- if (!strcmp(mode, "hdr"))
- drmModeAtomicAddProperty(req, conn->connector_id, p_hdr, blob_b);
- else
- drmModeAtomicAddProperty(req, conn->connector_id, p_rgb,
- cur_rgb == 1 ? 2 : 1);
- ret = do_commit(fd, req, DRM_MODE_ATOMIC_TEST_ONLY,
- "TEST_ONLY (proves it is not a modeset)");
- if (ret) {
- fprintf(stderr,
- "\ncheck rejected it -- the kernel wants a modeset here,\n"
- "so this path does not reach the streamless loop.\n");
- drmModeAtomicFree(req);
- unblank(fd, crtc_id, p_active);
- return 1;
- }
- if (dry_run) {
- printf("\ndry run: stopping before the commit that oopses.\n");
- drmModeAtomicFree(req);
- unblank(fd, crtc_id, p_active);
- return 0;
- }
- printf("\n*** committing for real - expect an oops ***\n");
- sleep(1);
- do_commit(fd, req, 0, "real commit");
- drmModeAtomicFree(req);
- printf("\nno crash: this kernel already has the fix.\n");
- unblank(fd, crtc_id, p_active);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment