Re: [PATCH v8 2/2] mm: remove zap_page_range and change callers to use zap_vma_range

Mike Kravetz
2022-11-10T22:18:13Z

On 11/10/22 14:02, Nadav Amit wrote:
> On Nov 10, 2022, at 1:27 PM, Peter Xu <peterx@redhat.com> wrote:
>
> > Hi, Nadav,
> >
> > On Thu, Nov 10, 2022 at 01:09:43PM -0800, Nadav Amit wrote:
> >> But, are the callers really able to guarantee that the ranges are all in a
> >> single VMA? I am not familiar with the users, but how for instance
> >> tcp_zerocopy_receive() can guarantee that no one did some mprotect() of some
> >> sorts that caused the original VMA to be split?
> >
> > Let me try to answer this one for Mike.. We have two callers in tcp
> > zerocopy code for this function:
> >
> > tcp_zerocopy_vm_insert_batch_error[2095] zap_page_range(vma, *address, maybe_zap_len);
> > tcp_zerocopy_receive[2237] zap_page_range(vma, address, total_bytes_to_map);
> >
> > Both of them take the mmap lock for read, so firstly mprotect is not
> > possible.
> >
> > The 1st call has:
> >
> > mmap_read_lock(current->mm);
> >
> > vma = vma_lookup(current->mm, address);
> > if (!vma || vma->vm_ops != &tcp_vm_ops) {
> > mmap_read_unlock(current->mm);
> > return -EINVAL;
> > }
> > vma_len = min_t(unsigned long, zc->length, vma->vm_end - address);
> > avail_len = min_t(u32, vma_len, inq);
> > total_bytes_to_map = avail_len & ~(PAGE_SIZE - 1);
> > if (total_bytes_to_map) {
> > if (!(zc->flags & TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT))
> > zap_page_range(vma, address, total_bytes_to_map);
> >
> > Here total_bytes_to_map comes from avail_len <--- vma_len, which is a min()
> > of the rest vma range. So total_bytes_to_map will never go beyond the vma.
> >
> > The 2nd call uses maybe_zap_len as len, we need to look two layers of the
> > callers, but ultimately it's something smaller than total_bytes_to_map we
> > discussed. Hopefully it proves 100% safety on tcp zerocopy.
>
> Thanks Peter for the detailed explanation.
>
> I had another look at the code and indeed it should not break. I am not sure
> whether users who zero-copy receive and mprotect() part of the memory would
> not be surprised, but I guess that’s a different story, which I should
> further study at some point.

I did audit all calling sites and am fairly certain passed ranges are within
a single vma. Because of this, Peter suggested removing zap_page_range. If
there is concern, we can just fix up the mmu notifiers in zap_page_range and
leave it. This is what is done in the patch which is currently in
mm-hotfixes-unstable.

--
Mike Kravetz