ANDROID: uid_sys_stats: fix negative write bytes.
authorJin Qian <jinqian@google.com>
Tue, 28 Feb 2017 23:09:42 +0000 (15:09 -0800)
committerDanny Wood <danwood76@gmail.com>
Sun, 31 Mar 2019 08:48:11 +0000 (09:48 +0100)
A task can cancel writes made by other tasks. In rare cases,
cancelled_write_bytes is larger than write_bytes if the task
itself didn't make any write. This doesn't affect total size
but may cause confusion when looking at IO usage on individual
tasks.

Bug: 35851986
Change-Id: If6cb549aeef9e248e18d804293401bb2b91918ca
Signed-off-by: Jin Qian <jinqian@google.com>
Signed-off-by: Francisco Franco <franciscofranco.1990@gmail.com>
drivers/misc/uid_sys_stats.c

index 0b26866617ae5953cdb577f87f46fe01a65c09c4..21b6c112697b02382c1199e4f51e069dc7f05d67 100644 (file)
@@ -211,14 +211,21 @@ static const struct file_operations uid_remove_fops = {
        .write          = uid_remove_write,
 };
 
+static u64 compute_write_bytes(struct task_struct *task)
+{
+       if (task->ioac.write_bytes <= task->ioac.cancelled_write_bytes)
+               return 0;
+
+       return task->ioac.write_bytes - task->ioac.cancelled_write_bytes;
+}
+
 static void add_uid_io_curr_stats(struct uid_entry *uid_entry,
                        struct task_struct *task)
 {
        struct io_stats *io_curr = &uid_entry->io[UID_STATE_TOTAL_CURR];
 
        io_curr->read_bytes += task->ioac.read_bytes;
-       io_curr->write_bytes +=
-               task->ioac.write_bytes - task->ioac.cancelled_write_bytes;
+       io_curr->write_bytes += compute_write_bytes(task);
        io_curr->rchar += task->ioac.rchar;
        io_curr->wchar += task->ioac.wchar;
 }
@@ -229,8 +236,7 @@ static void clean_uid_io_last_stats(struct uid_entry *uid_entry,
        struct io_stats *io_last = &uid_entry->io[UID_STATE_TOTAL_LAST];
 
        io_last->read_bytes -= task->ioac.read_bytes;
-       io_last->write_bytes -=
-               task->ioac.write_bytes - task->ioac.cancelled_write_bytes;
+       io_last->write_bytes -= compute_write_bytes(task);
        io_last->rchar -= task->ioac.rchar;
        io_last->wchar -= task->ioac.wchar;
 }