Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix driver probing, DRI3 and Xwayland support #614

Merged
merged 3 commits into from Sep 27, 2022

Conversation

evelikov
Copy link
Contributor

@evelikov evelikov commented Jul 7, 2022

This MR fixes some pre-existing bugs in the libva (backend) driver handling and adds DRI3 support.

In other words:

  • the correct module is picked on radeon (drm) + radeonsi (mesa/va) drivers
  • the LIBVA_DRIVER_NAME override works correctly pulled out for now
  • the render-node is properly detected, in some corner case
  • DRI3 Xserver and Xwayland works.

Note: Only DRI3 open/fd retrieval is added enough for GetDisplay. All the winsys part is deliberately omitted, since it is heavily driver specific and makes little sense to add here.

In practise the x11/wayland winsys code in libva-{x11,wayland} has never been appealing enough to be used in the Mesa VA drivers. If i965 (legacy intel-driver) or iHD (new intel-media-driver) are interested they can adapt it to their needs.

@evelikov
Copy link
Contributor Author

evelikov commented Jul 8, 2022

Didn't realise the autotools build was still used. Squashed a fix in the respective commit.

@rmader
Copy link

rmader commented Jul 8, 2022

This closes #79 and #122, right?

@rmader rmader mentioned this pull request Jul 8, 2022
@evelikov
Copy link
Contributor Author

evelikov commented Jul 8, 2022

This closes #79 and #122, right?

Indeed it does. Thank you for tracking these down o/

@evelikov
Copy link
Contributor Author

Pushed a minor fix (were printing null instead of the env/api override string).

For testing I've used mpv --hwdec=vaapi and vainfo, against the radeonsi and i965 vaapi drivers. To confirm the correct display is used with I've used intel/libva-utils#278 and/or explicitly setting vainfo --display foo

  • drm session - switch to VT, kick off mpv
  • X11 session - plasma/x11 + local debug for the DRI3 codepath
  • wayland - switch to VT, start weston
  • wayland+xwayland - switch to VT, configure printf "[core]\nxwayland=true" >.~/config/weston.ini, start weston and unset WAYLAND_DISPLAY before using mpv
  • nested weston (with and w/o Xwayland) in a X11 session

For all the various readers, please give this a test. Let me know if it doesn't work or thumbs up if it does.
Thanks o/

@evelikov
Copy link
Contributor Author

@XinfengZhang @uartie humble ping?

@evelikov
Copy link
Contributor Author

Fixing the LIBVA_DRIVER_NAME high-lighted a pre-existing issue, so Ive pulled it out for now. Will follow-up at later stage addressing that.

For now the MR is good to land.

/*
* Copyright © 2022 Collabora Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would propose to use SPDX instead of whole license text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT no SPDX notations are used in the project. So while I'm in favour (esp with it's an ISO standard), it's something better handled a) as follow-up and b) across the whole project.

@okias
Copy link

okias commented Aug 2, 2022

Acked-by: David Heidelberg <david.heidelberg@collabora.com>

reply = xcb_dri3_open_reply(conn, cookie, NULL /* error */);

if (!reply || reply->nfd != 1) {
free(reply);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will crash if reply is NULL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? The !reply will be evaluated and the second comparison will be skipped.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, reply->nfd will be skipped, but !reply will be true and it will try to execute free(NULL). that's where it will crash. Though it depends on free() implementation, for what I remember it will crash.

I suggest code should be:

    if (!reply) {
        return -1;
    }
    if (reply->nfd != 1) {
        free(reply);
        return -1;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free(NULL) is safe for any compliant implementations. In particular Xorg, Mesa and Wayland (to name a few) depend on this behaviour. Adding a workaround here provides a disservice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick look the project already depends on well-behaved C runtime - see va/drm/va_drm.c for example.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, to dispel any doubts you may possibly have, here's the exact quote from C99 standard, paragraph 7.20.3.2 The free function (emphasis is mine):

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hi-Angel do you mind sending a mesa patch for fold the if checks? Alternatively I'll get to it tomorrow. Using it as an argument is clutching at straws.

@dvrogozh I appreciate the time review comments - especially the two issues you've spotted.

Although it feels like we've lost track of the bigger picture and we're waist deep into bikeshedding. This is a
feature (or bugfix if you're likely minded) that users have been waiting for over 5 years. Other MRs #612 #613 fix serious bugs, or remove obsolete and irrelevant code #617 or make the codebase more consistent/flexible and future/feature prone #615.

Splitting hairs about if checks before free() isn't the most productive use of our time. There are genuine useful MRs and many more to come that sorely need maintainer. As suggested earlier - if the team is short on time, do consider granting commit access to other active people.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no problem

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hi-Angel do you mind sending a mesa patch for fold the if checks?

Done https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17974

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still did not change my opinion on this item. If function don't need to be called, it should not be called. To me the following code makes much more sense regardless that it takes more space.

    if (!reply)
        return -1;
    if (reply->nfd != 1) {
        free(reply);
        return -1;
    }

As for the other places within libva - we are not fixing everything in this PR. If I would notice that in other PRs I would comment the same. So, that's not a motivation to use the same here.

I will remove my -1 from this review, but I won't +1 it as well.

va/x11/va_dri3.c Show resolved Hide resolved
va/x11/va_dri3.c Show resolved Hide resolved
va/x11/va_dri3.c Outdated Show resolved Hide resolved
@evelikov
Copy link
Contributor Author

evelikov commented Aug 5, 2022

v4 includes:

  • rebase against master
  • beefy comment about the lack of close/fd ownership issue
  • correct node-type switch

@evelikov
Copy link
Contributor Author

evelikov commented Aug 5, 2022

v5: drop bonus fd ownership comment, close() as applicable

reply = xcb_dri3_open_reply(conn, cookie, NULL /* error */);

if (!reply || reply->nfd != 1) {
free(reply);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, reply->nfd will be skipped, but !reply will be true and it will try to execute free(NULL). that's where it will crash. Though it depends on free() implementation, for what I remember it will crash.

I suggest code should be:

    if (!reply) {
        return -1;
    }
    if (reply->nfd != 1) {
        free(reply);
        return -1;
    }

va/x11/va_dri3.c Show resolved Hide resolved
va/x11/va_dri3.c Show resolved Hide resolved
reply = xcb_dri3_open_reply(conn, cookie, NULL /* error */);

if (!reply || reply->nfd != 1) {
free(reply);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a workaround here provides a disservice.

I am sorry, but that's not a workaround. Pointer might be NULL. You have no reason to call free, so don't call it.

va/x11/va_dri3.c Show resolved Hide resolved
va/x11/va_dri3.c Show resolved Hide resolved
Copy link
Contributor

@XinfengZhang XinfengZhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for late response, from the code logic, it will check the connection , and finally get the device node name, then open it and call drm function to get the driver name and candidates , right?
so, it resolve the problem of driver name retrieving on XWayland which only support DRI3 , not support DRI2. but it expose another question about the support of vaPutSurface:
from this logic, if driver still only support DRI2, the vaPutSurface will certainly failed, of course, maybe it could be resolved later (we are recommended to use DMA buffer sharing instead of vaPutSurface)

va/x11/va_dri3.c Outdated Show resolved Hide resolved
va/x11/va_dri3.c Show resolved Hide resolved
@dvrogozh
Copy link
Contributor

the vaPutSurface will certainly failed, of course, maybe it could be resolved later

I think we can do that step-by-step. Authentication goes first. But adding a note for vaPutSurface behavior with DRI3 would be nice.

@evelikov : can you, please, also rebase the code on tip of the master. This will take in some ci adjustments.

@XinfengZhang
Copy link
Contributor

@evelikov : can you, please, also rebase the code on tip of the master. This will take in some ci adjustments.

I rebased it with web operation

@evelikov
Copy link
Contributor Author

evelikov commented Aug 31, 2022

but it expose another question about the support of vaPutSurface: from this logic, if driver still only support DRI2, the vaPutSurface will certainly failed, of course, maybe it could be resolved later (we are recommended to use DMA buffer sharing instead of vaPutSurface)

As mentioned in the summary - I've explicitly omitted vaPutSurface. There are many reasons for that:

  • as you said - people should be using buffer sharing dmabuf or otherwise
  • the exact *implementation details of are driver specific and makes little sense to add here - for example: mesa's VA drivers do not use any of the libva x11-dri2 code, plus already has libva x11-dri3 support
  • I could not find any software that uses vaPutSurface apart from libva-utils
  • vaPutSurface does not work with intel-vaapi-driver on my BDW GPU - oops, is that intentional?
  • vaPurSurface is available only on va-x11 - inconsistent API across targets makes for limited adoption and testing

@XinfengZhang
Copy link
Contributor

there are conflict with #615 , and also need run style_unify script.
have no other concern.

@evelikov
Copy link
Contributor Author

there are conflict with #615 , and also need run style_unify script. have no other concern.

Should be all good now

Copy link
Contributor

@XinfengZhang XinfengZhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM , thanks

@dvrogozh dvrogozh self-requested a review September 21, 2022 17:22
The radeonsi driver can work with both radeon and amdgpu. Add the extra
combo into the mapping table.

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Reuse the upstream drmGetNodeTypeFromFd(), which has been part of libdrm
version 2.4.59 (we require 2.4.60).

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
DRI3 has been a thing for 5+ years, sadly libva never learned how to
query the server for the fd.

Add basic support for that, where we fall-back to the DRM module's drm
module name to driver mappings.

v2: setup the autotools build
v3: explicitly handle all node types in va_isDRI3Connected()
v4: enhance fd ownership comment, flip node-type switch arms.
v5: drop fd ownership comment, close() as applicable
v6: null check after XGetXCBConnection()

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
@evelikov
Copy link
Contributor Author

Humble ping - what can I do to move this forward?

Would be great to have it merged before its 3 month anniversary 😉

@XinfengZhang
Copy link
Contributor

sorry to let you feel any inconvenience , actually, I just tested it, it could pass with vainfo --display=x11 under wayland.
will merge it soon, but it is an important change, prefer to keep it in master branch more time, dont want to cherrypick to 2.16 directly, because we already test 2.16 with media driver and oneVPL several rounds.

@XinfengZhang
Copy link
Contributor

and it will be a part of 2.17 release, is it ok?

@evelikov
Copy link
Contributor Author

evelikov commented Sep 27, 2022

Above all - I fully agree, this is not a something to pick for 2.16 and it should be in master for at least a week before going into a release.

Also there is no inconvenience, although clarity is sorely missing.

In particular, it is fine if you prefer that both maintainers have to review every MR. It's also fine for reviews or merging to take time. But please document that in the CONTRIBUTING (as requested in #622).

For example:
The current project maintainers are X, Y, Z ... If you haven't received any feedback on your PR, within X weeks please @ tag them directly. Currently X (one, two, all) maintainers must approve a PR before it gets merged.

In some cases they will approve PRs, while not merging them due to X reasons. In such cases reply to the PR if it has been left unmerged for X weeks.

Feel free to reuse ^^ substituting X/Y/Z as applicable.

@XinfengZhang XinfengZhang merged commit ef1df02 into intel:master Sep 27, 2022
@XinfengZhang
Copy link
Contributor

thanks, will change the contributing guide, 1 approval from maintainer is mandatory, and no objection from maintainers , merged it

@evelikov evelikov deleted the fix-driver-probing branch September 27, 2022 18:06
@evelikov
Copy link
Contributor Author

@dvrogozh
Copy link
Contributor

Do you mind closing the following PRs and issues

I closed them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants