Skip to content

Commit

Permalink
lib: percpu_counter_sub
Browse files Browse the repository at this point in the history
Hugh spotted that some code does:
  percpu_counter_add(&counter, -unsignedlong)

which, when the amount argument is of type s32, sort-of works thanks to
two's-complement. However when we'd change the type to s64 this breaks on 32bit
machines, because the promotion rules zero extend the unsigned number.

Provide percpu_counter_sub() to hide the s64 cast. That is:
  percpu_counter_sub(&counter, foo)
is equal to:
  percpu_counter_add(&counter, -(s64)foo);

Signed-off-by: Peter Zijlstra <[email protected]>
Cc: Hugh Dickins <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Peter Zijlstra authored and Linus Torvalds committed Oct 17, 2007
1 parent aa0dff2 commit 3cb4f9f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fs/ext2/balloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int reserve_blocks(struct super_block *sb, int count)
return 0;
}

percpu_counter_add(&sbi->s_freeblocks_counter, -count);
percpu_counter_sub(&sbi->s_freeblocks_counter, count);
sb->s_dirt = 1;
return count;
}
Expand Down
2 changes: 1 addition & 1 deletion fs/ext3/balloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode,
gdp->bg_free_blocks_count =
cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num);
spin_unlock(sb_bgl_lock(sbi, group_no));
percpu_counter_add(&sbi->s_freeblocks_counter, -num);
percpu_counter_sub(&sbi->s_freeblocks_counter, num);

BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
err = ext3_journal_dirty_metadata(handle, gdp_bh);
Expand Down
2 changes: 1 addition & 1 deletion fs/ext4/balloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
gdp->bg_free_blocks_count =
cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num);
spin_unlock(sb_bgl_lock(sbi, group_no));
percpu_counter_add(&sbi->s_freeblocks_counter, -num);
percpu_counter_sub(&sbi->s_freeblocks_counter, num);

BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
err = ext4_journal_dirty_metadata(handle, gdp_bh);
Expand Down
5 changes: 5 additions & 0 deletions include/linux/percpu_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ static inline void percpu_counter_dec(struct percpu_counter *fbc)
percpu_counter_add(fbc, -1);
}

static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_add(fbc, -amount);
}

#endif /* _LINUX_PERCPU_COUNTER_H */

0 comments on commit 3cb4f9f

Please sign in to comment.