From patchwork Fri Dec 9 14:19:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 9468329 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 491FA60586 for ; Fri, 9 Dec 2016 14:20:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3BE2828619 for ; Fri, 9 Dec 2016 14:20:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2F92C2861F; Fri, 9 Dec 2016 14:20:29 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=unavailable 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 CB18528619 for ; Fri, 9 Dec 2016 14:20:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 17BE96E9F1; Fri, 9 Dec 2016 14:19:53 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x242.google.com (mail-wm0-x242.google.com [IPv6:2a00:1450:400c:c09::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id 144986E9F7 for ; Fri, 9 Dec 2016 14:19:46 +0000 (UTC) Received: by mail-wm0-x242.google.com with SMTP id a20so4313505wme.2 for ; Fri, 09 Dec 2016 06:19:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8YSfL1eLQpa9nkNNNAp72tMs5w8pKm21zWco7gSp4OY=; b=kSRWANuhfuzbW3JDOYpi7imZn6izH3/4QSSVDIj58k9GOZufMAmWqn76Lm5doghEHf k87YDcZNrAU5mvDAnZ4MXnEEhGk5ttVFp0rBwnZ4Qdz67jgd1p/sPE9D5dZvp1VaVmot NW3zwrfpk2RvQ0PGK2NbBpeuDDkY+VQOi4OVM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8YSfL1eLQpa9nkNNNAp72tMs5w8pKm21zWco7gSp4OY=; b=PmloD/aG5YQ/jEtY5DS2sE6tDkrYfGHMy23gM9N/tOHsN8zsKWwJwDZ5ZXQ4FSuAmM 2rbwIYVdprs0MCAd4n1Drl6wSqFynXnCpQtJfZyXwZ6drQi4HCKS6USDt4VmdVx3WswN O7icuA10X9fZxMflV+a+BUIzFveVbq53me7W6yWC5krbG86QeJJ+pPjjcSl1LtXxQc3Q l95hGQn5pYfvdeKyz+4falJ6debGGBSWvOfzyjPAFGH1eLL8Je7Za+LzBQOW3Dn0As/u lt4uZnlgwJdCng11uReuplV28h3TDcx93bEXjJSGr20O1NgUp4sEk2PnZx6T5qxcPefz eDqg== X-Gm-Message-State: AKaTC03JGe7rjuxfP9E455ZvuN5StpIhOCNuGIGF2NJV0fy6yg6rwwI61R+Rhksq7YhmRA== X-Received: by 10.28.105.81 with SMTP id e78mr7516258wmc.140.1481293183801; Fri, 09 Dec 2016 06:19:43 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:56b5:0:ac27:b86c:7764:9429]) by smtp.gmail.com with ESMTPSA id y4sm42573752wjp.0.2016.12.09.06.19.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Dec 2016 06:19:43 -0800 (PST) From: Daniel Vetter To: DRI Development Subject: [PATCH 5/7] drm: Don't compute obj counts expensively in get_resources Date: Fri, 9 Dec 2016 15:19:42 +0100 Message-Id: <20161209141944.22121-5-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161209141944.22121-1-daniel.vetter@ffwll.ch> References: <20161209141944.22121-1-daniel.vetter@ffwll.ch> Cc: Daniel Vetter , Intel Graphics Development X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Looping when we keep track of this is silly. Only thing we have to be careful is with sampling the connector count. To avoid inconsisten results due to gcc re-computing this, use READ_ONCE. And to avoid surprising userspace, make sure we don't copy more connectors than planned, and report the actual number of connectors copied. That way any racing hot-add/remove will be handled. v2: Actually try to not blow up, somehow I lost the hunk that checks we don't copy too much. Noticed by Chris. Cc: Chris Wilson Signed-off-by: Daniel Vetter --- drivers/gpu/drm/drm_mode_config.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/drm_mode_config.c b/drivers/gpu/drm/drm_mode_config.c index 2735a5847ffa..64c2971478db 100644 --- a/drivers/gpu/drm/drm_mode_config.c +++ b/drivers/gpu/drm/drm_mode_config.c @@ -90,10 +90,10 @@ int drm_mode_getresources(struct drm_device *dev, void *data, struct drm_crtc *crtc; struct drm_encoder *encoder; int ret = 0; - int connector_count = 0; - int crtc_count = 0; + int connector_count = READ_ONCE(dev->mode_config.num_connector); + int crtc_count = dev->mode_config.num_crtc; int fb_count = 0; - int encoder_count = 0; + int encoder_count = dev->mode_config.num_encoder; int copied = 0; uint32_t __user *fb_id; uint32_t __user *crtc_id; @@ -131,15 +131,6 @@ int drm_mode_getresources(struct drm_device *dev, void *data, /* mode_config.mutex protects the connector list against e.g. DP MST * connector hot-adding. CRTC/Plane lists are invariant. */ mutex_lock(&dev->mode_config.mutex); - drm_for_each_crtc(crtc, dev) - crtc_count++; - - drm_for_each_connector(connector, dev) - connector_count++; - - drm_for_each_encoder(encoder, dev) - encoder_count++; - card_res->max_height = dev->mode_config.max_height; card_res->min_height = dev->mode_config.min_height; card_res->max_width = dev->mode_config.max_width; @@ -179,6 +170,9 @@ int drm_mode_getresources(struct drm_device *dev, void *data, copied = 0; connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr; drm_for_each_connector(connector, dev) { + if (copied >= connector_count) + break; + if (put_user(connector->base.id, connector_id + copied)) { ret = -EFAULT; @@ -186,6 +180,7 @@ int drm_mode_getresources(struct drm_device *dev, void *data, } copied++; } + connector_count = copied; } card_res->count_connectors = connector_count;