Skip to content

Commit d4ad53a

Browse files
author
Thomas Hellström
committed
drm/ttm: Remove the struct ttm_backup abstraction
The abstraction was previously added to support separate ttm_backup implementations. However with the current implementation casting from a struct file to a struct ttm_backup, we run into trouble since struct file may have randomized the layout and gcc complains. Remove the struct ttm_backup abstraction Cc: dri-devel@lists.freedesktop.org Cc: Matthew Brost <matthew.brost@intel.com> Cc: Dave Airlie <airlied@redhat.com> Cc: Christian König <christian.koenig@amd.com> Cc: Matthew Auld <matthew.auld@intel.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Reported-by: Kees Cook <kees@kernel.org> Closes: https://lore.kernel.org/dri-devel/9c8dbbafdaf9f3f089da2cde5a772d69579b3795.camel@linux.intel.com/T/#mb153ab9216cb813b92bdeb36f391ad4808c2ba29 Suggested-by: Christian König <christian.koenig@amd.com> Fixes: 70d645d ("drm/ttm: Add helpers for shrinking") Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com> Reviewed-by: Christian König <christian.koenig@amd.com> Link: https://lore.kernel.org/r/20250502130014.3156-1-thomas.hellstrom@linux.intel.com
1 parent 2bb04ea commit d4ad53a

File tree

5 files changed

+21
-43
lines changed

5 files changed

+21
-43
lines changed

drivers/gpu/drm/ttm/ttm_backup.c

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@
77
#include <linux/page-flags.h>
88
#include <linux/swap.h>
99

