Skip to contentSkip to conversation

generic_file_write code segment in 2.2.18

DISCUSSION Asang K Dani 3 participants 4 messages Jan 4, 2001

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

hi everyone,
I was trying to understand following piece of code in
'generic_file_read' (mm/filemap.c) for 2.2.18 kernel. The code segment
is as follows:

----------------------------------------------------------------

		dest = (char *) page_address(page) + offset;
		if (dest != buf) { /* See comment in
                                      update_vm_cache_cond. */
			bytes -= copy_from_user(dest, buf, bytes);
			flush_dcache_page(page_address(page));
		}
		status = -EFAULT;
		if (bytes)
			status = inode->i_op->updatepage(file, page,                      
                      offset, bytes, sync);

unlock:

		/* Mark it unlocked again and drop the page.. */
		clear_bit(PG_locked, &page->flags);
		wake_up(&page->wait);
		page_cache_release(page);

		if (status < 0)
			break;

		written += status;
		count -= status;
		pos += status;
		buf += status;

----------------------------------------------------------------
copy_from_user returns the number of bytes copied from userspace to 'dest'. In case it succeeds, 'bytes' will be set to 0 after the call. However, 'status' is set to -EFAULT. So wouldn't it break out of the while loop (prematurely)?

Please post/CC your comments to me if this is not of general interest to the list and is too silly to be answered there.

regards,
Asang Dani

__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://photos.yahoo.com/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#1 lore raw
Quoted 31 lines · Asang K Dani

hi everyone,
I was trying to understand following piece of code in
'generic_file_read' (mm/filemap.c) for 2.2.18 kernel. The code segment
is as follows:

----------------------------------------------------------------

		dest = (char *) page_address(page) + offset;
		if (dest != buf) { /* See comment in
                                      update_vm_cache_cond. */
			bytes -= copy_from_user(dest, buf, bytes);
			flush_dcache_page(page_address(page));
		}
		status = -EFAULT;
		if (bytes)
			status = inode->i_op->updatepage(file, page,                      
                      offset, bytes, sync);

unlock:

		/* Mark it unlocked again and drop the page.. */
		clear_bit(PG_locked, &page->flags);
		wake_up(&page->wait);
		page_cache_release(page);

		if (status < 0)
			break;

		written += status;
		count -= status;
		pos += status;
		buf += status;
Quoted 5 lines

----------------------------------------------------------------
copy_from_user returns the number of bytes copied from userspace to 'dest'. In case it succeeds, 'bytes' will be set to 0 after the call. However, 'status' is set to -EFAULT. So wouldn't it break out of the while loop (prematurely)?

The code is buggy as far as I can see. copy_from_user doesn't return the number of bytes copied, but the number of bytes not copied when an error occurs (or 0 on success).

Correct would be:

mm/filemap.c renamed from mm/filemap.c-o +3 -3
@@ -1685,11 +1685,11 @@
 		 */
 		dest = (char *) page_address(page) + offset;
 		if (dest != buf) { /* See comment in update_vm_cache_cond. */
-			bytes -= copy_from_user(dest, buf, bytes);
+			if (copy_from_user(dest, buf, bytes))
+				status = -EFAULT; 
 			flush_dcache_page(page_address(page));
 		}
-		status = -EFAULT;
-		if (bytes)
+		if (!status)
 			status = inode->i_op->updatepage(file, page, offset, bytes, sync);
 
  unlock:

-Andi

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw
In reply to Asang K Dani

Hi,

Quoted 5 lines · Andi Kleen

On Wed, Jan 03, 2001 at 09:29:48PM -0800, Asang K Dani wrote:

The code is buggy as far as I can see. copy_from_user doesn't return the number of bytes copied, but the number of bytes not copied when an error occurs (or 0 on success).

But in this case, _any_ non-zero number of bytes copied is a success, because it indicates that we have dirtied a portion of the page cache.

Quoted 5 lines

Correct would be:

--- linux-work/mm/filemap.c-o Wed Jan 3 17:37:27 2001

(quoted diff) +0 -0
 		dest = (char *) page_address(page) + offset;

Review

Quoted 10 lines
(quoted diff) +3 -3
 		if (dest != buf) { /* See comment in update_vm_cache_cond. */
-			bytes -= copy_from_user(dest, buf, bytes);
+			if (copy_from_user(dest, buf, bytes))
+				status = -EFAULT; 
 			flush_dcache_page(page_address(page));
 		}
-		status = -EFAULT;
-		if (bytes)
+		if (!status)
 			status = inode->i_op->updatepage(file, page, offset, bytes, sync);

No, because then you'd be skipping the updatepage() call if we took a fault mid-copy after copying some data. That would imply you had dirtied the page cache without an updatepage().

The current behaviour should just result in a short IO, which should be fine.

--Stephen
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw
In reply to Asang K Dani
Quoted 6 lines · Stephen C. Tweedie

No, because then you'd be skipping the updatepage() call if we took a fault mid-copy after copying some data. That would imply you had dirtied the page cache without an updatepage().

The current behaviour should just result in a short IO, which should be fine.

The problem is that the short write is not reported to the caller, even when only zero bytes are copied (the page is corrupted anyways though because cfu zeros the uncopied rest).

-Andi
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/