Skip to content

Commit 2651ee5

Browse files
Jakob-Koschelaxboe
authored andcommitted
drbd: remove check of list iterator against head past the loop body
When list_for_each_entry() completes the iteration over the whole list without breaking the loop, the iterator value will be a bogus pointer computed based on the head element. While it is safe to use the pointer to determine if it was computed based on the head element, either with list_entry_is_head() or &pos->member == head, using the iterator variable after the loop should be avoided. In preparation to limit the scope of a list iterator to the list traversal loop, use a dedicated pointer to point to the found element [1]. Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com> Reviewed-by: Christoph Böhmwalder <christoph.boehmwalder@linbit.com> Link: https://lore.kernel.org/r/20220331220349.885126-2-jakobkoschel@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 901aeda commit 2651ee5

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

drivers/block/drbd/drbd_req.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,21 @@ static void set_if_null_req_next(struct drbd_peer_device *peer_device, struct dr
334334
static void advance_conn_req_next(struct drbd_peer_device *peer_device, struct drbd_request *req)
335335
{
336336
struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
337+
struct drbd_request *iter = req;
337338
if (!connection)
338339
return;
339340
if (connection->req_next != req)
340341
return;
341-
list_for_each_entry_continue(req, &connection->transfer_log, tl_requests) {
342-
const unsigned s = req->rq_state;
343-
if (s & RQ_NET_QUEUED)
342+
343+
req = NULL;
344+
list_for_each_entry_continue(iter, &connection->transfer_log, tl_requests) {
345+
const unsigned int s = iter->rq_state;
346+
347+
if (s & RQ_NET_QUEUED) {
348+
req = iter;
344349
break;
350+
}
345351
}
346-
if (&req->tl_requests == &connection->transfer_log)
347-
req = NULL;
348352
connection->req_next = req;
349353
}
350354

@@ -360,17 +364,21 @@ static void set_if_null_req_ack_pending(struct drbd_peer_device *peer_device, st
360364
static void advance_conn_req_ack_pending(struct drbd_peer_device *peer_device, struct drbd_request *req)
361365
{
362366
struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
367+
struct drbd_request *iter = req;
363368
if (!connection)
364369
return;
365370
if (connection->req_ack_pending != req)
366371
return;
367-
list_for_each_entry_continue(req, &connection->transfer_log, tl_requests) {
368-
const unsigned s = req->rq_state;
369-
if ((s & RQ_NET_SENT) && (s & RQ_NET_PENDING))
372+
373+
req = NULL;
374+
list_for_each_entry_continue(iter, &connection->transfer_log, tl_requests) {
375+
const unsigned int s = iter->rq_state;
376+
377+
if ((s & RQ_NET_SENT) && (s & RQ_NET_PENDING)) {
378+
req = iter;
370379
break;
380+
}
371381
}
372-
if (&req->tl_requests == &connection->transfer_log)
373-
req = NULL;
374382
connection->req_ack_pending = req;
375383
}
376384

@@ -386,17 +394,21 @@ static void set_if_null_req_not_net_done(struct drbd_peer_device *peer_device, s
386394
static void advance_conn_req_not_net_done(struct drbd_peer_device *peer_device, struct drbd_request *req)
387395
{
388396
struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
397+
struct drbd_request *iter = req;
389398
if (!connection)
390399
return;
391400
if (connection->req_not_net_done != req)
392401
return;
393-
list_for_each_entry_continue(req, &connection->transfer_log, tl_requests) {
394-
const unsigned s = req->rq_state;
395-
if ((s & RQ_NET_SENT) && !(s & RQ_NET_DONE))
402+
403+
req = NULL;
404+
list_for_each_entry_continue(iter, &connection->transfer_log, tl_requests) {
405+
const unsigned int s = iter->rq_state;
406+
407+
if ((s & RQ_NET_SENT) && !(s & RQ_NET_DONE)) {
408+
req = iter;
396409
break;
410+
}
397411
}
398-
if (&req->tl_requests == &connection->transfer_log)
399-
req = NULL;
400412
connection->req_not_net_done = req;
401413
}
402414

0 commit comments

Comments
 (0)