|
54 | 54 |
|
55 | 55 | #include <linux/filter.h> |
56 | 56 | #include <linux/rculist_nulls.h> |
| 57 | +#include <linux/poll.h> |
57 | 58 |
|
58 | 59 | #include <asm/atomic.h> |
59 | 60 | #include <net/dst.h> |
@@ -1241,6 +1242,71 @@ static inline int sk_has_allocations(const struct sock *sk) |
1241 | 1242 | return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk); |
1242 | 1243 | } |
1243 | 1244 |
|
| 1245 | +/** |
| 1246 | + * sk_has_sleeper - check if there are any waiting processes |
| 1247 | + * @sk: socket |
| 1248 | + * |
| 1249 | + * Returns true if socket has waiting processes |
| 1250 | + * |
| 1251 | + * The purpose of the sk_has_sleeper and sock_poll_wait is to wrap the memory |
| 1252 | + * barrier call. They were added due to the race found within the tcp code. |
| 1253 | + * |
| 1254 | + * Consider following tcp code paths: |
| 1255 | + * |
| 1256 | + * CPU1 CPU2 |
| 1257 | + * |
| 1258 | + * sys_select receive packet |
| 1259 | + * ... ... |
| 1260 | + * __add_wait_queue update tp->rcv_nxt |
| 1261 | + * ... ... |
| 1262 | + * tp->rcv_nxt check sock_def_readable |
| 1263 | + * ... { |
| 1264 | + * schedule ... |
| 1265 | + * if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
| 1266 | + * wake_up_interruptible(sk->sk_sleep) |
| 1267 | + * ... |
| 1268 | + * } |
| 1269 | + * |
| 1270 | + * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay |
| 1271 | + * in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1 |
| 1272 | + * could then endup calling schedule and sleep forever if there are no more |
| 1273 | + * data on the socket. |
| 1274 | + */ |
| 1275 | +static inline int sk_has_sleeper(struct sock *sk) |
| 1276 | +{ |
| 1277 | + /* |
| 1278 | + * We need to be sure we are in sync with the |
| 1279 | + * add_wait_queue modifications to the wait queue. |
| 1280 | + * |
| 1281 | + * This memory barrier is paired in the sock_poll_wait. |
| 1282 | + */ |
| 1283 | + smp_mb(); |
| 1284 | + return sk->sk_sleep && waitqueue_active(sk->sk_sleep); |
| 1285 | +} |
| 1286 | + |
| 1287 | +/** |
| 1288 | + * sock_poll_wait - place memory barrier behind the poll_wait call. |
| 1289 | + * @filp: file |
| 1290 | + * @wait_address: socket wait queue |
| 1291 | + * @p: poll_table |
| 1292 | + * |
| 1293 | + * See the comments in the sk_has_sleeper function. |
| 1294 | + */ |
| 1295 | +static inline void sock_poll_wait(struct file *filp, |
| 1296 | + wait_queue_head_t *wait_address, poll_table *p) |
| 1297 | +{ |
| 1298 | + if (p && wait_address) { |
| 1299 | + poll_wait(filp, wait_address, p); |
| 1300 | + /* |
| 1301 | + * We need to be sure we are in sync with the |
| 1302 | + * socket flags modification. |
| 1303 | + * |
| 1304 | + * This memory barrier is paired in the sk_has_sleeper. |
| 1305 | + */ |
| 1306 | + smp_mb(); |
| 1307 | + } |
| 1308 | +} |
| 1309 | + |
1244 | 1310 | /* |
1245 | 1311 | * Queue a received datagram if it will fit. Stream and sequenced |
1246 | 1312 | * protocols can't normally use this as they need to fit buffers in |
|
0 commit comments