Guest User

Untitled

a guest
Aug 13th, 2026
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.44 KB | None | 0 0
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Reproducer for the NULL dc_stream dereference in
  4. * amdgpu_dm_atomic_commit_tail()'s connector stream-update loop.
  5. *
  6. * Atomic DPMS (CRTC ACTIVE=0) removes the DC stream but leaves the connector
  7. * routed to an enabled CRTC. A later property-only commit does not require a
  8. * modeset, so it reaches the connector loop with dm_crtc_state->stream == NULL.
  9. *
  10. * rgb mode: "Broadcast RGB" change -> deref of
  11. * dm_old_crtc_state->stream->output_color_space
  12. * (amdgpu_dm.c, before the update bailout)
  13. * Needs HDMI-A or non-MST DisplayPort.
  14. *
  15. * hdr mode: HDR_OUTPUT_METADATA change -> dc_stream_get_status(NULL)
  16. * Needs three commits: amdgpu_dm_connector_atomic_check() forces a
  17. * modeset when *entering or exiting* HDR (old or new blob NULL),
  18. * so we must first install a blob while lit, then blank, then
  19. * change blob->blob.
  20. * Works on eDP.
  21. *
  22. * Build:
  23. * gcc -O2 -Wall -o dm-repro dm-streamless-conn-repro.c \
  24. * $(pkg-config --cflags --libs libdrm)
  25. *
  26. * Run as root from a bare VT with the display manager stopped, ideally
  27. * driven over SSH from another machine so you can read dmesg afterwards.
  28. */
  29. #define _GNU_SOURCE
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <stdbool.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38.  
  39. #include <xf86drm.h>
  40. #include <xf86drmMode.h>
  41. #include <drm_mode.h>
  42.  
  43. static uint32_t find_prop(int fd, uint32_t obj_id, uint32_t obj_type,
  44. const char *name, uint64_t *cur_val)
  45. {
  46. drmModeObjectProperties *props;
  47. uint32_t found = 0;
  48.  
  49. props = drmModeObjectGetProperties(fd, obj_id, obj_type);
  50. if (!props)
  51. return 0;
  52.  
  53. for (uint32_t i = 0; i < props->count_props && !found; i++) {
  54. drmModePropertyRes *p = drmModeGetProperty(fd, props->props[i]);
  55.  
  56. if (!p)
  57. continue;
  58. if (!strcmp(p->name, name)) {
  59. found = p->prop_id;
  60. if (cur_val)
  61. *cur_val = props->prop_values[i];
  62. }
  63. drmModeFreeProperty(p);
  64. }
  65.  
  66. drmModeFreeObjectProperties(props);
  67. return found;
  68. }
  69.  
  70. /* Put the CRTC back on, so a run that does not oops leaves a usable screen. */
  71. static void unblank(int fd, uint32_t crtc_id, uint32_t p_active)
  72. {
  73. drmModeAtomicReq *req = drmModeAtomicAlloc();
  74.  
  75. if (!req)
  76. return;
  77. drmModeAtomicAddProperty(req, crtc_id, p_active, 1);
  78. if (drmModeAtomicCommit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL))
  79. fprintf(stderr, "warning: could not unblank: %s\n",
  80. strerror(errno));
  81. else
  82. printf("restored CRTC ACTIVE = 1\n");
  83. drmModeAtomicFree(req);
  84. }
  85.  
  86. static int do_commit(int fd, drmModeAtomicReq *req, uint32_t flags,
  87. const char *what)
  88. {
  89. int ret = drmModeAtomicCommit(fd, req, flags, NULL);
  90.  
  91. printf(" %-44s %s\n", what, ret ? strerror(errno) : "ok");
  92. return ret;
  93. }
  94.  
  95. /* Returns 0 on success. Fills *crtc_id with the CRTC currently driving conn. */
  96. static int connector_crtc(int fd, drmModeConnector *conn, uint32_t *crtc_id)
  97. {
  98. drmModeEncoder *enc;
  99.  
  100. if (!conn->encoder_id)
  101. return -ENODEV;
  102.  
  103. enc = drmModeGetEncoder(fd, conn->encoder_id);
  104. if (!enc)
  105. return -ENODEV;
  106.  
  107. *crtc_id = enc->crtc_id;
  108. drmModeFreeEncoder(enc);
  109.  
  110. return *crtc_id ? 0 : -ENODEV;
  111. }
  112.  
  113. static const char *conn_type_name(uint32_t type)
  114. {
  115. switch (type) {
  116. case DRM_MODE_CONNECTOR_eDP: return "eDP";
  117. case DRM_MODE_CONNECTOR_HDMIA: return "HDMI-A";
  118. case DRM_MODE_CONNECTOR_DisplayPort: return "DP";
  119. case DRM_MODE_CONNECTOR_LVDS: return "LVDS";
  120. default: return "other";
  121. }
  122. }
  123.  
  124. static void fill_hdr_blob(struct hdr_output_metadata *m, uint16_t max_cll)
  125. {
  126. memset(m, 0, sizeof(*m));
  127. m->metadata_type = 0; /* STATIC_METADATA_TYPE1 */
  128. m->hdmi_metadata_type1.eotf = 2; /* SMPTE ST 2084 */
  129. m->hdmi_metadata_type1.metadata_type = 0;
  130. m->hdmi_metadata_type1.max_display_mastering_luminance = 1000;
  131. m->hdmi_metadata_type1.min_display_mastering_luminance = 1;
  132. m->hdmi_metadata_type1.max_cll = max_cll;
  133. m->hdmi_metadata_type1.max_fall = 400;
  134. }
  135.  
  136. static void usage(const char *argv0)
  137. {
  138. fprintf(stderr,
  139. "usage: %s [--card N] [--connector ID] [--mode rgb|hdr] [--dry-run]\n"
  140. "\n"
  141. " --card N use /dev/dri/cardN (default: probe 0..3)\n"
  142. " --connector ID use this connector id (default: first suitable)\n"
  143. " --mode rgb|hdr which property to poke (default: rgb if available)\n"
  144. " --dry-run stop before the final, crashing commit\n",
  145. argv0);
  146. }
  147.  
  148. int main(int argc, char **argv)
  149. {
  150. int card = -1, fd = -1, ret;
  151. uint32_t want_conn = 0, crtc_id = 0;
  152. const char *mode = NULL;
  153. bool dry_run = false;
  154. drmModeRes *res = NULL;
  155. drmModeConnector *conn = NULL;
  156. uint32_t p_active, p_rgb = 0, p_hdr = 0;
  157. uint64_t cur_rgb = 0, cur_active = 0;
  158. drmModeAtomicReq *req;
  159. uint32_t blob_a = 0, blob_b = 0;
  160. struct hdr_output_metadata meta;
  161. char path[64];
  162.  
  163. setvbuf(stdout, NULL, _IOLBF, 0);
  164.  
  165. for (int i = 1; i < argc; i++) {
  166. if (!strcmp(argv[i], "--card") && i + 1 < argc)
  167. card = atoi(argv[++i]);
  168. else if (!strcmp(argv[i], "--connector") && i + 1 < argc)
  169. want_conn = strtoul(argv[++i], NULL, 0);
  170. else if (!strcmp(argv[i], "--mode") && i + 1 < argc)
  171. mode = argv[++i];
  172. else if (!strcmp(argv[i], "--dry-run"))
  173. dry_run = true;
  174. else {
  175. usage(argv[0]);
  176. return 2;
  177. }
  178. }
  179.  
  180. if (mode && strcmp(mode, "rgb") && strcmp(mode, "hdr")) {
  181. usage(argv[0]);
  182. return 2;
  183. }
  184.  
  185. /* Open the card. */
  186. for (int i = (card < 0 ? 0 : card); i <= (card < 0 ? 3 : card); i++) {
  187. drmVersion *ver;
  188.  
  189. snprintf(path, sizeof(path), "/dev/dri/card%d", i);
  190. fd = open(path, O_RDWR | O_CLOEXEC);
  191. if (fd < 0)
  192. continue;
  193.  
  194. ver = drmGetVersion(fd);
  195. if (ver && !strcmp(ver->name, "amdgpu")) {
  196. drmFreeVersion(ver);
  197. printf("device: %s (amdgpu)\n", path);
  198. break;
  199. }
  200. if (ver)
  201. drmFreeVersion(ver);
  202. close(fd);
  203. fd = -1;
  204. }
  205. if (fd < 0) {
  206. fprintf(stderr, "no amdgpu DRM device found (run as root?)\n");
  207. return 1;
  208. }
  209.  
  210. if (drmSetMaster(fd)) {
  211. fprintf(stderr,
  212. "drmSetMaster: %s\n"
  213. " -> stop your display manager and run from a bare VT\n",
  214. strerror(errno));
  215. return 1;
  216. }
  217.  
  218. if (drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1)) {
  219. fprintf(stderr, "atomic cap: %s\n", strerror(errno));
  220. return 1;
  221. }
  222.  
  223. res = drmModeGetResources(fd);
  224. if (!res) {
  225. fprintf(stderr, "drmModeGetResources: %s\n", strerror(errno));
  226. return 1;
  227. }
  228.  
  229. /* Pick a connected connector that is currently lit. */
  230. for (int i = 0; i < res->count_connectors; i++) {
  231. drmModeConnector *c = drmModeGetConnector(fd, res->connectors[i]);
  232.  
  233. if (!c)
  234. continue;
  235. if (want_conn && c->connector_id != want_conn) {
  236. drmModeFreeConnector(c);
  237. continue;
  238. }
  239. if (c->connection == DRM_MODE_CONNECTED &&
  240. !connector_crtc(fd, c, &crtc_id)) {
  241. conn = c;
  242. break;
  243. }
  244. drmModeFreeConnector(c);
  245. }
  246.  
  247. if (!conn) {
  248. fprintf(stderr,
  249. "no connected, actively-driven connector found.\n"
  250. " -> make sure the console is lit on the output you want\n");
  251. return 1;
  252. }
  253.  
  254. printf("connector: %u (%s), crtc: %u\n", conn->connector_id,
  255. conn_type_name(conn->connector_type), crtc_id);
  256.  
  257. p_active = find_prop(fd, crtc_id, DRM_MODE_OBJECT_CRTC, "ACTIVE",
  258. &cur_active);
  259. p_rgb = find_prop(fd, conn->connector_id, DRM_MODE_OBJECT_CONNECTOR,
  260. "Broadcast RGB", &cur_rgb);
  261. p_hdr = find_prop(fd, conn->connector_id, DRM_MODE_OBJECT_CONNECTOR,
  262. "HDR_OUTPUT_METADATA", NULL);
  263.  
  264. if (!p_active) {
  265. fprintf(stderr, "CRTC has no ACTIVE property?\n");
  266. return 1;
  267. }
  268.  
  269. if (!mode)
  270. mode = p_rgb ? "rgb" : "hdr";
  271.  
  272. if (!strcmp(mode, "rgb") && !p_rgb) {
  273. fprintf(stderr,
  274. "connector has no \"Broadcast RGB\" (eDP?); try --mode hdr\n");
  275. return 1;
  276. }
  277. if (!strcmp(mode, "hdr") && !p_hdr) {
  278. fprintf(stderr, "connector has no HDR_OUTPUT_METADATA\n");
  279. return 1;
  280. }
  281.  
  282. printf("mode: %s%s\n\n", mode, dry_run ? " (dry run)" : "");
  283.  
  284. if (!strcmp(mode, "hdr")) {
  285. fill_hdr_blob(&meta, 1000);
  286. if (drmModeCreatePropertyBlob(fd, &meta, sizeof(meta), &blob_a)) {
  287. fprintf(stderr, "create blob A: %s\n", strerror(errno));
  288. return 1;
  289. }
  290. fill_hdr_blob(&meta, 500);
  291. if (drmModeCreatePropertyBlob(fd, &meta, sizeof(meta), &blob_b)) {
  292. fprintf(stderr, "create blob B: %s\n", strerror(errno));
  293. return 1;
  294. }
  295.  
  296. /*
  297. * Step 1: enter HDR while the CRTC is still active. This one
  298. * legitimately forces a modeset (old blob is NULL).
  299. */
  300. printf("[1] install HDR metadata blob while lit\n");
  301. req = drmModeAtomicAlloc();
  302. drmModeAtomicAddProperty(req, conn->connector_id, p_hdr, blob_a);
  303. ret = do_commit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET,
  304. "set HDR_OUTPUT_METADATA = blob A");
  305. drmModeAtomicFree(req);
  306. if (ret)
  307. return 1;
  308. sleep(1);
  309. }
  310.  
  311. /* Blank the CRTC: this is what drops the DC stream. */
  312. printf("[%c] blank the CRTC (ACTIVE=0) - screen goes dark here\n",
  313. !strcmp(mode, "hdr") ? '2' : '1');
  314. req = drmModeAtomicAlloc();
  315. drmModeAtomicAddProperty(req, crtc_id, p_active, 0);
  316. ret = do_commit(fd, req, DRM_MODE_ATOMIC_ALLOW_MODESET,
  317. "set CRTC ACTIVE = 0");
  318. drmModeAtomicFree(req);
  319. if (ret)
  320. return 1;
  321. sleep(1);
  322.  
  323. /*
  324. * Property-only commit against the now streamless CRTC. Deliberately
  325. * NOT passing ALLOW_MODESET: if the kernel decides this needs a modeset
  326. * the commit fails with EINVAL and we know the repro did not apply.
  327. */
  328. printf("[%c] property-only commit against the streamless CRTC\n",
  329. !strcmp(mode, "hdr") ? '3' : '2');
  330.  
  331. req = drmModeAtomicAlloc();
  332. if (!strcmp(mode, "hdr"))
  333. drmModeAtomicAddProperty(req, conn->connector_id, p_hdr, blob_b);
  334. else
  335. drmModeAtomicAddProperty(req, conn->connector_id, p_rgb,
  336. cur_rgb == 1 ? 2 : 1);
  337.  
  338. ret = do_commit(fd, req, DRM_MODE_ATOMIC_TEST_ONLY,
  339. "TEST_ONLY (proves it is not a modeset)");
  340. if (ret) {
  341. fprintf(stderr,
  342. "\ncheck rejected it -- the kernel wants a modeset here,\n"
  343. "so this path does not reach the streamless loop.\n");
  344. drmModeAtomicFree(req);
  345. unblank(fd, crtc_id, p_active);
  346. return 1;
  347. }
  348.  
  349. if (dry_run) {
  350. printf("\ndry run: stopping before the commit that oopses.\n");
  351. drmModeAtomicFree(req);
  352. unblank(fd, crtc_id, p_active);
  353. return 0;
  354. }
  355.  
  356. printf("\n*** committing for real - expect an oops ***\n");
  357. sleep(1);
  358. do_commit(fd, req, 0, "real commit");
  359. drmModeAtomicFree(req);
  360.  
  361. printf("\nno crash: this kernel already has the fix.\n");
  362. unblank(fd, crtc_id, p_active);
  363. return 0;
  364. }
  365.  
Advertisement
Add Comment
Please, Sign In to add comment