From patchwork Wed Nov 9 14:30:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 9419501 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 4A79B601C2 for ; Wed, 9 Nov 2016 14:30:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E0E629334 for ; Wed, 9 Nov 2016 14:30:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 32C2929349; Wed, 9 Nov 2016 14:30:14 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_SPAM,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 B3FBC29334 for ; Wed, 9 Nov 2016 14:30:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9C2BB6E6BD; Wed, 9 Nov 2016 14:30:12 +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 1CBAA6E6BD for ; Wed, 9 Nov 2016 14:30:11 +0000 (UTC) Received: by mail-wm0-x244.google.com with SMTP id a20so18858635wme.2 for ; Wed, 09 Nov 2016 06:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ursulin-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=zJ9AaPAYinOlPajvVC0S5tM5NpSa36xj3YIamOLhswc=; b=YcS8B8NBJN5mryGl/TtGLpJI+i9HlKpq59iI8cf6TUQ29Heij+fLnC+zicN9c3Ci0f 9tRyjF3RKNUEw0JDfurdOK6ZqaYkzZMRml9zIsz2l/6r7sRV764mGqjYhA6Uhf7pBnv7 6edxTuDwxGZlHzfn4xxDVDtA8goT8ZTSQy0N8q8k3/2sUhKAZ9XpvFwH6IFJ+jRV2zOE Tn9rPyPKt0pTxjCqE7DP3+E5bm/M4Xq/2t78va0jLPVCARnURC9nwfIOMWpZxsoQ4jW3 QP0CSM4EQqDJQDQ5gd+XdExMU3anj5UA86RS1eI4VaTNFx6Ryj+zibtuGznsRozsFdWA jmAQ== 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; bh=zJ9AaPAYinOlPajvVC0S5tM5NpSa36xj3YIamOLhswc=; b=BzzhTxjaOEJpaDOTLaC6UANX+8WrvgyfWR6JC0E/WU0fVamSvUDHwsPsWNsaW3svlF iuIO6inXZyb2eUkJsJKYaECiY/Go+EQwUTFoUyzILknUX754B18RXsQ4S5uPQEuzc3Zd bNeckPAsTSgWJpcqiEF9LNCpIB/wDSH40CDg71TB+wNvOS6wLHrmEo5VHBJQ9+fIty8F 39ur4xotiDMthotT3NM9iaKYmAoNfSIHSvQoP3d3nbSUwehK/tsCeF5Ir46iK927CPdg ZU5ikfFlxFBanUACNn5NVC5YIUPLgcsrrWTHHoGUJfYy0TozYENh3kyjRGE1N7r1mj+J I0Qw== X-Gm-Message-State: ABUngvdk1zHMzd7WTBDhDI7ZxLkroBUEH1JIH+EunoQasaGjPiEHODGRpTWEMyCN7C+n+w== X-Received: by 10.28.145.85 with SMTP id t82mr615740wmd.40.1478701809631; Wed, 09 Nov 2016 06:30:09 -0800 (PST) Received: from t460p.intel ([2a02:c7d:9bd6:b000:f37e:1e07:a7d8:46cd]) by smtp.gmail.com with ESMTPSA id f3sm7296546wmf.10.2016.11.09.06.30.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Nov 2016 06:30:09 -0800 (PST) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org Date: Wed, 9 Nov 2016 14:30:02 +0000 Message-Id: <1478701802-2982-1-git-send-email-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.7.4 Subject: [Intel-gfx] [PATCH] drm/i915: Trim the object sg table 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 From: Tvrtko Ursulin At the moment we allocate enough sg table entries assuming we will not be able to do any coallescing. But since in practice we most often can, and more so very effectively, this ends up wasting a lot of memory. A simple and effective way of trimming the over-allocated entries is to copy the table over to a new one allocated to the exact size. Experiment on my freshly logged and idle desktop (KDE) showed that by doing this we can save approximately 1 MiB of RAM, or when running a typical benchmark like gl_manhattan I have even seen a 6 MiB saving. v2: * Update commit message. * Use temporary sg_table on stack. (Chris Wilson) Signed-off-by: Tvrtko Ursulin Cc: Chris Wilson Reviewed-by: Chris Wilson --- drivers/gpu/drm/i915/i915_gem.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c index d2ad73d0b5b9..411aae535abe 100644 --- a/drivers/gpu/drm/i915/i915_gem.c +++ b/drivers/gpu/drm/i915/i915_gem.c @@ -2232,6 +2232,28 @@ static unsigned int swiotlb_max_size(void) #endif } +static void i915_sg_trim(struct sg_table *orig_st) +{ + struct sg_table new_st; + struct scatterlist *sg, *new_sg; + unsigned int i; + + if (orig_st->nents == orig_st->orig_nents) + return; + + if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL)) + return; + + new_sg = new_st.sgl; + for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { + sg_set_page(new_sg, sg_page(sg), sg->length, 0); + new_sg = sg_next(new_sg); + } + + sg_free_table(orig_st); + memcpy(orig_st, &new_st, sizeof(*orig_st)); +} + static struct sg_table * i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) { @@ -2317,6 +2339,9 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) if (sg) /* loop terminated early; short sg table */ sg_mark_end(sg); + /* Trim unused sg entries to avoid wasting memory. */ + i915_sg_trim(st); + ret = i915_gem_gtt_prepare_pages(obj, st); if (ret) goto err_pages;