<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div dir="auto">
<div><br>
<div class="gmail_extra"><br>
<div class="gmail_quote">Am 21.08.2019 20:28 schrieb "Thomas Hellström (VMware)" <thomas_os@shipmail.org>:<br type="attribution">
<blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><font size="2"><span style="font-size:11pt">
<div>On 8/21/19 8:11 PM, Daniel Vetter wrote:<br>
> On Wed, Aug 21, 2019 at 7:06 PM Thomas Hellström (VMware)<br>
> <thomas_os@shipmail.org> wrote:<br>
>> On 8/21/19 6:34 PM, Daniel Vetter wrote:<br>
>>> On Wed, Aug 21, 2019 at 05:54:27PM +0200, Thomas Hellström (VMware) wrote:<br>
>>>> On 8/20/19 4:53 PM, Daniel Vetter wrote:<br>
>>>>> Full audit of everyone:<br>
>>>>><br>
>>>>> - i915, radeon, amdgpu should be clean per their maintainers.<br>
>>>>><br>
>>>>> - vram helpers should be fine, they don't do command submission, so<br>
>>>>> really no business holding struct_mutex while doing copy_*_user. But<br>
>>>>> I haven't checked them all.<br>
>>>>><br>
>>>>> - panfrost seems to dma_resv_lock only in panfrost_job_push, which<br>
>>>>> looks clean.<br>
>>>>><br>
>>>>> - v3d holds dma_resv locks in the tail of its v3d_submit_cl_ioctl(),<br>
>>>>> copying from/to userspace happens all in v3d_lookup_bos which is<br>
>>>>> outside of the critical section.<br>
>>>>><br>
>>>>> - vmwgfx has a bunch of ioctls that do their own copy_*_user:<br>
>>>>> - vmw_execbuf_process: First this does some copies in<br>
>>>>> vmw_execbuf_cmdbuf() and also in the vmw_execbuf_process() itself.<br>
>>>>> Then comes the usual ttm reserve/validate sequence, then actual<br>
>>>>> submission/fencing, then unreserving, and finally some more<br>
>>>>> copy_to_user in vmw_execbuf_copy_fence_user. Glossing over tons of<br>
>>>>> details, but looks all safe.<br>
>>>>> - vmw_fence_event_ioctl: No ttm_reserve/dma_resv_lock anywhere to be<br>
>>>>> seen, seems to only create a fence and copy it out.<br>
>>>>> - a pile of smaller ioctl in vmwgfx_ioctl.c, no reservations to be<br>
>>>>> found there.<br>
>>>>> Summary: vmwgfx seems to be fine too.<br>
>>>>><br>
>>>>> - virtio: There's virtio_gpu_execbuffer_ioctl, which does all the<br>
>>>>> copying from userspace before even looking up objects through their<br>
>>>>> handles, so safe. Plus the getparam/getcaps ioctl, also both safe.<br>
>>>>><br>
>>>>> - qxl only has qxl_execbuffer_ioctl, which calls into<br>
>>>>> qxl_process_single_command. There's a lovely comment before the<br>
>>>>> __copy_from_user_inatomic that the slowpath should be copied from<br>
>>>>> i915, but I guess that never happened. Try not to be unlucky and get<br>
>>>>> your CS data evicted between when it's written and the kernel tries<br>
>>>>> to read it. The only other copy_from_user is for relocs, but those<br>
>>>>> are done before qxl_release_reserve_list(), which seems to be the<br>
>>>>> only thing reserving buffers (in the ttm/dma_resv sense) in that<br>
>>>>> code. So looks safe.<br>
>>>>><br>
>>>>> - A debugfs file in nouveau_debugfs_pstate_set() and the usif ioctl in<br>
>>>>> usif_ioctl() look safe. nouveau_gem_ioctl_pushbuf() otoh breaks this<br>
>>>>> everywhere and needs to be fixed up.<br>
>>>>><br>
>>>>> Cc: Alex Deucher <alexander.deucher@amd.com><br>
>>>>> Cc: Christian König <christian.koenig@amd.com><br>
>>>>> Cc: Chris Wilson <chris@chris-wilson.co.uk><br>
>>>>> Cc: Thomas Zimmermann <tzimmermann@suse.de><br>
>>>>> Cc: Rob Herring <robh@kernel.org><br>
>>>>> Cc: Tomeu Vizoso <tomeu.vizoso@collabora.com><br>
>>>>> Cc: Eric Anholt <eric@anholt.net><br>
>>>>> Cc: Dave Airlie <airlied@redhat.com><br>
>>>>> Cc: Gerd Hoffmann <kraxel@redhat.com><br>
>>>>> Cc: Ben Skeggs <bskeggs@redhat.com><br>
>>>>> Cc: "VMware Graphics" <linux-graphics-maintainer@vmware.com><br>
>>>>> Cc: Thomas Hellstrom <thellstrom@vmware.com><br>
>>>>> Signed-off-by: Daniel Vetter <daniel.vetter@intel.com><br>
>>>>> ---<br>
>>>>> drivers/dma-buf/dma-resv.c | 12 ++++++++++++<br>
>>>>> 1 file changed, 12 insertions(+)<br>
>>>>><br>
>>>>> diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c<br>
>>>>> index 42a8f3f11681..3edca10d3faf 100644<br>
>>>>> --- a/drivers/dma-buf/dma-resv.c<br>
>>>>> +++ b/drivers/dma-buf/dma-resv.c<br>
>>>>> @@ -34,6 +34,7 @@<br>
>>>>> #include <linux/dma-resv.h><br>
>>>>> #include <linux/export.h><br>
>>>>> +#include <linux/sched/mm.h><br>
>>>>> /**<br>
>>>>> * DOC: Reservation Object Overview<br>
>>>>> @@ -107,6 +108,17 @@ void dma_resv_init(struct dma_resv *obj)<br>
>>>>> &reservation_seqcount_class);<br>
>>>>> RCU_INIT_POINTER(obj->fence, NULL);<br>
>>>>> RCU_INIT_POINTER(obj->fence_excl, NULL);<br>
>>>>> +<br>
>>>>> + if (IS_ENABLED(CONFIG_LOCKDEP)) {<!-- --><br>
>>>>> + if (current->mm)<br>
>>>>> + down_read(¤t->mm->mmap_sem);<br>
>>>>> + ww_mutex_lock(&obj->lock, NULL);<br>
>>>>> + fs_reclaim_acquire(GFP_KERNEL);<br>
>>>>> + fs_reclaim_release(GFP_KERNEL);<br>
>>>>> + ww_mutex_unlock(&obj->lock);<br>
>>>>> + if (current->mm)<br>
>>>>> + up_read(¤t->mm->mmap_sem);<br>
>>>>> + }<br>
>>>>> }<br>
>>>>> EXPORT_SYMBOL(dma_resv_init);<br>
>>>> I assume if this would have been easily done and maintainable using only<br>
>>>> lockdep annotation instead of actually acquiring the locks, that would have<br>
>>>> been done?<br>
>>> There's might_lock(), plus a pile of macros, but they don't map obviuosly,<br>
>>> so pretty good chances I accidentally end up with the wrong type of<br>
>>> annotation. Easier to just take the locks quickly, and stuff that all into<br>
>>> a lockdep-only section to avoid overhead.<br>
>>><br>
>>>> Otherwise LGTM.<br>
>>>><br>
>>>> Reviewed-by: Thomas Hellström <thellstrom@vmware.com><br>
>>>><br>
>>>> Will test this and let you know if it trips on vmwgfx, but it really<br>
>>>> shouldn't.<br>
>>> Thanks, Daniel<br>
>> One thing that strikes me is that this puts restrictions on where you<br>
>> can actually initialize a dma_resv, even if locking orders are otherwise<br>
>> obeyed. But that might not be a big problem.<br>
> Hm yeah ... the trouble is a need a non-kthread thread so that I have<br>
> a current->mm. Otherwise I'd have put it into some init section with a<br>
> temp dma_buf. And I kinda don't want to create a fake ->mm just for<br>
> lockdep priming. I don't expect this to be a real problem in practice,<br>
> since before you've called dma_resv_init the reservation lock doesn't<br>
> exist, so you can't hold it. And you've probably just allocated it, so<br>
> fs_reclaim is going to be fine. And if you allocate dma_resv objects<br>
> from your fault handlers I have questions anyway :-)<br>
<br>
Coming to think of it, I think vmwgfx sometimes create bos with other <br>
bo's reservation lock held. I guess that would trip both the mmap_sem <br>
check the ww_mutex check?<br>
</div>
</span></font></div>
</blockquote>
</div>
</div>
</div>
<div dir="auto"><br>
</div>
<div dir="auto">Dito inside TTM for ghost objects.</div>
<div dir="auto"><br>
</div>
<div dir="auto">We even used to have an dma_resv_init() in an atomic section, but that should be gone by now.</div>
<div dir="auto"><br>
</div>
<div dir="auto">Anyway, probably have to take my review back since this will certainly go up in flames.</div>
<div dir="auto"><br>
</div>
<div dir="auto">Need a better place for this,</div>
<div dir="auto">Christian.</div>
<div dir="auto"><br>
</div>
<div dir="auto">
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><font size="2"><span style="font-size:11pt">
<div><br>
/Thomas<br>
<br>
<br>
/Thomas<br>
<br>
<br>
<br>
<br>
><br>
> So I think this should be safe.<br>
> -Daniel<br>
<br>
<br>
</div>
</span></font></div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</body>
</html>