@@ -45,7 +45,7 @@ static __always_inline bool udp_nearly_out_of_memory(struct sock *sk,
4545 struct proto * prot = BPFTUNE_CORE_READ (sk , sk_prot );
4646 atomic_long_t * memory_allocated = BPFTUNE_CORE_READ (prot , memory_allocated );
4747 long * sysctl_mem = BPFTUNE_CORE_READ (prot , sysctl_mem );
48- __u8 shift_left = 0 , shift_right = 0 ;
48+ __s8 shift = 0 ;
4949 struct net * net ;
5050 int i ;
5151
@@ -64,22 +64,22 @@ static __always_inline bool udp_nearly_out_of_memory(struct sock *sk,
6464 if (!mem [0 ] || !mem [1 ] || !mem [2 ])
6565 return false;
6666
67- if (kernel_page_shift >= sk_mem_quantum_shift ) {
68- shift_left = kernel_page_shift - sk_mem_quantum_shift ;
69- if (shift_left >= 32 )
70- return false;
71- } else {
72- shift_right = sk_mem_quantum_shift - kernel_page_shift ;
73- if (shift_right >= 32 )
67+ if (LINUX_KERNEL_VERSION < KERNEL_VERSION (5 , 16 , 0 )) {
68+ /* we are on v5.15 or earlier; mem quantum is used
69+ * to shift limits.
70+ */
71+ shift = sk_mem_quantum_shift - kernel_page_shift ;
72+ if (shift >= 32 || shift <= -32 )
7473 return false;
7574 }
7675
7776 for (i = 0 ; i < 3 ; i ++ ) {
7877 limit_sk_mem_quantum [i ] = mem [i ];
79- if (shift_left )
80- limit_sk_mem_quantum [i ] <<= shift_left ;
81- if (shift_right )
82- limit_sk_mem_quantum [i ] >>= shift_right ;
78+ if (shift > 0 )
79+ limit_sk_mem_quantum [i ] >>= shift ;
80+ else if (shift < 0 )
81+ limit_sk_mem_quantum [i ] <<= shift ;
82+
8383 if (limit_sk_mem_quantum [i ] <= 0 )
8484 return false;
8585 }
@@ -245,6 +245,7 @@ int BPF_PROG(bpftune_sock_exceed_buf_limit, struct sock *sk, struct proto *prot,
245245{
246246 if (kind == SK_MEM_RECV ) {
247247 __u16 proto = BPFTUNE_CORE_READ (sk , sk_protocol );
248+
248249 if (proto == IPPROTO_UDP )
249250 return udp_fail_rcv (- ENOBUFS , sk );
250251 }
0 commit comments