2018-05-02 13:46:21 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
2009-06-10 13:20:19 +00: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 04:43:50 +00:00
|
|
|
#define pr_fmt(fmt) "[TTM] " fmt
|
|
|
|
|
2009-06-10 13:20:19 +00:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/pagemap.h>
|
2011-06-27 23:18:17 +00:00
|
|
|
#include <linux/shmem_fs.h>
|
2009-06-10 13:20:19 +00:00
|
|
|
#include <linux/file.h>
|
2012-10-02 17:01:07 +00:00
|
|
|
#include <drm/drm_cache.h>
|
|
|
|
#include <drm/ttm/ttm_bo_driver.h>
|
|
|
|
#include <drm/ttm/ttm_page_alloc.h>
|
2018-08-01 05:49:33 +00:00
|
|
|
#include <drm/ttm/ttm_set_memory.h>
|
2009-06-10 13:20:19 +00:00
|
|
|
|
2018-02-22 07:54:57 +00:00
|
|
|
/**
|
|
|
|
* Allocates a ttm structure for the given BO.
|
|
|
|
*/
|
|
|
|
int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
|
|
|
|
{
|
|
|
|
struct ttm_bo_device *bdev = bo->bdev;
|
|
|
|
uint32_t page_flags = 0;
|
|
|
|
|
2019-08-11 08:06:32 +00:00
|
|
|
dma_resv_assert_held(bo->base.resv);
|
2018-02-22 07:54:57 +00:00
|
|
|
|
|
|
|
if (bdev->need_dma32)
|
|
|
|
page_flags |= TTM_PAGE_FLAG_DMA32;
|
|
|
|
|
|
|
|
if (bdev->no_retry)
|
|
|
|
page_flags |= TTM_PAGE_FLAG_NO_RETRY;
|
|
|
|
|
|
|
|
switch (bo->type) {
|
|
|
|
case ttm_bo_type_device:
|
|
|
|
if (zero_alloc)
|
|
|
|
page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
|
2018-02-22 07:54:57 +00:00
|
|
|
break;
|
2018-02-22 07:54:57 +00:00
|
|
|
case ttm_bo_type_kernel:
|
|
|
|
break;
|
|
|
|
case ttm_bo_type_sg:
|
2018-02-22 07:54:57 +00:00
|
|
|
page_flags |= TTM_PAGE_FLAG_SG;
|
2018-02-22 07:54:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-08-12 03:03:49 +00:00
|
|
|
bo->ttm = NULL;
|
2018-02-22 07:54:57 +00:00
|
|
|
pr_err("Illegal buffer object type\n");
|
2018-02-22 07:54:57 +00:00
|
|
|
return -EINVAL;
|
2018-02-22 07:54:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 09:18:14 +00:00
|
|
|
bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
|
2018-02-22 07:54:57 +00:00
|
|
|
if (unlikely(bo->ttm == NULL))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
2018-02-22 07:54:57 +00:00
|
|
|
}
|
|
|
|
|
2009-06-10 13:20:19 +00:00
|
|
|
/**
|
|
|
|
* Allocates storage for pointers to the pages that back the ttm.
|
|
|
|
*/
|
2018-01-25 18:24:03 +00:00
|
|
|
static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
2017-05-17 12:23:12 +00:00
|
|
|
ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
|
|
|
|
GFP_KERNEL | __GFP_ZERO);
|
2018-01-25 18:24:03 +00:00
|
|
|
if (!ttm->pages)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 18:24:03 +00:00
|
|
|
static int ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
2017-05-17 12:23:12 +00:00
|
|
|
ttm->ttm.pages = kvmalloc_array(ttm->ttm.num_pages,
|
2014-08-04 09:28:54 +00:00
|
|
|
sizeof(*ttm->ttm.pages) +
|
2017-05-17 12:23:12 +00:00
|
|
|
sizeof(*ttm->dma_address),
|
|
|
|
GFP_KERNEL | __GFP_ZERO);
|
2018-01-25 18:24:03 +00:00
|
|
|
if (!ttm->ttm.pages)
|
|
|
|
return -ENOMEM;
|
2016-09-16 09:32:26 +00:00
|
|
|
ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
|
2018-01-25 18:24:03 +00:00
|
|
|
return 0;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 14:12:00 +00:00
|
|
|
static int ttm_sg_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
|
|
|
|
{
|
|
|
|
ttm->dma_address = kvmalloc_array(ttm->ttm.num_pages,
|
|
|
|
sizeof(*ttm->dma_address),
|
|
|
|
GFP_KERNEL | __GFP_ZERO);
|
|
|
|
if (!ttm->dma_address)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:49:33 +00:00
|
|
|
static int ttm_tt_set_page_caching(struct page *p,
|
|
|
|
enum ttm_caching_state c_old,
|
|
|
|
enum ttm_caching_state c_new)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
2010-01-12 17:49:43 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2009-06-10 13:20:19 +00:00
|
|
|
if (PageHighMem(p))
|
|
|
|
return 0;
|
|
|
|
|
2010-02-19 21:30:15 +00:00
|
|
|
if (c_old != tt_cached) {
|
2010-01-12 17:49:43 +00:00
|
|
|
/* p isn't in the default caching state, set it to
|
|
|
|
* writeback first to free its current memtype. */
|
|
|
|
|
2018-08-01 05:49:33 +00:00
|
|
|
ret = ttm_set_pages_wb(p, 1);
|
2010-01-12 17:49:43 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
2010-01-12 17:49:43 +00:00
|
|
|
|
2010-02-19 21:30:15 +00:00
|
|
|
if (c_new == tt_wc)
|
2018-08-01 05:49:33 +00:00
|
|
|
ret = ttm_set_pages_wc(p, 1);
|
2010-02-19 21:30:15 +00:00
|
|
|
else if (c_new == tt_uncached)
|
2018-08-01 05:49:33 +00:00
|
|
|
ret = ttm_set_pages_uc(p, 1);
|
2010-01-12 17:49:43 +00:00
|
|
|
|
|
|
|
return ret;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct page *cur_page;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (ttm->caching_state == c_state)
|
|
|
|
return 0;
|
|
|
|
|
2010-04-01 12:44:57 +00:00
|
|
|
if (ttm->state == tt_unpopulated) {
|
|
|
|
/* Change caching but don't populate */
|
|
|
|
ttm->caching_state = c_state;
|
|
|
|
return 0;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ttm->caching_state == tt_cached)
|
2009-08-26 23:53:47 +00:00
|
|
|
drm_clflush_pages(ttm->pages, ttm->num_pages);
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
|
|
|
cur_page = ttm->pages[i];
|
|
|
|
if (likely(cur_page != NULL)) {
|
2010-02-19 21:30:15 +00:00
|
|
|
ret = ttm_tt_set_page_caching(cur_page,
|
|
|
|
ttm->caching_state,
|
|
|
|
c_state);
|
2009-06-10 13:20:19 +00:00
|
|
|
if (unlikely(ret != 0))
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ttm->caching_state = c_state;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_err:
|
|
|
|
for (j = 0; j < i; ++j) {
|
|
|
|
cur_page = ttm->pages[j];
|
|
|
|
if (likely(cur_page != NULL)) {
|
2010-02-19 21:30:15 +00:00
|
|
|
(void)ttm_tt_set_page_caching(cur_page, c_state,
|
2009-06-10 13:20:19 +00:00
|
|
|
ttm->caching_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
|
|
|
|
{
|
|
|
|
enum ttm_caching_state state;
|
|
|
|
|
|
|
|
if (placement & TTM_PL_FLAG_WC)
|
|
|
|
state = tt_wc;
|
|
|
|
else if (placement & TTM_PL_FLAG_UNCACHED)
|
|
|
|
state = tt_uncached;
|
|
|
|
else
|
|
|
|
state = tt_cached;
|
|
|
|
|
|
|
|
return ttm_tt_set_caching(ttm, state);
|
|
|
|
}
|
2009-10-30 03:31:26 +00:00
|
|
|
EXPORT_SYMBOL(ttm_tt_set_placement_caching);
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
void ttm_tt_destroy(struct ttm_tt *ttm)
|
|
|
|
{
|
2016-06-06 08:17:51 +00:00
|
|
|
if (ttm == NULL)
|
2009-06-10 13:20:19 +00:00
|
|
|
return;
|
|
|
|
|
2016-07-21 10:18:19 +00:00
|
|
|
ttm_tt_unbind(ttm);
|
2009-06-10 13:20:19 +00:00
|
|
|
|
2014-01-03 10:47:23 +00:00
|
|
|
if (ttm->state == tt_unbound)
|
|
|
|
ttm_tt_unpopulate(ttm);
|
2009-06-10 13:20:19 +00:00
|
|
|
|
2011-04-03 23:25:18 +00:00
|
|
|
if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
|
2009-06-10 13:20:19 +00:00
|
|
|
ttm->swap_storage)
|
|
|
|
fput(ttm->swap_storage);
|
|
|
|
|
2011-11-02 00:46:13 +00:00
|
|
|
ttm->swap_storage = NULL;
|
|
|
|
ttm->func->destroy(ttm);
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 11:59:01 +00:00
|
|
|
static void ttm_tt_init_fields(struct ttm_tt *ttm,
|
|
|
|
struct ttm_buffer_object *bo,
|
|
|
|
uint32_t page_flags)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
2018-02-22 09:18:14 +00:00
|
|
|
ttm->bdev = bo->bdev;
|
|
|
|
ttm->num_pages = bo->num_pages;
|
2009-06-10 13:20:19 +00:00
|
|
|
ttm->caching_state = tt_cached;
|
|
|
|
ttm->page_flags = page_flags;
|
2011-11-02 00:46:13 +00:00
|
|
|
ttm->state = tt_unpopulated;
|
2012-01-03 22:37:37 +00:00
|
|
|
ttm->swap_storage = NULL;
|
2018-02-28 08:48:22 +00:00
|
|
|
ttm->sg = bo->sg;
|
2018-02-23 14:12:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 09:18:14 +00:00
|
|
|
int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
|
|
|
|
uint32_t page_flags)
|
2018-02-23 14:12:00 +00:00
|
|
|
{
|
2018-02-22 09:18:14 +00:00
|
|
|
ttm_tt_init_fields(ttm, bo, page_flags);
|
2009-06-10 13:20:19 +00:00
|
|
|
|
2018-01-25 18:24:03 +00:00
|
|
|
if (ttm_tt_alloc_page_directory(ttm)) {
|
2012-03-17 04:43:50 +00:00
|
|
|
pr_err("Failed allocating page table\n");
|
2011-11-02 00:46:13 +00:00
|
|
|
return -ENOMEM;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
2011-11-02 00:46:13 +00:00
|
|
|
return 0;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
2011-11-02 00:46:13 +00:00
|
|
|
EXPORT_SYMBOL(ttm_tt_init);
|
2009-06-10 13:20:19 +00:00
|
|
|
|
2011-11-09 22:15:26 +00:00
|
|
|
void ttm_tt_fini(struct ttm_tt *ttm)
|
|
|
|
{
|
2017-05-17 12:23:12 +00:00
|
|
|
kvfree(ttm->pages);
|
2011-11-09 22:15:26 +00:00
|
|
|
ttm->pages = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_tt_fini);
|
|
|
|
|
2018-02-22 09:18:14 +00:00
|
|
|
int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
|
|
|
|
uint32_t page_flags)
|
2011-11-09 22:15:26 +00:00
|
|
|
{
|
|
|
|
struct ttm_tt *ttm = &ttm_dma->ttm;
|
|
|
|
|
2018-02-22 09:18:14 +00:00
|
|
|
ttm_tt_init_fields(ttm, bo, page_flags);
|
2011-11-09 22:15:26 +00:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&ttm_dma->pages_list);
|
2018-01-25 18:24:03 +00:00
|
|
|
if (ttm_dma_tt_alloc_page_directory(ttm_dma)) {
|
2012-03-17 04:43:50 +00:00
|
|
|
pr_err("Failed allocating page table\n");
|
2011-11-09 22:15:26 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_dma_tt_init);
|
|
|
|
|
2018-02-22 09:18:14 +00:00
|
|
|
int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
|
|
|
|
uint32_t page_flags)
|
2018-02-23 14:12:00 +00:00
|
|
|
{
|
|
|
|
struct ttm_tt *ttm = &ttm_dma->ttm;
|
|
|
|
int ret;
|
|
|
|
|
2018-02-22 09:18:14 +00:00
|
|
|
ttm_tt_init_fields(ttm, bo, page_flags);
|
2018-02-23 14:12:00 +00:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&ttm_dma->pages_list);
|
|
|
|
if (page_flags & TTM_PAGE_FLAG_SG)
|
|
|
|
ret = ttm_sg_tt_alloc_page_directory(ttm_dma);
|
|
|
|
else
|
|
|
|
ret = ttm_dma_tt_alloc_page_directory(ttm_dma);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed allocating page table\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_sg_tt_init);
|
|
|
|
|
2011-11-09 22:15:26 +00:00
|
|
|
void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
|
|
|
|
{
|
|
|
|
struct ttm_tt *ttm = &ttm_dma->ttm;
|
|
|
|
|
2018-02-23 14:12:00 +00:00
|
|
|
if (ttm->pages)
|
|
|
|
kvfree(ttm->pages);
|
|
|
|
else
|
|
|
|
kvfree(ttm_dma->dma_address);
|
2011-11-09 22:15:26 +00:00
|
|
|
ttm->pages = NULL;
|
|
|
|
ttm_dma->dma_address = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_dma_tt_fini);
|
|
|
|
|
2009-06-10 13:20:19 +00:00
|
|
|
void ttm_tt_unbind(struct ttm_tt *ttm)
|
|
|
|
{
|
|
|
|
if (ttm->state == tt_bound) {
|
2020-07-28 04:00:03 +00:00
|
|
|
ttm->func->unbind(ttm);
|
2009-06-10 13:20:19 +00:00
|
|
|
ttm->state = tt_unbound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:56:32 +00:00
|
|
|
int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem,
|
2017-12-21 09:42:51 +00:00
|
|
|
struct ttm_operation_ctx *ctx)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!ttm)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (ttm->state == tt_bound)
|
|
|
|
return 0;
|
|
|
|
|
2018-02-01 13:39:29 +00:00
|
|
|
ret = ttm_tt_populate(ttm, ctx);
|
2009-06-10 13:20:19 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2011-11-02 00:46:13 +00:00
|
|
|
ret = ttm->func->bind(ttm, bo_mem);
|
2010-10-29 08:46:49 +00:00
|
|
|
if (unlikely(ret != 0))
|
2009-06-10 13:20:19 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
ttm->state = tt_bound;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_tt_bind);
|
|
|
|
|
2011-11-03 03:59:28 +00:00
|
|
|
int ttm_tt_swapin(struct ttm_tt *ttm)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
|
|
|
struct address_space *swap_space;
|
|
|
|
struct file *swap_storage;
|
|
|
|
struct page *from_page;
|
|
|
|
struct page *to_page;
|
|
|
|
int i;
|
2010-02-20 02:22:21 +00:00
|
|
|
int ret = -ENOMEM;
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
swap_storage = ttm->swap_storage;
|
|
|
|
BUG_ON(swap_storage == NULL);
|
|
|
|
|
2015-12-05 04:45:44 +00:00
|
|
|
swap_space = swap_storage->f_mapping;
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
2017-12-22 13:12:40 +00:00
|
|
|
gfp_t gfp_mask = mapping_gfp_mask(swap_space);
|
|
|
|
|
|
|
|
gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
|
|
|
|
from_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
|
|
|
|
|
2010-02-20 02:22:21 +00:00
|
|
|
if (IS_ERR(from_page)) {
|
|
|
|
ret = PTR_ERR(from_page);
|
2009-06-10 13:20:19 +00:00
|
|
|
goto out_err;
|
2010-02-20 02:22:21 +00:00
|
|
|
}
|
2011-11-03 03:59:28 +00:00
|
|
|
to_page = ttm->pages[i];
|
2009-06-10 13:20:19 +00:00
|
|
|
if (unlikely(to_page == NULL))
|
|
|
|
goto out_err;
|
|
|
|
|
2012-09-25 11:57:02 +00: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 12:29:47 +00:00
|
|
|
put_page(from_page);
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 23:25:18 +00:00
|
|
|
if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
|
2009-06-10 13:20:19 +00:00
|
|
|
fput(swap_storage);
|
|
|
|
ttm->swap_storage = NULL;
|
|
|
|
ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
out_err:
|
2010-02-20 02:22:21 +00:00
|
|
|
return ret;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 23:25:18 +00:00
|
|
|
int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
|
2009-06-10 13:20:19 +00:00
|
|
|
{
|
|
|
|
struct address_space *swap_space;
|
|
|
|
struct file *swap_storage;
|
|
|
|
struct page *from_page;
|
|
|
|
struct page *to_page;
|
|
|
|
int i;
|
2010-02-20 02:22:21 +00:00
|
|
|
int ret = -ENOMEM;
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
|
|
|
|
BUG_ON(ttm->caching_state != tt_cached);
|
|
|
|
|
2011-04-03 23:25:18 +00:00
|
|
|
if (!persistent_swap_storage) {
|
2009-06-10 13:20:19 +00:00
|
|
|
swap_storage = shmem_file_setup("ttm swap",
|
|
|
|
ttm->num_pages << PAGE_SHIFT,
|
|
|
|
0);
|
2015-07-31 08:38:24 +00:00
|
|
|
if (IS_ERR(swap_storage)) {
|
2012-03-17 04:43:50 +00:00
|
|
|
pr_err("Failed allocating swap storage\n");
|
2010-02-20 02:22:21 +00:00
|
|
|
return PTR_ERR(swap_storage);
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
2018-01-25 18:29:42 +00:00
|
|
|
} else {
|
2011-04-03 23:25:18 +00:00
|
|
|
swap_storage = persistent_swap_storage;
|
2018-01-25 18:29:42 +00:00
|
|
|
}
|
2009-06-10 13:20:19 +00:00
|
|
|
|
2015-12-05 04:45:44 +00:00
|
|
|
swap_space = swap_storage->f_mapping;
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
2017-12-22 13:12:40 +00:00
|
|
|
gfp_t gfp_mask = mapping_gfp_mask(swap_space);
|
|
|
|
|
|
|
|
gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
|
|
|
|
|
2009-06-10 13:20:19 +00:00
|
|
|
from_page = ttm->pages[i];
|
|
|
|
if (unlikely(from_page == NULL))
|
|
|
|
continue;
|
2017-12-22 13:12:40 +00:00
|
|
|
|
|
|
|
to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
|
2015-07-31 08:38:24 +00:00
|
|
|
if (IS_ERR(to_page)) {
|
2010-02-20 02:22:21 +00:00
|
|
|
ret = PTR_ERR(to_page);
|
2009-06-10 13:20:19 +00:00
|
|
|
goto out_err;
|
2010-02-20 02:22:21 +00:00
|
|
|
}
|
2012-09-25 11:57:02 +00:00
|
|
|
copy_highpage(to_page, from_page);
|
2009-06-10 13:20:19 +00: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 12:29:47 +00:00
|
|
|
put_page(to_page);
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 10:47:23 +00:00
|
|
|
ttm_tt_unpopulate(ttm);
|
2009-06-10 13:20:19 +00:00
|
|
|
ttm->swap_storage = swap_storage;
|
|
|
|
ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
|
2011-04-03 23:25:18 +00:00
|
|
|
if (persistent_swap_storage)
|
|
|
|
ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
|
2009-06-10 13:20:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
out_err:
|
2011-04-03 23:25:18 +00:00
|
|
|
if (!persistent_swap_storage)
|
2009-06-10 13:20:19 +00:00
|
|
|
fput(swap_storage);
|
|
|
|
|
2010-02-20 02:22:21 +00:00
|
|
|
return ret;
|
2009-06-10 13:20:19 +00:00
|
|
|
}
|
2014-01-03 10:47:23 +00:00
|
|
|
|
2018-02-01 13:52:50 +00:00
|
|
|
static void ttm_tt_add_mapping(struct ttm_tt *ttm)
|
|
|
|
{
|
|
|
|
pgoff_t i;
|
|
|
|
|
|
|
|
if (ttm->page_flags & TTM_PAGE_FLAG_SG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < ttm->num_pages; ++i)
|
|
|
|
ttm->pages[i]->mapping = ttm->bdev->dev_mapping;
|
|
|
|
}
|
|
|
|
|
2018-02-01 13:39:29 +00:00
|
|
|
int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
|
|
|
|
{
|
2018-02-01 13:52:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-02-01 13:39:29 +00:00
|
|
|
if (ttm->state != tt_unpopulated)
|
|
|
|
return 0;
|
|
|
|
|
2018-02-22 11:00:05 +00:00
|
|
|
if (ttm->bdev->driver->ttm_tt_populate)
|
|
|
|
ret = ttm->bdev->driver->ttm_tt_populate(ttm, ctx);
|
|
|
|
else
|
|
|
|
ret = ttm_pool_populate(ttm, ctx);
|
2018-02-01 13:52:50 +00:00
|
|
|
if (!ret)
|
|
|
|
ttm_tt_add_mapping(ttm);
|
|
|
|
return ret;
|
2018-02-01 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 10:47:23 +00:00
|
|
|
static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
|
|
|
|
{
|
|
|
|
pgoff_t i;
|
|
|
|
struct page **page = ttm->pages;
|
|
|
|
|
2014-02-05 08:18:26 +00:00
|
|
|
if (ttm->page_flags & TTM_PAGE_FLAG_SG)
|
|
|
|
return;
|
|
|
|
|
2014-01-03 10:47:23 +00:00
|
|
|
for (i = 0; i < ttm->num_pages; ++i) {
|
|
|
|
(*page)->mapping = NULL;
|
|
|
|
(*page++)->index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ttm_tt_unpopulate(struct ttm_tt *ttm)
|
|
|
|
{
|
|
|
|
if (ttm->state == tt_unpopulated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ttm_tt_clear_mapping(ttm);
|
2018-02-22 11:00:05 +00:00
|
|
|
if (ttm->bdev->driver->ttm_tt_unpopulate)
|
|
|
|
ttm->bdev->driver->ttm_tt_unpopulate(ttm);
|
|
|
|
else
|
|
|
|
ttm_pool_unpopulate(ttm);
|
2014-01-03 10:47:23 +00:00
|
|
|
}
|