Skip to contentSkip to conversation

Where do ttm->pages come from?

DISCUSSION Matthew Wilcox 2 participants 4 messages Mar 18, 2025

lore
Share & tools
lore
#0 lore raw

We're close to removing page->mapping from the kernel, and I've run into amdgpu's use of page->mapping (setting it to ->dev_mapping and comparing it against ->dev_mapping).

What I need to understand to know how to fix this properly is who allocates these pages for what purpose? I've got this far:

amdgpu_ttm_tt_set_user_pages() populates ttm->pages. That's called from amdgpu_cs_parser_bos() which got e->userpages from amdgpu_ttm_tt_get_user_pages(). amdgpu_ttm_tt_get_user_pages() got them from amdgpu_hmm_range_get_pages()
amdgpu_hmm_range_get_pages() gets them from calling hmm_pfn_to_page() which ... could get pages from anywhere?

But cleaarly they can't be _any_ pages because if they were anon or file pages, overwriting page->mapping would blow up horribly today. So they must be driver pages, but I can't see where they're allocated. Is it ttm_pool_alloc_page()?

#1 lore raw

Hi Matthew,

you actually got your understanding exactly inverted :)

Amdgpu takes a look at page->mapping to make sure they are *not* coming from the call sequence starting with amdgpu_ttm_tt_set_user_pages() or be imported from another driver through DMA-buf etc...

The only pages which are allowed to pass this check are the private driver allocated ones, see this code function amdgpu_ttm_tt_populate():

        for (i = 0; i < ttm->num_pages; ++i) ttm->pages[i]->mapping = bdev->dev_mapping;

So far that has been save since nobody else should be using the address space object from our drivers inode under /dev.

But when you plan to remove page->mapping we probably have a problem here.

Regards,
Christian.

Am 17.03.25 um 20:37 schrieb Matthew Wilcox:

Quoted 19 lines

We're close to removing page->mapping from the kernel, and I've run into amdgpu's use of page->mapping (setting it to ->dev_mapping and comparing it against ->dev_mapping).

What I need to understand to know how to fix this properly is who allocates these pages for what purpose? I've got this far:

amdgpu_ttm_tt_set_user_pages() populates ttm->pages. That's called from amdgpu_cs_parser_bos() which got e->userpages from amdgpu_ttm_tt_get_user_pages(). amdgpu_ttm_tt_get_user_pages() got them from amdgpu_hmm_range_get_pages()
amdgpu_hmm_range_get_pages() gets them from calling hmm_pfn_to_page() which ... could get pages from anywhere?

But cleaarly they can't be _any_ pages because if they were anon or file pages, overwriting page->mapping would blow up horribly today. So they must be driver pages, but I can't see where they're allocated. Is it ttm_pool_alloc_page()?

#2 lore raw
Quoted 3 lines · Christian König

Hi Matthew,

you actually got your understanding exactly inverted :)

Thanks; that's very helpful!

Quoted 10 lines

Amdgpu takes a look at page->mapping to make sure they are *not* coming from the call sequence starting with amdgpu_ttm_tt_set_user_pages() or be imported from another driver through DMA-buf etc...

The only pages which are allowed to pass this check are the private driver allocated ones, see this code function amdgpu_ttm_tt_populate():

        for (i = 0; i < ttm->num_pages; ++i) ttm->pages[i]->mapping = bdev->dev_mapping;

So far that has been save since nobody else should be using the address space object from our drivers inode under /dev.

But when you plan to remove page->mapping we probably have a problem here.

OK. This makes the problem a lot more tractable.

From the MM side, we have a short-term and a long-term goal. The long-term goal is to reduce struct page to a single tagged pointer called a memdesc. Details here:

https://kernelnewbies.org/MatthewWilcox/Memdescs

In the short term, we're removing page->mapping, ->index, ->lru and ->memcg_data, leaving ->flags, ->_refcount and ->private as well as the newly-introduced ->memdesc.

So if all you needed to do was identify which pages belong to you, I'd suggest setting ->private to a unique value (eg bdev->dev_mapping). But it feels like you need more than that; you need a refcount, and you need to map pages to userspace. Do you need to support GUP on these pages (ie can it be the user buffer for O_DIRECT, can it be registered with RDMA, that kind of thing), or is this memory just for userspace to do load/store, and no fancy stuff is allowed?

At this point, I'd suggest that you allocate folios rather than pages. That lets us continue to use folio->mapping to distinguish these pages. There would be an opportunity to save some memory in the future by using a slimmer control structure once we understand the new system better, but maybe it's OK to just keep using a folio forever.

Thoughts?

#3 lore raw

Am 18.03.25 um 16:01 schrieb Matthew Wilcox:

Quoted 22 lines · Christian König
Quoted 3 lines

Hi Matthew,

you actually got your understanding exactly inverted :)

Thanks; that's very helpful!

Quoted 10 lines

Amdgpu takes a look at page->mapping to make sure they are *not* coming from the call sequence starting with amdgpu_ttm_tt_set_user_pages() or be imported from another driver through DMA-buf etc...

The only pages which are allowed to pass this check are the private driver allocated ones, see this code function amdgpu_ttm_tt_populate():

        for (i = 0; i < ttm->num_pages; ++i) ttm->pages[i]->mapping = bdev->dev_mapping;

So far that has been save since nobody else should be using the address space object from our drivers inode under /dev.

But when you plan to remove page->mapping we probably have a problem here.

OK. This makes the problem a lot more tractable.

From the MM side, we have a short-term and a long-term goal. The long-term goal is to reduce struct page to a single tagged pointer called a memdesc. Details here:

Quoted 8 lines

https://kernelnewbies.org/MatthewWilcox/Memdescs

In the short term, we're removing page->mapping, ->index, ->lru and ->memcg_data, leaving ->flags, ->_refcount and ->private as well as the newly-introduced ->memdesc.

So if all you needed to do was identify which pages belong to you, I'd suggest setting ->private to a unique value (eg bdev->dev_mapping).

The problem is ->private is already used for something else in that use case. See the code in ttm_pool.c.

Quoted 5 lines

But it feels like you need more than that; you need a refcount, and you need to map pages to userspace. Do you need to support GUP on these pages (ie can it be the user buffer for O_DIRECT, can it be registered with RDMA, that kind of thing), or is this memory just for userspace to do load/store, and no fancy stuff is allowed?

GUP is actually strictly forbidden. We have a repeating problem with people trying to enable that and running into data corruption after a while.

Userspace mapping is just using the PFN, so no refcount needed either.

Quoted 7 lines

At this point, I'd suggest that you allocate folios rather than pages. That lets us continue to use folio->mapping to distinguish these pages. There would be an opportunity to save some memory in the future by using a slimmer control structure once we understand the new system better, but maybe it's OK to just keep using a folio forever.

Thoughts?

Yeah, switching to folios in TTM is on the TODO list for like as long as folios exist. But that is easier said than done.

Regards,
Christian.