From: Rodrigo Siqueira Subject: Re: [PATCH 3/3] drm/vkms: Reduce critical section in vblank_simulate Date: Tue, 3 Sep 2019 08:50:29 -0400 Message-ID: <20190903125029.6jctvqvwoq2yzyka@smtp.gmail.com> References: <20190719152314.7706-1-daniel.vetter@ffwll.ch> <20190719152314.7706-3-daniel.vetter@ffwll.ch> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0879870989==" Return-path: In-Reply-To: <20190719152314.7706-3-daniel.vetter@ffwll.ch> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" To: Daniel Vetter Cc: Daniel Vetter , Intel Graphics Development , DRI Development , Haneen Mohammed List-Id: dri-devel@lists.freedesktop.org --===============0879870989== Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="yarmh4qjkc74q7rn" Content-Disposition: inline --yarmh4qjkc74q7rn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Thanks for this patch! It looks good for me. Reviewed-by: Rodrigo Siqueira On 07/19, Daniel Vetter wrote: > We can reduce the critical section in vkms_vblank_simulate under > output->lock quite a lot: >=20 > - hrtimer_forward_now just needs to be ordered correctly wrt > drm_crtc_handle_vblank. We already access the hrtimer timestamp > without locks. While auditing that I noticed that we don't correctly > annotate the read there, so sprinkle a READ_ONCE to make sure the > compiler doesn't do anything foolish. >=20 > - drm_crtc_handle_vblank must stay under the lock to avoid races with > drm_crtc_arm_vblank_event. >=20 > - The access to vkms_ouptut->crc_state also must stay under the lock. >=20 > - next problem is making sure the output->state structure doesn't get > freed too early. First we rely on a given hrtimer being serialized: > If we call drm_crtc_handle_vblank, then we are guaranteed that the > previous call to vkms_vblank_simulate has completed. The other side > of the coin is that the atomic updates waits for the vblank to > happen before it releases the old state. Both taken together means > that by the time the atomic update releases the old state, the > hrtimer won't access it anymore (it might be accessing the new state > at the same time, but that's ok). >=20 > - state is invariant, except the few fields separate protected by > state->crc_lock. So no need to hold the lock for that. >=20 > - finally the queue_work. We need to make sure there's no races with > the flush_work, i.e. when we call flush_work we need to guarantee > that the hrtimer can't requeue the work again. This is guaranteed by > the same vblank/hrtimer ordering guarantees like the reasoning above > why state won't be freed too early: flush_work on the old state is > called after wait_for_flip_done in the atomic commit code. >=20 > Therefore we can also move everything after the output->crc_state out > of the critical section. >=20 > Motivated by suggestions from Rodrigo. >=20 > Signed-off-by: Daniel Vetter > Cc: Rodrigo Siqueira > Cc: Haneen Mohammed > Cc: Daniel Vetter > --- > drivers/gpu/drm/vkms/vkms_crtc.c | 9 ++++----- > 1 file changed, 4 insertions(+), 5 deletions(-) >=20 > diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms= _crtc.c > index 927dafaebc76..74f703b8d22a 100644 > --- a/drivers/gpu/drm/vkms/vkms_crtc.c > +++ b/drivers/gpu/drm/vkms/vkms_crtc.c > @@ -16,17 +16,18 @@ static enum hrtimer_restart vkms_vblank_simulate(stru= ct hrtimer *timer) > u64 ret_overrun; > bool ret; > =20 > - spin_lock(&output->lock); > - > ret_overrun =3D hrtimer_forward_now(&output->vblank_hrtimer, > output->period_ns); > WARN_ON(ret_overrun !=3D 1); > =20 > + spin_lock(&output->lock); > ret =3D drm_crtc_handle_vblank(crtc); > if (!ret) > DRM_ERROR("vkms failure on handling vblank"); > =20 > state =3D output->composer_state; > + spin_unlock(&output->lock); > + > if (state && output->composer_enabled) { > u64 frame =3D drm_crtc_accurate_vblank_count(crtc); > =20 > @@ -48,8 +49,6 @@ static enum hrtimer_restart vkms_vblank_simulate(struct= hrtimer *timer) > DRM_DEBUG_DRIVER("Composer worker already queued\n"); > } > =20 > - spin_unlock(&output->lock); > - > return HRTIMER_RESTART; > } > =20 > @@ -85,7 +84,7 @@ bool vkms_get_vblank_timestamp(struct drm_device *dev, = unsigned int pipe, > struct vkms_output *output =3D &vkmsdev->output; > struct drm_vblank_crtc *vblank =3D &dev->vblank[pipe]; > =20 > - *vblank_time =3D output->vblank_hrtimer.node.expires; > + *vblank_time =3D READ_ONCE(output->vblank_hrtimer.node.expires); > =20 > if (WARN_ON(*vblank_time =3D=3D vblank->time)) > return true; > --=20 > 2.22.0 >=20 --=20 Rodrigo Siqueira Software Engineer, Advanced Micro Devices (AMD) https://siqueira.tech --yarmh4qjkc74q7rn Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEE4tZ+ii1mjMCMQbfkWJzP/comvP8FAl1uYZQACgkQWJzP/com vP9+hA/+KlJu2cW+Qi84gCDTbQhtlHO2k0hRdE6YTPytQy6qVaUH+Cmz7H+2RGJH QT7lgoeCg8WqyBJrCEYRQWYZtm4mBHml+GQ2ZlkIA5L43vjlWiOFhH29l8+T+/ML rHTJ9VCRPWXXp3z4qBLb+KynMA8Mf46SKMINq9k2SAb/R6HXIENdwAWmvwS4sluS 0kxCP30/BwcgbMvL+s7+K/pS34/mzug5lZOkkS2LwZW8md2dMKrt1zIfEgCLGqDD ncLxx/scKDR3LTvwsye+YgnVZN2TmUnt7pkyy+GSBgoncZ5VbgGIinKfj9WNF2GB 9rPHyjJIAfivucDdIm6BVV8V1ONjtiEYOgFzJ1gDmajqPnwYUi1i4qTWPgAxNn5B +5mSj1Wh2vBom6569Jn0/dj+K4OM5wn5YW2x41/t1Zc/7DtLRj2rjm5FURPALRWu 3qu+zOxQUro7g/FYKbP0DA5kXC/x5ulh+Rg7Ug8emCDNNV7onyG2gFeBuJVHCPyr dc+hrxjkgjCV82Hgv3pp3gUkAt8Xn7BbNVeK0XCMj18cerz5jhE7hNj0+rqBbP/P 163+bK0fGkUCcTsmuF/rL1gQBWP/bX+WLjtQkCXE2dr8pU0EZp1U0npG65nkg2DA OAYZ23kWEc1nSdzIz9jF13zlJobDI7PhwjcfQ99V4aiAc5MfTgo= =HLgx -----END PGP SIGNATURE----- --yarmh4qjkc74q7rn-- --===============0879870989== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KSW50ZWwtZ2Z4 IG1haWxpbmcgbGlzdApJbnRlbC1nZnhAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHBzOi8vbGlz dHMuZnJlZWRlc2t0b3Aub3JnL21haWxtYW4vbGlzdGluZm8vaW50ZWwtZ2Z4 --===============0879870989==--