10-
/*
11-
* Casting from randomized struct file * to struct ttm_backup * is fine since
12-
* struct ttm_backup is never defined nor dereferenced.
13-
*/
14-
static struct file *ttm_backup_to_file(struct ttm_backup *backup)
15-
{
16-
return (void *)backup;
17-
}
18-
19-
static struct ttm_backup *ttm_file_to_backup(struct file *file)
20-
{
21-
return (void *)file;
22-
}
23-
2410
/*
2511
* Need to map shmem indices to handle since a handle value
2612
* of 0 means error, following the swp_entry_t convention.
@@ -40,12 +26,12 @@ static pgoff_t ttm_backup_handle_to_shmem_idx(pgoff_t handle)
4026
* @backup: The struct backup pointer used to obtain the handle
4127
* @handle: The handle obtained from the @backup_page function.
4228
*/
43-
void ttm_backup_drop(struct ttm_backup *backup, pgoff_t handle)
29+
void ttm_backup_drop(struct file *backup, pgoff_t handle)
4430
{
4531
loff_t start = ttm_backup_handle_to_shmem_idx(handle);
4632

4733
start <<= PAGE_SHIFT;
48-
shmem_truncate_range(file_inode(ttm_backup_to_file(backup)), start,
34+
shmem_truncate_range(file_inode(backup), start,
4935
start + PAGE_SIZE - 1);
5036
}
5137

@@ -60,11 +46,10 @@ void ttm_backup_drop(struct ttm_backup *backup, pgoff_t handle)
6046
* Return: 0 on success, Negative error code on failure, notably
6147
* -EINTR if @intr was set to true and a signal is pending.
6248
*/
63-
int ttm_backup_copy_page(struct ttm_backup *backup, struct page *dst,
49+
int ttm_backup_copy_page(struct file *backup, struct page *dst,
6450
pgoff_t handle, bool intr)
6551
{
66-
struct file *filp = ttm_backup_to_file(backup);
67-
struct address_space *mapping = filp->f_mapping;
52+
struct address_space *mapping = backup->f_mapping;
6853
struct folio *from_folio;
6954
pgoff_t idx = ttm_backup_handle_to_shmem_idx(handle);
7055

@@ -106,12 +91,11 @@ int ttm_backup_copy_page(struct ttm_backup *backup, struct page *dst,
10691
* the folio size- and usage.
10792
*/
10893
s64
109-
ttm_backup_backup_page(struct ttm_backup *backup, struct page *page,
94+
ttm_backup_backup_page(struct file *backup, struct page *page,
11095
bool writeback, pgoff_t idx, gfp_t page_gfp,
11196
gfp_t alloc_gfp)
11297
{
113-
struct file *filp = ttm_backup_to_file(backup);
114-
struct address_space *mapping = filp->f_mapping;
98+
struct address_space *mapping = backup->f_mapping;
11599
unsigned long handle = 0;
116100
struct folio *to_folio;
117101
int ret;
@@ -161,9 +145,9 @@ ttm_backup_backup_page(struct ttm_backup *backup, struct page *page,
161145
*
162146
* After a call to this function, it's illegal to use the @backup pointer.
163147
*/
164-
void ttm_backup_fini(struct ttm_backup *backup)
148+
void ttm_backup_fini(struct file *backup)
165149
{
166-
fput(ttm_backup_to_file(backup));
150+
fput(backup);
167151
}
168152

169153
/**
@@ -194,14 +178,10 @@ EXPORT_SYMBOL_GPL(ttm_backup_bytes_avail);
194178
*
195179
* Create a backup utilizing shmem objects.
196180
*
197-
* Return: A pointer to a struct ttm_backup on success,
181+
* Return: A pointer to a struct file on success,
198182
* an error pointer on error.
199183
*/
200-
struct ttm_backup *ttm_backup_shmem_create(loff_t size)
184+
struct file *ttm_backup_shmem_create(loff_t size)
201185
{
202-
struct file *filp;
203-
204-
filp = shmem_file_setup("ttm shmem backup", size, 0);
205-
206-
return ttm_file_to_backup(filp);
186+
return shmem_file_setup("ttm shmem backup", size, 0);
207187
}

drivers/gpu/drm/ttm/ttm_pool.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ static void ttm_pool_allocated_page_commit(struct page *allocated,
506506
* if successful, populate the page-table and dma-address arrays.
507507
*/
508508
static int ttm_pool_restore_commit(struct ttm_pool_tt_restore *restore,
509-
struct ttm_backup *backup,
509+
struct file *backup,
510510
const struct ttm_operation_ctx *ctx,
511511
struct ttm_pool_alloc_state *alloc)
512512

@@ -655,7 +655,7 @@ static void ttm_pool_free_range(struct ttm_pool *pool, struct ttm_tt *tt,
655655
pgoff_t start_page, pgoff_t end_page)
656656
{
657657
struct page **pages = &tt->pages[start_page];
658-
struct ttm_backup *backup = tt->backup;
658+
struct file *backup = tt->backup;
659659
pgoff_t i, nr;
660660

661661
for (i = start_page; i < end_page; i += nr, pages += nr) {
@@ -963,7 +963,7 @@ void ttm_pool_drop_backed_up(struct ttm_tt *tt)
963963
long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt,
964964
const struct ttm_backup_flags *flags)
965965
{
966-
struct ttm_backup *backup = tt->backup;
966+
struct file *backup = tt->backup;
967967
struct page *page;
968968
unsigned long handle;
969969
gfp_t alloc_gfp;

drivers/gpu/drm/ttm/ttm_tt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ EXPORT_SYMBOL(ttm_tt_pages_limit);
544544
*/
545545
int ttm_tt_setup_backup(struct ttm_tt *tt)
546546
{
547-
struct ttm_backup *backup =
547+
struct file *backup =
548548
ttm_backup_shmem_create(((loff_t)tt->num_pages) << PAGE_SHIFT);
549549

550550
if (WARN_ON_ONCE(!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)))

include/drm/ttm/ttm_backup.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <linux/mm_types.h>
1010
#include <linux/shmem_fs.h>
1111

12-
struct ttm_backup;
13-
1412
/**
1513
* ttm_backup_handle_to_page_ptr() - Convert handle to struct page pointer
1614
* @handle: The handle to convert.
@@ -55,20 +53,20 @@ ttm_backup_page_ptr_to_handle(const struct page *page)
5553
return (unsigned long)page >> 1;
5654
}
5755

58-
void ttm_backup_drop(struct ttm_backup *backup, pgoff_t handle);
56+
void ttm_backup_drop(struct file *backup, pgoff_t handle);
5957

60-
int ttm_backup_copy_page(struct ttm_backup *backup, struct page *dst,
58+
int ttm_backup_copy_page(struct file *backup, struct page *dst,
6159
pgoff_t handle, bool intr);
6260

6361
s64
64-
ttm_backup_backup_page(struct ttm_backup *backup, struct page *page,
62+
ttm_backup_backup_page(struct file *backup, struct page *page,
6563
bool writeback, pgoff_t idx, gfp_t page_gfp,
6664
gfp_t alloc_gfp);
6765

68-
void ttm_backup_fini(struct ttm_backup *backup);
66+
void ttm_backup_fini(struct file *backup);
6967

7068
u64 ttm_backup_bytes_avail(void);
7169

72-
struct ttm_backup *ttm_backup_shmem_create(loff_t size);
70+
struct file *ttm_backup_shmem_create(loff_t size);
7371

7472
#endif

include/drm/ttm/ttm_tt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct ttm_tt {
118118
* ttm_tt_create() callback is responsible for assigning
119119
* this field.
120120
*/
121-
struct ttm_backup *backup;
121+
struct file *backup;
122122
/**
123123
* @caching: The current caching state of the pages, see enum
124124
* ttm_caching.

0 commit comments

Comments
 (0)