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: 9468309 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 9116A60586 for ; Fri, 9 Dec 2016 14:19:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83DAA28619 for ; Fri, 9 Dec 2016 14:19:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 78C802861F; Fri, 9 Dec 2016 14:19:57 +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=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 0CDE428619 for ; Fri, 9 Dec 2016 14:19:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A4EC26E9FB; Fri, 9 Dec 2016 14:19:48 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mail-wm0-x244.google.com (mail-wm0-x244.google.com [IPv6:2a00:1450:400c:c09::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 836D16E9F5 for ; Fri, 9 Dec 2016 14:19:45 +0000 (UTC) Received: by mail-wm0-x244.google.com with SMTP id u144so4298823wmu.0 for ; Fri, 09 Dec 2016 06:19:45 -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=ENr148OaNwP0WLicG7duPLqUmSmt0ts3H4yX3vNZSWN2MphdFKYjssr8H0GfkF/YYg 9GaefFA6rq1B0rnoHs2/SeeL+f2FeaxCGKAzb8A9PdgyKhXU+SYpfp66tkmagDGvH1iU P5E0RJ1d4zcLRr3AJU4sXWuQMviuCek/GygPLmMKALp3vYEf/hvCZMnXeHdo2ZG3Z/Aw N3naKPPLsWSXyRl0+/NYNG+SgstVUZsCN6HeLCzkv8fhekbDlbpJ784V8yXNIFazzuXF VNyLgFgmApXDalkb1wPnqw+EwmJ7AKxt/mzIIaLldkOL6+WM1HfEA04TjjUjB1sieOn6 327A== X-Gm-Message-State: AKaTC00ReqyWn/L5ztsj0XNEycZq2/4U1sQEja/xQFZG/eR/k8nLiUAyw3bYlIkwbWFVVw== 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 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 Subject: [Intel-gfx] [PATCH 5/7] drm: Don't compute obj counts expensively in get_resources X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" 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;