Skip to content

Commit 3fd8ec2

Browse files
Wang Zhaolongsmfrench
authored andcommitted
smb: client: smb: client: eliminate mid_flags field
This is step 3/4 of a patch series to fix mid_q_entry memory leaks caused by race conditions in callback execution. Replace the mid_flags bitmask with dedicated boolean fields to simplify locking logic and improve code readability: - Replace MID_DELETED with bool deleted_from_q - Replace MID_WAIT_CANCELLED with bool wait_cancelled - Remove mid_flags field entirely The new boolean fields have clearer semantics: - deleted_from_q: whether mid has been removed from pending_mid_q - wait_cancelled: whether request was cancelled during wait This change reduces memory usage (from 4-byte bitmask to 2 boolean flags) and eliminates confusion about which lock protects which flag bits, preparing for per-mid locking in the next patch. Signed-off-by: Wang Zhaolong <[email protected]> Acked-by: Enzo Matsumiya <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 9bd4279 commit 3fd8ec2

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

fs/smb/client/cifsglob.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,9 +1730,10 @@ struct mid_q_entry {
17301730
unsigned int resp_buf_size;
17311731
int mid_state; /* wish this were enum but can not pass to wait_event */
17321732
int mid_rc; /* rc for MID_RC */
1733-
unsigned int mid_flags;
17341733
__le16 command; /* smb command code */
17351734
unsigned int optype; /* operation type */
1735+
bool wait_cancelled:1; /* Cancelled while waiting for response */
1736+
bool deleted_from_q:1; /* Whether Mid has been dequeued frem pending_mid_q */
17361737
bool large_buf:1; /* if valid response, is pointer to large buf */
17371738
bool multiRsp:1; /* multiple trans2 responses for one request */
17381739
bool multiEnd:1; /* both received */
@@ -1894,10 +1895,6 @@ static inline bool is_replayable_error(int error)
18941895
#define MID_RESPONSE_READY 0x40 /* ready for other process handle the rsp */
18951896
#define MID_RC 0x80 /* mid_rc contains custom rc */
18961897

1897-
/* Flags */
1898-
#define MID_WAIT_CANCELLED 1 /* Cancelled while waiting for response */
1899-
#define MID_DELETED 2 /* Mid has been dequeued/deleted */
1900-
19011898
/* Types of response buffer returned from SendReceive2 */
19021899
#define CIFS_NO_BUFFER 0 /* Response buffer not returned */
19031900
#define CIFS_SMALL_BUFFER 1
@@ -2009,7 +2006,7 @@ require use of the stronger protocol */
20092006
* GlobalTotalActiveXid
20102007
* TCP_Server_Info->srv_lock (anything in struct not protected by another lock and can change)
20112008
* TCP_Server_Info->mid_queue_lock TCP_Server_Info->pending_mid_q cifs_get_tcp_session
2012-
* (any changes in mid_q_entry fields)
2009+
* mid_q_entry->deleted_from_q
20132010
* TCP_Server_Info->mid_counter_lock TCP_Server_Info->current_mid cifs_get_tcp_session
20142011
* TCP_Server_Info->req_lock TCP_Server_Info->in_flight cifs_get_tcp_session
20152012
* ->credits

fs/smb/client/connect.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ cifs_abort_connection(struct TCP_Server_Info *server)
327327
if (mid->mid_state == MID_REQUEST_SUBMITTED)
328328
mid->mid_state = MID_RETRY_NEEDED;
329329
list_move(&mid->qhead, &retry_list);
330-
mid->mid_flags |= MID_DELETED;
330+
mid->deleted_from_q = true;
331331
}
332332
spin_unlock(&server->mid_queue_lock);
333333
cifs_server_unlock(server);
@@ -888,7 +888,7 @@ is_smb_response(struct TCP_Server_Info *server, unsigned char type)
888888
list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
889889
kref_get(&mid->refcount);
890890
list_move(&mid->qhead, &dispose_list);
891-
mid->mid_flags |= MID_DELETED;
891+
mid->deleted_from_q = true;
892892
}
893893
spin_unlock(&server->mid_queue_lock);
894894

