Skip to contentSkip to conversation

mm: introduce MADV_COOL

RFC Hillf Danton 2 participants 2 messages May 28, 2019 Memory management

lore
Share & tools
lore
#0 lore raw
Quoted 38 lines · On Tue, 28 May 2019 18:58:15 +0800 Minchan Kim wrote:
Quoted 16 lines · Hillf Danton

Review

Quoted 12 lines · On Mon, 20 May 2019 12:52:48 +0900 Minchan Kim wrote:
(quoted diff) +12 -0
+static int madvise_cool_pte_range(pmd_t *pmd, unsigned long addr,
+				unsigned long end, struct mm_walk *walk)
+{
+	pte_t *orig_pte, *pte, ptent;
+	spinlock_t *ptl;
+	struct page *page;
+	struct vm_area_struct *vma = walk->vma;
+	unsigned long next;
+
+	next = pmd_addr_end(addr, end);
+	if (pmd_trans_huge(*pmd)) {
+		spinlock_t *ptl;

Seems not needed with another ptl declared above.

Will remove it.

Quoted 9 lines
Quoted 9 lines
(quoted diff) +9 -0
+
+		ptl = pmd_trans_huge_lock(pmd, vma);
+		if (!ptl)
+			return 0;
+
+		if (is_huge_zero_pmd(*pmd))
+			goto huge_unlock;
+
+		page = pmd_page(*pmd);
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+		if (page_mapcount(page) > 1)
+			goto huge_unlock;
+
+		if (next - addr != HPAGE_PMD_SIZE) {
+			int err;

Alternately, we deactivate thp only if the address range from userspace is sane enough, in order to avoid complex works we have to do here.

Quoted 3 lines

Not sure it's a good idea. That's the way we have done in MADV_FREE so want to be consistent.

Fair.

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
(quoted diff) +17 -0
+
+			get_page(page);
+			spin_unlock(ptl);
+			lock_page(page);
+			err = split_huge_page(page);
+			unlock_page(page);
+			put_page(page);
+			if (!err)
+				goto regular_page;
+			return 0;
+		}
+
+		pmdp_test_and_clear_young(vma, addr, pmd);
+		deactivate_page(page);
+huge_unlock:
+		spin_unlock(ptl);
+		return 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	}
+
+	if (pmd_trans_unstable(pmd))
+		return 0;
+
+regular_page:

Take a look at pending signal?

Quoted 3 lines

Do you have any reason to see pending signal here? I want to know what's your requirement so that what's the better place to handle it.

We could bail out without work done IMO if there is a fatal siganl pending. And we can do that, if it makes sense to you, before the hard work.

Quoted 8 lines
Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
+	for (pte = orig_pte; addr < end; pte++, addr += PAGE_SIZE) {

s/end/next/ ?

Why do you think it should be next?

Simply based on the following line, and afraid that next != end

	> > > +	next = pmd_addr_end(addr, end);
Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
(quoted diff) +27 -0
+		ptent = *pte;
+
+		if (pte_none(ptent))
+			continue;
+
+		if (!pte_present(ptent))
+			continue;
+
+		page = vm_normal_page(vma, addr, ptent);
+		if (!page)
+			continue;
+
+		if (page_mapcount(page) > 1)
+			continue;
+
+		ptep_test_and_clear_young(vma, addr, pte);
+		deactivate_page(page);
+	}
+
+	pte_unmap_unlock(orig_pte, ptl);
+	cond_resched();
+
+	return 0;
+}
+
+static long madvise_cool(struct vm_area_struct *vma,
+			unsigned long start_addr, unsigned long end_addr)
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+{
+	struct mm_struct *mm = vma->vm_mm;
+	struct mmu_gather tlb;
+
+	if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
+		return -EINVAL;

No service in case of VM_IO?

Quoted 4 lines

I don't know VM_IO would have regular LRU pages but just follow normal convention for DONTNEED and FREE.
Do you have anything in your mind?

I want to skip a mapping set up for DMA.

BR
Hillf

#1 lore raw

On Tue, May 28, 2019 at 08:15:23PM +0800, Hillf Danton wrote:
< snip >

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 13 lines
(quoted diff) +13 -0
+
+			get_page(page);
+			spin_unlock(ptl);
+			lock_page(page);
+			err = split_huge_page(page);
+			unlock_page(page);
+			put_page(page);
+			if (!err)
+				goto regular_page;
+			return 0;
+		}
+
+		pmdp_test_and_clear_young(vma, addr, pmd);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		deactivate_page(page);
+huge_unlock:
+		spin_unlock(ptl);
+		return 0;
+	}
+
+	if (pmd_trans_unstable(pmd))
+		return 0;
+
+regular_page:

Take a look at pending signal?

Quoted 5 lines
Quoted 3 lines

Do you have any reason to see pending signal here? I want to know what's your requirement so that what's the better place to handle it.

We could bail out without work done IMO if there is a fatal siganl pending. And we can do that, if it makes sense to you, before the hard work.

Make sense, especically, swapping out.
I will add it in next revision.

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+	orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
+	for (pte = orig_pte; addr < end; pte++, addr += PAGE_SIZE) {

s/end/next/ ?

Why do you think it should be next?

Simply based on the following line, and afraid that next != end

	> > > +	next = pmd_addr_end(addr, end);

pmd_addr_end will return smaller address so end is more proper.

Quoted 37 lines
Quoted 37 lines
Quoted 36 lines
Quoted 24 lines
(quoted diff) +23 -0
+		ptent = *pte;
+
+		if (pte_none(ptent))
+			continue;
+
+		if (!pte_present(ptent))
+			continue;
+
+		page = vm_normal_page(vma, addr, ptent);
+		if (!page)
+			continue;
+
+		if (page_mapcount(page) > 1)
+			continue;
+
+		ptep_test_and_clear_young(vma, addr, pte);
+		deactivate_page(page);
+	}
+
+	pte_unmap_unlock(orig_pte, ptl);
+	cond_resched();
+
+	return 0;

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static long madvise_cool(struct vm_area_struct *vma,
+			unsigned long start_addr, unsigned long end_addr)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	struct mmu_gather tlb;
+
+	if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
+		return -EINVAL;

No service in case of VM_IO?

Quoted 5 lines
Quoted 4 lines

I don't know VM_IO would have regular LRU pages but just follow normal convention for DONTNEED and FREE.
Do you have anything in your mind?

I want to skip a mapping set up for DMA.

What you meant is those pages in VM_IO vma are not in LRU list? Or
pages in the vma are always pinned so no worth to deactivate or reclaim?