Re: [PATCH v2 bpf-next 1/4] bpf: introduce task_vma bpf_iter
From: Song Liu <hidden>
Date: 2020-12-18 17:24:30
Also in:
bpf
On Dec 18, 2020, at 8:38 AM, Yonghong Song [off-list ref] wrote: On 12/17/20 9:23 PM, Alexei Starovoitov wrote:quoted
On Thu, Dec 17, 2020 at 8:33 PM Song Liu [off-list ref] wrote:quoted
quoted
ahh. I missed that. Makes sense. vm_file needs to be accurate, but vm_area_struct should be accessed as ptr_to_btf_id.Passing pointer of vm_area_struct into BPF will be tricky. For example, shall we allow the user to access vma->vm_file? IIUC, with ptr_to_btf_id the verifier will allow access of vma->vm_file as a valid pointer to struct file. However, since the vma might be freed, vma->vm_file could point to random data.I don't think so. The proposed patch will do get_file() on it. There is actually no need to assign it into a different variable. Accessing it via vma->vm_file is safe and cleaner.I did not check the code but do you have scenarios where vma is freed but old vma->vm_file is not freed due to reference counting, but freed vma area is reused so vma->vm_file could be garbage?
AFAIK, once we unlock mmap_sem, the vma could be freed and reused. I guess ptr_to_btf_id or probe_read would not help with this? Thanks, Song
quoted
quoted
quoted
quoted
[1] ff9f47f6f00c ("mm: proc: smaps_rollup: do not stall write attempts on mmap_lock")Thanks for this link. With "if (mmap_lock_is_contended())" check it should work indeed.To make sure we are on the same page: I am using slightly different mechanism in task_vma_iter, which doesn't require checking mmap_lock_is_contended(). In the smaps_rollup case, the code only unlock mmap_sem when the lock is contended. In task_iter, we always unlock mmap_sem between two iterations. This is because we don't want to hold mmap_sem while calling the BPF program, which may sleep (calling bpf_d_path).That part is clear. I had to look into mmap_read_lock_killable() implementation to realize that it's checking for lock_is_contended after acquiring and releasing if there is a contention. So it's the same behavior at the end.