Re: mm/filemap.c: atomic file read(2)/write(2) ?

From: Linus Torvalds (torvalds_at_osdl.org)
Date: 01/10/04

  • Next message: Thomas Steudten: "BUG: Kernel Panic: kernel-2.6.1 for alpha in scsi context ll_rw_blk.c"
    Date:	Sat, 10 Jan 2004 08:35:11 -0800 (PST)
    To: Klaus Ripke <paul@malete.org>
    
    

    On Fri, 9 Jan 2004, Klaus Ripke wrote:
    >
    > judging from mm/filemap.c it seems like
    > ordinary reads/writes should be atomic to each other
    > (read sees write completely or not at all,
    > not only where it "can be proven to be after the write"),
    > if

    Nope. There are file descriptors that have atomicity guarantees (pipes(,
    but regular files do not. In particular, even on UP you'll see nonatomic
    reads with PREEMPT enabled.

    Even without preempt you could see interesting partial updates if you take
    a page fault. That's true even if your user space only ever reads or
    writes within a whole page, since what you can get on the read() path is:

     - read one word from the page cache
     - try to write it to user space
     - take a page fault - sleep

     - writer now comes in and updates the region

     - the reader comes back after the page fault
     - the reader completes the copy_to_user(), but the _first_ word was read
       from the old page cache contents and not re-read.

    (This will depend on which particular version of copy_to_user() you use: a
    "rep movs" will re-read the page cache and give an "atomic" read, while
    all other forms of memory copies will only re-try the destination write
    instruction, with the old value read from the source).

                    Linus
    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/


  • Next message: Thomas Steudten: "BUG: Kernel Panic: kernel-2.6.1 for alpha in scsi context ll_rw_blk.c"

    Relevant Pages

    • Re: [PATCH] 2.6.0-test4 -- add context switch counters
      ... >> counters is going to add to the size of the data cache footprint and it ... I just know that the path to slowness is paved one cache ... Is that the fault of the Nth+1 guy? ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Re: Lock variables between two threads
      ... I guess the cache coherency protocol guarantees atomicity of the read/modify/write to the cache line even with an instruction without LOCK prefix. ... I guess the moral of the story is that avoiding false sharing is at least as important to performance as avoiding unnecessary LOCK prefixes (although maybe only with a few cores, whereas the LOCK prefix is worse with many-core). ...
      (microsoft.public.vc.language)
    • Re: Lock variables between two threads
      ... L2/L3 cache shared between cores), or always an entire word (or ... atomicity of the read/modify/write to the cache line even with ... Reading or writing a byte. ...
      (microsoft.public.vc.language)
    • Re: Merging relayfs?
      ... > consumed buffers and these are only ever updated or read in the slow ... writing on another when not having imposed any atomicity. ... IBM Advanced Linux Response Team - Linux Technology Centre ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Re: TSCs are a no-no on i386
      ... >> applications since cmpxchg has a wrong opcode on this early CPU. ... >> atomicity can't be guaranted! ... When do you have a heart between your knees? ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)