From a1caec1f2bebe09f685716f13d7b55f84d8a8145 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 19 Nov 2020 13:48:14 +0100 Subject: [KERNEL] Fix several connect/disconnect race conditions Previously disconnect was protected against concurrent calls, but connect wasn't. It was easy to crash the kernel when calling connect and disconnect IOCTLs in a tight loop concurrently. A global lock was introduced to make sure only one caller can change the connection state at a time. dev->connection_lock needs to be aquired when calling dnbd3_net_connect or _disconnect. This atomic_t based locking mechanism should be turned into a mutex in a next step, relying on mutex_trylock for cases where we don't have the cmpxchg-schedule() loop. Along the way it was noticed that the send/receive timeouts don't apply to kernel_connect, which might have been the case in older 3.x kernel versions. A crude workaround using nonblocking connect has been introduced to emulate this, but a clean solution for this is welcomed. Also, devices are now properly closed on module unload. --- src/kernel/dnbd3_main.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/kernel/dnbd3_main.h') diff --git a/src/kernel/dnbd3_main.h b/src/kernel/dnbd3_main.h index a3c2828..ec8c8cf 100644 --- a/src/kernel/dnbd3_main.h +++ b/src/kernel/dnbd3_main.h @@ -62,7 +62,8 @@ typedef struct dnbd3_server_t alt_servers[NUMBER_SERVERS]; // array of alt servers int new_servers_num; // number of new alt servers that are waiting to be copied to above array dnbd3_server_entry_t new_servers[NUMBER_SERVERS]; // pending new alt servers - uint8_t discover, panic, disconnecting, update_available, panic_count; + uint8_t discover, panic, update_available, panic_count; + atomic_t connection_lock; uint8_t use_server_provided_alts; uint16_t rid; uint32_t heartbeat_count; -- cgit v1.2.3-55-g7522