@@ -966,12 +966,12 @@ dequeue_mid(struct mid_q_entry *mid, bool malformed)
966966
* Trying to handle/dequeue a mid after the send_recv()
967967
* function has finished processing it is a bug.
968968
*/
969-
if (mid->mid_flags & MID_DELETED) {
969+
if (mid->deleted_from_q == true) {
970970
spin_unlock(&mid->server->mid_queue_lock);
971971
pr_warn_once("trying to dequeue a deleted mid\n");
972972
} else {
973973
list_del_init(&mid->qhead);
974-
mid->mid_flags |= MID_DELETED;
974+
mid->deleted_from_q = true;
975975
spin_unlock(&mid->server->mid_queue_lock);
976976
}
977977
}
@@ -1108,7 +1108,7 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
11081108
kref_get(&mid_entry->refcount);
11091109
mid_entry->mid_state = MID_SHUTDOWN;
11101110
list_move(&mid_entry->qhead, &dispose_list);
1111-
mid_entry->mid_flags |= MID_DELETED;
1111+
mid_entry->deleted_from_q = true;
11121112
}
11131113
spin_unlock(&server->mid_queue_lock);
11141114

fs/smb/client/smb2ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
409409
kref_get(&mid->refcount);
410410
if (dequeue) {
411411
list_del_init(&mid->qhead);
412-
mid->mid_flags |= MID_DELETED;
412+
mid->deleted_from_q = true;
413413
}
414414
spin_unlock(&server->mid_queue_lock);
415415
return mid;
@@ -4817,7 +4817,7 @@ static void smb2_decrypt_offload(struct work_struct *work)
48174817
} else {
48184818
spin_lock(&dw->server->mid_queue_lock);
48194819
mid->mid_state = MID_REQUEST_SUBMITTED;
4820-
mid->mid_flags &= ~(MID_DELETED);
4820+
mid->deleted_from_q = false;
48214821
list_add_tail(&mid->qhead,
48224822
&dw->server->pending_mid_q);
48234823
spin_unlock(&dw->server->mid_queue_lock);

fs/smb/client/transport.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void __release_mid(struct kref *refcount)
8989
#endif
9090
struct TCP_Server_Info *server = midEntry->server;
9191

92-
if (midEntry->resp_buf && (midEntry->mid_flags & MID_WAIT_CANCELLED) &&
92+
if (midEntry->resp_buf && (midEntry->wait_cancelled) &&
9393
(midEntry->mid_state == MID_RESPONSE_RECEIVED ||
9494
midEntry->mid_state == MID_RESPONSE_READY) &&
9595
server->ops->handle_cancelled_mid)
@@ -161,9 +161,9 @@ void
161161
delete_mid(struct mid_q_entry *mid)
162162
{
163163
spin_lock(&mid->server->mid_queue_lock);
164-
if (!(mid->mid_flags & MID_DELETED)) {
164+
if (mid->deleted_from_q == false) {
165165
list_del_init(&mid->qhead);
166-
mid->mid_flags |= MID_DELETED;
166+
mid->deleted_from_q = true;
167167
}
168168
spin_unlock(&mid->server->mid_queue_lock);
169169

@@ -898,9 +898,9 @@ cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
898898
rc = mid->mid_rc;
899899
break;
900900
default:
901-
if (!(mid->mid_flags & MID_DELETED)) {
901+
if (mid->deleted_from_q == false) {
902902
list_del_init(&mid->qhead);
903-
mid->mid_flags |= MID_DELETED;
903+
mid->deleted_from_q = true;
904904
}
905905
spin_unlock(&server->mid_queue_lock);
906906
cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
@@ -1214,7 +1214,7 @@ compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
12141214
midQ[i]->mid, le16_to_cpu(midQ[i]->command));
12151215
send_cancel(server, &rqst[i], midQ[i]);
12161216
spin_lock(&server->mid_queue_lock);
1217-
midQ[i]->mid_flags |= MID_WAIT_CANCELLED;
1217+
midQ[i]->wait_cancelled = true;
12181218
if (midQ[i]->mid_state == MID_REQUEST_SUBMITTED ||
12191219
midQ[i]->mid_state == MID_RESPONSE_RECEIVED) {
12201220
midQ[i]->callback = cifs_cancelled_callback;

0 commit comments

Comments
 (0)