2018-05-02 21:46:21 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
2009-06-10 21:20:19 +08:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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 COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS 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: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
|
|
|
|
*/
|
|
|
|
|
2012-03-17 12:43:50 +08:00
|
|
|
#define pr_fmt(fmt) "[TTM] " fmt
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/pagemap.h>
|
2011-06-28 07:18:17 +08:00
|
|
|
#include <linux/shmem_fs.h>
|
2009-06-10 21:20:19 +08:00
|
|
|
#include <linux/file.h>
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drm_cache.h>
|
|
|
|
#include <drm/ttm/ttm_bo_driver.h>
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2020-11-16 20:47:11 +08:00
|
|
|
#include "ttm_module.h"
|
|
|
|
|
|
|
|
static struct shrinker mm_shrinker;
|
|
|
|
static atomic_long_t swapable_pages;
|
|
|
|
|
2020-11-17 01:41:02 +08:00
|
|
|
/*
|
2018-02-22 15:54:57 +08:00
|
|
|
* Allocates a ttm structure for the given BO.
|
|
|
|
*/
|
|
|
|
int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
|
|
|
|
{
|
2020-10-01 20:51:40 +08:00
|
|
|
struct ttm_device *bdev = bo->bdev;
|
2018-02-22 15:54:57 +08:00
|
|
|
uint32_t page_flags = 0;
|
|
|
|
|
2019-08-11 16:06:32 +08:00
|
|
|
dma_resv_assert_held(bo->base.resv);
|
2018-02-22 15:54:57 +08:00
|
|
|
|
2020-08-19 21:27:48 +08:00
|
|
|
if (bo->ttm)
|
|
|
|
return 0;
|
|
|
|
|
2018-02-22 15:54:57 +08:00
|
|
|
switch (bo->type) {
|
|
|
|
case ttm_bo_type_device:
|
|
|
|
if (zero_alloc)
|
|
|
|
page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
|
2018-02-22 15:54:57 +08:00
|
|
|
break;
|
2018-02-22 15:54:57 +08:00
|
|
|
case ttm_bo_type_kernel:
|
|
|
|
break;
|
|
|
|
case ttm_bo_type_sg:
|
2018-02-22 15:54:57 +08:00
|
|
|
page_flags |= TTM_PAGE_FLAG_SG;
|
2018-02-22 15:54:57 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pr_err("Illegal buffer object type\n");
|
2018-02-22 15:54:57 +08:00
|
|
|
return -EINVAL;
|
2018-02-22 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
|
2018-02-22 15:54:57 +08:00
|
|
|
if (unlikely(bo->ttm == NULL))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
2018-02-22 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 01:41:02 +08:00
|
|
|
/*
|
2009-06-10 21:20:19 +08:00
|
|
|
* Allocates storage for pointers to the pages that back the ttm.
|
|
|
|
*/
|
2018-01-26 02:24:03 +08:00
|
|
|
static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2017-05-17 20:23:12 +08:00
|
|
|
ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
|
|
|
|
GFP_KERNEL | __GFP_ZERO);
|
2018-01-26 02:24:03 +08:00
|
|
|
if (!ttm->pages)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 20:06:49 +08:00
|
|
|
static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2020-10-21 20:06:49 +08:00
|
|
|
ttm->pages = kvmalloc_array(ttm->num_pages,
|
|
|
|
sizeof(*ttm->pages) +
|
|
|
|
sizeof(*ttm->dma_address),
|
|
|
|
GFP_KERNEL | __GFP_ZERO);
|
|
|
|
if (!ttm->pages)
|
2018-01-26 02:24:03 +08:00
|
|
|
return -ENOMEM;
|
2020-10-21 20:06:49 +08:00
|
|
|
|
|
|
|
ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
|
2018-01-26 02:24:03 +08:00
|
|
|
return 0;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 20:06:49 +08:00
|
|
|
static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
|
2018-02-23 22:12:00 +08:00
|
|
|
{
|
2020-10-21 20:06:49 +08:00
|
|
|
ttm->dma_address = kvmalloc_array(ttm->num_pages,
|
2018-02-23 22:12:00 +08:00
|
|
|
sizeof(*ttm->dma_address),
|
|
|
|
GFP_KERNEL | __GFP_ZERO);
|
|
|
|
if (!ttm->dma_address)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2020-09-15 08:21:15 +08:00
|
|
|
ttm_tt_unpopulate(bdev, ttm);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2020-09-17 19:27:35 +08:00
|
|
|
if (ttm->swap_storage)
|
2009-06-10 21:20:19 +08:00
|
|
|
fput(ttm->swap_storage);
|
|
|
|
|
2011-11-02 08:46:13 +08:00
|
|
|
ttm->swap_storage = NULL;
|
2020-09-17 11:20:48 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_tt_destroy_common);
|
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
|
2020-09-17 11:20:48 +08:00
|
|
|
{
|
2020-10-01 20:51:40 +08:00
|
|
|
bdev->funcs->ttm_tt_destroy(bdev, ttm);
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2019-11-04 19:59:01 +08:00
|
|
|
static void ttm_tt_init_fields(struct ttm_tt *ttm,
|
|
|
|
struct ttm_buffer_object *bo,
|
2020-09-30 16:38:48 +08:00
|
|
|
uint32_t page_flags,
|
|
|
|
enum ttm_caching caching)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2020-12-09 22:07:50 +08:00
|
|
|
ttm->num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
|
2020-09-30 16:38:48 +08:00
|
|
|
ttm->caching = ttm_cached;
|
2009-06-10 21:20:19 +08:00
|
|
|
ttm->page_flags = page_flags;
|
2020-10-21 20:06:49 +08:00
|
|
|
ttm->dma_address = NULL;
|
2012-01-04 06:37:37 +08:00
|
|
|
ttm->swap_storage = NULL;
|
2018-02-28 16:48:22 +08:00
|
|
|
ttm->sg = bo->sg;
|
2020-09-30 16:38:48 +08:00
|
|
|
ttm->caching = caching;
|
2018-02-23 22:12:00 +08:00
|
|
|
}
|
|
|
|
|
2018-02-22 17:18:14 +08:00
|
|
|
int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
|
2020-09-30 16:38:48 +08:00
|
|
|
uint32_t page_flags, enum ttm_caching caching)
|
2018-02-23 22:12:00 +08:00
|
|
|
{
|
2020-09-30 16:38:48 +08:00
|
|
|
ttm_tt_init_fields(ttm, bo, page_flags, caching);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2018-01-26 02:24:03 +08:00
|
|
|
if (ttm_tt_alloc_page_directory(ttm)) {
|
2012-03-17 12:43:50 +08:00
|
|
|
pr_err("Failed allocating page table\n");
|
2011-11-02 08:46:13 +08:00
|
|
|
return -ENOMEM;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
2011-11-02 08:46:13 +08:00
|
|
|
return 0;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
2011-11-02 08:46:13 +08:00
|
|
|
EXPORT_SYMBOL(ttm_tt_init);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2011-11-10 06:15:26 +08:00
|
|
|
void ttm_tt_fini(struct ttm_tt *ttm)
|
|
|
|
{
|
2020-10-21 20:06:49 +08:00
|
|
|
if (ttm->pages)
|
|
|
|
kvfree(ttm->pages);
|
|
|
|
else
|
|
|
|
kvfree(ttm->dma_address);
|
2011-11-10 06:15:26 +08:00
|
|
|
ttm->pages = NULL;
|
2020-10-21 20:06:49 +08:00
|
|
|
ttm->dma_address = NULL;
|
2011-11-10 06:15:26 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_tt_fini);
|
|
|
|
|
2020-10-21 20:06:49 +08:00
|
|
|
int ttm_sg_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
|
2020-09-30 16:38:48 +08:00
|
|
|
uint32_t page_flags, enum ttm_caching caching)
|
2018-02-23 22:12:00 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-09-30 16:38:48 +08:00
|
|
|
ttm_tt_init_fields(ttm, bo, page_flags, caching);
|
2018-02-23 22:12:00 +08:00
|
|
|
|
|
|
|
if (page_flags & TTM_PAGE_FLAG_SG)
|
2020-10-21 20:06:49 +08:00
|
|
|
ret = ttm_sg_tt_alloc_page_directory(ttm);
|
2018-02-23 22:12:00 +08:00
|
|
|
else
|
2020-10-21 20:06:49 +08:00
|
|
|
ret = ttm_dma_tt_alloc_page_directory(ttm);
|
2018-02-23 22:12:00 +08:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed allocating page table\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_sg_tt_init);
|
|
|
|
|
2011-11-03 11:59:28 +08:00
|
|
|
int ttm_tt_swapin(struct ttm_tt *ttm)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
|
|
|
struct address_space *swap_space;
|
|
|
|
struct file *swap_storage;
|
|
|
|
struct page *from_page;
|
|
|
|
struct page *to_page;
|
2020-09-17 19:27:35 +08:00
|
|
|
gfp_t gfp_mask;
|
|
|
|
int i, ret;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
swap_storage = ttm->swap_storage;
|
|
|
|
BUG_ON(swap_storage == NULL);
|
|
|
|
|
2015-12-05 12:45:44 +08:00
|
|
|
swap_space = swap_storage->f_mapping;
|
2020-09-17 19:27:35 +08:00
|
|
|
gfp_mask = mapping_gfp_mask(swap_space);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
2020-09-17 19:27:35 +08:00
|
|
|
from_page = shmem_read_mapping_page_gfp(swap_space, i,
|
|
|
|
gfp_mask);
|
2010-02-20 10:22:21 +08:00
|
|
|
if (IS_ERR(from_page)) {
|
|
|
|
ret = PTR_ERR(from_page);
|
2009-06-10 21:20:19 +08:00
|
|
|
goto out_err;
|
2010-02-20 10:22:21 +08:00
|
|
|
}
|
2011-11-03 11:59:28 +08:00
|
|
|
to_page = ttm->pages[i];
|
2020-09-17 19:27:35 +08:00
|
|
|
if (unlikely(to_page == NULL)) {
|
|
|
|
ret = -ENOMEM;
|
2009-06-10 21:20:19 +08:00
|
|
|
goto out_err;
|
2020-09-17 19:27:35 +08:00
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2012-09-25 19:57:02 +08:00
|
|
|
copy_highpage(to_page, from_page);
|
mm, fs: get rid of PAGE_CACHE_* and page_cache_{get,release} macros
PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} macros were introduced *long* time
ago with promise that one day it will be possible to implement page
cache with bigger chunks than PAGE_SIZE.
This promise never materialized. And unlikely will.
We have many places where PAGE_CACHE_SIZE assumed to be equal to
PAGE_SIZE. And it's constant source of confusion on whether
PAGE_CACHE_* or PAGE_* constant should be used in a particular case,
especially on the border between fs and mm.
Global switching to PAGE_CACHE_SIZE != PAGE_SIZE would cause to much
breakage to be doable.
Let's stop pretending that pages in page cache are special. They are
not.
The changes are pretty straight-forward:
- <foo> << (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- <foo> >> (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} -> PAGE_{SIZE,SHIFT,MASK,ALIGN};
- page_cache_get() -> get_page();
- page_cache_release() -> put_page();
This patch contains automated changes generated with coccinelle using
script below. For some reason, coccinelle doesn't patch header files.
I've called spatch for them manually.
The only adjustment after coccinelle is revert of changes to
PAGE_CAHCE_ALIGN definition: we are going to drop it later.
There are few places in the code where coccinelle didn't reach. I'll
fix them manually in a separate patch. Comments and documentation also
will be addressed with the separate patch.
virtual patch
@@
expression E;
@@
- E << (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
expression E;
@@
- E >> (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
@@
- PAGE_CACHE_SHIFT
+ PAGE_SHIFT
@@
@@
- PAGE_CACHE_SIZE
+ PAGE_SIZE
@@
@@
- PAGE_CACHE_MASK
+ PAGE_MASK
@@
expression E;
@@
- PAGE_CACHE_ALIGN(E)
+ PAGE_ALIGN(E)
@@
expression E;
@@
- page_cache_get(E)
+ get_page(E)
@@
expression E;
@@
- page_cache_release(E)
+ put_page(E)
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-04-01 20:29:47 +08:00
|
|
|
put_page(from_page);
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:27:35 +08:00
|
|
|
fput(swap_storage);
|
2009-06-10 21:20:19 +08:00
|
|
|
ttm->swap_storage = NULL;
|
|
|
|
ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
|
|
|
|
|
|
|
|
return 0;
|
2020-09-17 19:27:35 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
out_err:
|
2010-02-20 10:22:21 +08:00
|
|
|
return ret;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 20:47:11 +08:00
|
|
|
/**
|
|
|
|
* ttm_tt_swapout - swap out tt object
|
|
|
|
*
|
|
|
|
* @bdev: TTM device structure.
|
|
|
|
* @ttm: The struct ttm_tt.
|
|
|
|
* @gfp_flags: Flags to use for memory allocation.
|
|
|
|
*
|
|
|
|
* Swapout a TT object to a shmem_file, return number of pages swapped out or
|
|
|
|
* negative error code.
|
|
|
|
*/
|
|
|
|
int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
|
|
|
|
gfp_t gfp_flags)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2020-11-16 20:47:11 +08:00
|
|
|
loff_t size = (loff_t)ttm->num_pages << PAGE_SHIFT;
|
2009-06-10 21:20:19 +08:00
|
|
|
struct address_space *swap_space;
|
|
|
|
struct file *swap_storage;
|
|
|
|
struct page *from_page;
|
|
|
|
struct page *to_page;
|
2020-09-17 19:27:35 +08:00
|
|
|
int i, ret;
|
|
|
|
|
2020-11-16 20:47:11 +08:00
|
|
|
swap_storage = shmem_file_setup("ttm swap", size, 0);
|
2020-09-17 19:27:35 +08:00
|
|
|
if (IS_ERR(swap_storage)) {
|
|
|
|
pr_err("Failed allocating swap storage\n");
|
|
|
|
return PTR_ERR(swap_storage);
|
2018-01-26 02:29:42 +08:00
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2015-12-05 12:45:44 +08:00
|
|
|
swap_space = swap_storage->f_mapping;
|
2020-11-16 20:47:11 +08:00
|
|
|
gfp_flags &= mapping_gfp_mask(swap_space);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
|
|
|
from_page = ttm->pages[i];
|
|
|
|
if (unlikely(from_page == NULL))
|
|
|
|
continue;
|
2017-12-22 21:12:40 +08:00
|
|
|
|
2020-11-16 20:47:11 +08:00
|
|
|
to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_flags);
|
2015-07-31 16:38:24 +08:00
|
|
|
if (IS_ERR(to_page)) {
|
2010-02-20 10:22:21 +08:00
|
|
|
ret = PTR_ERR(to_page);
|
2009-06-10 21:20:19 +08:00
|
|
|
goto out_err;
|
2010-02-20 10:22:21 +08:00
|
|
|
}
|
2012-09-25 19:57:02 +08:00
|
|
|
copy_highpage(to_page, from_page);
|
2009-06-10 21:20:19 +08:00
|
|
|
set_page_dirty(to_page);
|
|
|
|
mark_page_accessed(to_page);
|
mm, fs: get rid of PAGE_CACHE_* and page_cache_{get,release} macros
PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} macros were introduced *long* time
ago with promise that one day it will be possible to implement page
cache with bigger chunks than PAGE_SIZE.
This promise never materialized. And unlikely will.
We have many places where PAGE_CACHE_SIZE assumed to be equal to
PAGE_SIZE. And it's constant source of confusion on whether
PAGE_CACHE_* or PAGE_* constant should be used in a particular case,
especially on the border between fs and mm.
Global switching to PAGE_CACHE_SIZE != PAGE_SIZE would cause to much
breakage to be doable.
Let's stop pretending that pages in page cache are special. They are
not.
The changes are pretty straight-forward:
- <foo> << (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- <foo> >> (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} -> PAGE_{SIZE,SHIFT,MASK,ALIGN};
- page_cache_get() -> get_page();
- page_cache_release() -> put_page();
This patch contains automated changes generated with coccinelle using
script below. For some reason, coccinelle doesn't patch header files.
I've called spatch for them manually.
The only adjustment after coccinelle is revert of changes to
PAGE_CAHCE_ALIGN definition: we are going to drop it later.
There are few places in the code where coccinelle didn't reach. I'll
fix them manually in a separate patch. Comments and documentation also
will be addressed with the separate patch.
virtual patch
@@
expression E;
@@
- E << (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
expression E;
@@
- E >> (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
@@
- PAGE_CACHE_SHIFT
+ PAGE_SHIFT
@@
@@
- PAGE_CACHE_SIZE
+ PAGE_SIZE
@@
@@
- PAGE_CACHE_MASK
+ PAGE_MASK
@@
expression E;
@@
- PAGE_CACHE_ALIGN(E)
+ PAGE_ALIGN(E)
@@
expression E;
@@
- page_cache_get(E)
+ get_page(E)
@@
expression E;
@@
- page_cache_release(E)
+ put_page(E)
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-04-01 20:29:47 +08:00
|
|
|
put_page(to_page);
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 07:46:00 +08:00
|
|
|
ttm_tt_unpopulate(bdev, ttm);
|
2009-06-10 21:20:19 +08:00
|
|
|
ttm->swap_storage = swap_storage;
|
|
|
|
ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
|
|
|
|
|
2020-11-16 20:47:11 +08:00
|
|
|
return ttm->num_pages;
|
2020-09-17 19:27:35 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
out_err:
|
2020-09-17 19:27:35 +08:00
|
|
|
fput(swap_storage);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2010-02-20 10:22:21 +08:00
|
|
|
return ret;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
2014-01-03 18:47:23 +08:00
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
|
2018-02-01 21:52:50 +08:00
|
|
|
{
|
|
|
|
pgoff_t i;
|
|
|
|
|
|
|
|
if (ttm->page_flags & TTM_PAGE_FLAG_SG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i)
|
2020-08-25 07:46:00 +08:00
|
|
|
ttm->pages[i]->mapping = bdev->dev_mapping;
|
2020-11-16 20:47:11 +08:00
|
|
|
|
|
|
|
atomic_long_add(ttm->num_pages, &swapable_pages);
|
2018-02-01 21:52:50 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
int ttm_tt_populate(struct ttm_device *bdev,
|
2020-08-25 07:46:00 +08:00
|
|
|
struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
|
2018-02-01 21:39:29 +08:00
|
|
|
{
|
2018-02-01 21:52:50 +08:00
|
|
|
int ret;
|
|
|
|
|
2020-09-15 09:16:53 +08:00
|
|
|
if (!ttm)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-09-15 08:21:15 +08:00
|
|
|
if (ttm_tt_is_populated(ttm))
|
2018-02-01 21:39:29 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
if (bdev->funcs->ttm_tt_populate)
|
|
|
|
ret = bdev->funcs->ttm_tt_populate(bdev, ttm, ctx);
|
2018-02-22 19:00:05 +08:00
|
|
|
else
|
2020-10-24 19:10:28 +08:00
|
|
|
ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
|
2020-10-20 00:49:27 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ttm_tt_add_mapping(bdev, ttm);
|
|
|
|
ttm->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
|
2020-10-20 00:56:45 +08:00
|
|
|
if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
|
|
|
|
ret = ttm_tt_swapin(ttm);
|
|
|
|
if (unlikely(ret != 0)) {
|
|
|
|
ttm_tt_unpopulate(bdev, ttm);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 00:49:27 +08:00
|
|
|
return 0;
|
2018-02-01 21:39:29 +08:00
|
|
|
}
|
2020-09-15 09:16:53 +08:00
|
|
|
EXPORT_SYMBOL(ttm_tt_populate);
|
2018-02-01 21:39:29 +08:00
|
|
|
|
2014-01-03 18:47:23 +08:00
|
|
|
static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
|
|
|
|
{
|
|
|
|
pgoff_t i;
|
|
|
|
struct page **page = ttm->pages;
|
|
|
|
|
2014-02-05 16:18:26 +08:00
|
|
|
if (ttm->page_flags & TTM_PAGE_FLAG_SG)
|
|
|
|
return;
|
|
|
|
|
2014-01-03 18:47:23 +08:00
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
|
|
|
(*page)->mapping = NULL;
|
|
|
|
(*page++)->index = 0;
|
|
|
|
}
|
2020-11-16 20:47:11 +08:00
|
|
|
|
|
|
|
atomic_long_sub(ttm->num_pages, &swapable_pages);
|
2014-01-03 18:47:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 20:51:40 +08:00
|
|
|
void ttm_tt_unpopulate(struct ttm_device *bdev,
|
2020-08-25 07:46:00 +08:00
|
|
|
struct ttm_tt *ttm)
|
2014-01-03 18:47:23 +08:00
|
|
|
{
|
2020-09-15 08:21:15 +08:00
|
|
|
if (!ttm_tt_is_populated(ttm))
|
2014-01-03 18:47:23 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ttm_tt_clear_mapping(ttm);
|
2020-10-01 20:51:40 +08:00
|
|
|
if (bdev->funcs->ttm_tt_unpopulate)
|
|
|
|
bdev->funcs->ttm_tt_unpopulate(bdev, ttm);
|
2018-02-22 19:00:05 +08:00
|
|
|
else
|
2020-10-24 19:10:28 +08:00
|
|
|
ttm_pool_free(&bdev->pool, ttm);
|
2020-10-20 00:49:27 +08:00
|
|
|
ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
|
2014-01-03 18:47:23 +08:00
|
|
|
}
|
2020-11-16 20:47:11 +08:00
|
|
|
|
|
|
|
/* As long as pages are available make sure to release at least one */
|
|
|
|
static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink,
|
|
|
|
struct shrink_control *sc)
|
|
|
|
{
|
|
|
|
struct ttm_operation_ctx ctx = {
|
|
|
|
.no_wait_gpu = false
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ttm_bo_swapout(&ctx, GFP_NOFS);
|
|
|
|
return ret < 0 ? SHRINK_EMPTY : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of pages available or SHRINK_EMPTY if we have none */
|
|
|
|
static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink,
|
|
|
|
struct shrink_control *sc)
|
|
|
|
{
|
|
|
|
unsigned long num_pages;
|
|
|
|
|
|
|
|
num_pages = atomic_long_read(&swapable_pages);
|
|
|
|
return num_pages ? num_pages : SHRINK_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
|
|
|
|
/* Test the shrinker functions and dump the result */
|
|
|
|
static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
|
|
|
|
{
|
|
|
|
struct shrink_control sc = { .gfp_mask = GFP_KERNEL };
|
|
|
|
|
|
|
|
fs_reclaim_acquire(GFP_KERNEL);
|
|
|
|
seq_printf(m, "%lu/%lu\n", ttm_tt_shrinker_count(&mm_shrinker, &sc),
|
|
|
|
ttm_tt_shrinker_scan(&mm_shrinker, &sc));
|
|
|
|
fs_reclaim_release(GFP_KERNEL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ttm_tt_mgr_init - register with the MM shrinker
|
|
|
|
*
|
|
|
|
* Register with the MM shrinker for swapping out BOs.
|
|
|
|
*/
|
|
|
|
int ttm_tt_mgr_init(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
|
|
|
|
&ttm_tt_debugfs_shrink_fops);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mm_shrinker.count_objects = ttm_tt_shrinker_count;
|
|
|
|
mm_shrinker.scan_objects = ttm_tt_shrinker_scan;
|
|
|
|
mm_shrinker.seeks = 1;
|
|
|
|
return register_shrinker(&mm_shrinker);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ttm_tt_mgr_fini - unregister our MM shrinker
|
|
|
|
*
|
|
|
|
* Unregisters the MM shrinker.
|
|
|
|
*/
|
|
|
|
void ttm_tt_mgr_fini(void)
|
|
|
|
{
|
|
|
|
unregister_shrinker(&mm_shrinker);
|
|
|
|
}
|