Commit 28a326c5 authored by Maarten Lankhorst's avatar Maarten Lankhorst Committed by Alex Deucher

drm/radeon: fixup locking inversion between, mmap_sem and reservations

op 08-10-13 18:58, Thomas Hellstrom schreef:
> On 10/08/2013 06:47 PM, Jerome Glisse wrote:
>> On Tue, Oct 08, 2013 at 06:29:35PM +0200, Thomas Hellstrom wrote:
>>> On 10/08/2013 04:55 PM, Jerome Glisse wrote:
>>>> On Tue, Oct 08, 2013 at 04:45:18PM +0200, Christian König wrote:
>>>>> Am 08.10.2013 16:33, schrieb Jerome Glisse:
>>>>>> On Tue, Oct 08, 2013 at 04:14:40PM +0200, Maarten Lankhorst wrote:
>>>>>>> Allocate and copy all kernel memory before doing reservations. This prevents a locking
>>>>>>> inversion between mmap_sem and reservation_class, and allows us to drop the trylocking
>>>>>>> in ttm_bo_vm_fault without upsetting lockdep.
>>>>>>>
>>>>>>> Signed-off-by: Maarten Lankhorst <maarten.lankhorst@canonical.com>
>>>>>> I would say NAK. Current code only allocate temporary page in AGP case.
>>>>>> So AGP case is userspace -> temp page -> cs checker -> radeon ib.
>>>>>>
>>>>>> Non AGP is directly memcpy to radeon IB.
>>>>>>
>>>>>> Your patch allocate memory memcpy userspace to it and it will then be
>>>>>> memcpy to IB. Which means you introduce an extra memcpy in the process
>>>>>> not something we want.
>>>>> Totally agree. Additional to that there is no good reason to provide
>>>>> anything else than anonymous system memory to the CS ioctl, so the
>>>>> dependency between the mmap_sem and reservations are not really
>>>>> clear to me.
>>>>>
>>>>> Christian.
>>>> I think is that in other code path you take mmap_sem first then reserve
>>>> bo. But here we reserve bo and then we take mmap_sem because of copy
>>> >from user.
>>>> Cheers,
>>>> Jerome
>>>>
>>> Actually the log message is a little confusing. I think the mmap_sem
>>> locking inversion problem is orthogonal to what's being fixed here.

> >>> This patch fixes the possible recursive bo::reserve caused by
> >>> malicious user-space handing a pointer to ttm memory so that the ttm
> >>> fault handler is called when bos are already reserved. That may
> >>> cause a (possibly interruptible) livelock.

>>> Once that is fixed, we are free to choose the mmap_sem ->
>>> bo::reserve locking order. Currently it's bo::reserve->mmap_sem(),
>>> but the hack required in the ttm fault handler is admittedly a bit
>>> ugly.  The plan is to change the locking order to
>>> mmap_sem->bo::reserve

> >>> I'm not sure if it applies to this particular case, but it should be
> >>> possible to make sure that copy_from_user_inatomic() will always
> >>> succeed, by making sure the pages are present using
> >>> get_user_pages(), and release the pages after
> >>> copy_from_user_inatomic() is done. That way there's no need for a
> >>> double memcpy slowpath, but if the copied data is very fragmented I
> >>> guess the resulting code may look ugly. The get_user_pages()
> >>> function will return an error if it hits TTM pages.

>>> /Thomas
>> get_user_pages + copy_from_user_inatomic is overkill. We should just
>> do get_user_pages which fails with ttm memory and then use copy_highpage
>> helper.
>>
>> Cheers,
>> Jerome
> Yeah, it may well be that that's the preferred solution.
>
> /Thomas
>
I still disagree, and shuffled radeon_ib_get around to be called sooner.

How does the patch below look?
8<-------
Allocate and copy all kernel memory before doing reservations. This prevents a locking
inversion between mmap_sem and reservation_class, and allows us to drop the trylocking
in ttm_bo_vm_fault without upsetting lockdep.

Changes since v1:
- Kill extra memcpy for !AGP case.
Signed-off-by: default avatarMaarten Lankhorst <maarten.lankhorst@canonical.com>
Reviewed-by: default avatarJerome Glisse <jglisse@redhat.com>
Signed-off-by: default avatarAlex Deucher <alexander.deucher@amd.com>
parent db96bd25
...@@ -2328,13 +2328,8 @@ static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error) ...@@ -2328,13 +2328,8 @@ static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error)
unsigned i; unsigned i;
kfree(parser->relocs); kfree(parser->relocs);
for (i = 0; i < parser->nchunks; i++) { for (i = 0; i < parser->nchunks; i++)
kfree(parser->chunks[i].kdata); drm_free_large(parser->chunks[i].kdata);
if (parser->rdev && (parser->rdev->flags & RADEON_IS_AGP)) {
kfree(parser->chunks[i].kpage[0]);
kfree(parser->chunks[i].kpage[1]);
}
}
kfree(parser->chunks); kfree(parser->chunks);
kfree(parser->chunks_array); kfree(parser->chunks_array);
} }
...@@ -2391,13 +2386,12 @@ int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp, ...@@ -2391,13 +2386,12 @@ int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp,
ib_chunk = &parser.chunks[parser.chunk_ib_idx]; ib_chunk = &parser.chunks[parser.chunk_ib_idx];
parser.ib.length_dw = ib_chunk->length_dw; parser.ib.length_dw = ib_chunk->length_dw;
*l = parser.ib.length_dw; *l = parser.ib.length_dw;
r = r600_cs_parse(&parser); if (DRM_COPY_FROM_USER(ib, ib_chunk->user_ptr, ib_chunk->length_dw * 4)) {
if (r) { r = -EFAULT;
DRM_ERROR("Invalid command stream !\n");
r600_cs_parser_fini(&parser, r); r600_cs_parser_fini(&parser, r);
return r; return r;
} }
r = radeon_cs_finish_pages(&parser); r = r600_cs_parse(&parser);
if (r) { if (r) {
DRM_ERROR("Invalid command stream !\n"); DRM_ERROR("Invalid command stream !\n");
r600_cs_parser_fini(&parser, r); r600_cs_parser_fini(&parser, r);
......
...@@ -973,12 +973,8 @@ struct radeon_cs_reloc { ...@@ -973,12 +973,8 @@ struct radeon_cs_reloc {
struct radeon_cs_chunk { struct radeon_cs_chunk {
uint32_t chunk_id; uint32_t chunk_id;
uint32_t length_dw; uint32_t length_dw;
int kpage_idx[2];
uint32_t *kpage[2];
uint32_t *kdata; uint32_t *kdata;
void __user *user_ptr; void __user *user_ptr;
int last_copied_page;
int last_page_index;
}; };
struct radeon_cs_parser { struct radeon_cs_parser {
...@@ -1013,8 +1009,15 @@ struct radeon_cs_parser { ...@@ -1013,8 +1009,15 @@ struct radeon_cs_parser {
struct ww_acquire_ctx ticket; struct ww_acquire_ctx ticket;
}; };
extern int radeon_cs_finish_pages(struct radeon_cs_parser *p); static inline u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
extern u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx); {
struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
if (ibc->kdata)
return ibc->kdata[idx];
return p->ib.ptr[idx];
}
struct radeon_cs_packet { struct radeon_cs_packet {
unsigned idx; unsigned idx;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment