Skip to contentSkip to conversation

update ctime and mtime for mmaped write

PATCH RESEND [email protected] 4 participants 13 messages Mar 27, 2007 – Mar 29, 2007 Memory management

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines

Yes, this will make msync(MS_ASYNC) more heavyweight again. But if an application doesn't want to update the timestamps, it should just omit this call, since it does nothing else.

Er... FWIW, I have an application that makes heavy use of msync(MS_ASYNC) and doesn't care about timestamps. (In fact, sometimes it's configured to write to a raw device and there are no timestamps.)

It's used as a poor man's portable async I/O. The application logs data to disk, and sometimes needs to sync it to disk to ensure it has all been written.

To reduce long pauses when doing msync(MS_SYNC), it does msync(MS_ASYNC) as soon as a page is filled up to prompt asynchronous writeback. "I'm done writing this page and don't intend to write it again. Please start committing it to stable storage, but don't block me."

Then, occasionally, there's an msync(MS_SYNC) call to be sure the data is synced to disk. This caused annoying hiccups before the MS_ASYNC calls were added.

I agree that msync(MS_ASYNC) has no semantics if time is ignored. But it's a useful way to tell the OS that the page is not going to be dirtied again.

#1 lore raw
Quoted 17 lines
Quoted 3 lines

Yes, this will make msync(MS_ASYNC) more heavyweight again. But if an application doesn't want to update the timestamps, it should just omit this call, since it does nothing else.

Er... FWIW, I have an application that makes heavy use of msync(MS_ASYNC) and doesn't care about timestamps. (In fact, sometimes it's configured to write to a raw device and there are no timestamps.)

It's used as a poor man's portable async I/O. The application logs data to disk, and sometimes needs to sync it to disk to ensure it has all been written.

To reduce long pauses when doing msync(MS_SYNC), it does msync(MS_ASYNC) as soon as a page is filled up to prompt asynchronous writeback. "I'm done writing this page and don't intend to write it again. Please start committing it to stable storage, but don't block me."

Quoted 8 lines

Then, occasionally, there's an msync(MS_SYNC) call to be sure the data is synced to disk. This caused annoying hiccups before the MS_ASYNC calls were added.

I agree that msync(MS_ASYNC) has no semantics if time is ignored. But it's a useful way to tell the OS that the page is not going to be dirtied again.

Just to clarify, here's the header comment for sys_msync():

/*
* MS_SYNC syncs the entire file - including mappings.
*
* MS_ASYNC does not start I/O (it used to, up to 2.5.67).
* Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
* Now it doesn't do anything, since dirty pages are properly tracked. *
* The application may now run fsync() to
* write out the dirty pages and wait on the writeout and check the result.
* Or the application may run fadvise(FADV_DONTNEED) against the fd to start
* async writeout immediately.
* So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
* applications.
*/

It's actually wrong about FADV_DONTNEED, which I think doesn't start writeout either. So there you have it ;)

Miklos

#2 lore raw
Quoted 21 lines
Quoted 3 lines

I agree that msync(MS_ASYNC) has no semantics if time is ignored. But it's a useful way to tell the OS that the page is not going to be dirtied again.

Just to clarify, here's the header comment for sys_msync():

/*
* MS_SYNC syncs the entire file - including mappings.
*
* MS_ASYNC does not start I/O (it used to, up to 2.5.67).
* Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
* Now it doesn't do anything, since dirty pages are properly tracked. *
* The application may now run fsync() to
* write out the dirty pages and wait on the writeout and check the result.
* Or the application may run fadvise(FADV_DONTNEED) against the fd to start
* async writeout immediately.
* So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
* applications.
*/

Quoted 2 lines

It's actually wrong about FADV_DONTNEED, which I think doesn't start writeout either. So there you have it ;)

Sorry, FADV_DONTNEED _does_ seem to start writeout, but it does it indiscriminately, not just on the specified range. That should be easy to fix though.

Miklos

maintainer
#3 lore raw
In reply to Miklos Szeredi
Quoted 2 lines · On Tue, 27 Mar 2007 20:55:51 +0200 Miklos Szeredi <[email protected]> wrote:

It's actually wrong about FADV_DONTNEED, which I think doesn't start writeout either.

        case POSIX_FADV_DONTNEED:
                if (!bdi_write_congested(mapping->backing_dev_info))
                        filemap_flush(mapping);
#4 lore raw
In reply to Miklos Szeredi
Quoted 1 line

* MS_ASYNC does not start I/O (it used to, up to 2.5.67).

Yes, I noticed. See
http://www.ussg.iu.edu/hypermail/linux/kernel/0602.1/0450.html
for a bug report on the subject February 2006.

That's why this application is still running on 2.4.

As I mentioned at the time, the SUS says:
(http://opengroup.org/onlinepubs/007908799/xsh/msync.html)
"When MS_ASYNC is specified, msync() returns immediately once all the write operations are initiated or queued for servicing."

You can argue that putting it on the dirty list constitutes "queued for servicing", but the intent seems pretty clear to me: MS_ASYNC is supposed to start the I/O. Although strict standards-ese parsing says that either branch of an or is acceptable, it is a common English language convention that the first alternative is preferred and the second is a fallback.

It makes sense in this case: start the write or, if that's not possible (the disk is already busy), queue it for service as soon as the disk is available.

They perhaps didn't mandate it this strictly, but that's clearly the intent.

maintainer
#5 lore raw
Quoted 5 lines · On 27 Mar 2007 15:24:52 -0400 [email protected] wrote:
Quoted 1 line

* MS_ASYNC does not start I/O (it used to, up to 2.5.67).

Yes, I noticed. See
http://www.ussg.iu.edu/hypermail/linux/kernel/0602.1/0450.html
for a bug report on the subject February 2006.

Suggest you use msync(MS_ASYNC), then sync_file_range(SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE).

The new (post 2.6.17) MAP_SHARED dirty-page semantics mean that the msync() isn't actually needed.

Quoted 14 lines

That's why this application is still running on 2.4.

As I mentioned at the time, the SUS says:
(http://opengroup.org/onlinepubs/007908799/xsh/msync.html)
"When MS_ASYNC is specified, msync() returns immediately once all the write operations are initiated or queued for servicing."

You can argue that putting it on the dirty list constitutes "queued for servicing", but the intent seems pretty clear to me: MS_ASYNC is supposed to start the I/O. Although strict standards-ese parsing says that either branch of an or is acceptable, it is a common English language convention that the first alternative is preferred and the second is a fallback.

Quoted 6 lines

It makes sense in this case: start the write or, if that's not possible (the disk is already busy), queue it for service as soon as the disk is available.

They perhaps didn't mandate it this strictly, but that's clearly the intent.

We can fix your application, and we'll break someone else's.

I don't think it's solveable, really - the range of applications is so broad, and the "standard" is so vague as to be useless. This is why we've been extending these things with linux-specific goodies which permit applications to actually tell the kernel what they want to be done in a more finely-grained fashion.

#6 lore raw
Quoted 2 lines

Suggest you use msync(MS_ASYNC), then sync_file_range(SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE).

Thank you; I didn't know about that. And I can handle -ENOSYS by falling back to the old behavior.

Quoted 1 line

We can fix your application, and we'll break someone else's.

If you can point to an application that it'll break, I'd be a lot more understanding. Nobody did, last year.

Quoted 2 lines

I don't think it's solveable, really - the range of applications is so broad, and the "standard" is so vague as to be useless.

I agree that standards are sometimes vague, but that one seemed about as clear as it's possible to be without imposing unreasonably on the file system and device driver layers.

What part of "The msync() function writes all modified data to permanent storage locations [...] For mappings to files, the msync() function ensures that all write operations are completed as defined for synchronised I/O data integrity completion." suggests that it's not supposed to do disk I/O? How is that uselessly vague?

It says to me that msync's raison d'etre is to write data from RAM to stable storage. If an application calls it too often, that's the application's fault just as if it called sync(2) too often.

Quoted 4 lines

This is why we've
been extending these things with linux-specific goodies which permit applications to actually tell the kernel what they want to be done in a more finely-grained fashion.

Well, I still think the current Linux behavior is a bug, but there's a usable (and run-time compatible) workaround that doesn't unreasonably complicate the code, and that's good enough.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
Quoted 18 lines
Quoted 2 lines

Suggest you use msync(MS_ASYNC), then sync_file_range(SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE).

Thank you; I didn't know about that. And I can handle -ENOSYS by falling back to the old behavior.

Quoted 1 line

We can fix your application, and we'll break someone else's.

If you can point to an application that it'll break, I'd be a lot more understanding. Nobody did, last year.

Quoted 2 lines

I don't think it's solveable, really - the range of applications is so broad, and the "standard" is so vague as to be useless.

I agree that standards are sometimes vague, but that one seemed about as clear as it's possible to be without imposing unreasonably on the file system and device driver layers.

Quoted 5 lines

What part of "The msync() function writes all modified data to permanent storage locations [...] For mappings to files, the msync() function ensures that all write operations are completed as defined for synchronised I/O data integrity completion." suggests that it's not supposed to do disk I/O? How is that uselessly vague?

Linux _will_ write all modified data to permanent storage locations. Since 2.6.17 it will do this regardless of msync(). Before 2.6.17 you do need msync() to enable data to be written back.

But it will not start I/O immediately, which is not a requirement in the standard, or at least it's pretty vague about that.

Miklos

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 6 lines

Linux _will_ write all modified data to permanent storage locations. Since 2.6.17 it will do this regardless of msync(). Before 2.6.17 you do need msync() to enable data to be written back.

But it will not start I/O immediately, which is not a requirement in the standard, or at least it's pretty vague about that.

As I've said before, I disagree, but I'm not going to start a major flame war about it.

The most relevant paragraph is:

# When MS_ASYNC is specified, msync() returns immediately once all the

# write operations are initiated or queued for servicing; when MS_SYNC is
# specified, msync() will not return until all write operations are
# completed as defined for synchronised I/O data integrity completion.
# Either MS_ASYNC or MS_SYNC is specified, but not both.

Note two things:
1) In the paragraphs before, what msync does is defined independently of the MS_* flags. Only the time of the return to user space varies. Thus, whatever the delay between calling msync() and the data being written, it should be the same whether MS_SYNC or MS_ASYNC is used.

The implementation intended is:
- Start all I/O
- If MS_SYNC, wait for I/O to complete
- Return to user space

2) "all the write operations are initiated or queued for servicing". It is a common convention in English (and most languages, I expect) that in the "or" is a preference for the first alternative. The second is a permitted alternative if the first is not possible.

And "queued for servicing", especially "initiated or queued for servicing", to me imples queuing waiting for some resource. To have the resource being waited for be a timer expiry seems like rather a cheat to me. It's perhaps doesn't break the letter of the standard, but definitely bends it. It feels like a fiddle.

Still, the basic hint function of msync(MS_ASYNC) *is* being accomplished:
"I don't expect to write this page any more, so now would be a good time to clean it."
It would just make my life easier if the kernel procrastinated less.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw
Quoted 25 lines · [email protected] wrote:
Quoted 6 lines

Linux _will_ write all modified data to permanent storage locations. Since 2.6.17 it will do this regardless of msync(). Before 2.6.17 you do need msync() to enable data to be written back.

But it will not start I/O immediately, which is not a requirement in the standard, or at least it's pretty vague about that.

As I've said before, I disagree, but I'm not going to start a major flame war about it.

The most relevant paragraph is:

# When MS_ASYNC is specified, msync() returns immediately once all the

# write operations are initiated or queued for servicing; when MS_SYNC is
# specified, msync() will not return until all write operations are
# completed as defined for synchronised I/O data integrity completion.
# Either MS_ASYNC or MS_SYNC is specified, but not both.

Note two things:
1) In the paragraphs before, what msync does is defined independently of the MS_* flags. Only the time of the return to user space varies. Thus, whatever the delay between calling msync() and the data being written, it should be the same whether MS_SYNC or MS_ASYNC is used.

Quoted 4 lines

The implementation intended is:
- Start all I/O
- If MS_SYNC, wait for I/O to complete
- Return to user space

set_page_dirty queues pages for IO, and the writeout daemon will service that queue when it sees fit. IMO it is sufficient that you cannot say the implementation does not meet the standard.

Quoted 11 lines

2) "all the write operations are initiated or queued for servicing". It is a common convention in English (and most languages, I expect) that in the "or" is a preference for the first alternative. The second is a permitted alternative if the first is not possible.

And "queued for servicing", especially "initiated or queued for servicing", to me imples queuing waiting for some resource. To have the resource being waited for be a timer expiry seems like rather a cheat to me. It's perhaps doesn't break the letter of the standard, but definitely bends it. It feels like a fiddle.

Quoted 4 lines

Still, the basic hint function of msync(MS_ASYNC) *is* being accomplished:
"I don't expect to write this page any more, so now would be a good time to clean it."
It would just make my life easier if the kernel procrastinated less.

But if you didn't notice until now, then the current implementation must be pretty reasonable for you use as well.

Signature · 8 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw
Quoted 2 lines

But if you didn't notice until now, then the current implementation must be pretty reasonable for you use as well.

Oh, I definitely noticed. As soon as I tried to port my application to 2.6, it broke - as evidenced by my complaints last year. The current solution is simple - since it's running on dedicated boxes, leave them on 2.4.

I've now got the hint on how to make it work on 2.6 (sync_file_range()), so I can try again. But the pressure to upgrade is not strong, so it might be a while.

You may recall, this subthread started when I responding to "the only reason to use msync(MS_ASYNC) is to update timestamps" with a counterexample. I still think the purpose of the call is a hint to the kernel that writing to the specified page(s) is complete and now would be a good time to clean them. Which has very little to do with timestamps.

Now, my application, which leaves less than a second between the MS_ASYNC and a subsequent MS_SYNC to check whether it's done, broke, but I can imagine similar cases where MS_ASYNC would remain a useful hint to reduce the sort of memory hogging generally associated with "dd if=/dev/zero" type operations.

Reading between the lines of the standard, that seems (to me, at least) to obviously be the intended purpose of msync(MS_ASYNC). I wonder if there's any historical documentation describing the original intent behind creating the call.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw
Quoted 8 lines · [email protected] wrote:
Quoted 2 lines

But if you didn't notice until now, then the current implementation must be pretty reasonable for you use as well.

Oh, I definitely noticed. As soon as I tried to port my application to 2.6, it broke - as evidenced by my complaints last year. The current solution is simple - since it's running on dedicated boxes, leave them on 2.4.

Well I didn't know that was a change in behaviour vs 2.4 (or maybe I did and forgot). That was probably a bit silly, unless there was a good reason for it.

Signature · 7 lines
SUSE Labs, Novell Inc.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#12 lore raw
In reply to [email protected]
Quoted 6 lines · On 27 Mar 2007 16:09:33 -0400 [email protected] wrote:

What part of "The msync() function writes all modified data to permanent storage locations [...] For mappings to files, the msync() function ensures that all write operations are completed as defined for synchronised I/O data integrity completion." suggests that it's not supposed to do disk I/O? How is that uselessly vague?

Because for MS_ASYNC, "msync() shall return immediately once all the write operations are initiated or queued for servicing".

ie: the writes can complete one millisecond or one week later. We chose 30 seconds.

And this is not completely fatuous - before 2.6.17, MAP_SHARED pages could float about in memory in a dirty state for arbitrarily long periods - potentially for the entire application lifetime. It was quite reasonable for our MS_ASYNC implementation to do what it did: tell the VM about the dirtiness of these pages so they get written back soon.

Post-2.6.17 we preserved that behaviour.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>