drbd: Proper locking for updates to net_conf under RCU

Removing the get_net_conf()/put_net_conf() functions

Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
diff --git a/drivers/block/drbd/drbd_receiver.c b/drivers/block/drbd/drbd_receiver.c
index 59f9af9..397f977 100644
--- a/drivers/block/drbd/drbd_receiver.c
+++ b/drivers/block/drbd/drbd_receiver.c
@@ -3138,6 +3138,7 @@
 	unsigned int header_size, data_size, exp_max_sz;
 	struct crypto_hash *verify_tfm = NULL;
 	struct crypto_hash *csums_tfm = NULL;
+	struct net_conf *old_conf, *new_conf = NULL;
 	const int apv = tconn->agreed_pro_version;
 	int *rs_plan_s = NULL;
 	int fifo_size = 0;
@@ -3212,10 +3213,13 @@
 			p->csums_alg[SHARED_SECRET_MAX-1] = 0;
 		}
 
-		if (strcmp(mdev->tconn->net_conf->verify_alg, p->verify_alg)) {
+		mutex_lock(&mdev->tconn->net_conf_update);
+		old_conf = mdev->tconn->net_conf;
+
+		if (strcmp(old_conf->verify_alg, p->verify_alg)) {
 			if (mdev->state.conn == C_WF_REPORT_PARAMS) {
 				dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
-				    mdev->tconn->net_conf->verify_alg, p->verify_alg);
+				    old_conf->verify_alg, p->verify_alg);
 				goto disconnect;
 			}
 			verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
@@ -3226,10 +3230,10 @@
 			}
 		}
 
-		if (apv >= 89 && strcmp(mdev->tconn->net_conf->csums_alg, p->csums_alg)) {
+		if (apv >= 89 && strcmp(old_conf->csums_alg, p->csums_alg)) {
 			if (mdev->state.conn == C_WF_REPORT_PARAMS) {
 				dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
-				    mdev->tconn->net_conf->csums_alg, p->csums_alg);
+				    old_conf->csums_alg, p->csums_alg);
 				goto disconnect;
 			}
 			csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
@@ -3259,22 +3263,38 @@
 			put_ldev(mdev);
 		}
 
+		if (verify_tfm || csums_tfm) {
+			new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
+			if (!new_conf) {
+				dev_err(DEV, "Allocation of new net_conf failed\n");
+				goto disconnect;
+			}
+
+			*new_conf = *old_conf;
+
+			if (verify_tfm) {
+				strcpy(new_conf->verify_alg, p->verify_alg);
+				new_conf->verify_alg_len = strlen(p->verify_alg) + 1;
+				crypto_free_hash(mdev->tconn->verify_tfm);
+				mdev->tconn->verify_tfm = verify_tfm;
+				dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
+			}
+			if (csums_tfm) {
+				strcpy(new_conf->csums_alg, p->csums_alg);
+				new_conf->csums_alg_len = strlen(p->csums_alg) + 1;
+				crypto_free_hash(mdev->tconn->csums_tfm);
+				mdev->tconn->csums_tfm = csums_tfm;
+				dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
+			}
+			rcu_assign_pointer(tconn->net_conf, new_conf);
+		}
+		mutex_unlock(&mdev->tconn->net_conf_update);
+		if (new_conf) {
+			synchronize_rcu();
+			kfree(old_conf);
+		}
+
 		spin_lock(&mdev->peer_seq_lock);
-		/* lock against drbd_nl_syncer_conf() */
-		if (verify_tfm) {
-			strcpy(mdev->tconn->net_conf->verify_alg, p->verify_alg);
-			mdev->tconn->net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
-			crypto_free_hash(mdev->tconn->verify_tfm);
-			mdev->tconn->verify_tfm = verify_tfm;
-			dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
-		}
-		if (csums_tfm) {
-			strcpy(mdev->tconn->net_conf->csums_alg, p->csums_alg);
-			mdev->tconn->net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
-			crypto_free_hash(mdev->tconn->csums_tfm);
-			mdev->tconn->csums_tfm = csums_tfm;
-			dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
-		}
 		if (fifo_size != mdev->rs_plan_s.size) {
 			kfree(mdev->rs_plan_s.values);
 			mdev->rs_plan_s.values = rs_plan_s;
@@ -3286,6 +3306,7 @@
 	return 0;
 
 disconnect:
+	mutex_unlock(&mdev->tconn->net_conf_update);
 	/* just for completeness: actually not needed,
 	 * as this is not reached if csums_tfm was ok. */
 	crypto_free_hash(csums_tfm);
@@ -3715,7 +3736,9 @@
 		}
 	}
 
-	mdev->tconn->net_conf->want_lose = 0;
+	mutex_lock(&mdev->tconn->net_conf_update);
+	mdev->tconn->net_conf->want_lose = 0; /* without copy; single bit op is atomic */
+	mutex_unlock(&mdev->tconn->net_conf_update);
 
 	drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
 
@@ -4183,13 +4206,17 @@
 	spin_unlock_irq(&tconn->req_lock);
 
 	if (oc == C_DISCONNECTING) {
-		wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0);
+		struct net_conf *old_conf;
 
-		crypto_free_hash(tconn->cram_hmac_tfm);
-		tconn->cram_hmac_tfm = NULL;
+		mutex_lock(&tconn->net_conf_update);
+		old_conf = tconn->net_conf;
+		rcu_assign_pointer(tconn->net_conf, NULL);
+		conn_free_crypto(tconn);
+		mutex_unlock(&tconn->net_conf_update);
 
-		kfree(tconn->net_conf);
-		tconn->net_conf = NULL;
+		synchronize_rcu();
+		kfree(old_conf);
+
 		conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE);
 	}
 }
@@ -4568,12 +4595,8 @@
 		}
 	} while (h == 0);
 
-	if (h > 0) {
-		if (get_net_conf(tconn)) {
-			drbdd(tconn);
-			put_net_conf(tconn);
-		}
-	}
+	if (h > 0)
+		drbdd(tconn);
 
 	drbd_disconnect(tconn);