Hi,
Attached new version of pool allocator not much changed.
- Use kmap_atomic_prot on highmem page to clear them if
necessary (please check that i do use proper pgprot)
- Force populate on cache change (discussed previously
the reason for this).
- Allocate structure to hold the pool information along
with the global memory ttm facilities.
Comments ? Thought ?
I need to do a patch to add a proper set_pages_array_wc
to the kernel rather than the hack i added to the patch.
I saw the CPA thread on related to this. Will look into
that if no one beat me to it.
Cheers,
Jerome
>From 32cdd208c34e3fdc96986eeb52baf420056166eb Mon Sep 17 00:00:00 2001
From: Jerome Glisse <jgli...@redhat.com>
Date: Thu, 16 Jul 2009 18:12:22 +0200
Subject: [PATCH] ttm: add pool wc/uc page allocator
On AGP system we might allocate/free routinely uncached or wc memory,
changing page from cached (wb) to uc or wc is very expensive and involves
a lot of flushing. To improve performance this allocator use a pool
of uc,wc pages.
Currently each pool (wc, uc) is 256 pages big, improvement would be
to tweak this according to memory pressure so we can give back memory
to system.
Signed-off-by: Dave Airlie <airl...@redhat.com>
Signed-off-by: Jerome Glisse <jgli...@redhat.com>
---
drivers/gpu/drm/ttm/Makefile | 2 +-
drivers/gpu/drm/ttm/ttm_memory.c | 3 +
drivers/gpu/drm/ttm/ttm_page_alloc.c | 353 ++++++++++++++++++++++++++++++++++
drivers/gpu/drm/ttm/ttm_page_alloc.h | 36 ++++
drivers/gpu/drm/ttm/ttm_tt.c | 31 +---
5 files changed, 400 insertions(+), 25 deletions(-)
create mode 100644 drivers/gpu/drm/ttm/ttm_page_alloc.c
create mode 100644 drivers/gpu/drm/ttm/ttm_page_alloc.h
diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
index b0a9de7..93e002c 100644
--- a/drivers/gpu/drm/ttm/Makefile
+++ b/drivers/gpu/drm/ttm/Makefile
@@ -3,6 +3,6 @@
ccflags-y := -Iinclude/drm
ttm-y := ttm_agp_backend.o ttm_memory.o ttm_tt.o ttm_bo.o \
- ttm_bo_util.o ttm_bo_vm.o ttm_module.o ttm_global.o
+ ttm_bo_util.o ttm_bo_vm.o ttm_module.o ttm_global.o ttm_page_alloc.o
obj-$(CONFIG_DRM_TTM) += ttm.o
diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
index 87323d4..6da4a08 100644
--- a/drivers/gpu/drm/ttm/ttm_memory.c
+++ b/drivers/gpu/drm/ttm/ttm_memory.c
@@ -32,6 +32,7 @@
#include <linux/mm.h>
#include <linux/module.h>
+#include "ttm_page_alloc.h"
#define TTM_PFX "[TTM] "
#define TTM_MEMORY_ALLOC_RETRIES 4
@@ -124,6 +125,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
printk(KERN_INFO TTM_PFX "TTM available object memory: %llu MiB\n",
glob->max_memory >> 20);
+ ttm_page_alloc_init();
return 0;
}
EXPORT_SYMBOL(ttm_mem_global_init);
@@ -135,6 +137,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
flush_workqueue(glob->swap_queue);
destroy_workqueue(glob->swap_queue);
glob->swap_queue = NULL;
+ ttm_page_alloc_fini();
}
EXPORT_SYMBOL(ttm_mem_global_release);
diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.c b/drivers/gpu/drm/ttm/ttm_page_alloc.c
new file mode 100644
index 0000000..6a79e9c
--- /dev/null
+++ b/drivers/gpu/drm/ttm/ttm_page_alloc.c
@@ -0,0 +1,353 @@
+/*
+ * Copyright (c) Red Hat Inc.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sub license,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Dave Airlie <airl...@redhat.com>
+ * Jerome Glisse <jgli...@redhat.com>
+ */
+
+/* simple list based uncached page allocator
+ * - Add chunks of 1MB to the allocator at a time.
+ * - Use page->lru to keep a free list
+ * - doesn't track currently in use pages
+ */
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/mm_types.h>
+
+#include <asm/agp.h>
+#include "drmP.h"
+#include "ttm/ttm_bo_driver.h"
+#include "ttm_page_alloc.h"
+
+/* add 1MB at a time */
+#define NUM_PAGES_TO_ADD 256
+
+struct page_pool {
+ struct list_head list;
+ int npages;
+ int nzeropages;
+ pgprot_t pgprot;
+};
+
+struct pool_allocator {
+ struct page *pages[NUM_PAGES_TO_ADD];
+ int npages_to_free;
+ struct page_pool wc_pool;
+ struct page_pool uc_pool;
+ struct page_pool wc_pool_dma32;
+ struct page_pool uc_pool_dma32;
+ struct mutex page_alloc_mutex;
+};
+
+static struct pool_allocator *_pool = NULL;
+
+
+#ifdef CONFIG_X86
+/* TODO: add this to x86 like _uc, this version here is inefficient */
+static int set_pages_array_wc(struct page **pages, int addrinarray)
+{
+ int i;
+
+ for (i = 0; i < addrinarray; i++) {
+ set_memory_wc((unsigned long)page_address(pages[i]), 1);
+ }
+ return 0;
+}
+#else
+static int set_pages_array_wb(struct page **pages, int addrinarray)
+{
+#ifdef TTM_HAS_AGP
+ int i;
+
+ for (i = 0; i < addrinarray; i++) {
+ unmap_page_from_agp(pages[i]);
+ }
+#endif
+ return 0;
+}
+
+static int set_pages_array_wc(struct page **pages, int addrinarray)
+{
+#ifdef TTM_HAS_AGP
+ int i;
+
+ for (i = 0; i < addrinarray; i++) {
+ map_page_into_agp(pages[i]);
+ }
+#endif
+ return 0;
+}
+
+static int set_pages_array_uc(struct page **pages, int addrinarray)
+{
+#ifdef TTM_HAS_AGP
+ int i;
+
+ for (i = 0; i < addrinarray; i++) {
+ map_page_into_agp(pages[i]);
+ }
+#endif
+ return 0;
+}
+#endif
+
+
+void pages_free_locked(void)
+{
+ int i;
+
+ set_pages_array_wb(_pool->pages, _pool->npages_to_free);
+ for (i = 0; i < _pool->npages_to_free; i++) {
+ __free_page(_pool->pages[i]);
+ }
+ _pool->npages_to_free = 0;
+}
+
+static void ttm_page_pool_init_locked(struct page_pool *pool)
+{
+ INIT_LIST_HEAD(&pool->list);
+ pool->npages = 0;
+ pool->nzeropages = 0;
+}
+
+static int page_pool_fill_locked(struct page_pool *pool, int gfp_flags,
+ enum ttm_caching_state cstate)
+{
+ struct page *page;
+ int i, cpages, npages;
+
+ /* We need the pages table to change page cache status so empty it */
+ if (cstate != tt_cached && _pool->npages_to_free)
+ pages_free_locked();
+
+ npages = NUM_PAGES_TO_ADD - pool->npages - pool->nzeropages;
+ for (i = 0, cpages = 0; i < npages; i++) {
+ page = alloc_page(gfp_flags);
+ if (!page) {
+ printk(KERN_ERR "unable to get page %d\n", i);
+ return -ENOMEM;
+ }
+ if (gfp_flags & __GFP_ZERO) {
+ list_add(&page->lru, &pool->list);
+ pool->nzeropages++;
+ } else {
+ list_add_tail(&page->lru, &pool->list);
+ pool->npages++;
+ }
+ if (!PageHighMem(page) && cstate != tt_cached) {
+ _pool->pages[cpages++] = page;
+ }
+ }
+ switch(cstate) {
+ case tt_uncached:
+ drm_clflush_pages(_pool->pages, cpages);
+ set_pages_array_uc(_pool->pages, cpages);
+ break;
+ case tt_wc:
+ drm_clflush_pages(_pool->pages, cpages);
+ set_pages_array_wc(_pool->pages, cpages);
+ break;
+ case tt_cached:
+ default:
+ break;
+ }
+ return 0;
+}
+
+static inline void ttm_page_put_locked(struct page *page)
+{
+ if (_pool->npages_to_free >= NUM_PAGES_TO_ADD)
+ pages_free_locked();
+ _pool->pages[_pool->npages_to_free++] = page;
+}
+
+static void ttm_page_pool_empty_locked(struct page_pool *pool)
+{
+ struct page *page, *tmp;
+
+ list_for_each_entry_safe(page, tmp, &pool->list, lru) {
+ list_del(&page->lru);
+#ifdef CONFIG_X86
+ if (PageHighMem(page)) {
+ __free_page(page);
+ } else
+#endif
+ {
+ ttm_page_put_locked(page);
+ }
+ }
+ pool->npages = 0;
+ pool->nzeropages = 0;
+}
+
+static struct page *ttm_page_pool_get_locked(struct page_pool *pool, int flags)
+{
+ struct page *page = NULL;
+ void *ptr;
+
+ if (flags & __GFP_ZERO) {
+ if (pool->nzeropages) {
+ page = list_first_entry(&pool->list, struct page, lru);
+ list_del(&page->lru);
+ pool->nzeropages--;
+ return page;
+ } else {
+ page = list_first_entry(&pool->list, struct page, lru);
+ list_del(&page->lru);
+ pool->npages--;
+ if (PageHighMem(page)) {
+ ptr = kmap_atomic_prot(page, KM_USER0,
+ pool->pgprot);
+ if (ptr == NULL)
+ return NULL;
+ clear_page(ptr);
+ kunmap_atomic(ptr, KM_USER0);
+ } else {
+ clear_page(page_address(page));
+ }
+ return page;
+ }
+ }
+ if (pool->npages) {
+ page = list_entry(pool->list.prev, struct page, lru);
+ list_del(&page->lru);
+ pool->npages--;
+ return page;
+ }
+ page = list_first_entry(&pool->list, struct page, lru);
+ list_del(&page->lru);
+ pool->nzeropages--;
+ return page;
+}
+
+
+struct page *ttm_get_page(int flags, enum ttm_caching_state cstate)
+{
+ struct page_pool *pool;
+ struct page *page = NULL;
+ int gfp_flags = GFP_USER;
+ int r;
+
+ if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
+ gfp_flags |= __GFP_ZERO;
+ if (flags & TTM_PAGE_FLAG_DMA32) {
+ gfp_flags |= __GFP_DMA32;
+ } else {
+ gfp_flags |= __GFP_HIGHMEM;
+ }
+ switch (cstate) {
+ case tt_uncached:
+ if (gfp_flags & __GFP_DMA32)
+ pool = &_pool->uc_pool_dma32;
+ else
+ pool = &_pool->uc_pool;
+ break;
+ case tt_wc:
+ if (gfp_flags & __GFP_DMA32)
+ pool = &_pool->wc_pool_dma32;
+ else
+ pool = &_pool->wc_pool;
+ break;
+ case tt_cached:
+ default:
+ return alloc_page(gfp_flags);
+ }
+ mutex_lock(&_pool->page_alloc_mutex);
+ if (!(pool->npages + pool->nzeropages)) {
+ r = page_pool_fill_locked(pool, gfp_flags, cstate);
+ if (r) {
+ mutex_unlock(&_pool->page_alloc_mutex);
+ return NULL;
+ }
+ }
+ page = ttm_page_pool_get_locked(pool, gfp_flags);
+ mutex_unlock(&_pool->page_alloc_mutex);
+ return page;
+}
+
+void ttm_put_page(struct page *page, int flags, enum ttm_caching_state cstate)
+{
+ struct page_pool *pool;
+
+ switch (cstate) {
+ case tt_uncached:
+ if (flags & TTM_PAGE_FLAG_DMA32)
+ pool = & _pool->uc_pool_dma32;
+ else
+ pool = & _pool->uc_pool;
+ break;
+ case tt_wc:
+ if (flags & TTM_PAGE_FLAG_DMA32)
+ pool = & _pool->wc_pool_dma32;
+ else
+ pool = & _pool->wc_pool;
+ break;
+ case tt_cached:
+ default:
+ __free_page(page);
+ return;
+ }
+ mutex_lock(&_pool->page_alloc_mutex);
+ if (pool->npages >= NUM_PAGES_TO_ADD) {
+ ttm_page_put_locked(page);
+ } else {
+ list_add_tail(&page->lru, &pool->list);
+ pool->npages++;
+ }
+ mutex_unlock(&_pool->page_alloc_mutex);
+}
+
+int ttm_page_alloc_init(void)
+{
+ if (_pool)
+ return 0;
+ _pool = (struct pool_allocator*)kmalloc(sizeof(struct pool_allocator),
+ GFP_KERNEL);
+ if (_pool == NULL)
+ return -ENOMEM;
+ mutex_init(&_pool->page_alloc_mutex);
+ ttm_page_pool_init_locked(&_pool->wc_pool);
+ ttm_page_pool_init_locked(&_pool->uc_pool);
+ ttm_page_pool_init_locked(&_pool->wc_pool_dma32);
+ ttm_page_pool_init_locked(&_pool->uc_pool_dma32);
+ _pool->npages_to_free = 0;
+ return 0;
+}
+
+void ttm_page_alloc_fini(void)
+{
+ if (_pool == NULL)
+ return;
+ mutex_lock(&_pool->page_alloc_mutex);
+ ttm_page_pool_empty_locked(&_pool->wc_pool);
+ ttm_page_pool_empty_locked(&_pool->uc_pool);
+ ttm_page_pool_empty_locked(&_pool->wc_pool_dma32);
+ ttm_page_pool_empty_locked(&_pool->uc_pool_dma32);
+ _pool->wc_pool.pgprot = pgprot_writecombine(PAGE_KERNEL);
+ _pool->wc_pool_dma32.pgprot = pgprot_writecombine(PAGE_KERNEL);
+ _pool->uc_pool.pgprot = pgprot_noncached(PAGE_KERNEL);
+ _pool->uc_pool_dma32.pgprot = pgprot_noncached(PAGE_KERNEL);
+ pages_free_locked();
+ mutex_unlock(&_pool->page_alloc_mutex);
+ kfree(_pool);
+ _pool = NULL;
+}
diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc.h b/drivers/gpu/drm/ttm/ttm_page_alloc.h
new file mode 100644
index 0000000..9212554
--- /dev/null
+++ b/drivers/gpu/drm/ttm/ttm_page_alloc.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) Red Hat Inc.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sub license,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Dave Airlie <airl...@redhat.com>
+ * Jerome Glisse <jgli...@redhat.com>
+ */
+#ifndef TTM_PAGE_ALLOC
+#define TTM_PAGE_ALLOC
+
+#include "ttm/ttm_bo_driver.h"
+
+void ttm_put_page(struct page *page, int flags, enum ttm_caching_state cstate);
+struct page *ttm_get_page(int flags, enum ttm_caching_state cstate);
+int ttm_page_alloc_init(void);
+void ttm_page_alloc_fini(void);
+
+#endif
diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c
index b8b6c4a..1c24636 100644
--- a/drivers/gpu/drm/ttm/ttm_tt.c
+++ b/drivers/gpu/drm/ttm/ttm_tt.c
@@ -37,6 +37,7 @@
#include "ttm/ttm_module.h"
#include "ttm/ttm_bo_driver.h"
#include "ttm/ttm_placement.h"
+#include "ttm_page_alloc.h"
static int ttm_tt_swapin(struct ttm_tt *ttm);
@@ -135,21 +136,6 @@ static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
ttm->pages = NULL;
}
-static struct page *ttm_tt_alloc_page(unsigned page_flags)
-{
- gfp_t gfp_flags = GFP_USER;
-
- if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
- gfp_flags |= __GFP_ZERO;
-
- if (page_flags & TTM_PAGE_FLAG_DMA32)
- gfp_flags |= __GFP_DMA32;
- else
- gfp_flags |= __GFP_HIGHMEM;
-
- return alloc_page(gfp_flags);
-}
-
static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
{
int write;
@@ -195,7 +181,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
int ret;
while (NULL == (p = ttm->pages[index])) {
- p = ttm_tt_alloc_page(ttm->page_flags);
+ p = ttm_get_page(ttm->page_flags, ttm->caching_state);
if (!p)
return NULL;
@@ -218,7 +204,8 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
}
return p;
out_err:
- put_page(p);
+ if (p)
+ ttm_put_page(p, ttm->page_flags, ttm->caching_state);
return NULL;
}
@@ -292,7 +279,6 @@ static inline int ttm_tt_set_page_caching(struct page *p,
* Change caching policy for the linear kernel map
* for range of pages in a ttm.
*/
-
static int ttm_tt_set_caching(struct ttm_tt *ttm,
enum ttm_caching_state c_state)
{
@@ -320,11 +306,8 @@ static int ttm_tt_set_caching(struct ttm_tt *ttm,
goto out_err;
}
}
-
ttm->caching_state = c_state;
-
return 0;
-
out_err:
for (j = 0; j < i; ++j) {
cur_page = ttm->pages[j];
@@ -359,7 +342,6 @@ static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
if (be)
be->func->clear(be);
- (void)ttm_tt_set_caching(ttm, tt_cached);
for (i = 0; i < ttm->num_pages; ++i) {
cur_page = ttm->pages[i];
ttm->pages[i] = NULL;
@@ -367,10 +349,11 @@ static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
if (page_count(cur_page) != 1)
printk(KERN_ERR TTM_PFX
"Erroneous page count. "
- "Leaking pages.\n");
+ "Leaking pages (%d).\n",
+ page_count(cur_page));
ttm_mem_global_free(ttm->bdev->mem_glob, PAGE_SIZE,
PageHighMem(cur_page));
- __free_page(cur_page);
+ ttm_put_page(cur_page, ttm->page_flags, ttm->caching_state);
}
}
ttm->state = tt_unpopulated;
--
1.6.4
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
--
_______________________________________________
Dri-devel mailing list
Dri-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dri-devel