From patchwork Wed Nov 9 15:13:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 9419663 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 CC4F1601C2 for ; Wed, 9 Nov 2016 15:13:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BEED5287DA for ; Wed, 9 Nov 2016 15:13:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B1F4128A17; Wed, 9 Nov 2016 15:13:55 +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 69B41287DA for ; Wed, 9 Nov 2016 15:13:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BDEA06E6D4; Wed, 9 Nov 2016 15:13:54 +0000 (UTC) X-Original-To: Intel-gfx@lists.freedesktop.org Delivered-To: Intel-gfx@lists.freedesktop.org Received: from mail-wm0-x243.google.com (mail-wm0-x243.google.com [IPv6:2a00:1450:400c:c09::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3F32D6E6D4 for ; Wed, 9 Nov 2016 15:13:53 +0000 (UTC) Received: by mail-wm0-x243.google.com with SMTP id u144so29854220wmu.0 for ; Wed, 09 Nov 2016 07:13:53 -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:in-reply-to:references; bh=vTphxKTnYvUmuzJOkL6/9VCz//+W4BvEYQxgFZLva4A=; b=CMmr0wpzXD2xJvB3KkFDCUyRu/WUD98MW8c+BkQ8FQ9WfJb181a1HxRRyUWTmkqcmf wXIYv3Tb/VtXvpTOphH72VE7QVrIw8/Z1JNFksm42txCakAgxDcjakAVzQ+bNLG3hnMy mRS0yMiEJ6pE/pr8NHCk5mbVsPJRuVw5L4PvaJdgU+lwiP8Te27PLoH6ElRnd86OIyyg VDmGlKC+g7P/0FYl6ZVLSAiMkMvdHrQyQIt1rYY2msDPvmJs0D2btO9t9fI03FJ6QyAa 3KszZ4VrDHWnVIRnMgVDQOZuOHIqA9otjzYk3zUw31VnCeu7DkkhH+36QDCVDoAD27zK Mg+w== 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=vTphxKTnYvUmuzJOkL6/9VCz//+W4BvEYQxgFZLva4A=; b=Jn1138xwPYo36dQehUniDe3lSX4boR2JrUwG55L/bVmkO2DuZQPxhGM25Wv8fnyZhO uPjcib6VtonM+w/sTu03pY6LKk0+TrHyRILQJOJdSsvicEN2gBJrIZPSanUHKqFKvSdK Cd2263Mz6fjcURdpAI1oL/KF3elxnbPErfwK7J8GNXD4DOKZ6wAOk2XN1flTUgbzLX4x 3Ze5dO/tNBY6XKSnpRLeI1BhaZfyAp4tVwhECYNlKlxVJqsGYK+MbfDKBDtpeDb4H+Do HlIRzfnqW5u/h5rNu30k9TLta6LpegVZxkkBXRzgpJqeewopAzlXbMX3Vfrwk9cfSnsr FbbA== X-Gm-Message-State: ABUngvd5vjSQIBkxzzhB5tsqE/LGHkskt2TA05dDS8h6nQMRn8EXt2/eapfqJqd9MiU9Zg== X-Received: by 10.28.72.3 with SMTP id v3mr19835130wma.20.1478704431642; Wed, 09 Nov 2016 07:13:51 -0800 (PST) Received: from t460p.intel ([2a02:c7d:9bd6:b000:f37e:1e07:a7d8:46cd]) by smtp.gmail.com with ESMTPSA id f10sm18237wjl.28.2016.11.09.07.13.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Nov 2016 07:13:51 -0800 (PST) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org Date: Wed, 9 Nov 2016 15:13:43 +0000 Message-Id: <1478704423-7447-1-git-send-email-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <20161109144436.GB7229@nuc-i3427.alporthouse.com> References: <20161109144436.GB7229@nuc-i3427.alporthouse.com> Subject: [Intel-gfx] [PATCH v3] 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 coalescing. 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. Experiments 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. More complicated techniques such as only copying the last used page and freeing the rest are left to the reader. v2: * Update commit message. * Use temporary sg_table on stack. (Chris Wilson) v3: * Commit message update. * Comment added. * Replace memcpy with copy assignment. (Chris Wilson) Signed-off-by: Tvrtko Ursulin Cc: Chris Wilson Reviewed-by: Chris Wilson --- drivers/gpu/drm/i915/i915_gem.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c index d2ad73d0b5b9..1c20edba7f2a 100644 --- a/drivers/gpu/drm/i915/i915_gem.c +++ b/drivers/gpu/drm/i915/i915_gem.c @@ -2232,6 +2232,30 @@ 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); + /* called before being DMA mapped, no need to copy sg->dma_* */ + new_sg = sg_next(new_sg); + } + + sg_free_table(orig_st); + + *orig_st = new_st; +} + static struct sg_table * i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) { @@ -2317,6 +2341,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;