Skip to content

Commit 21371f7

Browse files
Eric Dumazetdavem330
authored andcommitted
[SOCK] Avoid divides in sk_stream_pages() and __sk_stream_mem_reclaim()
sk_forward_alloc being signed, we should take care of divides by SK_STREAM_MEM_QUANTUM we do in sk_stream_pages() and __sk_stream_mem_reclaim() This patchs introduces SK_STREAM_MEM_QUANTUM_SHIFT, defined as ilog2(SK_STREAM_MEM_QUANTUM), to be able to use right shifts instead of plain divides. This should help compiler to choose right shifts instead of expensive divides (as seen with CONFIG_CC_OPTIMIZE_FOR_SIZE=y on x86) Signed-off-by: Eric Dumazet <dada1@cosmosbay.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b15c4bc commit 21371f7

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

‎include/net/sock.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,11 @@ extern void __sk_stream_mem_reclaim(struct sock *sk);
716716
extern int sk_stream_mem_schedule(struct sock *sk, int size, int kind);
717717

718718
#define SK_STREAM_MEM_QUANTUM ((int)PAGE_SIZE)
719+
#define SK_STREAM_MEM_QUANTUM_SHIFT ilog2(SK_STREAM_MEM_QUANTUM)
719720

720721
static inline int sk_stream_pages(int amt)
721722
{
722-
return DIV_ROUND_UP(amt, SK_STREAM_MEM_QUANTUM);
723+
return (amt + SK_STREAM_MEM_QUANTUM - 1) >> SK_STREAM_MEM_QUANTUM_SHIFT;
723724
}
724725

725726
static inline void sk_stream_mem_reclaim(struct sock *sk)

‎net/core/stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ EXPORT_SYMBOL(sk_stream_error);
196196

197197
void __sk_stream_mem_reclaim(struct sock *sk)
198198
{
199-
atomic_sub(sk->sk_forward_alloc / SK_STREAM_MEM_QUANTUM,
199+
atomic_sub(sk->sk_forward_alloc >> SK_STREAM_MEM_QUANTUM_SHIFT,
200200
sk->sk_prot->memory_allocated);
201201
sk->sk_forward_alloc &= SK_STREAM_MEM_QUANTUM - 1;
202202
if (*sk->sk_prot->memory_pressure &&

0 commit comments

Comments
 (0)