Skip to content
/ linux Public
forked from torvalds/linux

Commit

Permalink
acct: fix accuracy loss for input value of encode_comp_t()
Browse files Browse the repository at this point in the history
Patch series "Fix encode_comp_t()".

Type conversion in encode_comp_t() may look a bit problematic.


This patch (of 2):

See calculation of ac_{u,s}time in fill_ac():
  > ac->ac_utime = encode_comp_t(nsec_to_AHZ(pacct->ac_utime));
  > ac->ac_stime = encode_comp_t(nsec_to_AHZ(pacct->ac_stime));

Return value of nsec_to_AHZ() is always type of 'u64', but it is handled
as type of 'unsigned long' in encode_comp_t, and accuracy loss would
happen on 32-bit platform when 'unsigned long' value is 32-bit-width.

So 'u64' value of encode_comp_t() may look better.

Link: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Zheng Yejian <[email protected]>
Cc: Hanjun Guo <[email protected]>
Cc: Randy Dunlap <[email protected]> # build-tested
Cc: Vlastimil Babka <[email protected]>
Cc: Zhang Jinhao <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
Zheng Yejian authored and akpm00 committed Dec 1, 2022
1 parent de985c1 commit 457139f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions kernel/acct.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void acct_exit_ns(struct pid_namespace *ns)
}

/*
* encode an unsigned long into a comp_t
* encode an u64 into a comp_t
*
* This routine has been adopted from the encode_comp_t() function in
* the kern_acct.c file of the FreeBSD operating system. The encoding
Expand All @@ -331,7 +331,7 @@ void acct_exit_ns(struct pid_namespace *ns)
#define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
#define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */

static comp_t encode_comp_t(unsigned long value)
static comp_t encode_comp_t(u64 value)
{
int exp, rnd;

Expand Down

0 comments on commit 457139f

Please sign in to comment.