Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2872749imm; Fri, 24 Aug 2018 06:52:12 -0700 (PDT) X-Google-Smtp-Source: ANB0VdavIb8mGbJFbVQo344CfWl/cHhxblohmYkgOOjw5QkDA2dUb56vyd9LwpR+NWhIS+C6d9nF X-Received: by 2002:a17:902:8d91:: with SMTP id v17-v6mr1855851plo.9.1535118732881; Fri, 24 Aug 2018 06:52:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535118732; cv=none; d=google.com; s=arc-20160816; b=QBupLXKVUSOJJMP5skxxNbV0nwUAkI1AydByOz3eGfMTqzlN35ZYHjya2R+cAhX4qr 2ifogwzgGSMA8Icz6jADh86jEVs0TP24JU7h6RVvzrHZDWPF7E0R1TexXs/wVCX+RqPK roOqPBd/pSI+9sJaSU4+/+47yaQ+MT/mLcHOWFtV3Xmgmi9qM4JMt6nBkXK2+6ZXIwVU mEg3mGadFJ5bjRi/Kx7LQ+72BHCspX2olzC8toqS5BmhRMNmZA5oVZyv7zVP0sUwDlb5 cjc64KamzwMWc9Rw9wfCUmm5XryC7lhr+aujD6JJejIZKKWBi6QMdffs4RsrbimnNbd5 VGDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :organization:in-reply-to:subject:cc:to:from :arc-authentication-results; bh=IN6i5tgTcZ/50/AYINzGxV3ZOUAuYZgUUyQhIX0CRQM=; b=lUaDpfTehrIIBne/5oZN3ECXuHWm23mMsWMwff+G8V2/ymKUg55jomKscMXLJTaS+1 f4dE/SREF8gUihdCZ8rvh9sLLtqgU1eFzhWXuXGhDarP8kbJNq0js3lh1l7YHGbVQM26 tbBrJ3Rg3xVhf9Ku5HHuR8kTmNM4lHk0dyLleCLi1Fo6Q0ZEZqIiv46Lj+wewiOrwFo5 DEaEg2Ojj299j3Di4IpwQBdIRqrTC/lcjAEV6+NaJ9dnnf60nm1SXShG2UAS2hAMqgYu heprb/3OnCo8LPS3TpGR7fTPo+jy6cLKQkJwmceuq2cE4XXCJOpoWMNCyy8XP84XhT36 YtOQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q3-v6si4535937plb.102.2018.08.24.06.51.57; Fri, 24 Aug 2018 06:52:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727957AbeHXRZS (ORCPT + 99 others); Fri, 24 Aug 2018 13:25:18 -0400 Received: from mga07.intel.com ([134.134.136.100]:59407 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727099AbeHXRZS (ORCPT ); Fri, 24 Aug 2018 13:25:18 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Aug 2018 06:50:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,281,1531810800"; d="scan'208";a="84189414" Received: from bpeschka-mobl.amr.corp.intel.com (HELO localhost) ([10.252.24.83]) by fmsmga001.fm.intel.com with ESMTP; 24 Aug 2018 06:50:08 -0700 From: Jani Nikula To: Satendra Singh Thakur , Gustavo Padovan , Maarten Lankhorst , Sean Paul , David Airlie , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Cc: hemanshu.s@samsung.com, vineet.j@samsung.com, sst2005@gmail.com, Satendra Singh Thakur Subject: Re: [PATCH] drm/kms/crtc: Improving the func drm_mode_setcrtc In-Reply-To: <20180727101302epcas5p22561edd1d60101e611933ccff3e3d008~FMiUBZnCN2101321013epcas5p2Z@epcas5p2.samsung.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo References: <20180727101302epcas5p22561edd1d60101e611933ccff3e3d008~FMiUBZnCN2101321013epcas5p2Z@epcas5p2.samsung.com> Date: Fri, 24 Aug 2018 16:50:35 +0300 Message-ID: <87wosflvpw.fsf@intel.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 27 Jul 2018, Satendra Singh Thakur wrote: > Following changes are done to this func: Just a drive-by observation, if your commit message lists a number of changes, with "also", it's usually an indication they should be separate patches. BR, Jani. > 1. Currently there are many redundant error checks for > count_connectors, mode, fb and mode_valid. > if (crtc_req->mode_valid) > if (crtc_req->count_connectors == 0 && mode) > if (crtc_req->count_connectors > 0 && (!mode || !fb)) > if (crtc_req->count_connectors > 0) > if (crtc_req->count_connectors > config->num_connector) > > These 5 checks are replaced by just 2 checks. > if (!crtc_req->mode_valid) > if (!crtc_req->count_connectors || > crtc_req->count_connectors > config->num_connector) > > 2. Also, currently, if user pass invalid args > count_connectors=0, mode=NULL, fb=NULL, code wont throw > any errors and eventually __drm_mode_set_config_internal > will be called. > With the modified code, this will be taken care. > > 3. Also, these error checks alongwith following > if (!file_priv->aspect_ratio_allowed && > (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) > > has been moved before taking mutex and modeset lock > because they don't need any lock. Moreover, after grabbing locks, > if its found that user supplied invalid args, it will > be too late as getting lock may require considerable time. > > 4. Also, if modeset_lock is tried many times in case of EDEADLK > error, then this will be the code flow > > retry: > ret = drm_modeset_lock_all_ctx(crtc->dev, &ctx); > > if (ret)-->this is true > goto out; > > out: > if (fb) > if (connector_set) > drm_mode_destroy(dev, mode); > if (ret == -EDEADLK) > goto retry; > It can be observed that checks on fb, connector_set and call to > mode_destroy are redundant in retry-case. > If we keep if (ret == -EDEADLK) just after out:, > that will avoid redundant checks. > > In the normal case (non-retry), all checks will be required. > Thus shifting of if (ret == -EDEADLK) right after out label > won't affect normal case. > > 5. Also, kfree is moved inside if (connector_set). > 6. Also, if major error checks are in the beginning of the func > and if user supplied invalid params, we will exit the func sooner > without wasting much effort in finding crtc and framebuffer etc. > > Signed-off-by: Satendra Singh Thakur > --- > drivers/gpu/drm/drm_crtc.c | 207 +++++++++++++++++++++------------------------ > 1 file changed, 98 insertions(+), 109 deletions(-) > > diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c > index 98a36e6..15927e7 100644 > --- a/drivers/gpu/drm/drm_crtc.c > +++ b/drivers/gpu/drm/drm_crtc.c > @@ -578,7 +578,25 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data, > */ > if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000) > return -ERANGE; > - > + if (!file_priv->aspect_ratio_allowed && > + (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != > + DRM_MODE_FLAG_PIC_AR_NONE) { > + DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n"); > + return -EINVAL; > + } > + /* Check if the flag is set*/ > + if (!crtc_req->mode_valid) { > + DRM_DEBUG_KMS("mode_valid flag [%d] is not set\n", > + crtc_req->mode_valid); > + return -EINVAL; > + } > + /* Check the validity of count_connectors supplied by user */ > + if (!crtc_req->count_connectors || > + crtc_req->count_connectors > config->num_connector) { > + DRM_DEBUG_KMS("Invalid connectors' count %d\n", > + crtc_req->count_connectors); > + return -EINVAL; > + } > crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id); > if (!crtc) { > DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id); > @@ -595,134 +613,105 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data, > if (ret) > goto out; > > - if (crtc_req->mode_valid) { > - /* If we have a mode we need a framebuffer. */ > - /* If we pass -1, set the mode with the currently bound fb */ > - if (crtc_req->fb_id == -1) { > - struct drm_framebuffer *old_fb; > + /* If we have a mode we need a framebuffer. */ > + /* If we pass -1, set the mode with the currently bound fb */ > + if (crtc_req->fb_id == -1) { > + struct drm_framebuffer *old_fb; > > - if (plane->state) > - old_fb = plane->state->fb; > - else > - old_fb = plane->fb; > + if (plane->state) > + old_fb = plane->state->fb; > + else > + old_fb = plane->fb; > > - if (!old_fb) { > - DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); > - ret = -EINVAL; > - goto out; > - } > - > - fb = old_fb; > - /* Make refcounting symmetric with the lookup path. */ > - drm_framebuffer_get(fb); > - } else { > - fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id); > - if (!fb) { > - DRM_DEBUG_KMS("Unknown FB ID%d\n", > - crtc_req->fb_id); > - ret = -ENOENT; > - goto out; > - } > - } > - > - mode = drm_mode_create(dev); > - if (!mode) { > - ret = -ENOMEM; > - goto out; > - } > - if (!file_priv->aspect_ratio_allowed && > - (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) { > - DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n"); > + if (!old_fb) { > + DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); > ret = -EINVAL; > goto out; > } > > - > - ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode); > - if (ret) { > - DRM_DEBUG_KMS("Invalid mode\n"); > + fb = old_fb; > + /* Make refcounting symmetric with the lookup path. */ > + drm_framebuffer_get(fb); > + } else { > + fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id); > + if (!fb) { > + DRM_DEBUG_KMS("Unknown FB ID%d\n", > + crtc_req->fb_id); > + ret = -ENOENT; > goto out; > } > - > - /* > - * Check whether the primary plane supports the fb pixel format. > - * Drivers not implementing the universal planes API use a > - * default formats list provided by the DRM core which doesn't > - * match real hardware capabilities. Skip the check in that > - * case. > - */ > - if (!plane->format_default) { > - ret = drm_plane_check_pixel_format(plane, > - fb->format->format, > - fb->modifier); > - if (ret) { > - struct drm_format_name_buf format_name; > - DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n", > - drm_get_format_name(fb->format->format, > - &format_name), > - fb->modifier); > - goto out; > - } > - } > - > - ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y, > - mode, fb); > - if (ret) > - goto out; > - > } > > - if (crtc_req->count_connectors == 0 && mode) { > - DRM_DEBUG_KMS("Count connectors is 0 but mode set\n"); > - ret = -EINVAL; > + mode = drm_mode_create(dev); > + if (!mode) { > + ret = -ENOMEM; > goto out; > } > > - if (crtc_req->count_connectors > 0 && (!mode || !fb)) { > - DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n", > - crtc_req->count_connectors); > - ret = -EINVAL; > + ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode); > + if (ret) { > + DRM_DEBUG_KMS("Invalid mode\n"); > goto out; > } > > - if (crtc_req->count_connectors > 0) { > - u32 out_id; > + /* > + * Check whether the primary plane supports the fb pixel format. > + * Drivers not implementing the universal planes API use a > + * default formats list provided by the DRM core which doesn't > + * match real hardware capabilities. Skip the check in that > + * case. > + */ > + if (!plane->format_default) { > + ret = drm_plane_check_pixel_format(plane, > + fb->format->format, > + fb->modifier); > + if (ret) { > + struct drm_format_name_buf format_name; > > - /* Avoid unbounded kernel memory allocation */ > - if (crtc_req->count_connectors > config->num_connector) { > - ret = -EINVAL; > + DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n", > + drm_get_format_name(fb->format->format, > + &format_name), > + fb->modifier); > goto out; > } > + } > > - connector_set = kmalloc_array(crtc_req->count_connectors, > + ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y, > + mode, fb); > + if (ret) > + goto out; > + > + connector_set = kmalloc_array(crtc_req->count_connectors, > sizeof(struct drm_connector *), > GFP_KERNEL); > - if (!connector_set) { > - ret = -ENOMEM; > - goto out; > - } > + if (!connector_set) { > + ret = -ENOMEM; > + goto out; > + } > > - for (i = 0; i < crtc_req->count_connectors; i++) { > - connector_set[i] = NULL; > - set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; > - if (get_user(out_id, &set_connectors_ptr[i])) { > - ret = -EFAULT; > - goto out; > - } > + for (i = 0; i < crtc_req->count_connectors; i++) { > + u32 out_id; > > - connector = drm_connector_lookup(dev, file_priv, out_id); > - if (!connector) { > - DRM_DEBUG_KMS("Connector id %d unknown\n", > - out_id); > - ret = -ENOENT; > - goto out; > - } > - DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", > - connector->base.id, > - connector->name); > + connector_set[i] = NULL; > + set_connectors_ptr = > + (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; > + if (get_user(out_id, &set_connectors_ptr[i])) { > + ret = -EFAULT; > + goto out; > + } > > - connector_set[i] = connector; > + connector = drm_connector_lookup(dev, file_priv, out_id); > + if (!connector) { > + DRM_DEBUG_KMS("Connector id %d unknown\n", > + out_id); > + ret = -ENOENT; > + goto out; > } > + DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", > + connector->base.id, > + connector->name); > + > + connector_set[i] = connector; > } > > set.crtc = crtc; > @@ -735,6 +724,11 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data, > ret = __drm_mode_set_config_internal(&set, &ctx); > > out: > + if (ret == -EDEADLK) { > + ret = drm_modeset_backoff(&ctx); > + if (!ret) > + goto retry; > + } > if (fb) > drm_framebuffer_put(fb); > > @@ -743,14 +737,9 @@ int drm_mode_setcrtc(struct drm_device *dev, void *data, > if (connector_set[i]) > drm_connector_put(connector_set[i]); > } > + kfree(connector_set); > } > - kfree(connector_set); > drm_mode_destroy(dev, mode); > - if (ret == -EDEADLK) { > - ret = drm_modeset_backoff(&ctx); > - if (!ret) > - goto retry; > - } > drm_modeset_drop_locks(&ctx); > drm_modeset_acquire_fini(&ctx); > mutex_unlock(&crtc->dev->mode_config.mutex); -- Jani Nikula, Intel Open Source Graphics Center