Skip to content

Commit 296ab4a

Browse files
committed
net/9p: use a dedicated spinlock for trans_fd
Shamelessly copying the explanation from Tetsuo Handa's suggested patch[1] (slightly reworded): syzbot is reporting inconsistent lock state in p9_req_put()[2], for p9_tag_remove() from p9_req_put() from IRQ context is using spin_lock_irqsave() on "struct p9_client"->lock but trans_fd (not from IRQ context) is using spin_lock(). Since the locks actually protect different things in client.c and in trans_fd.c, just replace trans_fd.c's lock by a new one specific to the transport (client.c's protect the idr for fid/tag allocations, while trans_fd.c's protects its own req list and request status field that acts as the transport's state machine) Link: https://lore.kernel.org/r/20220904112928.1308799-1-asmadeus@codewreck.org Link: https://lkml.kernel.org/r/2470e028-9b05-2013-7198-1fdad071d999@I-love.SAKURA.ne.jp [1] Link: https://syzkaller.appspot.com/bug?extid=2f20b523930c32c160cc [2] Reported-by: syzbot <syzbot+2f20b523930c32c160cc@syzkaller.appspotmail.com> Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Reviewed-by: Christian Schoenebeck <linux_oss@crudebyte.com> Signed-off-by: Dominique Martinet <asmadeus@codewreck.org>
1 parent ef57528 commit 296ab4a

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

net/9p/trans_fd.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct p9_poll_wait {
9191
* @mux_list: list link for mux to manage multiple connections (?)
9292
* @client: reference to client instance for this connection
9393
* @err: error state
94+
* @req_lock: lock protecting req_list and requests statuses
9495
* @req_list: accounting for requests which have been sent
9596
* @unsent_req_list: accounting for requests that haven't been sent
9697
* @rreq: read request
@@ -114,6 +115,7 @@ struct p9_conn {
114115
struct list_head mux_list;
115116
struct p9_client *client;
116117
int err;
118+
spinlock_t req_lock;
117119
struct list_head req_list;
118120
struct list_head unsent_req_list;
119121
struct p9_req_t *rreq;
@@ -189,10 +191,10 @@ static void p9_conn_cancel(struct p9_conn *m, int err)
189191

190192
p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
191193

192-
spin_lock(&m->client->lock);
194+
spin_lock(&m->req_lock);
193195

194196
if (m->err) {
195-
spin_unlock(&m->client->lock);
197+
spin_unlock(&m->req_lock);
196198
return;
197199
}
198200

@@ -205,7 +207,7 @@ static void p9_conn_cancel(struct p9_conn *m, int err)
205207
list_move(&req->req_list, &cancel_list);
206208
}
207209

208-
spin_unlock(&m->client->lock);
210+
spin_unlock(&m->req_lock);
209211

210212
list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
211213
p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
@@ -360,7 +362,7 @@ static void p9_read_work(struct work_struct *work)
360362
if ((m->rreq) && (m->rc.offset == m->rc.capacity)) {
361363
p9_debug(P9_DEBUG_TRANS, "got new packet\n");
362364
m->rreq->rc.size = m->rc.offset;
363-
spin_lock(&m->client->lock);
365+
spin_lock(&m->req_lock);
364366
if (m->rreq->status == REQ_STATUS_SENT) {
365367
list_del(&m->rreq->req_list);
366368
p9_client_cb(m->client, m->rreq, REQ_STATUS_RCVD);
@@ -369,14 +371,14 @@ static void p9_read_work(struct work_struct *work)
369371
p9_debug(P9_DEBUG_TRANS,
370372
"Ignore replies associated with a cancelled request\n");
371373
} else {
372-
spin_unlock(&m->client->lock);
374+
spin_unlock(&m->req_lock);
373375
p9_debug(P9_DEBUG_ERROR,
374376
"Request tag %d errored out while we were reading the reply\n",
375377
m->rc.tag);
376378
err = -EIO;
377379
goto error;
378380
}
379-
spin_unlock(&m->client->lock);
381+
spin_unlock(&m->req_lock);
380382
m->rc.sdata = NULL;
381383
m->rc.offset = 0;
382384
m->rc.capacity = 0;
@@ -454,10 +456,10 @@ static void p9_write_work(struct work_struct *work)
454456
}
455457

456458
if (!m->wsize) {
457-
spin_lock(&m->client->lock);
459+
spin_lock(&m->req_lock);
458460
if (list_empty(&m->unsent_req_list)) {
459461
clear_bit(Wworksched, &m->wsched);
460-
spin_unlock(&m->client->lock);
462+
spin_unlock(&m->req_lock);
461463
return;
462464
}
463465

@@ -472,7 +474,7 @@ static void p9_write_work(struct work_struct *work)
472474
m->wpos = 0;
473475
p9_req_get(req);
474476
m->wreq = req;
475-
spin_unlock(&m->client->lock);
477+
spin_unlock(&m->req_lock);
476478
}
477479

478480
p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
@@ -589,6 +591,7 @@ static void p9_conn_create(struct p9_client *client)
589591
INIT_LIST_HEAD(&m->mux_list);
590592
m->client = client;
591593

594+
spin_lock_init(&m->req_lock);
592595
INIT_LIST_HEAD(&m->req_list);
593596
INIT_LIST_HEAD(&m->unsent_req_list);
594597
INIT_WORK(&m->rq, p9_read_work);
@@ -670,10 +673,10 @@ static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
670673
if (m->err < 0)
671674
return m->err;
672675

673-
spin_lock(&client->lock);
676+
spin_lock(&m->req_lock);
674677
req->status = REQ_STATUS_UNSENT;
675678
list_add_tail(&req->req_list, &m->unsent_req_list);
676-
spin_unlock(&client->lock);
679+
spin_unlock(&m->req_lock);
677680

678681
if (test_and_clear_bit(Wpending, &m->wsched))
679682
n = EPOLLOUT;
@@ -688,33 +691,38 @@ static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
688691

689692
static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
690693
{
694+
struct p9_trans_fd *ts = client->trans;
695+
struct p9_conn *m = &ts->conn;
691696
int ret = 1;
692697

693698
p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
694699

695-
spin_lock(&client->lock);
700+
spin_lock(&m->req_lock);
696701

697702
if (req->status == REQ_STATUS_UNSENT) {
698703
list_del(&req->req_list);
699704
req->status = REQ_STATUS_FLSHD;
700705
p9_req_put(client, req);
701706
ret = 0;
702707
}
703-
spin_unlock(&client->lock);
708+
spin_unlock(&m->req_lock);
704709

705710
return ret;
706711
}
707712

708713
static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
709714
{
715+
struct p9_trans_fd *ts = client->trans;
716+
struct p9_conn *m = &ts->conn;
717+
710718
p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
711719

712-
spin_lock(&client->lock);
720+
spin_lock(&m->req_lock);
713721
/* Ignore cancelled request if message has been received
714722
* before lock.
715723
*/
716724
if (req->status == REQ_STATUS_RCVD) {
717-
spin_unlock(&client->lock);
725+
spin_unlock(&m->req_lock);
718726
return 0;
719727
}
720728

@@ -723,7 +731,8 @@ static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
723731
*/
724732
list_del(&req->req_list);
725733
req->status = REQ_STATUS_FLSHD;
726-
spin_unlock(&client->lock);
734+
spin_unlock(&m->req_lock);
735+
727736
p9_req_put(client, req);
728737

729738
return 0;

0 commit comments

Comments
 (0)