hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/include/net/sock.h
....@@ -317,7 +317,7 @@
317317 * @sk_tskey: counter to disambiguate concurrent tstamp requests
318318 * @sk_zckey: counter to order MSG_ZEROCOPY notifications
319319 * @sk_socket: Identd and reporting IO signals
320
- * @sk_user_data: RPC layer private data
320
+ * @sk_user_data: RPC layer private data. Write-protected by @sk_callback_lock.
321321 * @sk_frag: cached page frag
322322 * @sk_peek_off: current peek_offset value
323323 * @sk_send_head: front of stuff to transmit
....@@ -1092,8 +1092,12 @@
10921092 * OR an additional socket flag
10931093 * [1] : sk_state and sk_prot are in the same cache line.
10941094 */
1095
- if (sk->sk_state == TCP_ESTABLISHED)
1096
- sock_rps_record_flow_hash(sk->sk_rxhash);
1095
+ if (sk->sk_state == TCP_ESTABLISHED) {
1096
+ /* This READ_ONCE() is paired with the WRITE_ONCE()
1097
+ * from sock_rps_save_rxhash() and sock_rps_reset_rxhash().
1098
+ */
1099
+ sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash));
1100
+ }
10971101 }
10981102 #endif
10991103 }
....@@ -1102,15 +1106,19 @@
11021106 const struct sk_buff *skb)
11031107 {
11041108 #ifdef CONFIG_RPS
1105
- if (unlikely(sk->sk_rxhash != skb->hash))
1106
- sk->sk_rxhash = skb->hash;
1109
+ /* The following WRITE_ONCE() is paired with the READ_ONCE()
1110
+ * here, and another one in sock_rps_record_flow().
1111
+ */
1112
+ if (unlikely(READ_ONCE(sk->sk_rxhash) != skb->hash))
1113
+ WRITE_ONCE(sk->sk_rxhash, skb->hash);
11071114 #endif
11081115 }
11091116
11101117 static inline void sock_rps_reset_rxhash(struct sock *sk)
11111118 {
11121119 #ifdef CONFIG_RPS
1113
- sk->sk_rxhash = 0;
1120
+ /* Paired with READ_ONCE() in sock_rps_record_flow() */
1121
+ WRITE_ONCE(sk->sk_rxhash, 0);
11141122 #endif
11151123 }
11161124
....@@ -1240,6 +1248,7 @@
12401248 /*
12411249 * Pressure flag: try to collapse.
12421250 * Technical note: it is used by multiple contexts non atomically.
1251
+ * Make sure to use READ_ONCE()/WRITE_ONCE() for all reads/writes.
12431252 * All the __sk_mem_schedule() is of this nature: accounting
12441253 * is strict, actions are advisory and have some latency.
12451254 */
....@@ -1353,6 +1362,12 @@
13531362 return sk->sk_prot->memory_pressure != NULL;
13541363 }
13551364
1365
+static inline bool sk_under_global_memory_pressure(const struct sock *sk)
1366
+{
1367
+ return sk->sk_prot->memory_pressure &&
1368
+ !!READ_ONCE(*sk->sk_prot->memory_pressure);
1369
+}
1370
+
13561371 static inline bool sk_under_memory_pressure(const struct sock *sk)
13571372 {
13581373 if (!sk->sk_prot->memory_pressure)
....@@ -1362,7 +1377,7 @@
13621377 mem_cgroup_under_socket_pressure(sk->sk_memcg))
13631378 return true;
13641379
1365
- return !!*sk->sk_prot->memory_pressure;
1380
+ return !!READ_ONCE(*sk->sk_prot->memory_pressure);
13661381 }
13671382
13681383 static inline long
....@@ -1416,7 +1431,7 @@
14161431 {
14171432 if (!prot->memory_pressure)
14181433 return false;
1419
- return !!*prot->memory_pressure;
1434
+ return !!READ_ONCE(*prot->memory_pressure);
14201435 }
14211436
14221437
....@@ -1796,7 +1811,12 @@
17961811 * Default socket callbacks and setup code
17971812 */
17981813
1799
-/* Initialise core socket variables */
1814
+/* Initialise core socket variables using an explicit uid. */
1815
+void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid);
1816
+
1817
+/* Initialise core socket variables.
1818
+ * Assumes struct socket *sock is embedded in a struct socket_alloc.
1819
+ */
18001820 void sock_init_data(struct socket *sock, struct sock *sk);
18011821
18021822 /*
....@@ -1936,6 +1956,7 @@
19361956 }
19371957
19381958 kuid_t sock_i_uid(struct sock *sk);
1959
+unsigned long __sock_i_ino(struct sock *sk);
19391960 unsigned long sock_i_ino(struct sock *sk);
19401961
19411962 static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
....@@ -2264,6 +2285,19 @@
22642285 return false;
22652286 }
22662287
2288
+static inline struct sk_buff *skb_clone_and_charge_r(struct sk_buff *skb, struct sock *sk)
2289
+{
2290
+ skb = skb_clone(skb, sk_gfp_mask(sk, GFP_ATOMIC));
2291
+ if (skb) {
2292
+ if (sk_rmem_schedule(sk, skb, skb->truesize)) {
2293
+ skb_set_owner_r(skb, sk);
2294
+ return skb;
2295
+ }
2296
+ __kfree_skb(skb);
2297
+ }
2298
+ return NULL;
2299
+}
2300
+
22672301 void sk_reset_timer(struct sock *sk, struct timer_list *timer,
22682302 unsigned long expires);
22692303
....@@ -2538,7 +2572,7 @@
25382572 __sock_recv_ts_and_drops(msg, sk, skb);
25392573 else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
25402574 sock_write_timestamp(sk, skb->tstamp);
2541
- else if (unlikely(sk->sk_stamp == SK_DEFAULT_STAMP))
2575
+ else if (unlikely(sock_read_timestamp(sk) == SK_DEFAULT_STAMP))
25422576 sock_write_timestamp(sk, 0);
25432577 }
25442578