On Fri, Nov 29, 2024 at 12:44:10PM +0100, Alice Ryhl wrote: > On Wed, Nov 27, 2024 at 4:41 PM Jann Horn <jannh@xxxxxxxxxx> wrote: > > > > On Wed, Nov 27, 2024 at 1:01 PM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote: > > > On Tue, Nov 26, 2024 at 11:10 PM Jann Horn <jannh@xxxxxxxxxx> wrote: > > > > > > > > On Fri, Nov 22, 2024 at 4:41 PM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote: > > > > > This adds a type called VmAreaRef which is used when referencing a vma > > > > > that you have read access to. Here, read access means that you hold > > > > > either the mmap read lock or the vma read lock (or stronger). > > > > > > > > > > Additionally, a vma_lookup method is added to the mmap read guard, which > > > > > enables you to obtain a &VmAreaRef in safe Rust code. > > > > > > > > > > This patch only provides a way to lock the mmap read lock, but a > > > > > follow-up patch also provides a way to just lock the vma read lock. > > > > > > > > > > Acked-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx> (for mm bits) > > > > > Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx> > > > > > > > > Reviewed-by: Jann Horn <jannh@xxxxxxxxxx> > > > > > > Thanks! > > > > > > > with one comment: > > > > > > > > > + /// Zap pages in the given page range. > > > > > + /// > > > > > + /// This clears page table mappings for the range at the leaf level, leaving all other page > > > > > + /// tables intact, and freeing any memory referenced by the VMA in this range. That is, > > > > > + /// anonymous memory is completely freed, file-backed memory has its reference count on page > > > > > + /// cache folio's dropped, any dirty data will still be written back to disk as usual. > > > > > + #[inline] > > > > > + pub fn zap_page_range_single(&self, address: usize, size: usize) { > > > > > + // SAFETY: By the type invariants, the caller has read access to this VMA, which is > > > > > + // sufficient for this method call. This method has no requirements on the vma flags. Any > > > > > + // value of `address` and `size` is allowed. > > > > > > > > If we really want to allow any address and size, we might want to add > > > > an early bailout in zap_page_range_single(). The comment on top of > > > > zap_page_range_single() currently says "The range must fit into one > > > > VMA", and it looks like by the point we reach a bailout, we could have > > > > gone through an interval tree walk via > > > > mmu_notifier_invalidate_range_start()->__mmu_notifier_invalidate_range_start()->mn_itree_invalidate() > > > > for a range that ends before it starts; I don't know how safe that is. > > > > > > I could change the comment on zap_page_range_single() to say: > > > > > > "The range should be contained within a single VMA. Otherwise an error > > > is returned." > > > > > > And then I can add an overflow check at the top of > > > zap_page_range_single(). Sounds ok? > > > > Yes, I think changing the comment like that and adding a check for > > whether address+size wraps around there addresses this. > > Hmm. Looking at this again now ... > > For one, the function returns void so we can at best handle overflow > by performing a no-op. > > Another question is, are we actually okay to call it with ranges > outside the vma? Does that just no-op or what? Maybe the Rust side > should just bail out early if the address range is not a subset of the > vma? In unmap_single_vma() invoked by zap_page_range_single() we check this: unsigned long start = max(vma->vm_start, start_addr); ... if (start >= vma->vm_end) return; end = min(vma->vm_end, end_addr); if (end <= vma->vm_start) return; So you could specify start <= vma->vm_end and end > vma->vm_start and it'll get clamped like: 0 MAX start <-clamp-> <---------------------->xxxxxxxxxxxxxxxxxxxx |--------| VMA xxxxxxxxxxxxxxxx<--------------------------> <-clamp-> However note that we will tell mmum_notifier_range_init() and hugetlb_zap_begin() incorrect values in this case... not sure. I'd prefer if rust would strictly only allowed ranges within the VMA. > > Alice