From: John Stultz <john.stultz@linaro.org>
To: Jan Kara <jack@suse.cz>
Cc: LKML <linux-kernel@vger.kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Android Kernel Team <kernel-team@android.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Robert Love <rlove@google.com>, Mel Gorman <mel@csn.ul.ie>,
Hugh Dickins <hughd@google.com>, Dave Hansen <dave@sr71.net>,
Rik van Riel <riel@redhat.com>,
Dmitry Adamushko <dmitry.adamushko@gmail.com>,
Neil Brown <neilb@suse.de>,
Andrea Arcangeli <aarcange@redhat.com>,
Mike Hommey <mh@glandium.org>, Taras Glek <tglek@mozilla.com>,
Dhaval Giani <dgiani@mozilla.com>,
KOSAKI Motohiro <kosaki.motohiro@gmail.com>,
Michel Lespinasse <walken@google.com>,
Minchan Kim <minchan@kernel.org>,
"linux-mm@kvack.org" <linux-mm@kvack.org>
Subject: Re: [PATCH 1/3] vrange: Add vrange syscall and handle splitting/merging and marking vmas
Date: Mon, 17 Mar 2014 15:19:33 -0700 [thread overview]
Message-ID: <532774F5.5070001@linaro.org> (raw)
In-Reply-To: <20140317092118.GA2210@quack.suse.cz>
On 03/17/2014 02:21 AM, Jan Kara wrote:
> On Fri 14-03-14 11:33:31, John Stultz wrote:
>> This patch introduces the vrange() syscall, which allows for specifying
>> ranges of memory as volatile, and able to be discarded by the system.
>>
>> This initial patch simply adds the syscall, and the vma handling,
>> splitting and merging the vmas as needed, and marking them with
>> VM_VOLATILE.
>>
>> No purging or discarding of volatile ranges is done at this point.
>>
>> Example man page:
>>
>> NAME
>> vrange - Mark or unmark range of memory as volatile
>>
>> SYNOPSIS
>> int vrange(unsigned_long start, size_t length, int mode,
>> int *purged);
>>
>> DESCRIPTION
>> Applications can use vrange(2) to advise the kernel how it should
>> handle paging I/O in this VM area. The idea is to help the kernel
>> discard pages of vrange instead of reclaiming when memory pressure
>> happens. It means kernel doesn't discard any pages of vrange if
>> there is no memory pressure.
> I'd say that the advantage is kernel doesn't have to swap volatile pages,
> it can just directly discard them on memory pressure. You should also
> mention somewhere vrange() is currently supported only for anonymous pages.
> So maybe we can have the description like:
> Applications can use vrange(2) to advise kernel that pages of anonymous
> mapping in the given VM area can be reclaimed without swapping (or can no
> longer be reclaimed without swapping). The idea is that application can
> help kernel with page reclaim under memory pressure by specifying data
> it can easily regenerate and thus kernel can discard the data if needed.
Good point. This man page description originated from previous patches
where we did handle file paging, so I'll try to update it and make it
more clear we currently don't (although I very much want to re-add that
functionality eventually).
>
>> mode:
>> VRANGE_VOLATILE
>> hint to kernel so VM can discard in vrange pages when
>> memory pressure happens.
>> VRANGE_NONVOLATILE
>> hint to kernel so VM doesn't discard vrange pages
>> any more.
>>
>> If user try to access purged memory without VRANGE_NONVOLATILE call,
> ^^^ tries
>
>> he can encounter SIGBUS if the page was discarded by kernel.
>>
>> purged: Pointer to an integer which will return 1 if
>> mode == VRANGE_NONVOLATILE and any page in the affected range
>> was purged. If purged returns zero during a mode ==
>> VRANGE_NONVOLATILE call, it means all of the pages in the range
>> are intact.
>>
>> RETURN VALUE
>> On success vrange returns the number of bytes marked or unmarked.
>> Similar to write(), it may return fewer bytes then specified
>> if it ran into a problem.
> I believe you may need to better explain what is 'purged' argument good
> for. Because in my naive understanding *purged == 1 iff return value !=
> length. I recall your discussion with Johannes about error conditions and
> the need to return error but also the state of the range, is that right?
> But that should be really explained somewhere so that poor application
> programmer is aware of those corner cases as well.
Right. So the purged flag is separate/independent from the byte/error
return. Basically we want to describe how much memory has been changed
from volatile to non-volatile state (or vice versa), as well as
providing if any of those pages were purged while they were volatile.
One could mark 1meg of previously volatile memory non-volatile, and find
purged == 0 if there was no memory pressure, or if there was pressure,
find purged == 1.
The error case is that should we run out of memory (or hit some other
error condition that prevents us from successfully marking all of the
specified memory as non-volatile) half way through, we need to return to
the user the purged state for the pages that we did change.
Now, its true that if we ran out of memory, its likely that the memory
pressure caused pages to be purged before being marked, but one could
imagine a situation were we got half way through marking non-purged
pages and memory pressure suddenly went up, causing an allocation to
fail. Thus in that case, you would see the return value != length, and
purged == 0.
But thank you for the feedback, I'll try to rework that part to be more
clear. Any suggestions would also be welcome, as I worry my head is a
bit too steeped in this to see what would make it more clear to fresh eyes.
[snip]
>> diff --git a/mm/vrange.c b/mm/vrange.c
>> new file mode 100644
>> index 0000000..acb4356
>> --- /dev/null
>> +++ b/mm/vrange.c
>> @@ -0,0 +1,150 @@
>> +#include <linux/syscalls.h>
>> +#include <linux/vrange.h>
>> +#include <linux/mm_inline.h>
>> +#include <linux/pagemap.h>
>> +#include <linux/rmap.h>
>> +#include <linux/hugetlb.h>
>> +#include <linux/mmu_notifier.h>
>> +#include <linux/mm_inline.h>
>> +#include "internal.h"
>> +
>> +static ssize_t do_vrange(struct mm_struct *mm, unsigned long start,
>> + unsigned long end, int mode, int *purged)
>> +{
>> + struct vm_area_struct *vma, *prev;
>> + unsigned long orig_start = start;
>> + ssize_t count = 0, ret = 0;
>> + int lpurged = 0;
>> +
>> + down_read(&mm->mmap_sem);
>> +
>> + vma = find_vma_prev(mm, start, &prev);
>> + if (vma && start > vma->vm_start)
>> + prev = vma;
>> +
>> + for (;;) {
>> + unsigned long new_flags;
>> + pgoff_t pgoff;
>> + unsigned long tmp;
>> +
>> + if (!vma)
>> + goto out;
>> +
>> + if (vma->vm_flags & (VM_SPECIAL|VM_LOCKED|VM_MIXEDMAP|
>> + VM_HUGETLB))
>> + goto out;
>> +
>> + /* We don't support volatility on files for now */
>> + if (vma->vm_file) {
>> + ret = -EINVAL;
>> + goto out;
>> + }
>> +
>> + new_flags = vma->vm_flags;
>> +
>> + if (start < vma->vm_start) {
>> + start = vma->vm_start;
>> + if (start >= end)
>> + goto out;
>> + }
>> + tmp = vma->vm_end;
>> + if (end < tmp)
>> + tmp = end;
>> +
>> + switch (mode) {
>> + case VRANGE_VOLATILE:
>> + new_flags |= VM_VOLATILE;
>> + break;
>> + case VRANGE_NONVOLATILE:
>> + new_flags &= ~VM_VOLATILE;
>> + }
>> +
>> + pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
>> + prev = vma_merge(mm, prev, start, tmp, new_flags,
>> + vma->anon_vma, vma->vm_file, pgoff,
>> + vma_policy(vma));
>> + if (prev)
>> + goto success;
>> +
>> + if (start != vma->vm_start) {
>> + ret = split_vma(mm, vma, start, 1);
>> + if (ret)
>> + goto out;
>> + }
>> +
>> + if (tmp != vma->vm_end) {
>> + ret = split_vma(mm, vma, tmp, 0);
>> + if (ret)
>> + goto out;
>> + }
>> +
>> + prev = vma;
>> +success:
>> + vma->vm_flags = new_flags;
>> + *purged = lpurged;
>> +
>> + /* update count to distance covered so far*/
>> + count = tmp - orig_start;
>> +
>> + if (prev && start < prev->vm_end)
> In which case 'prev' can be NULL? And when start >= prev->vm_end? In all
> the cases I can come up with this condition seems to be true...
>
>> + start = prev->vm_end;
>> + if (start >= end)
>> + goto out;
>> + if (prev)
> Ditto regarding 'prev'...
I haven't had the chance to look closely here today, but I'll double
check on these two.
>> + vma = prev->vm_next;
>> + else /* madvise_remove dropped mmap_sem */
>> + vma = find_vma(mm, start);
> The comment regarding madvise_remove() looks bogus...
Yep. Thanks for pointing it out, that's from my starting with the
madvise logic and reworking it.
Thanks so much for the feedback here! I really appreciate it, and will
rework things appropriately.
thanks again!
-john
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2014-03-17 22:19 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-14 18:33 [PATCH 0/3] Volatile Ranges (v11) John Stultz
2014-03-14 18:33 ` [PATCH 1/3] vrange: Add vrange syscall and handle splitting/merging and marking vmas John Stultz
2014-03-17 9:21 ` Jan Kara
2014-03-17 9:43 ` Jan Kara
2014-03-18 0:36 ` John Stultz
2014-03-17 22:19 ` John Stultz [this message]
2014-03-14 18:33 ` [PATCH 2/3] vrange: Add purged page detection on setting memory non-volatile John Stultz
2014-03-17 9:39 ` Jan Kara
2014-03-17 22:22 ` John Stultz
2014-03-14 18:33 ` [PATCH 3/3] vrange: Add page purging logic & SIGBUS trap John Stultz
2014-03-18 12:24 ` [PATCH 0/3] Volatile Ranges (v11) Michal Hocko
2014-03-18 17:53 ` John Stultz
2014-03-20 0:38 ` Dave Hansen
2014-03-20 0:57 ` John Stultz
2014-03-20 7:45 ` Minchan Kim
2014-03-18 15:11 ` Minchan Kim
2014-03-18 18:07 ` John Stultz
2014-03-19 0:49 ` Minchan Kim
2014-03-19 10:12 ` Jan Kara
2014-03-20 1:09 ` Minchan Kim
2014-03-20 8:13 ` Jan Kara
2014-03-21 5:29 ` Minchan Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=532774F5.5070001@linaro.org \
--to=john.stultz@linaro.org \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=dave@sr71.net \
--cc=dgiani@mozilla.com \
--cc=dmitry.adamushko@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=kernel-team@android.com \
--cc=kosaki.motohiro@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mel@csn.ul.ie \
--cc=mh@glandium.org \
--cc=minchan@kernel.org \
--cc=neilb@suse.de \
--cc=riel@redhat.com \
--cc=rlove@google.com \
--cc=tglek@mozilla.com \
--cc=walken@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).