From patchwork Thu Aug 23 23:16:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haneen Mohammed X-Patchwork-Id: 10574845 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 247991579 for ; Fri, 24 Aug 2018 07:10:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F2742C472 for ; Fri, 24 Aug 2018 07:10:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 03B2D2C477; Fri, 24 Aug 2018 07:10:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1C7832C472 for ; Fri, 24 Aug 2018 07:09:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5EB056E62D; Fri, 24 Aug 2018 07:09:42 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-qk0-x241.google.com (mail-qk0-x241.google.com [IPv6:2607:f8b0:400d:c09::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id E52756E5E3 for ; Thu, 23 Aug 2018 23:16:38 +0000 (UTC) Received: by mail-qk0-x241.google.com with SMTP id 93-v6so4784215qks.3 for ; Thu, 23 Aug 2018 16:16:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=RZYI+VzXUTPym0dCU+AiSClgEeLlJl3r9kABpHpJhvM=; b=ksOGu2ygHxroB4Ks0hijWcSEg6s8Xpf2MfowfYCA1MfU9oCkpnRWO8xWD4MDP+kSop 4GuUcQNZtSO29sA1u+Jzf+qj0KbYixjuHzXB+FbO3s55aXrqnbWB5teJV3inpexXqavR 54sz2UyRIl1R++yIRab6TZPe4JTP0zE3Y7epKzGPKrWjGl5KKzXdGawkM0zLHbhEvU2L FOtbbq4xTwELik2DEVrBQrEgfZa0MaEhLyKtsHYDY+G1uNT00aAy5kRyoOxn9SdgjxeB YIhfjONorg+kq87VmGNTJCM3a8se/Bm/RrUofNl+GXTOIsWn2YtIvIK+PVLCOYfkQIra wg5Q== X-Gm-Message-State: AOUpUlEjAeHuBR8lO0qk73mwSrxNbLaMgyEBs3kvIG9kvpB/SbuLnJFV UNaOo6UcwKwzb3rzmafPoQkyGJao X-Google-Smtp-Source: AA+uWPyEih/z8PV7u2z32bZKDM0Tizm8Dh7RObGa7ju0Pah3fRvj1JClmdazmrH1DxXcsHEzF9E7vA== X-Received: by 2002:a37:95c4:: with SMTP id x187-v6mr59189774qkd.195.1535066197787; Thu, 23 Aug 2018 16:16:37 -0700 (PDT) Received: from haneenDRM (dyn-160-39-42-212.dyn.columbia.edu. [160.39.42.212]) by smtp.gmail.com with ESMTPSA id v129-v6sm3221446qkd.86.2018.08.23.16.16.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 Aug 2018 16:16:37 -0700 (PDT) Date: Fri, 24 Aug 2018 02:16:34 +0300 From: Haneen Mohammed To: dri-devel@lists.freedesktop.org Subject: [PATCH] drm/vkms: Fix race condition around accessing frame number Message-ID: <20180823231634.GA17011@haneenDRM> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Mailman-Approved-At: Fri, 24 Aug 2018 07:09:20 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: seanpaul@chromium.org, rodrigosiqueiramelo@gmail.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP crtc_state is accessed by both vblank_handle() and the ordered work_struct handle vkms_crc_work_handle() to retrieve and or update the frame number for computed CRC. Since work_struct can fail, add frame_end to account for missing frame numbers. use atomic_t witth appropriate flags for synchronization between hrtimer callback and ordered work_struct handle since spinlock can't be used with work_struct handle and mutex can't be used with hrtimer callback. This patch passes the following subtests from igt kms_pipe_crc_basic test: bad-source, read-crc-pipe-A, read-crc-pipe-A-frame-sequence, nonblocking-crc-pipe-A, nonblocking-crc-pipe-A-frame-sequence Signed-off-by: Haneen Mohammed --- drivers/gpu/drm/vkms/vkms_crc.c | 33 ++++++++++++++++++++++++++++++-- drivers/gpu/drm/vkms/vkms_crtc.c | 13 +++++++++++-- drivers/gpu/drm/vkms/vkms_drv.h | 6 ++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/vkms/vkms_crc.c b/drivers/gpu/drm/vkms/vkms_crc.c index ed47d67cecd6..4a1ba5b7886a 100644 --- a/drivers/gpu/drm/vkms/vkms_crc.c +++ b/drivers/gpu/drm/vkms/vkms_crc.c @@ -34,6 +34,15 @@ static uint32_t _vkms_get_crc(struct vkms_crc_data *crc_data) return crc; } +/** + * vkms_crc_work_handle - ordered work_struct to compute CRC + * + * @work: work_struct + * + * Work handler for computing CRCs. work_struct scheduled in + * an ordered workqueue that's periodically scheduled to run by + * _vblank_handle() and flushed at vkms_atomic_crtc_destroy_state(). + */ void vkms_crc_work_handle(struct work_struct *work) { struct vkms_crtc_state *crtc_state = container_of(work, @@ -45,8 +54,18 @@ void vkms_crc_work_handle(struct work_struct *work) output); struct vkms_crc_data *primary_crc = NULL; struct drm_plane *plane; - u32 crc32 = 0; + u32 frame_start, frame_end; + + frame_start = atomic_read(&crtc_state->frame_start); + frame_end = atomic_read(&crtc_state->frame_end); + /* _vblank_handle() hasn't updated frame_start yet */ + if (!frame_start) { + return; + } else if (frame_start == frame_end) { + atomic_set(&crtc_state->frame_start, 0); + return; + } drm_for_each_plane(plane, &vdev->drm) { struct vkms_plane_state *vplane_state; @@ -67,7 +86,17 @@ void vkms_crc_work_handle(struct work_struct *work) if (primary_crc) crc32 = _vkms_get_crc(primary_crc); - drm_crtc_add_crc_entry(crtc, true, crtc_state->n_frame, &crc32); + frame_end = drm_crtc_accurate_vblank_count(crtc); + + /* queue_work can fail to schedule crc_work; add crc for + * missing frames + */ + while (frame_start <= frame_end) + drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32); + + /* to avoid using the same value again */ + atomic_set(&crtc_state->frame_end, frame_end); + atomic_set(&crtc_state->frame_start, 0); } static int vkms_crc_parse_source(const char *src_name, bool *enabled) diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c index 9d0b1a325a78..a170677acd46 100644 --- a/drivers/gpu/drm/vkms/vkms_crtc.c +++ b/drivers/gpu/drm/vkms/vkms_crtc.c @@ -22,8 +22,17 @@ static void _vblank_handle(struct vkms_output *output) DRM_ERROR("vkms failure on handling vblank"); if (state && output->crc_enabled) { - state->n_frame = drm_crtc_accurate_vblank_count(crtc); - queue_work(output->crc_workq, &state->crc_work); + u32 frame = drm_crtc_accurate_vblank_count(crtc); + + /* update frame_start only if a queued vkms_crc_work_handle has + * read the data + */ + if (!atomic_read(&state->frame_start)) + atomic_set(&state->frame_start, frame); + + ret = queue_work(output->crc_workq, &state->crc_work); + if (!ret) + DRM_WARN("failed to queue vkms_crc_work_handle"); } spin_unlock(&output->lock); diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h index 2017a2ccc43d..4a3956a0549e 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.h +++ b/drivers/gpu/drm/vkms/vkms_drv.h @@ -39,12 +39,14 @@ struct vkms_plane_state { * vkms_crtc_state - Driver specific CRTC state * @base: base CRTC state * @crc_work: work struct to compute and add CRC entries - * @n_frame: frame number for computed CRC + * @n_frame_start: start frame number for computed CRC + * @n_frame_end: end frame number for computed CRC */ struct vkms_crtc_state { struct drm_crtc_state base; struct work_struct crc_work; - unsigned int n_frame; + atomic_t frame_start; + atomic_t frame_end; }; struct vkms_output {