Skip to contentSkip to conversation

scsi: target: Fix unmap setup during configuration

PATCH Superseded Mike Christie 3 participants 5 messages Feb 7, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: target: Fix unmap setup during configuration

This issue was found and also debugged by Carl Lei <[email protected]>.

If the device is not enabled, iblock/file will have not setup their se_device to bdev/file mappings. If a user tries to config the unmap settings at this time, we will then crash trying to access a NULL pointer where the bdev/file should be.

This patch adds a check to make sure the device is configured before we try to call the configure_unmap callout.

Fixes 34bd1dcacf0d ("scsi: target: Detect UNMAP support post configuration")
---
 drivers/target/target_core_configfs.c | 46 +++++++++++++++++----------
 1 file changed, 30 insertions(+), 16 deletions(-)
drivers/target/target_core_configfs.c +30 -16
index a5f58988130a..d87f6eba4eda 100644
@@ -759,6 +759,27 @@ static ssize_t emulate_tas_store(struct config_item *item,
 	return count;
 }
 
+static int target_try_configure_unmap(struct se_device *dev,
+				      const char *config_opt)
+{
+	if (!dev->transport->configure_unmap)
+		return 0;
+
+	if (!target_dev_configured(dev)) {
+		pr_err("Generic Block Discard setup for %s requires device to be configured\n",
+		       config_opt);
+		return -ENODEV;
+	}
+
+	if (!dev->transport->configure_unmap(dev)) {
+		pr_err("Generic Block Discard setup for %s failed\n",
+		       config_opt);
+		return -ENOSYS;
+	}
+
+	return 0;
+}
+
 static ssize_t emulate_tpu_store(struct config_item *item,
 		const char *page, size_t count)
 {
@@ -776,11 +797,9 @@ static ssize_t emulate_tpu_store(struct config_item *item,
 	 * Discard supported is detected iblock_create_virtdevice().
 	 */
 	if (flag && !da->max_unmap_block_desc_count) {
-		if (!dev->transport->configure_unmap ||
-		    !dev->transport->configure_unmap(dev)) {
-			pr_err("Generic Block Discard not supported\n");
-			return -ENOSYS;
-		}
+		ret = target_try_configure_unmap(dev, "emulate_tpu");
+		if (ret)
+			return ret;
 	}
 
 	da->emulate_tpu = flag;
@@ -806,11 +825,9 @@ static ssize_t emulate_tpws_store(struct config_item *item,
 	 * Discard supported is detected iblock_create_virtdevice().
 	 */
 	if (flag && !da->max_unmap_block_desc_count) {
-		if (!dev->transport->configure_unmap ||
-		    !dev->transport->configure_unmap(dev)) {
-			pr_err("Generic Block Discard not supported\n");
-			return -ENOSYS;
-		}
+		ret = target_try_configure_unmap(dev, "emulate_tpws");
+		if (ret)
+			return ret;
 	}
 
 	da->emulate_tpws = flag;
@@ -1022,12 +1039,9 @@ static ssize_t unmap_zeroes_data_store(struct config_item *item,
 	 * Discard supported is detected iblock_configure_device().
 	 */
 	if (flag && !da->max_unmap_block_desc_count) {
-		if (!dev->transport->configure_unmap ||
-		    !dev->transport->configure_unmap(dev)) {
-			pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set because max_unmap_block_desc_count is zero\n",
-			       da->da_dev);
-			return -ENOSYS;
-		}
+		ret = target_try_configure_unmap(dev, "unmap_zeroes_data");
+		if (ret)
+			return ret;
 	}
 	da->unmap_zeroes_data = flag;
 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
Signature · 1 line
2.34.1
#1 lore raw

Hi Mike,

Quoted 25 lines · Mike Christie
drivers/target/target_core_configfs.c +21 -0
+static int target_try_configure_unmap(struct se_device *dev,
+                                     const char *config_opt)
+{
+       if (!dev->transport->configure_unmap)
+               return 0;
+
+       if (!target_dev_configured(dev)) {
+               pr_err("Generic Block Discard setup for %s requires device to be configured\n",
+                      config_opt);
+               return -ENODEV;
+       }
+
+       if (!dev->transport->configure_unmap(dev)) {
+               pr_err("Generic Block Discard setup for %s failed\n",
+                      config_opt);
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
 static ssize_t emulate_tpu_store(struct config_item *item,
                const char *page, size_t count)
 {

drivers/target/target_core_configfs.c static ssize_t emulate_tpu_store(struct config_item *item, · line 779

Quoted 4 lines
drivers/target/target_core_configfs.c +0 -1
@@ -776,11 +797,9 @@ static ssize_t emulate_tpu_store(struct config_item *item,
         * Discard supported is detected iblock_create_virtdevice().
         */
        if (flag && !da->max_unmap_block_desc_count) {
-               if (!dev->transport->configure_unmap ||

You removed this check but that callout is not mandatory and exists just in 2 backstore modules.

BR,
Dmitry

#2 lore raw
Quoted 9 lines · Dmitry Bogdanov

Hi Mike,

drivers/target/target_core_configfs.c static ssize_t emulate_tas_store(struct config_item *item, · line 766

Quoted 5 lines · Mike Christie
drivers/target/target_core_configfs.c +5 -0
+static int target_try_configure_unmap(struct se_device *dev,
+                                     const char *config_opt)
+{
+       if (!dev->transport->configure_unmap)
+               return 0;

Oh, sorry, here it is :)

Quoted 27 lines
Quoted 20 lines
drivers/target/target_core_configfs.c +16 -0
+
+       if (!target_dev_configured(dev)) {
+               pr_err("Generic Block Discard setup for %s requires device to be configured\n",
+                      config_opt);
+               return -ENODEV;
+       }
+
+       if (!dev->transport->configure_unmap(dev)) {
+               pr_err("Generic Block Discard setup for %s failed\n",
+                      config_opt);
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
 static ssize_t emulate_tpu_store(struct config_item *item,
                const char *page, size_t count)
 {

drivers/target/target_core_configfs.c static ssize_t emulate_tpu_store(struct config_item *item, · line 779

Quoted 4 lines
drivers/target/target_core_configfs.c +0 -1
@@ -776,11 +797,9 @@ static ssize_t emulate_tpu_store(struct config_item *item,
         * Discard supported is detected iblock_create_virtdevice().
         */
        if (flag && !da->max_unmap_block_desc_count) {
-               if (!dev->transport->configure_unmap ||

You removed this check but that callout is not mandatory and exists just in 2 backstore modules.

#3 lore raw
In reply to Mike Christie

st 7. 2. 2024 v 3:19 odesílatel Mike Christie
<[email protected]> napsal:

drivers/target/target_core_configfs.c static ssize_t emulate_tas_store(struct config_item *item, · line 766

Quoted 6 lines
drivers/target/target_core_configfs.c +5 -0
+static int target_try_configure_unmap(struct se_device *dev,
+                                     const char *config_opt)
+{
+       if (!dev->transport->configure_unmap)
+               return 0;

With this patch, if the configure_unmap callback is NULL then we return 0, implying that discard is supported.

Before, a NULL configure_unmap callback triggered an error:

        if (flag && !da->max_unmap_block_desc_count) {
                if (!dev->transport->configure_unmap ||   <<------
                    !dev->transport->configure_unmap(dev)) {
                        pr_err("Generic Block Discard not supported\n");
                        return -ENOSYS;
                }
        }

Shouldn't you return -ENOSYS in target_try_configure_unmap() if configure_unmap is NULL?

Maurizio

#4 lore raw
Quoted 22 lines · Maurizio Lombardi

st 7. 2. 2024 v 3:19 odesílatel Mike Christie
<[email protected]> napsal:

drivers/target/target_core_configfs.c static ssize_t emulate_tas_store(struct config_item *item, · line 766

Quoted 6 lines
drivers/target/target_core_configfs.c +5 -0
+static int target_try_configure_unmap(struct se_device *dev,
+                                     const char *config_opt)
+{
+       if (!dev->transport->configure_unmap)
+               return 0;

With this patch, if the configure_unmap callback is NULL then we return 0, implying that discard is supported.

Before, a NULL configure_unmap callback triggered an error:

        if (flag && !da->max_unmap_block_desc_count) {
                if (!dev->transport->configure_unmap ||   <<------
                    !dev->transport->configure_unmap(dev)) {
                        pr_err("Generic Block Discard not supported\n");
                        return -ENOSYS;
                }
        }
Quoted 3 lines

Shouldn't you return -ENOSYS in target_try_configure_unmap() if configure_unmap is NULL?

You are right. Will fix.