Skip to contentSkip to conversation

Add XEN pvSCSI support

PATCH Superseded [email protected] 3 participants 41 messages Aug 8, 2014 – Aug 18, 2014

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v4 1/4 Add XEN pvSCSI protocol description

In reply to [email protected]

From: Juergen Gross <[email protected]>

Add the definition of pvSCSI protocol used between the pvSCSI frontend in a XEN domU and the pvSCSI backend in a XEN driver domain (usually Dom0).

This header was originally provided by Fujitsu for XEN based on Linux 2.6.18. Changes are:
- added comment
- adapt to Linux style guide
- add support for larger SG-lists by putting them in an own granted page
- remove stale definitions

---
 include/xen/interface/io/vscsiif.h | 214 +++++++++++++++++++++++++++++++++++++
 1 file changed, 214 insertions(+)
 create mode 100644 include/xen/interface/io/vscsiif.h
include/xen/interface/io/vscsiif.h new file +214 -0
new file mode 100644
index 0000000..4291889
@@ -0,0 +1,214 @@
+/******************************************************************************
+ * vscsiif.h
+ *
+ * Based on the blkif.h code.
+ *
+ * This interface is to be regarded as a stable API between XEN domains
+ * running potentially different Linux kernel versions.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright(c) FUJITSU Limited 2008.
+ */
+
+#ifndef __XEN__PUBLIC_IO_SCSI_H__
+#define __XEN__PUBLIC_IO_SCSI_H__
+
+#include "ring.h"
+#include "../grant_table.h"
+
+/*
+ * Front->back notifications: When enqueuing a new request, sending a
+ * notification can be made conditional on req_event (i.e., the generic
+ * hold-off mechanism provided by the ring macros). Backends must set
+ * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()).
+ *
+ * Back->front notifications: When enqueuing a new response, sending a
+ * notification can be made conditional on rsp_event (i.e., the generic
+ * hold-off mechanism provided by the ring macros). Frontends must set
+ * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()).
+ */
+
+/*
+ * Feature and Parameter Negotiation
+ * =================================
+ * The two halves of a Xen pvSCSI driver utilize nodes within the XenStore to
+ * communicate capabilities and to negotiate operating parameters.  This
+ * section enumerates these nodes which reside in the respective front and
+ * backend portions of the XenStore, following the XenBus convention.
+ *
+ * All data in the XenStore is stored as strings.  Nodes specifying numeric
+ * values are encoded in decimal.  Integer value ranges listed below are
+ * expressed as fixed sized integer types capable of storing the conversion
+ * of a properly formated node string, without loss of information.
+ *
+ * Any specified default value is in effect if the corresponding XenBus node
+ * is not present in the XenStore.
+ *
+ * XenStore nodes in sections marked "PRIVATE" are solely for use by the
+ * driver side whose XenBus tree contains them.
+ *
+ *****************************************************************************
+ *                            Backend XenBus Nodes
+ *****************************************************************************
+ *
+ *------------------ Backend Device Identification (PRIVATE) ------------------
+ *
+ * p-devname
+ *      Values:         string
+ *
+ *      A free string used to identify the physical device (e.g. a disk name).
+ *
+ * p-dev
+ *      Values:         string
+ *
+ *      A string specifying the backend device: either a 4-tuple "h:c:t:l"
+ *      (host, controller, target, lun, all integers), or a WWN (e.g.
+ *      "naa.60014054ac780582").
+ *
+ * v-dev
+ *      Values:         string
+ *
+ *      A string specifying the frontend device in form of a 4-tuple "h:c:t:l"
+ *      (host, controller, target, lun, all integers).
+ *
+ *--------------------------------- Features ---------------------------------
+ *
+ * feature-sg-grant
+ *      Values:         <uint16_t>
+ *      Default Value:  0
+ *
+ *      Specifies the maximum number of scatter/gather elements in grant pages
+ *      supported. If not set, the backend supports up to VSCSIIF_SG_TABLESIZE
+ *      SG elements specified directly in the request.
+ *
+ *****************************************************************************
+ *                            Frontend XenBus Nodes
+ *****************************************************************************
+ *
+ *----------------------- Request Transport Parameters -----------------------
+ *
+ * event-channel
+ *      Values:         <uint32_t>
+ *
+ *      The identifier of the Xen event channel used to signal activity
+ *      in the ring buffer.
+ *
+ * ring-ref
+ *      Values:         <uint32_t>
+ *
+ * protocol
+ *      Values:         string (XEN_IO_PROTO_ABI_*)
+ *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
+ *
+ *      The machine ABI rules governing the format of all ring request and
+ *      response structures.
+ */
+
+/* Requests from the frontend to the backend */
+
+/*
+ * Request a SCSI operation specified via a CDB in vscsiif_request.cmnd.
+ * The target is specified via channel, id and lun.
+ */
+#define VSCSIIF_ACT_SCSI_CDB		1
+
+/*
+ * Request abort of a running operation for the specified target given by
+ * channel, id, lun and the operation's rqid in ref_rqid.
+ */
+#define VSCSIIF_ACT_SCSI_ABORT		2
+
+/*
+ * Request a device reset of the specified target (channel and id).
+ */
+#define VSCSIIF_ACT_SCSI_RESET		3
+
+/*
+ * Preset scatter/gather elements for a following request. Deprecated.
+ * Keeping the define only to avoid usage of the value "4" for other actions.
+ */
+#define VSCSIIF_ACT_SCSI_SG_PRESET	4
+
+/*
+ * Maximum scatter/gather segments per request.
+ *
+ * Considering balance between allocating at least 16 "vscsiif_request"
+ * structures on one page (4096 bytes) and the number of scatter/gather
+ * elements needed, we decided to use 26 as a magic number.
+ *
+ * If "feature-sg-grant" is set, more scatter/gather elements can be specified
+ * by placing them in one or more (up to VSCSIIF_SG_TABLESIZE) granted pages.
+ * In this case the vscsiif_request seg elements don't contain references to
+ * the user data, but to the SG elements referencing the user data.
+ */
+#define VSCSIIF_SG_TABLESIZE		26
+
+/*
+ * based on Linux kernel 2.6.18, still valid
+ * Changing these values requires support of multiple protocols via the rings
+ * as "old clients" will blindly use these values and the resulting structure
+ * sizes.
+ */
+#define VSCSIIF_MAX_COMMAND_SIZE	16
+#define VSCSIIF_SENSE_BUFFERSIZE	96
+
+struct scsiif_request_segment {
+	grant_ref_t gref;
+	uint16_t offset;
+	uint16_t length;
+};
+
+/* Size of one request is 252 bytes */
+struct vscsiif_request {
+	uint16_t rqid;		/* private guest value, echoed in resp  */
+	uint8_t act;		/* command between backend and frontend */
+	uint8_t cmd_len;	/* valid CDB bytes */
+
+	uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];	/* the CDB */
+	uint16_t timeout_per_command;
+	uint16_t channel, id, lun;	/* (virtual) device specification */
+	uint16_t ref_rqid;		/* command abort reference */
+	uint8_t sc_data_direction;	/* for DMA_TO_DEVICE(1)
+					   DMA_FROM_DEVICE(2)
+					   DMA_NONE(3) requests */
+	uint8_t nr_segments;		/* Number of pieces of scatter-gather */
+#define VSCSIIF_SG_GRANT	0x80	/* flag: SG elements via grant page */
+					/* nr_segments counts grant pages with
+					   SG elements
+					   usable if "feature-sg-grant" set */
+
+	struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
+	uint32_t reserved[3];
+};
+
+struct vscsiif_response {
+	uint16_t rqid;		/* identifies request */
+	uint8_t padding;
+	uint8_t sense_len;
+	uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
+	int32_t rslt;
+	uint32_t residual_len;	/* request bufflen -
+				   return the value from physical device */
+	uint32_t reserved[36];
+};
+
+DEFINE_RING_TYPES(vscsiif, struct vscsiif_request, struct vscsiif_response);
+
+#endif /*__XEN__PUBLIC_IO_SCSI_H__*/
Signature · 1 line
1.8.4.5
author maintainer
#3 lore raw

PATCH v4 2/4 Introduce xen-scsifront module

In reply to [email protected]

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI frontend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- move module to appropriate location in kernel tree
- adapt to Linux style guide
- some minor code simplifications
- replace constants with defines
- remove not used defines
- add support for larger SG lists by putting them in a granted page

Xen related parts

---
 drivers/scsi/Kconfig         |    9 +
 drivers/scsi/Makefile        |    1 +
 drivers/scsi/xen-scsifront.c | 1011 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1021 insertions(+)
 create mode 100644 drivers/scsi/xen-scsifront.c
drivers/scsi/Kconfig +9 -0
index baca589..e860c16 100644
@@ -611,6 +611,15 @@ config VMWARE_PVSCSI
 	  To compile this driver as a module, choose M here: the
 	  module will be called vmw_pvscsi.
 
+config XEN_SCSI_FRONTEND
+	tristate "XEN SCSI frontend driver"
+	depends on SCSI && XEN
+	help
+	  The XEN SCSI frontend driver allows the kernel to access SCSI Devices
+	  within another guest OS (usually Dom0).
+	  Only needed if the kernel is running in a XEN guest and generic
+	  SCSI access to a device is needed.
+
 config HYPERV_STORAGE
 	tristate "Microsoft Hyper-V virtual storage driver"
 	depends on SCSI && HYPERV
drivers/scsi/Makefile +1 -0
index e172d4f..a4ee9c5 100644
@@ -144,6 +144,7 @@ obj-$(CONFIG_SCSI_ESAS2R)	+= esas2r/
 obj-$(CONFIG_SCSI_PMCRAID)	+= pmcraid.o
 obj-$(CONFIG_SCSI_VIRTIO)	+= virtio_scsi.o
 obj-$(CONFIG_VMWARE_PVSCSI)	+= vmw_pvscsi.o
+obj-$(CONFIG_XEN_SCSI_FRONTEND)	+= xen-scsifront.o
 obj-$(CONFIG_HYPERV_STORAGE)	+= hv_storvsc.o
 
 obj-$(CONFIG_ARM)		+= arm/
drivers/scsi/xen-scsifront.c new file +1011 -0

1,011 lines not on this page · raw message

Signature · 1 line
1.8.4.5
author maintainer
#12 lore raw

PATCH v4 2/4 Introduce xen-scsifront module

In reply to [email protected]

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI frontend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- move module to appropriate location in kernel tree
- adapt to Linux style guide
- some minor code simplifications
- replace constants with defines
- remove not used defines
- add support for larger SG lists by putting them in a granted page

Xen related parts

---
 drivers/scsi/Kconfig         |    9 +
 drivers/scsi/Makefile        |    1 +
 drivers/scsi/xen-scsifront.c | 1011 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1021 insertions(+)
 create mode 100644 drivers/scsi/xen-scsifront.c
drivers/scsi/Kconfig +9 -0
index baca589..e860c16 100644
@@ -611,6 +611,15 @@ config VMWARE_PVSCSI
 	  To compile this driver as a module, choose M here: the
 	  module will be called vmw_pvscsi.
 
+config XEN_SCSI_FRONTEND
+	tristate "XEN SCSI frontend driver"
+	depends on SCSI && XEN
+	help
+	  The XEN SCSI frontend driver allows the kernel to access SCSI Devices
+	  within another guest OS (usually Dom0).
+	  Only needed if the kernel is running in a XEN guest and generic
+	  SCSI access to a device is needed.
+
 config HYPERV_STORAGE
 	tristate "Microsoft Hyper-V virtual storage driver"
 	depends on SCSI && HYPERV
drivers/scsi/Makefile +1 -0
index e172d4f..a4ee9c5 100644
@@ -144,6 +144,7 @@ obj-$(CONFIG_SCSI_ESAS2R)	+= esas2r/
 obj-$(CONFIG_SCSI_PMCRAID)	+= pmcraid.o
 obj-$(CONFIG_SCSI_VIRTIO)	+= virtio_scsi.o
 obj-$(CONFIG_VMWARE_PVSCSI)	+= vmw_pvscsi.o
+obj-$(CONFIG_XEN_SCSI_FRONTEND)	+= xen-scsifront.o
 obj-$(CONFIG_HYPERV_STORAGE)	+= hv_storvsc.o
 
 obj-$(CONFIG_ARM)		+= arm/
drivers/scsi/xen-scsifront.c new file +1011 -0

1,011 lines not on this page · raw message

Signature · 1 line
1.8.4.5
author maintainer
#13 lore raw

PATCH v4 3/4 Introduce XEN scsiback module

In reply to [email protected]

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI backend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- adapt to Linux style guide
- use target core infrastructure instead doing pure pass-through
- enable module unloading
- support SG-list in grant page(s)
- support task abort
- remove redundant struct backend
- allocate resources dynamically
- correct minor error in scsiback_fast_flush_area
- free allocated resources in case of error during I/O preparation
- remove CDB emulation, now handled by target core infrastructure

Xen related parts

---
 drivers/xen/Kconfig        |    9 +
 drivers/xen/Makefile       |    1 +
 drivers/xen/xen-scsiback.c | 2282 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 2292 insertions(+)
 create mode 100644 drivers/xen/xen-scsiback.c
drivers/xen/Kconfig +9 -0
index 38fb36e..42fb963 100644
@@ -172,6 +172,15 @@ config XEN_PCIDEV_BACKEND
 
 	  If in doubt, say m.
 
+config XEN_SCSI_BACKEND
+	tristate "XEN SCSI backend driver"
+	depends on XEN && XEN_BACKEND && TARGET_CORE
+	help
+	  The SCSI backend driver allows the kernel to export its SCSI Devices
+	  to other guests via a high-performance shared-memory interface.
+	  Only needed for systems running as XEN driver domains (e.g. Dom0) and
+	  if guests need generic access to SCSI devices.
+
 config XEN_PRIVCMD
 	tristate
 	depends on XEN
drivers/xen/Makefile +1 -0
index 45e00af..b42ee75 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_XEN_STUB)			+= xen-stub.o
 obj-$(CONFIG_XEN_ACPI_HOTPLUG_MEMORY)	+= xen-acpi-memhotplug.o
 obj-$(CONFIG_XEN_ACPI_HOTPLUG_CPU)	+= xen-acpi-cpuhotplug.o
 obj-$(CONFIG_XEN_ACPI_PROCESSOR)	+= xen-acpi-processor.o
+obj-$(CONFIG_XEN_SCSI_BACKEND)		+= xen-scsiback.o
 xen-evtchn-y				:= evtchn.o
 xen-gntdev-y				:= gntdev.o
 xen-gntalloc-y				:= gntalloc.o
drivers/xen/xen-scsiback.c new file +2282 -0

2,282 lines not on this page · raw message

Signature · 1 line
1.8.4.5
author maintainer
#39 lore raw

PATCH v4 4/4 add xen pvscsi maintainer

In reply to [email protected]

From: Juergen Gross <[email protected]>

Add myself as maintainer for the Xen pvSCSI stuff.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index c2066f4..d020bfd 100644
@@ -10046,6 +10046,14 @@ S:	Supported
 F:	arch/x86/pci/*xen*
 F:	drivers/pci/*xen*
 
+XEN PVSCSI DRIVERS
+M:	Juergen Gross <[email protected]>
+L:	[email protected] (moderated for non-subscribers)
+S:	Supported
+F:	drivers/scsi/xen-scsifront.c
+F:	drivers/xen/xen-scsiback.c
+F:	include/xen/interface/io/vscsiif.h
+
 XEN SWIOTLB SUBSYSTEM
 M:	Konrad Rzeszutek Wilk <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
Signature · 1 line
1.8.4.5
author maintainer
#40 lore raw

PATCH v4 4/4 add xen pvscsi maintainer

In reply to [email protected]

From: Juergen Gross <[email protected]>

Add myself as maintainer for the Xen pvSCSI stuff.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index c2066f4..d020bfd 100644
@@ -10046,6 +10046,14 @@ S:	Supported
 F:	arch/x86/pci/*xen*
 F:	drivers/pci/*xen*
 
+XEN PVSCSI DRIVERS
+M:	Juergen Gross <[email protected]>
+L:	[email protected] (moderated for non-subscribers)
+S:	Supported
+F:	drivers/scsi/xen-scsifront.c
+F:	drivers/xen/xen-scsiback.c
+F:	include/xen/interface/io/vscsiif.h
+
 XEN SWIOTLB SUBSYSTEM
 M:	Konrad Rzeszutek Wilk <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
Signature · 1 line
1.8.4.5

[email protected]

author maintainer
#0 lore raw

This series adds XEN pvSCSI support. With pvSCSI it is possible to use physical SCSI devices from a XEN domain.

The support consists of a backend in the privileged Domain-0 doing the real I/O and a frontend in the unprivileged domU passing I/O-requests to the backend.

The code is taken (and adapted) from the original pvSCSI implementation done for Linux 2.6 in 2008 by Fujitsu.

[PATCH V2 1/4] Add XEN pvSCSI protocol description [PATCH V2 2/4] Introduce xen-scsifront module [PATCH V2 3/4] Introduce XEN scsiback module [PATCH V2 4/4] add xen pvscsi maintainer

Changes in V4:
- Re-add define for VSCSIIF_ACT_SCSI_SG_PRESET to vscsiif.h to indicate this action value should not be used in future enhancements

Changes in V3:
- added some comments to the protocol header file
- removed the CDB emulation from xen-scsiback, handled by core target infrastructure
- several changes in xen-scsifront after comments from Christoph Hellwig

Changes in V2:
- use core target infrastructure by backend instead of pure SCSI passthrough
- add support for larger SG lists by putting them in grant page(s)
- add command abort capability

author maintainer
#2 lore raw

PATCH v4 1/4 Add XEN pvSCSI protocol description

From: Juergen Gross <[email protected]>

Add the definition of pvSCSI protocol used between the pvSCSI frontend in a XEN domU and the pvSCSI backend in a XEN driver domain (usually Dom0).

This header was originally provided by Fujitsu for XEN based on Linux 2.6.18. Changes are:
- added comment
- adapt to Linux style guide
- add support for larger SG-lists by putting them in an own granted page
- remove stale definitions

---
 include/xen/interface/io/vscsiif.h | 214 +++++++++++++++++++++++++++++++++++++
 1 file changed, 214 insertions(+)
 create mode 100644 include/xen/interface/io/vscsiif.h
include/xen/interface/io/vscsiif.h new file +214 -0
new file mode 100644
index 0000000..4291889
@@ -0,0 +1,214 @@
+/******************************************************************************
+ * vscsiif.h
+ *
+ * Based on the blkif.h code.
+ *
+ * This interface is to be regarded as a stable API between XEN domains
+ * running potentially different Linux kernel versions.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright(c) FUJITSU Limited 2008.
+ */
+
+#ifndef __XEN__PUBLIC_IO_SCSI_H__
+#define __XEN__PUBLIC_IO_SCSI_H__
+
+#include "ring.h"
+#include "../grant_table.h"
+
+/*
+ * Front->back notifications: When enqueuing a new request, sending a
+ * notification can be made conditional on req_event (i.e., the generic
+ * hold-off mechanism provided by the ring macros). Backends must set
+ * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()).
+ *
+ * Back->front notifications: When enqueuing a new response, sending a
+ * notification can be made conditional on rsp_event (i.e., the generic
+ * hold-off mechanism provided by the ring macros). Frontends must set
+ * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()).
+ */
+
+/*
+ * Feature and Parameter Negotiation
+ * =================================
+ * The two halves of a Xen pvSCSI driver utilize nodes within the XenStore to
+ * communicate capabilities and to negotiate operating parameters.  This
+ * section enumerates these nodes which reside in the respective front and
+ * backend portions of the XenStore, following the XenBus convention.
+ *
+ * All data in the XenStore is stored as strings.  Nodes specifying numeric
+ * values are encoded in decimal.  Integer value ranges listed below are
+ * expressed as fixed sized integer types capable of storing the conversion
+ * of a properly formated node string, without loss of information.
+ *
+ * Any specified default value is in effect if the corresponding XenBus node
+ * is not present in the XenStore.
+ *
+ * XenStore nodes in sections marked "PRIVATE" are solely for use by the
+ * driver side whose XenBus tree contains them.
+ *
+ *****************************************************************************
+ *                            Backend XenBus Nodes
+ *****************************************************************************
+ *
+ *------------------ Backend Device Identification (PRIVATE) ------------------
+ *
+ * p-devname
+ *      Values:         string
+ *
+ *      A free string used to identify the physical device (e.g. a disk name).
+ *
+ * p-dev
+ *      Values:         string
+ *
+ *      A string specifying the backend device: either a 4-tuple "h:c:t:l"
+ *      (host, controller, target, lun, all integers), or a WWN (e.g.
+ *      "naa.60014054ac780582").
+ *
+ * v-dev
+ *      Values:         string
+ *
+ *      A string specifying the frontend device in form of a 4-tuple "h:c:t:l"
+ *      (host, controller, target, lun, all integers).
+ *
+ *--------------------------------- Features ---------------------------------
+ *
+ * feature-sg-grant
+ *      Values:         <uint16_t>
+ *      Default Value:  0
+ *
+ *      Specifies the maximum number of scatter/gather elements in grant pages
+ *      supported. If not set, the backend supports up to VSCSIIF_SG_TABLESIZE
+ *      SG elements specified directly in the request.
+ *
+ *****************************************************************************
+ *                            Frontend XenBus Nodes
+ *****************************************************************************
+ *
+ *----------------------- Request Transport Parameters -----------------------
+ *
+ * event-channel
+ *      Values:         <uint32_t>
+ *
+ *      The identifier of the Xen event channel used to signal activity
+ *      in the ring buffer.
+ *
+ * ring-ref
+ *      Values:         <uint32_t>
+ *
+ * protocol
+ *      Values:         string (XEN_IO_PROTO_ABI_*)
+ *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
+ *
+ *      The machine ABI rules governing the format of all ring request and
+ *      response structures.
+ */
+
+/* Requests from the frontend to the backend */
+
+/*
+ * Request a SCSI operation specified via a CDB in vscsiif_request.cmnd.
+ * The target is specified via channel, id and lun.
+ */
+#define VSCSIIF_ACT_SCSI_CDB		1
+
+/*
+ * Request abort of a running operation for the specified target given by
+ * channel, id, lun and the operation's rqid in ref_rqid.
+ */
+#define VSCSIIF_ACT_SCSI_ABORT		2
+
+/*
+ * Request a device reset of the specified target (channel and id).
+ */
+#define VSCSIIF_ACT_SCSI_RESET		3
+
+/*
+ * Preset scatter/gather elements for a following request. Deprecated.
+ * Keeping the define only to avoid usage of the value "4" for other actions.
+ */
+#define VSCSIIF_ACT_SCSI_SG_PRESET	4
+
+/*
+ * Maximum scatter/gather segments per request.
+ *
+ * Considering balance between allocating at least 16 "vscsiif_request"
+ * structures on one page (4096 bytes) and the number of scatter/gather
+ * elements needed, we decided to use 26 as a magic number.
+ *
+ * If "feature-sg-grant" is set, more scatter/gather elements can be specified
+ * by placing them in one or more (up to VSCSIIF_SG_TABLESIZE) granted pages.
+ * In this case the vscsiif_request seg elements don't contain references to
+ * the user data, but to the SG elements referencing the user data.
+ */
+#define VSCSIIF_SG_TABLESIZE		26
+
+/*
+ * based on Linux kernel 2.6.18, still valid
+ * Changing these values requires support of multiple protocols via the rings
+ * as "old clients" will blindly use these values and the resulting structure
+ * sizes.
+ */
+#define VSCSIIF_MAX_COMMAND_SIZE	16
+#define VSCSIIF_SENSE_BUFFERSIZE	96
+
+struct scsiif_request_segment {
+	grant_ref_t gref;
+	uint16_t offset;
+	uint16_t length;
+};
+
+/* Size of one request is 252 bytes */
+struct vscsiif_request {
+	uint16_t rqid;		/* private guest value, echoed in resp  */
+	uint8_t act;		/* command between backend and frontend */
+	uint8_t cmd_len;	/* valid CDB bytes */
+
+	uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];	/* the CDB */
+	uint16_t timeout_per_command;
+	uint16_t channel, id, lun;	/* (virtual) device specification */
+	uint16_t ref_rqid;		/* command abort reference */
+	uint8_t sc_data_direction;	/* for DMA_TO_DEVICE(1)
+					   DMA_FROM_DEVICE(2)
+					   DMA_NONE(3) requests */
+	uint8_t nr_segments;		/* Number of pieces of scatter-gather */
+#define VSCSIIF_SG_GRANT	0x80	/* flag: SG elements via grant page */
+					/* nr_segments counts grant pages with
+					   SG elements
+					   usable if "feature-sg-grant" set */
+
+	struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
+	uint32_t reserved[3];
+};
+
+struct vscsiif_response {
+	uint16_t rqid;		/* identifies request */
+	uint8_t padding;
+	uint8_t sense_len;
+	uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
+	int32_t rslt;
+	uint32_t residual_len;	/* request bufflen -
+				   return the value from physical device */
+	uint32_t reserved[36];
+};
+
+DEFINE_RING_TYPES(vscsiif, struct vscsiif_request, struct vscsiif_response);
+
+#endif /*__XEN__PUBLIC_IO_SCSI_H__*/
Signature · 1 line
1.8.4.5
author maintainer
#14 lore raw

PATCH v4 3/4 Introduce XEN scsiback module

In reply to [email protected]

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI backend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- adapt to Linux style guide
- use target core infrastructure instead doing pure pass-through
- enable module unloading
- support SG-list in grant page(s)
- support task abort
- remove redundant struct backend
- allocate resources dynamically
- correct minor error in scsiback_fast_flush_area
- free allocated resources in case of error during I/O preparation
- remove CDB emulation, now handled by target core infrastructure

Xen related parts

---
 drivers/xen/Kconfig        |    9 +
 drivers/xen/Makefile       |    1 +
 drivers/xen/xen-scsiback.c | 2282 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 2292 insertions(+)
 create mode 100644 drivers/xen/xen-scsiback.c
drivers/xen/Kconfig +9 -0
index 38fb36e..42fb963 100644
@@ -172,6 +172,15 @@ config XEN_PCIDEV_BACKEND
 
 	  If in doubt, say m.
 
+config XEN_SCSI_BACKEND
+	tristate "XEN SCSI backend driver"
+	depends on XEN && XEN_BACKEND && TARGET_CORE
+	help
+	  The SCSI backend driver allows the kernel to export its SCSI Devices
+	  to other guests via a high-performance shared-memory interface.
+	  Only needed for systems running as XEN driver domains (e.g. Dom0) and
+	  if guests need generic access to SCSI devices.
+
 config XEN_PRIVCMD
 	tristate
 	depends on XEN
drivers/xen/Makefile +1 -0
index 45e00af..b42ee75 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_XEN_STUB)			+= xen-stub.o
 obj-$(CONFIG_XEN_ACPI_HOTPLUG_MEMORY)	+= xen-acpi-memhotplug.o
 obj-$(CONFIG_XEN_ACPI_HOTPLUG_CPU)	+= xen-acpi-cpuhotplug.o
 obj-$(CONFIG_XEN_ACPI_PROCESSOR)	+= xen-acpi-processor.o
+obj-$(CONFIG_XEN_SCSI_BACKEND)		+= xen-scsiback.o
 xen-evtchn-y				:= evtchn.o
 xen-gntdev-y				:= gntdev.o
 xen-gntalloc-y				:= gntalloc.o
drivers/xen/xen-scsiback.c new file +2282 -0

2,282 lines not on this page · raw message

Signature · 1 line
1.8.4.5
#4 lore raw

Re: [PATCH V4 2/4] Introduce xen-scsifront module

In reply to [email protected]

drivers/scsi/xen-scsifront.c line 463

Quoted 3 lines
drivers/scsi/xen-scsifront.c new file +3 -0
+	BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
+
+	if (sc->cmd_len)

I can't see how you can get a zero cmd_len here.

Quoted 1 line

+static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)

Please add a comment explaining your unusual EH strategy here.

Quoted 13 lines
drivers/scsi/xen-scsifront.c new file +13 -0
+static void scsifront_free(struct vscsifrnt_info *info)
+{
+	if (info->host && info->host_active) {
+		/* Scsi_host not yet removed */
+		scsi_remove_host(info->host);
+		info->host_active = 0;
+	}
+
+	if (info->ring_ref != GRANT_INVALID_REF) {
+		gnttab_end_foreign_access(info->ring_ref, 0,
+					  (unsigned long)info->ring.sring);
+		info->ring_ref = GRANT_INVALID_REF;
+		info->ring.sring = NULL;

drivers/scsi/xen-scsifront.c line 641

Quoted 10 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+	}
+
+	if (info->irq)
+		unbind_from_irqhandler(info->irq, info);
+	info->irq = 0;
+	info->evtchn = 0;
+
+	if (info->host)
+		scsi_host_put(info->host);
+}

I don't think most of the ifs should be here, just use proper symmetric goto unwinding in the initialization error path instead.

The way this function can be called from different levels of the callstack on init failure is very confusing.

drivers/scsi/xen-scsifront.c line 882

Quoted 10 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+		switch (op) {
+		case VSCSIFRONT_OP_ADD_LUN:
+			if (device_state == XenbusStateInitialised) {
+				sdev = __scsi_add_device(info->host, chn, tgt,
+							lun, NULL);
+				err = (IS_ERR(sdev) || !sdev->hostdata);
+				if (!IS_ERR(sdev)) {
+					sdev->hostdata = NULL;
+					scsi_device_put(sdev);
+				}

Given that you put the device immediatly you should be using scsi_add_device instead of __scsi_add_device. Also all the messing with ->hostdata from ->slave_alloc looks wrong. For one thing every setup done ->slave_alloc should be paired with teardown in ->slave_destroy. Second I don't see any need for that.

drivers/scsi/xen-scsifront.c line 894

Quoted 5 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

#11 lore raw

Re: [PATCH V4 2/4] Introduce xen-scsifront module

In reply to [email protected]

drivers/scsi/xen-scsifront.c line 463

Quoted 3 lines
drivers/scsi/xen-scsifront.c new file +3 -0
+	BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
+
+	if (sc->cmd_len)

I can't see how you can get a zero cmd_len here.

Quoted 1 line

+static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)

Please add a comment explaining your unusual EH strategy here.

Quoted 13 lines
drivers/scsi/xen-scsifront.c new file +13 -0
+static void scsifront_free(struct vscsifrnt_info *info)
+{
+	if (info->host && info->host_active) {
+		/* Scsi_host not yet removed */
+		scsi_remove_host(info->host);
+		info->host_active = 0;
+	}
+
+	if (info->ring_ref != GRANT_INVALID_REF) {
+		gnttab_end_foreign_access(info->ring_ref, 0,
+					  (unsigned long)info->ring.sring);
+		info->ring_ref = GRANT_INVALID_REF;
+		info->ring.sring = NULL;

drivers/scsi/xen-scsifront.c line 641

Quoted 10 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+	}
+
+	if (info->irq)
+		unbind_from_irqhandler(info->irq, info);
+	info->irq = 0;
+	info->evtchn = 0;
+
+	if (info->host)
+		scsi_host_put(info->host);
+}

I don't think most of the ifs should be here, just use proper symmetric goto unwinding in the initialization error path instead.

The way this function can be called from different levels of the callstack on init failure is very confusing.

drivers/scsi/xen-scsifront.c line 882

Quoted 10 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+		switch (op) {
+		case VSCSIFRONT_OP_ADD_LUN:
+			if (device_state == XenbusStateInitialised) {
+				sdev = __scsi_add_device(info->host, chn, tgt,
+							lun, NULL);
+				err = (IS_ERR(sdev) || !sdev->hostdata);
+				if (!IS_ERR(sdev)) {
+					sdev->hostdata = NULL;
+					scsi_device_put(sdev);
+				}

Given that you put the device immediatly you should be using scsi_add_device instead of __scsi_add_device. Also all the messing with ->hostdata from ->slave_alloc looks wrong. For one thing every setup done ->slave_alloc should be paired with teardown in ->slave_destroy. Second I don't see any need for that.

drivers/scsi/xen-scsifront.c line 894

Quoted 5 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

author maintainer
#5 lore raw

Re: [Xen-devel] [PATCH V4 2/4] Introduce xen-scsifront module

In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

drivers/scsi/xen-scsifront.c line 463

Quoted 3 lines
drivers/scsi/xen-scsifront.c new file +3 -0
+	BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
+
+	if (sc->cmd_len)

I can't see how you can get a zero cmd_len here.

Ahh, thanks for spotting this. In a previous version it could be zero in case of reset.

Quoted 3 lines
Quoted 1 line

+static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)

Please add a comment explaining your unusual EH strategy here.

What do you mean with "unusual"? You mean transferring the EH action to Dom0?

Quoted 28 lines
Quoted 14 lines
drivers/scsi/xen-scsifront.c new file +13 -0
+static void scsifront_free(struct vscsifrnt_info *info)
+{
+	if (info->host && info->host_active) {
+		/* Scsi_host not yet removed */
+		scsi_remove_host(info->host);
+		info->host_active = 0;
+	}
+
+	if (info->ring_ref != GRANT_INVALID_REF) {
+		gnttab_end_foreign_access(info->ring_ref, 0,
+					  (unsigned long)info->ring.sring);
+		info->ring_ref = GRANT_INVALID_REF;
+		info->ring.sring = NULL;

drivers/scsi/xen-scsifront.c line 641

Quoted 10 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+	}
+
+	if (info->irq)
+		unbind_from_irqhandler(info->irq, info);
+	info->irq = 0;
+	info->evtchn = 0;
+
+	if (info->host)
+		scsi_host_put(info->host);
+}

I don't think most of the ifs should be here, just use proper symmetric goto unwinding in the initialization error path instead.

Quoted 2 lines

The way this function can be called from different levels of the callstack on init failure is very confusing.

Okay, I'll look into making it easier to understand.

Quoted 17 lines

drivers/scsi/xen-scsifront.c line 882

Quoted 11 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+		switch (op) {
+		case VSCSIFRONT_OP_ADD_LUN:
+			if (device_state == XenbusStateInitialised) {
+				sdev = __scsi_add_device(info->host, chn, tgt,
+							lun, NULL);
+				err = (IS_ERR(sdev) || !sdev->hostdata);
+				if (!IS_ERR(sdev)) {
+					sdev->hostdata = NULL;
+					scsi_device_put(sdev);
+				}

Given that you put the device immediatly you should be using scsi_add_device instead of __scsi_add_device. Also all the messing with ->hostdata from ->slave_alloc looks wrong. For one thing every setup done ->slave_alloc should be paired with teardown in ->slave_destroy. Second I don't see any need for that.

The problem is I have to take different actions depending on the device being new or not.

Quoted 8 lines

drivers/scsi/xen-scsifront.c line 894

Quoted 6 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

This is calling for problems, I think. xenbus_printf() is not just a printing function, but it changes an entry in the xenstore. And this requires locking, switching threads, ...

I doubt doing this while holding SCSI-internal locks is a good idea.

Juergen

author maintainer
#10 lore raw

Re: [PATCH V4 2/4] Introduce xen-scsifront module

In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig

drivers/scsi/xen-scsifront.c line 463

Quoted 3 lines
drivers/scsi/xen-scsifront.c new file +3 -0
+	BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
+
+	if (sc->cmd_len)

I can't see how you can get a zero cmd_len here.

Ahh, thanks for spotting this. In a previous version it could be zero in case of reset.

Quoted 3 lines
Quoted 1 line

+static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)

Please add a comment explaining your unusual EH strategy here.

What do you mean with "unusual"? You mean transferring the EH action to Dom0?

Quoted 28 lines
Quoted 14 lines
drivers/scsi/xen-scsifront.c new file +13 -0
+static void scsifront_free(struct vscsifrnt_info *info)
+{
+	if (info->host && info->host_active) {
+		/* Scsi_host not yet removed */
+		scsi_remove_host(info->host);
+		info->host_active = 0;
+	}
+
+	if (info->ring_ref != GRANT_INVALID_REF) {
+		gnttab_end_foreign_access(info->ring_ref, 0,
+					  (unsigned long)info->ring.sring);
+		info->ring_ref = GRANT_INVALID_REF;
+		info->ring.sring = NULL;

drivers/scsi/xen-scsifront.c line 641

Quoted 10 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+	}
+
+	if (info->irq)
+		unbind_from_irqhandler(info->irq, info);
+	info->irq = 0;
+	info->evtchn = 0;
+
+	if (info->host)
+		scsi_host_put(info->host);
+}

I don't think most of the ifs should be here, just use proper symmetric goto unwinding in the initialization error path instead.

Quoted 2 lines

The way this function can be called from different levels of the callstack on init failure is very confusing.

Okay, I'll look into making it easier to understand.

Quoted 17 lines

drivers/scsi/xen-scsifront.c line 882

Quoted 11 lines
drivers/scsi/xen-scsifront.c new file +10 -0
+		switch (op) {
+		case VSCSIFRONT_OP_ADD_LUN:
+			if (device_state == XenbusStateInitialised) {
+				sdev = __scsi_add_device(info->host, chn, tgt,
+							lun, NULL);
+				err = (IS_ERR(sdev) || !sdev->hostdata);
+				if (!IS_ERR(sdev)) {
+					sdev->hostdata = NULL;
+					scsi_device_put(sdev);
+				}

Given that you put the device immediatly you should be using scsi_add_device instead of __scsi_add_device. Also all the messing with ->hostdata from ->slave_alloc looks wrong. For one thing every setup done ->slave_alloc should be paired with teardown in ->slave_destroy. Second I don't see any need for that.

The problem is I have to take different actions depending on the device being new or not.

Quoted 8 lines

drivers/scsi/xen-scsifront.c line 894

Quoted 6 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

This is calling for problems, I think. xenbus_printf() is not just a printing function, but it changes an entry in the xenstore. And this requires locking, switching threads, ...

I doubt doing this while holding SCSI-internal locks is a good idea.

Juergen

#6 lore raw

Re: [PATCH V4 2/4] Introduce xen-scsifront module

In reply to Juergen Gross
Quoted 2 lines · Juergen Gross

What do you mean with "unusual"? You mean transferring the EH action to Dom0?

Yes. Note that hyperv tries something similar and they've run into timeout issues, you might want to read up the recent thread on that.

Quoted 8 lines
Quoted 7 lines

drivers/scsi/xen-scsifront.c line 894

Quoted 5 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

Quoted 5 lines

This is calling for problems, I think. xenbus_printf() is not just a printing function, but it changes an entry in the xenstore. And this requires locking, switching threads, ...

I doubt doing this while holding SCSI-internal locks is a good idea.

Oh, I thought xenbus_printf was just a logging wrapper.

Doing major work in the slave_* callouts is not a problem, that's what they were designed for.

For the successful case the xenbus_printf should be done in ->slave_configure. For the failure case you probably want to do it from ->slave_destroy based on the absence of a flag set in ->slave_configure, e.g. in slave_configure:

	sdev->hostdata = (void *)1UL;

and in ->slave_destroy:

	if (!sdev->hostdata)
		...

although you might see something like this based on external scanning through procfs/sysfs as mentioned earlier, so please take a look at how all these corner cases could effect you.

#7 lore raw

Re: [Xen-devel] [PATCH V4 2/4] Introduce xen-scsifront module

In reply to Juergen Gross
Quoted 2 lines · Juergen Gross

What do you mean with "unusual"? You mean transferring the EH action to Dom0?

Yes. Note that hyperv tries something similar and they've run into timeout issues, you might want to read up the recent thread on that.

Quoted 8 lines
Quoted 7 lines

drivers/scsi/xen-scsifront.c line 894

Quoted 5 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

Quoted 5 lines

This is calling for problems, I think. xenbus_printf() is not just a printing function, but it changes an entry in the xenstore. And this requires locking, switching threads, ...

I doubt doing this while holding SCSI-internal locks is a good idea.

Oh, I thought xenbus_printf was just a logging wrapper.

Doing major work in the slave_* callouts is not a problem, that's what they were designed for.

For the successful case the xenbus_printf should be done in ->slave_configure. For the failure case you probably want to do it from ->slave_destroy based on the absence of a flag set in ->slave_configure, e.g. in slave_configure:

	sdev->hostdata = (void *)1UL;

and in ->slave_destroy:

	if (!sdev->hostdata)
		...

although you might see something like this based on external scanning through procfs/sysfs as mentioned earlier, so please take a look at how all these corner cases could effect you.

#15 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to [email protected]

drivers/xen/xen-scsiback.c line 53

Quoted 3 lines
drivers/xen/xen-scsiback.c new file +3 -0
+#include <scsi/scsi_dbg.h>
+#include <scsi/scsi_eh.h>
+#include <scsi/scsi_tcq.h>

What do you need these for? Normally target drivers shouldn't need these.

drivers/xen/xen-scsiback.c line 157

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+struct vscsibk_emulate {
+	void (*pre_function)(struct vscsibk_pend *, void *);
+	void (*post_function)(struct vscsibk_pend *, void *);
+};

This doesn't seem to be used.

drivers/xen/xen-scsiback.c line 212

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+#define scsiback_get(_b) (atomic_inc(&(_b)->nr_unreplied_reqs))
+#define scsiback_put(_b)				\
+	do {						\
+		if (atomic_dec_and_test(&(_b)->nr_unreplied_reqs))	\
+			wake_up(&(_b)->waiting_to_free);\
+	} while (0)

Normal Linux style would be to make these inline functions.

drivers/xen/xen-scsiback.c line 620

Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_notify_work(struct vscsibk_info *info)
+{
+	info->waiting_reqs = 1;
+	wake_up(&info->wq);
+}
+
+static irqreturn_t scsiback_intr(int irq, void *dev_id)
+{
+	scsiback_notify_work((struct vscsibk_info *)dev_id);
+	return IRQ_HANDLED;
+}

Seems like this driver should get the same threaded irq treatment as the initiator side?

Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_disconnect(struct vscsibk_info *info)
+{
+	if (info->kthread) {
+		kthread_stop(info->kthread);
+		info->kthread = NULL;
+		wake_up(&info->shutdown_wq);
+	}
+
+	wait_event(info->waiting_to_free,
+		atomic_read(&info->nr_unreplied_reqs) == 0);
+

drivers/xen/xen-scsiback.c line 678

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (info->irq) {
+		unbind_from_irqhandler(info->irq, info);
+		info->irq = 0;
+	}
+
+	if (info->ring.sring) {
+		xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
+		info->ring.sring = NULL;
+	}
+}

Also the same treatment for goto based init failure unwinding.

#16 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to [email protected]

drivers/xen/xen-scsiback.c line 53

Quoted 3 lines
drivers/xen/xen-scsiback.c new file +3 -0
+#include <scsi/scsi_dbg.h>
+#include <scsi/scsi_eh.h>
+#include <scsi/scsi_tcq.h>

What do you need these for? Normally target drivers shouldn't need these.

drivers/xen/xen-scsiback.c line 157

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+struct vscsibk_emulate {
+	void (*pre_function)(struct vscsibk_pend *, void *);
+	void (*post_function)(struct vscsibk_pend *, void *);
+};

This doesn't seem to be used.

drivers/xen/xen-scsiback.c line 212

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+#define scsiback_get(_b) (atomic_inc(&(_b)->nr_unreplied_reqs))
+#define scsiback_put(_b)				\
+	do {						\
+		if (atomic_dec_and_test(&(_b)->nr_unreplied_reqs))	\
+			wake_up(&(_b)->waiting_to_free);\
+	} while (0)

Normal Linux style would be to make these inline functions.

drivers/xen/xen-scsiback.c line 620

Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_notify_work(struct vscsibk_info *info)
+{
+	info->waiting_reqs = 1;
+	wake_up(&info->wq);
+}
+
+static irqreturn_t scsiback_intr(int irq, void *dev_id)
+{
+	scsiback_notify_work((struct vscsibk_info *)dev_id);
+	return IRQ_HANDLED;
+}

Seems like this driver should get the same threaded irq treatment as the initiator side?

Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_disconnect(struct vscsibk_info *info)
+{
+	if (info->kthread) {
+		kthread_stop(info->kthread);
+		info->kthread = NULL;
+		wake_up(&info->shutdown_wq);
+	}
+
+	wait_event(info->waiting_to_free,
+		atomic_read(&info->nr_unreplied_reqs) == 0);
+

drivers/xen/xen-scsiback.c line 678

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (info->irq) {
+		unbind_from_irqhandler(info->irq, info);
+		info->irq = 0;
+	}
+
+	if (info->ring.sring) {
+		xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
+		info->ring.sring = NULL;
+	}
+}

Also the same treatment for goto based init failure unwinding.

author maintainer
#8 lore raw

Re: [PATCH V4 2/4] Introduce xen-scsifront module

In reply to Christoph Hellwig
Quoted 19 lines · Christoph Hellwig
Quoted 2 lines · Juergen Gross

What do you mean with "unusual"? You mean transferring the EH action to Dom0?

Yes. Note that hyperv tries something similar and they've run into timeout issues, you might want to read up the recent thread on that.

Quoted 12 lines
Quoted 7 lines

drivers/scsi/xen-scsifront.c line 894

Quoted 5 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

This is calling for problems, I think. xenbus_printf() is not just a printing function, but it changes an entry in the xenstore. And this requires locking, switching threads, ...

Quoted 6 lines
Quoted 1 line

I doubt doing this while holding SCSI-internal locks is a good idea.

Oh, I thought xenbus_printf was just a logging wrapper.

Doing major work in the slave_* callouts is not a problem, that's what they were designed for.

Okay.

Quoted 11 lines

For the successful case the xenbus_printf should be done in ->slave_configure. For the failure case you probably want to do it from ->slave_destroy based on the absence of a flag set in ->slave_configure, e.g. in slave_configure:

	sdev->hostdata = (void *)1UL;

and in ->slave_destroy:

	if (!sdev->hostdata)

I don't think I'll need the flag. The action is the same if the device is being destroyed again because of already existing or when it is really removed.

Quoted 5 lines
		...

although you might see something like this based on external scanning through procfs/sysfs as mentioned earlier, so please take a look at how all these corner cases could effect you.

I'll add a check if .slave_configure() and .slave_destroy() are running in the same task as scsi_add_device() or scsi_remove_device(). This should rule out all of these corner cases.

Juergen

author maintainer
#9 lore raw

Re: [Xen-devel] [PATCH V4 2/4] Introduce xen-scsifront module

In reply to Christoph Hellwig
Quoted 19 lines · Christoph Hellwig
Quoted 2 lines · Juergen Gross

What do you mean with "unusual"? You mean transferring the EH action to Dom0?

Yes. Note that hyperv tries something similar and they've run into timeout issues, you might want to read up the recent thread on that.

Quoted 12 lines
Quoted 7 lines

drivers/scsi/xen-scsifront.c line 894

Quoted 5 lines
drivers/scsi/xen-scsifront.c new file +5 -0
+				} else {
+					xenbus_printf(XBT_NIL, dev->nodename,
+						      state_str, "%d",
+						      XenbusStateConnected);
+				}

Just print this message in ->slave_configure.

This is calling for problems, I think. xenbus_printf() is not just a printing function, but it changes an entry in the xenstore. And this requires locking, switching threads, ...

Quoted 6 lines
Quoted 1 line

I doubt doing this while holding SCSI-internal locks is a good idea.

Oh, I thought xenbus_printf was just a logging wrapper.

Doing major work in the slave_* callouts is not a problem, that's what they were designed for.

Okay.

Quoted 11 lines

For the successful case the xenbus_printf should be done in ->slave_configure. For the failure case you probably want to do it from ->slave_destroy based on the absence of a flag set in ->slave_configure, e.g. in slave_configure:

	sdev->hostdata = (void *)1UL;

and in ->slave_destroy:

	if (!sdev->hostdata)

I don't think I'll need the flag. The action is the same if the device is being destroyed again because of already existing or when it is really removed.

Quoted 5 lines
		...

although you might see something like this based on external scanning through procfs/sysfs as mentioned earlier, so please take a look at how all these corner cases could effect you.

I'll add a check if .slave_configure() and .slave_destroy() are running in the same task as scsi_add_device() or scsi_remove_device(). This should rule out all of these corner cases.

Juergen

author maintainer
#17 lore raw

Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Christoph Hellwig
Quoted 1 line · Christoph Hellwig
Quoted 1 line

+#include <scsi/scsi_dbg.h>

__scsi_print_sense()

Quoted 1 line
Quoted 1 line

+#include <scsi/scsi_eh.h>

struct scsi_sense_hdr

Quoted 1 line
Quoted 1 line

+#include <scsi/scsi_tcq.h>

SG_ALL

Quoted 10 lines

What do you need these for? Normally target drivers shouldn't need these.

drivers/xen/xen-scsiback.c line 157

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+struct vscsibk_emulate {
+	void (*pre_function)(struct vscsibk_pend *, void *);
+	void (*post_function)(struct vscsibk_pend *, void *);
+};

This doesn't seem to be used.

Correct. Will delete.

Quoted 9 lines

drivers/xen/xen-scsiback.c line 212

Quoted 7 lines
drivers/xen/xen-scsiback.c new file +6 -0
+#define scsiback_get(_b) (atomic_inc(&(_b)->nr_unreplied_reqs))
+#define scsiback_put(_b)				\
+	do {						\
+		if (atomic_dec_and_test(&(_b)->nr_unreplied_reqs))	\
+			wake_up(&(_b)->waiting_to_free);\
+	} while (0)

Normal Linux style would be to make these inline functions.

Okay. I'll change those.

Quoted 15 lines

drivers/xen/xen-scsiback.c line 620

Quoted 12 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_notify_work(struct vscsibk_info *info)
+{
+	info->waiting_reqs = 1;
+	wake_up(&info->wq);
+}
+
+static irqreturn_t scsiback_intr(int irq, void *dev_id)
+{
+	scsiback_notify_work((struct vscsibk_info *)dev_id);
+	return IRQ_HANDLED;
+}

Seems like this driver should get the same threaded irq treatment as the initiator side?

Indeed.

Quoted 16 lines
Quoted 16 lines
drivers/xen/xen-scsiback.c new file +15 -0
+static void scsiback_disconnect(struct vscsibk_info *info)
+{
+	if (info->kthread) {
+		kthread_stop(info->kthread);
+		info->kthread = NULL;
+		wake_up(&info->shutdown_wq);
+	}
+
+	wait_event(info->waiting_to_free,
+		atomic_read(&info->nr_unreplied_reqs) == 0);
+
+	if (info->irq) {
+		unbind_from_irqhandler(info->irq, info);
+		info->irq = 0;
+	}
Quoted 8 lines

drivers/xen/xen-scsiback.c line 678

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+
+	if (info->ring.sring) {
+		xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
+		info->ring.sring = NULL;
+	}
+}

Also the same treatment for goto based init failure unwinding.

Yep.

Juergen

author maintainer
#20 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Christoph Hellwig
Quoted 1 line · Christoph Hellwig
Quoted 1 line

+#include <scsi/scsi_dbg.h>

__scsi_print_sense()

Quoted 1 line
Quoted 1 line

+#include <scsi/scsi_eh.h>

struct scsi_sense_hdr

Quoted 1 line
Quoted 1 line

+#include <scsi/scsi_tcq.h>

SG_ALL

Quoted 10 lines

What do you need these for? Normally target drivers shouldn't need these.

drivers/xen/xen-scsiback.c line 157

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+struct vscsibk_emulate {
+	void (*pre_function)(struct vscsibk_pend *, void *);
+	void (*post_function)(struct vscsibk_pend *, void *);
+};

This doesn't seem to be used.

Correct. Will delete.

Quoted 9 lines

drivers/xen/xen-scsiback.c line 212

Quoted 7 lines
drivers/xen/xen-scsiback.c new file +6 -0
+#define scsiback_get(_b) (atomic_inc(&(_b)->nr_unreplied_reqs))
+#define scsiback_put(_b)				\
+	do {						\
+		if (atomic_dec_and_test(&(_b)->nr_unreplied_reqs))	\
+			wake_up(&(_b)->waiting_to_free);\
+	} while (0)

Normal Linux style would be to make these inline functions.

Okay. I'll change those.

Quoted 15 lines

drivers/xen/xen-scsiback.c line 620

Quoted 12 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_notify_work(struct vscsibk_info *info)
+{
+	info->waiting_reqs = 1;
+	wake_up(&info->wq);
+}
+
+static irqreturn_t scsiback_intr(int irq, void *dev_id)
+{
+	scsiback_notify_work((struct vscsibk_info *)dev_id);
+	return IRQ_HANDLED;
+}

Seems like this driver should get the same threaded irq treatment as the initiator side?

Indeed.

Quoted 16 lines
Quoted 16 lines
drivers/xen/xen-scsiback.c new file +15 -0
+static void scsiback_disconnect(struct vscsibk_info *info)
+{
+	if (info->kthread) {
+		kthread_stop(info->kthread);
+		info->kthread = NULL;
+		wake_up(&info->shutdown_wq);
+	}
+
+	wait_event(info->waiting_to_free,
+		atomic_read(&info->nr_unreplied_reqs) == 0);
+
+	if (info->irq) {
+		unbind_from_irqhandler(info->irq, info);
+		info->irq = 0;
+	}
Quoted 8 lines

drivers/xen/xen-scsiback.c line 678

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+
+	if (info->ring.sring) {
+		xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
+		info->ring.sring = NULL;
+	}
+}

Also the same treatment for goto based init failure unwinding.

Yep.

Juergen

author maintainer
#18 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross
Quoted 6 lines · Christoph Hellwig
Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+static void scsiback_notify_work(struct vscsibk_info *info)
+{
+    info->waiting_reqs = 1;
+    wake_up(&info->wq);
+}
+
Quoted 8 lines

drivers/xen/xen-scsiback.c line 620

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static irqreturn_t scsiback_intr(int irq, void *dev_id)
+{
+    scsiback_notify_work((struct vscsibk_info *)dev_id);
+    return IRQ_HANDLED;
+}

Seems like this driver should get the same threaded irq treatment as the initiator side?

Quoted 1 line

Indeed.

Turns out to be hard. The interrupt is allocated via bind_interdomain_evtchn_to_irqhandler() which is not capable of threaded irq handling. The underlying bind_interdomain_evtchn_to_irq() is not exported, so I can't use it.

I'll try to change this, but until then I have to leave the irq handling more or less the same.

Juergen

author maintainer
#19 lore raw

Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross
Quoted 6 lines · Christoph Hellwig
Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+static void scsiback_notify_work(struct vscsibk_info *info)
+{
+    info->waiting_reqs = 1;
+    wake_up(&info->wq);
+}
+
Quoted 8 lines

drivers/xen/xen-scsiback.c line 620

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static irqreturn_t scsiback_intr(int irq, void *dev_id)
+{
+    scsiback_notify_work((struct vscsibk_info *)dev_id);
+    return IRQ_HANDLED;
+}

Seems like this driver should get the same threaded irq treatment as the initiator side?

Quoted 1 line

Indeed.

Turns out to be hard. The interrupt is allocated via bind_interdomain_evtchn_to_irqhandler() which is not capable of threaded irq handling. The underlying bind_interdomain_evtchn_to_irq() is not exported, so I can't use it.

I'll try to change this, but until then I have to leave the irq handling more or less the same.

Juergen

#21 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to [email protected]

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Quoted 30 lines · On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI backend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- adapt to Linux style guide
- use target core infrastructure instead doing pure pass-through
- enable module unloading
- support SG-list in grant page(s)
- support task abort
- remove redundant struct backend
- allocate resources dynamically
- correct minor error in scsiback_fast_flush_area
- free allocated resources in case of error during I/O preparation
- remove CDB emulation, now handled by target core infrastructure

Xen related parts

---

<SNIP>

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +0 -0
new file mode 100644
index 0000000..4a0d6e3

<SNIP>

drivers/xen/xen-scsiback.c line 172

Quoted 9 lines
drivers/xen/xen-scsiback.c new file +9 -0
+struct scsiback_nacl {
+	/* Binary World Wide unique Port Name for pvscsi Initiator port */
+	u64 iport_wwpn;
+	/* ASCII formatted WWPN for Sas Initiator port */
+	char iport_name[VSCSI_NAMELEN];
+	/* Returned by scsiback_make_nodeacl() */
+	struct se_node_acl se_node_acl;
+};
+

Given that this code is similar to how loopback + vhost-scsi function, and uses a (locally) generated nexus for each WWPN endpoint, the scsiback_nacl and associated code will be unused should be be dropped.

<SNIP>

Quoted 55 lines
drivers/xen/xen-scsiback.c new file +55 -0
+static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
+			uint32_t resid, struct vscsibk_pend *pending_req)
+{
+	struct vscsiif_response *ring_res;
+	struct vscsibk_info *info = pending_req->info;
+	int notify;
+	struct scsi_sense_hdr sshdr;
+	unsigned long flags;
+	unsigned len;
+
+	spin_lock_irqsave(&info->ring_lock, flags);
+
+	ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
+	info->ring.rsp_prod_pvt++;
+
+	ring_res->rslt   = result;
+	ring_res->rqid   = pending_req->rqid;
+
+	if (sense_buffer != NULL &&
+	    scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
+				 &sshdr)) {
+		len = min_t(unsigned, 8 + sense_buffer[7],
+			    VSCSIIF_SENSE_BUFFERSIZE);
+		memcpy(ring_res->sense_buffer, sense_buffer, len);
+		ring_res->sense_len = len;
+	} else {
+		ring_res->sense_len = 0;
+	}
+
+	ring_res->residual_len = resid;
+
+	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
+	spin_unlock_irqrestore(&info->ring_lock, flags);
+
+	if (notify)
+		notify_remote_via_irq(info->irq);
+
+	if (pending_req->v2p)
+		kref_put(&pending_req->v2p->kref,
+			 scsiback_free_translation_entry);
+
+	kmem_cache_free(scsiback_cachep, pending_req);
+}
+
+static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
+{
+	struct vscsibk_info *info = pending_req->info;
+	unsigned char *sense_buffer;
+	unsigned int resid;
+	int errors;
+
+	sense_buffer = pending_req->sense_buffer;
+	resid        = pending_req->se_cmd.residual_count;
+	errors       = pending_req->result;
+

drivers/xen/xen-scsiback.c line 415

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (errors && log_print_stat)
+		scsiback_print_status(sense_buffer, errors, pending_req);
+
+	scsiback_fast_flush_area(pending_req);
+	scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
+	scsiback_put(info);
+
+	transport_generic_free_cmd(&pending_req->se_cmd, 0);
+}
+

The usage here of scsiback_do_resp_with_sense() -> kmem_cache_free() for *pending_req, and then invoking transport_generic_free_cmd() with &pending_req->se_cmd is an free after use bug..

So the way this should work is similar to how loopback currently does things:

- Move the kmem_cache_free() for pending_req from scsiback_do_resp_with_sense() to scsiback_release_cmd()
- Remove the transport_generic_free_cmd() from scsiback_cmd_done()
- Copy what tcm_loop_check_stop_free() does into scsiback_check_stop_free(), and remove target_put_sess_cmd()

drivers/xen/xen-scsiback.c line 426

Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
+{
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
+	int rc;
+
+	memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
+
+	memset(se_cmd, 0, sizeof(*se_cmd));
+	se_cmd->prot_pto = true;
+

No need to set prot_pto = true, as T10_PI support is not enabled..

Quoted 14 lines
drivers/xen/xen-scsiback.c new file +14 -0
+	scsiback_get(pending_req->info);
+	rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
+			pending_req->sense_buffer, pending_req->v2p->lun,
+			pending_req->data_len, 0,
+			pending_req->sc_data_direction, 0,
+			pending_req->sgl, pending_req->n_sg,
+			NULL, 0, NULL, 0);
+	if (rc < 0) {
+		transport_send_check_condition_and_sense(se_cmd,
+				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
+		transport_generic_free_cmd(se_cmd, 0);
+	}
+}
+

<SNIP>

Quoted 33 lines
drivers/xen/xen-scsiback.c new file +33 -0
+
+static void scsiback_device_action(struct vscsibk_pend *pending_req,
+	enum tcm_tmreq_table act, int tag)
+{
+	int rc, err = FAILED;
+	struct scsiback_tpg *tpg = pending_req->v2p->tpg;
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct scsiback_tmr *tmr;
+
+	tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
+	if (!tmr) {
+		pr_err("xen-pvscsi: %s: kmalloc() error\n", __func__);
+		goto out;
+	}
+	init_waitqueue_head(&tmr->tmr_wait);
+
+	transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
+		tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, MSG_SIMPLE_TAG,
+		&pending_req->sense_buffer[0]);
+
+	rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
+	if (rc < 0)
+		goto out;
+
+	se_cmd->se_tmr_req->ref_task_tag = tag;
+
+	if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
+		goto out;
+
+	transport_generic_handle_tmr(se_cmd);
+	wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
+
+	err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?

drivers/xen/xen-scsiback.c line 721

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+		SUCCESS : FAILED;
+
+out:
+	if (tmr) {
+		transport_generic_free_cmd(&pending_req->se_cmd, 1);
+		kfree(tmr);
+	}
+
+	scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
+}

With scsiback_do_resp_with_sense() no longer freeing pending_req directly, this special case will need a kmem_cache_free()

Quoted 20 lines
drivers/xen/xen-scsiback.c new file +20 -0
+static int _scsiback_do_cmd_fn(struct vscsibk_info *info)
+{
+	struct vscsiif_back_ring *ring = &info->ring;
+	struct vscsiif_request *ring_req;
+	struct vscsibk_pend *pending_req;
+	RING_IDX rc, rp;
+	int err, more_to_do = 0;
+	uint32_t result;
+	uint8_t act;
+
+	rc = ring->req_cons;
+	rp = ring->sring->req_prod;
+	rmb();	/* guest system is accessing ring, too */
+
+	if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
+		rc = ring->rsp_prod_pvt;
+		pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
+			   info->domid, rp, rc, rp - rc);
+		return -EACCES;
+	}

drivers/xen/xen-scsiback.c line 822

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	while ((rc != rp)) {
+		if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
+			break;
+		pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
+		if (NULL == pending_req) {
+			more_to_do = 1;
+			break;
+		}
+

Ideally this will end up using percpu_ida descriptor pre-allocation via se_sess->sess_tag_poll + se_sess->sess_cmd_map in order to avoid the fast-path memory allocation.

See commit 4824d3bfb909 for an example of how this was done for vhost-scsi, but should be considered a post-merge optimization..

Quoted 16 lines
drivers/xen/xen-scsiback.c new file +16 -0
+		ring_req = RING_GET_REQUEST(ring, rc);
+		ring->req_cons = ++rc;
+
+		act = ring_req->act;
+		err = prepare_pending_reqs(info, ring_req, pending_req);
+		if (err) {
+			switch (err) {
+			case -ENODEV:
+				result = DID_NO_CONNECT;
+				break;
+			default:
+				result = DRIVER_ERROR;
+				break;
+			}
+			scsiback_do_resp_with_sense(NULL, result << 24, 0,
+						    pending_req);

drivers/xen/xen-scsiback.c line 848

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+			more_to_do = 1;
+			break;
+		}
+
+		switch (act) {
+		case VSCSIIF_ACT_SCSI_CDB:
+			if (scsiback_gnttab_data_map(ring_req, pending_req)) {
+				scsiback_fast_flush_area(pending_req);
+				scsiback_do_resp_with_sense(NULL,
+					DRIVER_ERROR << 24, 0, pending_req);

Another special case for scsiback_do_resp_with_sense() that with the recommended changes require a kmem_cache_free() call.

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+			} else {
+				scsiback_cmd_exec(pending_req);
+			}
+			break;
+		case VSCSIIF_ACT_SCSI_ABORT:

drivers/xen/xen-scsiback.c line 863

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+			scsiback_device_action(pending_req, TMR_ABORT_TASK,
+				ring_req->ref_rqid);
+			break;
+		case VSCSIIF_ACT_SCSI_RESET:
+			scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
+			break;
+		default:
+			pr_err_ratelimited("xen-pvscsi: invalid request\n");
+			scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
+						    0, pending_req);

Ditto here.

Quoted 13 lines
drivers/xen/xen-scsiback.c new file +13 -0
+			break;
+		}
+
+		/* Yield point for this unbounded loop. */
+		cond_resched();
+	}
+
+	if (RING_HAS_UNCONSUMED_REQUESTS(ring))
+		more_to_do = 1;
+
+	return more_to_do;
+}
+

<SNIP>

Quoted 60 lines
drivers/xen/xen-scsiback.c new file +60 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+	struct scsiback_nacl *nacl;
+
+	nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+	if (!nacl) {
+		pr_err("Unable to allocate struct scsiback_nacl\n");
+		return NULL;
+	}
+
+	return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+			     struct se_node_acl *se_nacl)
+{
+	struct scsiback_nacl *nacl = container_of(se_nacl,
+			struct scsiback_nacl, se_node_acl);
+	kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+	return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+		       struct config_group *group,
+		       const char *name)
+{
+	struct se_node_acl *se_nacl, *se_nacl_new;
+	struct scsiback_nacl *nacl;
+	u64 wwpn = 0;
+	u32 nexus_depth;
+
+	se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+	if (!se_nacl_new)
+		return ERR_PTR(-ENOMEM);
+
+	nexus_depth = 1;
+	/*
+	 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+	 * when converting a NodeACL from demo mode -> explict
+	 */
+	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+				name, nexus_depth);
+	if (IS_ERR(se_nacl)) {
+		scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+		return se_nacl;
+	}
+	/*
+	 * Locate our struct scsiback_nacl and set the FC Nport WWPN
+	 */
+	nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+	nacl->iport_wwpn = wwpn;
+
+	return se_nacl;

drivers/xen/xen-scsiback.c line 1629

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+	struct scsiback_nacl *nacl = container_of(se_acl,
+				struct scsiback_nacl, se_node_acl);
+	core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
+	kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

drivers/xen/xen-scsiback.c line 1634

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static int scsiback_check_stop_free(struct se_cmd *se_cmd)
+{
+	return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
+}
+

As mentioned above, scsiback_check_stop_free() should follow what tcm_loop_check_stop_free() does here..

drivers/xen/xen-scsiback.c line 1638

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+static void scsiback_release_cmd(struct se_cmd *se_cmd)
+{
+}
+

Move the kmem_cache_free() for pending_req here..

Quoted 21 lines
drivers/xen/xen-scsiback.c new file +21 -0
+static int scsiback_shutdown_session(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static void scsiback_close_session(struct se_session *se_sess)
+{
+}
+
+static u32 scsiback_sess_get_index(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static int scsiback_write_pending(struct se_cmd *se_cmd)
+{
+	/* Go ahead and process the write immediately */
+	target_execute_cmd(se_cmd);
+
+	return 0;
+}

drivers/xen/xen-scsiback.c line 1669

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+static int scsiback_write_pending_status(struct se_cmd *se_cmd)
+{
+	return 0;
+}
+
+static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
+{
+}
+

Safe to drop this no-op too.

Quoted 48 lines
drivers/xen/xen-scsiback.c new file +48 -0
+static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	return pending_req->rqid;
+}
+
+static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
+{
+	return 0;
+}
+
+static int scsiback_queue_data_in(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	pending_req->result = SAM_STAT_GOOD;
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static int scsiback_queue_status(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	if (se_cmd->sense_buffer &&
+	    ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
+	     (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
+		pending_req->result = (DRIVER_SENSE << 24) |
+				      SAM_STAT_CHECK_CONDITION;
+	else
+		pending_req->result = se_cmd->scsi_status;
+
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
+}

drivers/xen/xen-scsiback.c line 1727

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+static void scsiback_aborted_task(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
+}
+

scsiback_aborted_task() should just return here, instead of the atomic_set + wake_up(). Eg: ABORT_TASK is already calling -> queue_tm_rsp(), and the ->aborted_task() callback is specifically for cleaning up any fabric specific descriptor resources, before ->release_cmd() gets called.

Quoted 59 lines
drivers/xen/xen-scsiback.c new file +59 -0
+static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
+					     char *page)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	ssize_t rb;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return rb;
+}
+
+static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
+					      const char *page, size_t count)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	int len;
+
+	if (strlen(page) >= VSCSI_NAMELEN) {
+		pr_err("param alias: %s, exceeds max: %d\n", page,
+			VSCSI_NAMELEN);
+		return -EINVAL;
+	}
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
+	if (tpg->param_alias[len - 1] == '\n')
+		tpg->param_alias[len - 1] = '\0';
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return count;
+}
+
+TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
+
+static struct configfs_attribute *scsiback_param_attrs[] = {
+	&scsiback_tpg_param_alias.attr,
+	NULL,
+};
+
+static int scsiback_make_nexus(struct scsiback_tpg *tpg,
+				const char *name)
+{
+	struct se_portal_group *se_tpg;
+	struct se_session *se_sess;
+	struct scsiback_nexus *tv_nexus;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	if (tpg->tpg_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("tpg->tpg_nexus already exists\n");
+		return -EEXIST;
+	}
+	se_tpg = &tpg->se_tpg;
+
+	tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);

drivers/xen/xen-scsiback.c line 1796

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (!tv_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_err("Unable to allocate struct scsiback_nexus\n");
+		return -ENOMEM;
+	}
+	/*
+	 *  Initialize the struct se_session pointer
+	 */
+	tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_DIN_PASS |
+						       TARGET_PROT_DOUT_PASS);

TARGET_PROT_D*_PASS incorrectly declares T10 PI support here, which should be TARGET_PROT_NORMAL instead.

Quoted 36 lines
drivers/xen/xen-scsiback.c new file +36 -0
+	if (IS_ERR(tv_nexus->tvn_se_sess)) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		kfree(tv_nexus);
+		return -ENOMEM;
+	}
+	se_sess = tv_nexus->tvn_se_sess;
+	/*
+	 * Since we are running in 'demo mode' this call with generate a
+	 * struct se_node_acl for the scsiback struct se_portal_group with
+	 * the SCSI Initiator port name of the passed configfs group 'name'.
+	 */
+	tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
+				se_tpg, (unsigned char *)name);
+	if (!tv_nexus->tvn_se_sess->se_node_acl) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+			 name);
+		goto out;
+	}
+	/*
+	 * Now register the TCM pvscsi virtual I_T Nexus as active with the
+	 * call to __transport_register_session()
+	 */
+	__transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
+			tv_nexus->tvn_se_sess, tv_nexus);
+	tpg->tpg_nexus = tv_nexus;
+
+	mutex_unlock(&tpg->tv_tpg_mutex);
+	return 0;
+
+out:
+	transport_free_session(se_sess);
+	kfree(tv_nexus);
+	return -ENOMEM;
+}
+

<SNIP>

Quoted 8 lines
drivers/xen/xen-scsiback.c new file +8 -0
+
+static int scsiback_port_link(struct se_portal_group *se_tpg,
+			       struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);

drivers/xen/xen-scsiback.c line 2020

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count++;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	mutex_unlock(&scsiback_mutex);
+
+	return 0;
+}
+

AFAICT, no need to hold scsiback_mutex while incrementing tpg->tv_tpg_port_count.

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static void scsiback_port_unlink(struct se_portal_group *se_tpg,
+				  struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);

drivers/xen/xen-scsiback.c line 2035

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count--;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	mutex_unlock(&scsiback_mutex);
+}
+

Ditto here.

#22 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to [email protected]

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Quoted 30 lines · On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI backend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- adapt to Linux style guide
- use target core infrastructure instead doing pure pass-through
- enable module unloading
- support SG-list in grant page(s)
- support task abort
- remove redundant struct backend
- allocate resources dynamically
- correct minor error in scsiback_fast_flush_area
- free allocated resources in case of error during I/O preparation
- remove CDB emulation, now handled by target core infrastructure

Xen related parts

---

<SNIP>

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +0 -0
new file mode 100644
index 0000000..4a0d6e3

<SNIP>

drivers/xen/xen-scsiback.c line 172

Quoted 9 lines
drivers/xen/xen-scsiback.c new file +9 -0
+struct scsiback_nacl {
+	/* Binary World Wide unique Port Name for pvscsi Initiator port */
+	u64 iport_wwpn;
+	/* ASCII formatted WWPN for Sas Initiator port */
+	char iport_name[VSCSI_NAMELEN];
+	/* Returned by scsiback_make_nodeacl() */
+	struct se_node_acl se_node_acl;
+};
+

Given that this code is similar to how loopback + vhost-scsi function, and uses a (locally) generated nexus for each WWPN endpoint, the scsiback_nacl and associated code will be unused should be be dropped.

<SNIP>

Quoted 55 lines
drivers/xen/xen-scsiback.c new file +55 -0
+static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
+			uint32_t resid, struct vscsibk_pend *pending_req)
+{
+	struct vscsiif_response *ring_res;
+	struct vscsibk_info *info = pending_req->info;
+	int notify;
+	struct scsi_sense_hdr sshdr;
+	unsigned long flags;
+	unsigned len;
+
+	spin_lock_irqsave(&info->ring_lock, flags);
+
+	ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
+	info->ring.rsp_prod_pvt++;
+
+	ring_res->rslt   = result;
+	ring_res->rqid   = pending_req->rqid;
+
+	if (sense_buffer != NULL &&
+	    scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
+				 &sshdr)) {
+		len = min_t(unsigned, 8 + sense_buffer[7],
+			    VSCSIIF_SENSE_BUFFERSIZE);
+		memcpy(ring_res->sense_buffer, sense_buffer, len);
+		ring_res->sense_len = len;
+	} else {
+		ring_res->sense_len = 0;
+	}
+
+	ring_res->residual_len = resid;
+
+	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
+	spin_unlock_irqrestore(&info->ring_lock, flags);
+
+	if (notify)
+		notify_remote_via_irq(info->irq);
+
+	if (pending_req->v2p)
+		kref_put(&pending_req->v2p->kref,
+			 scsiback_free_translation_entry);
+
+	kmem_cache_free(scsiback_cachep, pending_req);
+}
+
+static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
+{
+	struct vscsibk_info *info = pending_req->info;
+	unsigned char *sense_buffer;
+	unsigned int resid;
+	int errors;
+
+	sense_buffer = pending_req->sense_buffer;
+	resid        = pending_req->se_cmd.residual_count;
+	errors       = pending_req->result;
+

drivers/xen/xen-scsiback.c line 415

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (errors && log_print_stat)
+		scsiback_print_status(sense_buffer, errors, pending_req);
+
+	scsiback_fast_flush_area(pending_req);
+	scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
+	scsiback_put(info);
+
+	transport_generic_free_cmd(&pending_req->se_cmd, 0);
+}
+

The usage here of scsiback_do_resp_with_sense() -> kmem_cache_free() for *pending_req, and then invoking transport_generic_free_cmd() with &pending_req->se_cmd is an free after use bug..

So the way this should work is similar to how loopback currently does things:

- Move the kmem_cache_free() for pending_req from scsiback_do_resp_with_sense() to scsiback_release_cmd()
- Remove the transport_generic_free_cmd() from scsiback_cmd_done()
- Copy what tcm_loop_check_stop_free() does into scsiback_check_stop_free(), and remove target_put_sess_cmd()

drivers/xen/xen-scsiback.c line 426

Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
+{
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
+	int rc;
+
+	memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
+
+	memset(se_cmd, 0, sizeof(*se_cmd));
+	se_cmd->prot_pto = true;
+

No need to set prot_pto = true, as T10_PI support is not enabled..

Quoted 14 lines
drivers/xen/xen-scsiback.c new file +14 -0
+	scsiback_get(pending_req->info);
+	rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
+			pending_req->sense_buffer, pending_req->v2p->lun,
+			pending_req->data_len, 0,
+			pending_req->sc_data_direction, 0,
+			pending_req->sgl, pending_req->n_sg,
+			NULL, 0, NULL, 0);
+	if (rc < 0) {
+		transport_send_check_condition_and_sense(se_cmd,
+				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
+		transport_generic_free_cmd(se_cmd, 0);
+	}
+}
+

<SNIP>

Quoted 33 lines
drivers/xen/xen-scsiback.c new file +33 -0
+
+static void scsiback_device_action(struct vscsibk_pend *pending_req,
+	enum tcm_tmreq_table act, int tag)
+{
+	int rc, err = FAILED;
+	struct scsiback_tpg *tpg = pending_req->v2p->tpg;
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct scsiback_tmr *tmr;
+
+	tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
+	if (!tmr) {
+		pr_err("xen-pvscsi: %s: kmalloc() error\n", __func__);
+		goto out;
+	}
+	init_waitqueue_head(&tmr->tmr_wait);
+
+	transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
+		tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, MSG_SIMPLE_TAG,
+		&pending_req->sense_buffer[0]);
+
+	rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
+	if (rc < 0)
+		goto out;
+
+	se_cmd->se_tmr_req->ref_task_tag = tag;
+
+	if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
+		goto out;
+
+	transport_generic_handle_tmr(se_cmd);
+	wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
+
+	err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?

drivers/xen/xen-scsiback.c line 721

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+		SUCCESS : FAILED;
+
+out:
+	if (tmr) {
+		transport_generic_free_cmd(&pending_req->se_cmd, 1);
+		kfree(tmr);
+	}
+
+	scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
+}

With scsiback_do_resp_with_sense() no longer freeing pending_req directly, this special case will need a kmem_cache_free()

Quoted 20 lines
drivers/xen/xen-scsiback.c new file +20 -0
+static int _scsiback_do_cmd_fn(struct vscsibk_info *info)
+{
+	struct vscsiif_back_ring *ring = &info->ring;
+	struct vscsiif_request *ring_req;
+	struct vscsibk_pend *pending_req;
+	RING_IDX rc, rp;
+	int err, more_to_do = 0;
+	uint32_t result;
+	uint8_t act;
+
+	rc = ring->req_cons;
+	rp = ring->sring->req_prod;
+	rmb();	/* guest system is accessing ring, too */
+
+	if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
+		rc = ring->rsp_prod_pvt;
+		pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
+			   info->domid, rp, rc, rp - rc);
+		return -EACCES;
+	}

drivers/xen/xen-scsiback.c line 822

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	while ((rc != rp)) {
+		if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
+			break;
+		pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
+		if (NULL == pending_req) {
+			more_to_do = 1;
+			break;
+		}
+

Ideally this will end up using percpu_ida descriptor pre-allocation via se_sess->sess_tag_poll + se_sess->sess_cmd_map in order to avoid the fast-path memory allocation.

See commit 4824d3bfb909 for an example of how this was done for vhost-scsi, but should be considered a post-merge optimization..

Quoted 16 lines
drivers/xen/xen-scsiback.c new file +16 -0
+		ring_req = RING_GET_REQUEST(ring, rc);
+		ring->req_cons = ++rc;
+
+		act = ring_req->act;
+		err = prepare_pending_reqs(info, ring_req, pending_req);
+		if (err) {
+			switch (err) {
+			case -ENODEV:
+				result = DID_NO_CONNECT;
+				break;
+			default:
+				result = DRIVER_ERROR;
+				break;
+			}
+			scsiback_do_resp_with_sense(NULL, result << 24, 0,
+						    pending_req);

drivers/xen/xen-scsiback.c line 848

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+			more_to_do = 1;
+			break;
+		}
+
+		switch (act) {
+		case VSCSIIF_ACT_SCSI_CDB:
+			if (scsiback_gnttab_data_map(ring_req, pending_req)) {
+				scsiback_fast_flush_area(pending_req);
+				scsiback_do_resp_with_sense(NULL,
+					DRIVER_ERROR << 24, 0, pending_req);

Another special case for scsiback_do_resp_with_sense() that with the recommended changes require a kmem_cache_free() call.

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+			} else {
+				scsiback_cmd_exec(pending_req);
+			}
+			break;
+		case VSCSIIF_ACT_SCSI_ABORT:

drivers/xen/xen-scsiback.c line 863

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+			scsiback_device_action(pending_req, TMR_ABORT_TASK,
+				ring_req->ref_rqid);
+			break;
+		case VSCSIIF_ACT_SCSI_RESET:
+			scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
+			break;
+		default:
+			pr_err_ratelimited("xen-pvscsi: invalid request\n");
+			scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
+						    0, pending_req);

Ditto here.

Quoted 13 lines
drivers/xen/xen-scsiback.c new file +13 -0
+			break;
+		}
+
+		/* Yield point for this unbounded loop. */
+		cond_resched();
+	}
+
+	if (RING_HAS_UNCONSUMED_REQUESTS(ring))
+		more_to_do = 1;
+
+	return more_to_do;
+}
+

<SNIP>

Quoted 60 lines
drivers/xen/xen-scsiback.c new file +60 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+	struct scsiback_nacl *nacl;
+
+	nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+	if (!nacl) {
+		pr_err("Unable to allocate struct scsiback_nacl\n");
+		return NULL;
+	}
+
+	return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+			     struct se_node_acl *se_nacl)
+{
+	struct scsiback_nacl *nacl = container_of(se_nacl,
+			struct scsiback_nacl, se_node_acl);
+	kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+	return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+		       struct config_group *group,
+		       const char *name)
+{
+	struct se_node_acl *se_nacl, *se_nacl_new;
+	struct scsiback_nacl *nacl;
+	u64 wwpn = 0;
+	u32 nexus_depth;
+
+	se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+	if (!se_nacl_new)
+		return ERR_PTR(-ENOMEM);
+
+	nexus_depth = 1;
+	/*
+	 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+	 * when converting a NodeACL from demo mode -> explict
+	 */
+	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+				name, nexus_depth);
+	if (IS_ERR(se_nacl)) {
+		scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+		return se_nacl;
+	}
+	/*
+	 * Locate our struct scsiback_nacl and set the FC Nport WWPN
+	 */
+	nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+	nacl->iport_wwpn = wwpn;
+
+	return se_nacl;

drivers/xen/xen-scsiback.c line 1629

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+	struct scsiback_nacl *nacl = container_of(se_acl,
+				struct scsiback_nacl, se_node_acl);
+	core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
+	kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

drivers/xen/xen-scsiback.c line 1634

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static int scsiback_check_stop_free(struct se_cmd *se_cmd)
+{
+	return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
+}
+

As mentioned above, scsiback_check_stop_free() should follow what tcm_loop_check_stop_free() does here..

drivers/xen/xen-scsiback.c line 1638

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+static void scsiback_release_cmd(struct se_cmd *se_cmd)
+{
+}
+

Move the kmem_cache_free() for pending_req here..

Quoted 21 lines
drivers/xen/xen-scsiback.c new file +21 -0
+static int scsiback_shutdown_session(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static void scsiback_close_session(struct se_session *se_sess)
+{
+}
+
+static u32 scsiback_sess_get_index(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static int scsiback_write_pending(struct se_cmd *se_cmd)
+{
+	/* Go ahead and process the write immediately */
+	target_execute_cmd(se_cmd);
+
+	return 0;
+}

drivers/xen/xen-scsiback.c line 1669

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+static int scsiback_write_pending_status(struct se_cmd *se_cmd)
+{
+	return 0;
+}
+
+static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
+{
+}
+

Safe to drop this no-op too.

Quoted 48 lines
drivers/xen/xen-scsiback.c new file +48 -0
+static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	return pending_req->rqid;
+}
+
+static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
+{
+	return 0;
+}
+
+static int scsiback_queue_data_in(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	pending_req->result = SAM_STAT_GOOD;
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static int scsiback_queue_status(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	if (se_cmd->sense_buffer &&
+	    ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
+	     (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
+		pending_req->result = (DRIVER_SENSE << 24) |
+				      SAM_STAT_CHECK_CONDITION;
+	else
+		pending_req->result = se_cmd->scsi_status;
+
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
+}

drivers/xen/xen-scsiback.c line 1727

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+static void scsiback_aborted_task(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
+}
+

scsiback_aborted_task() should just return here, instead of the atomic_set + wake_up(). Eg: ABORT_TASK is already calling -> queue_tm_rsp(), and the ->aborted_task() callback is specifically for cleaning up any fabric specific descriptor resources, before ->release_cmd() gets called.

Quoted 59 lines
drivers/xen/xen-scsiback.c new file +59 -0
+static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
+					     char *page)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	ssize_t rb;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return rb;
+}
+
+static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
+					      const char *page, size_t count)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	int len;
+
+	if (strlen(page) >= VSCSI_NAMELEN) {
+		pr_err("param alias: %s, exceeds max: %d\n", page,
+			VSCSI_NAMELEN);
+		return -EINVAL;
+	}
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
+	if (tpg->param_alias[len - 1] == '\n')
+		tpg->param_alias[len - 1] = '\0';
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return count;
+}
+
+TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
+
+static struct configfs_attribute *scsiback_param_attrs[] = {
+	&scsiback_tpg_param_alias.attr,
+	NULL,
+};
+
+static int scsiback_make_nexus(struct scsiback_tpg *tpg,
+				const char *name)
+{
+	struct se_portal_group *se_tpg;
+	struct se_session *se_sess;
+	struct scsiback_nexus *tv_nexus;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	if (tpg->tpg_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("tpg->tpg_nexus already exists\n");
+		return -EEXIST;
+	}
+	se_tpg = &tpg->se_tpg;
+
+	tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);

drivers/xen/xen-scsiback.c line 1796

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (!tv_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_err("Unable to allocate struct scsiback_nexus\n");
+		return -ENOMEM;
+	}
+	/*
+	 *  Initialize the struct se_session pointer
+	 */
+	tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_DIN_PASS |
+						       TARGET_PROT_DOUT_PASS);

TARGET_PROT_D*_PASS incorrectly declares T10 PI support here, which should be TARGET_PROT_NORMAL instead.

Quoted 36 lines
drivers/xen/xen-scsiback.c new file +36 -0
+	if (IS_ERR(tv_nexus->tvn_se_sess)) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		kfree(tv_nexus);
+		return -ENOMEM;
+	}
+	se_sess = tv_nexus->tvn_se_sess;
+	/*
+	 * Since we are running in 'demo mode' this call with generate a
+	 * struct se_node_acl for the scsiback struct se_portal_group with
+	 * the SCSI Initiator port name of the passed configfs group 'name'.
+	 */
+	tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
+				se_tpg, (unsigned char *)name);
+	if (!tv_nexus->tvn_se_sess->se_node_acl) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+			 name);
+		goto out;
+	}
+	/*
+	 * Now register the TCM pvscsi virtual I_T Nexus as active with the
+	 * call to __transport_register_session()
+	 */
+	__transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
+			tv_nexus->tvn_se_sess, tv_nexus);
+	tpg->tpg_nexus = tv_nexus;
+
+	mutex_unlock(&tpg->tv_tpg_mutex);
+	return 0;
+
+out:
+	transport_free_session(se_sess);
+	kfree(tv_nexus);
+	return -ENOMEM;
+}
+

<SNIP>

Quoted 8 lines
drivers/xen/xen-scsiback.c new file +8 -0
+
+static int scsiback_port_link(struct se_portal_group *se_tpg,
+			       struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);

drivers/xen/xen-scsiback.c line 2020

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count++;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	mutex_unlock(&scsiback_mutex);
+
+	return 0;
+}
+

AFAICT, no need to hold scsiback_mutex while incrementing tpg->tv_tpg_port_count.

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static void scsiback_port_unlink(struct se_portal_group *se_tpg,
+				  struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);

drivers/xen/xen-scsiback.c line 2035

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count--;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	mutex_unlock(&scsiback_mutex);
+}
+

Ditto here.

author maintainer
#23 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

Quoted 3 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Thank you very much for your review!

Quoted 43 lines · On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:
Quoted 30 lines

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI backend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- adapt to Linux style guide
- use target core infrastructure instead doing pure pass-through
- enable module unloading
- support SG-list in grant page(s)
- support task abort
- remove redundant struct backend
- allocate resources dynamically
- correct minor error in scsiback_fast_flush_area
- free allocated resources in case of error during I/O preparation
- remove CDB emulation, now handled by target core infrastructure

Xen related parts

---

<SNIP>

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +0 -0
new file mode 100644
index 0000000..4a0d6e3

<SNIP>

Quoted 13 lines

drivers/xen/xen-scsiback.c line 172

Quoted 9 lines
drivers/xen/xen-scsiback.c new file +9 -0
+struct scsiback_nacl {
+	/* Binary World Wide unique Port Name for pvscsi Initiator port */
+	u64 iport_wwpn;
+	/* ASCII formatted WWPN for Sas Initiator port */
+	char iport_name[VSCSI_NAMELEN];
+	/* Returned by scsiback_make_nodeacl() */
+	struct se_node_acl se_node_acl;
+};
+

Given that this code is similar to how loopback + vhost-scsi function, and uses a (locally) generated nexus for each WWPN endpoint, the scsiback_nacl and associated code will be unused should be be dropped.

Done.

Quoted 73 lines

<SNIP>

Quoted 55 lines
drivers/xen/xen-scsiback.c new file +55 -0
+static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
+			uint32_t resid, struct vscsibk_pend *pending_req)
+{
+	struct vscsiif_response *ring_res;
+	struct vscsibk_info *info = pending_req->info;
+	int notify;
+	struct scsi_sense_hdr sshdr;
+	unsigned long flags;
+	unsigned len;
+
+	spin_lock_irqsave(&info->ring_lock, flags);
+
+	ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
+	info->ring.rsp_prod_pvt++;
+
+	ring_res->rslt   = result;
+	ring_res->rqid   = pending_req->rqid;
+
+	if (sense_buffer != NULL &&
+	    scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
+				 &sshdr)) {
+		len = min_t(unsigned, 8 + sense_buffer[7],
+			    VSCSIIF_SENSE_BUFFERSIZE);
+		memcpy(ring_res->sense_buffer, sense_buffer, len);
+		ring_res->sense_len = len;
+	} else {
+		ring_res->sense_len = 0;
+	}
+
+	ring_res->residual_len = resid;
+
+	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
+	spin_unlock_irqrestore(&info->ring_lock, flags);
+
+	if (notify)
+		notify_remote_via_irq(info->irq);
+
+	if (pending_req->v2p)
+		kref_put(&pending_req->v2p->kref,
+			 scsiback_free_translation_entry);
+
+	kmem_cache_free(scsiback_cachep, pending_req);
+}
+
+static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
+{
+	struct vscsibk_info *info = pending_req->info;
+	unsigned char *sense_buffer;
+	unsigned int resid;
+	int errors;
+
+	sense_buffer = pending_req->sense_buffer;
+	resid        = pending_req->se_cmd.residual_count;
+	errors       = pending_req->result;
+

drivers/xen/xen-scsiback.c line 415

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (errors && log_print_stat)
+		scsiback_print_status(sense_buffer, errors, pending_req);
+
+	scsiback_fast_flush_area(pending_req);
+	scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
+	scsiback_put(info);
+
+	transport_generic_free_cmd(&pending_req->se_cmd, 0);
+}
+

The usage here of scsiback_do_resp_with_sense() -> kmem_cache_free() for *pending_req, and then invoking transport_generic_free_cmd() with &pending_req->se_cmd is an free after use bug..

Quoted 8 lines

So the way this should work is similar to how loopback currently does things:

- Move the kmem_cache_free() for pending_req from scsiback_do_resp_with_sense() to scsiback_release_cmd()
- Remove the transport_generic_free_cmd() from scsiback_cmd_done()
- Copy what tcm_loop_check_stop_free() does into scsiback_check_stop_free(), and remove target_put_sess_cmd()

Done.

Quoted 14 lines

drivers/xen/xen-scsiback.c line 426

Quoted 12 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
+{
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
+	int rc;
+
+	memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
+
+	memset(se_cmd, 0, sizeof(*se_cmd));
+	se_cmd->prot_pto = true;
+

No need to set prot_pto = true, as T10_PI support is not enabled..

Removed.

Quoted 56 lines
Quoted 15 lines
drivers/xen/xen-scsiback.c new file +14 -0
+	scsiback_get(pending_req->info);
+	rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
+			pending_req->sense_buffer, pending_req->v2p->lun,
+			pending_req->data_len, 0,
+			pending_req->sc_data_direction, 0,
+			pending_req->sgl, pending_req->n_sg,
+			NULL, 0, NULL, 0);
+	if (rc < 0) {
+		transport_send_check_condition_and_sense(se_cmd,
+				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
+		transport_generic_free_cmd(se_cmd, 0);
+	}
+}
+

<SNIP>

Quoted 38 lines
drivers/xen/xen-scsiback.c new file +38 -0
+
+static void scsiback_device_action(struct vscsibk_pend *pending_req,
+	enum tcm_tmreq_table act, int tag)
+{
+	int rc, err = FAILED;
+	struct scsiback_tpg *tpg = pending_req->v2p->tpg;
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct scsiback_tmr *tmr;
+
+	tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
+	if (!tmr) {
+		pr_err("xen-pvscsi: %s: kmalloc() error\n", __func__);
+		goto out;
+	}
+	init_waitqueue_head(&tmr->tmr_wait);
+
+	transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
+		tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, MSG_SIMPLE_TAG,
+		&pending_req->sense_buffer[0]);
+
+	rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
+	if (rc < 0)
+		goto out;
+
+	se_cmd->se_tmr_req->ref_task_tag = tag;
+
+	if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
+		goto out;
+
+	transport_generic_handle_tmr(se_cmd);
+	wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
+
+	err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
+		SUCCESS : FAILED;
+
+out:
+	if (tmr) {
+		transport_generic_free_cmd(&pending_req->se_cmd, 1);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 721

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+		kfree(tmr);
+	}
+
+	scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
+}

With scsiback_do_resp_with_sense() no longer freeing pending_req directly, this special case will need a kmem_cache_free()

Added.

Quoted 36 lines
Quoted 21 lines
drivers/xen/xen-scsiback.c new file +20 -0
+static int _scsiback_do_cmd_fn(struct vscsibk_info *info)
+{
+	struct vscsiif_back_ring *ring = &info->ring;
+	struct vscsiif_request *ring_req;
+	struct vscsibk_pend *pending_req;
+	RING_IDX rc, rp;
+	int err, more_to_do = 0;
+	uint32_t result;
+	uint8_t act;
+
+	rc = ring->req_cons;
+	rp = ring->sring->req_prod;
+	rmb();	/* guest system is accessing ring, too */
+
+	if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
+		rc = ring->rsp_prod_pvt;
+		pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
+			   info->domid, rp, rc, rp - rc);
+		return -EACCES;
+	}

drivers/xen/xen-scsiback.c line 822

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	while ((rc != rp)) {
+		if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
+			break;
+		pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
+		if (NULL == pending_req) {
+			more_to_do = 1;
+			break;
+		}
+

Ideally this will end up using percpu_ida descriptor pre-allocation via se_sess->sess_tag_poll + se_sess->sess_cmd_map in order to avoid the fast-path memory allocation.

Quoted 2 lines

See commit 4824d3bfb909 for an example of how this was done for vhost-scsi, but should be considered a post-merge optimization..

Okay, put on my todo list.

Quoted 17 lines
Quoted 7 lines
drivers/xen/xen-scsiback.c new file +6 -0
+		ring_req = RING_GET_REQUEST(ring, rc);
+		ring->req_cons = ++rc;
+
+		act = ring_req->act;
+		err = prepare_pending_reqs(info, ring_req, pending_req);
+		if (err) {

drivers/xen/xen-scsiback.c line 838

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+			switch (err) {
+			case -ENODEV:
+				result = DID_NO_CONNECT;
+				break;
+			default:
+				result = DRIVER_ERROR;
+				break;
+			}
+			scsiback_do_resp_with_sense(NULL, result << 24, 0,
+						    pending_req);

kmem_cache_free() added.

Quoted 5 lines
Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+			more_to_do = 1;
+			break;
+		}
+
+		switch (act) {
Quoted 8 lines

drivers/xen/xen-scsiback.c line 848

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+		case VSCSIIF_ACT_SCSI_CDB:
+			if (scsiback_gnttab_data_map(ring_req, pending_req)) {
+				scsiback_fast_flush_area(pending_req);
+				scsiback_do_resp_with_sense(NULL,
+					DRIVER_ERROR << 24, 0, pending_req);

Another special case for scsiback_do_resp_with_sense() that with the recommended changes require a kmem_cache_free() call.

And here.

Quoted 10 lines
Quoted 10 lines
drivers/xen/xen-scsiback.c new file +9 -0
+			} else {
+				scsiback_cmd_exec(pending_req);
+			}
+			break;
+		case VSCSIIF_ACT_SCSI_ABORT:
+			scsiback_device_action(pending_req, TMR_ABORT_TASK,
+				ring_req->ref_rqid);
+			break;
+		case VSCSIIF_ACT_SCSI_RESET:
Quoted 8 lines

drivers/xen/xen-scsiback.c line 863

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+			scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
+			break;
+		default:
+			pr_err_ratelimited("xen-pvscsi: invalid request\n");
+			scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
+						    0, pending_req);

Ditto here.

And here.

Quoted 84 lines
Quoted 14 lines
drivers/xen/xen-scsiback.c new file +13 -0
+			break;
+		}
+
+		/* Yield point for this unbounded loop. */
+		cond_resched();
+	}
+
+	if (RING_HAS_UNCONSUMED_REQUESTS(ring))
+		more_to_do = 1;
+
+	return more_to_do;
+}
+

<SNIP>

Quoted 67 lines
drivers/xen/xen-scsiback.c new file +67 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+	struct scsiback_nacl *nacl;
+
+	nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+	if (!nacl) {
+		pr_err("Unable to allocate struct scsiback_nacl\n");
+		return NULL;
+	}
+
+	return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+			     struct se_node_acl *se_nacl)
+{
+	struct scsiback_nacl *nacl = container_of(se_nacl,
+			struct scsiback_nacl, se_node_acl);
+	kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+	return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+		       struct config_group *group,
+		       const char *name)
+{
+	struct se_node_acl *se_nacl, *se_nacl_new;
+	struct scsiback_nacl *nacl;
+	u64 wwpn = 0;
+	u32 nexus_depth;
+
+	se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+	if (!se_nacl_new)
+		return ERR_PTR(-ENOMEM);
+
+	nexus_depth = 1;
+	/*
+	 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+	 * when converting a NodeACL from demo mode -> explict
+	 */
+	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+				name, nexus_depth);
+	if (IS_ERR(se_nacl)) {
+		scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+		return se_nacl;
+	}
+	/*
+	 * Locate our struct scsiback_nacl and set the FC Nport WWPN
+	 */
+	nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+	nacl->iport_wwpn = wwpn;
+
+	return se_nacl;
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+	struct scsiback_nacl *nacl = container_of(se_acl,
+				struct scsiback_nacl, se_node_acl);
+	core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1582

Quoted 3 lines
drivers/xen/xen-scsiback.c new file +3 -0
+	kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

Deleted.

Quoted 9 lines

drivers/xen/xen-scsiback.c line 1634

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static int scsiback_check_stop_free(struct se_cmd *se_cmd)
+{
+	return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
+}
+

As mentioned above, scsiback_check_stop_free() should follow what tcm_loop_check_stop_free() does here..

Yep.

Quoted 7 lines

drivers/xen/xen-scsiback.c line 1638

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +4 -0
+static void scsiback_release_cmd(struct se_cmd *se_cmd)
+{
+}
+

Move the kmem_cache_free() for pending_req here..

Done.

Quoted 26 lines
Quoted 26 lines
drivers/xen/xen-scsiback.c new file +25 -0
+static int scsiback_shutdown_session(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static void scsiback_close_session(struct se_session *se_sess)
+{
+}
+
+static u32 scsiback_sess_get_index(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static int scsiback_write_pending(struct se_cmd *se_cmd)
+{
+	/* Go ahead and process the write immediately */
+	target_execute_cmd(se_cmd);
+
+	return 0;
+}
+
+static int scsiback_write_pending_status(struct se_cmd *se_cmd)
+{
+	return 0;
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1669

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+}
+
+static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
+{
+}
+

Safe to drop this no-op too.

Okay.

Quoted 57 lines
Quoted 57 lines
drivers/xen/xen-scsiback.c new file +56 -0
+static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	return pending_req->rqid;
+}
+
+static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
+{
+	return 0;
+}
+
+static int scsiback_queue_data_in(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	pending_req->result = SAM_STAT_GOOD;
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static int scsiback_queue_status(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	if (se_cmd->sense_buffer &&
+	    ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
+	     (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
+		pending_req->result = (DRIVER_SENSE << 24) |
+				      SAM_STAT_CHECK_CONDITION;
+	else
+		pending_req->result = se_cmd->scsi_status;
+
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
+}
+
+static void scsiback_aborted_task(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
Quoted 8 lines

drivers/xen/xen-scsiback.c

Quoted 2 lines
drivers/xen/xen-scsiback.c new file +2 -0
+}
+

scsiback_aborted_task() should just return here, instead of the atomic_set + wake_up(). Eg: ABORT_TASK is already calling -> queue_tm_rsp(), and the ->aborted_task() callback is specifically for cleaning up any fabric specific descriptor resources, before ->release_cmd() gets called.

Okay.

Quoted 65 lines
Quoted 65 lines
drivers/xen/xen-scsiback.c new file +64 -0
+static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
+					     char *page)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	ssize_t rb;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return rb;
+}
+
+static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
+					      const char *page, size_t count)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	int len;
+
+	if (strlen(page) >= VSCSI_NAMELEN) {
+		pr_err("param alias: %s, exceeds max: %d\n", page,
+			VSCSI_NAMELEN);
+		return -EINVAL;
+	}
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
+	if (tpg->param_alias[len - 1] == '\n')
+		tpg->param_alias[len - 1] = '\0';
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return count;
+}
+
+TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
+
+static struct configfs_attribute *scsiback_param_attrs[] = {
+	&scsiback_tpg_param_alias.attr,
+	NULL,
+};
+
+static int scsiback_make_nexus(struct scsiback_tpg *tpg,
+				const char *name)
+{
+	struct se_portal_group *se_tpg;
+	struct se_session *se_sess;
+	struct scsiback_nexus *tv_nexus;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	if (tpg->tpg_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("tpg->tpg_nexus already exists\n");
+		return -EEXIST;
+	}
+	se_tpg = &tpg->se_tpg;
+
+	tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
+	if (!tv_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_err("Unable to allocate struct scsiback_nexus\n");
+		return -ENOMEM;
+	}
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1796

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+	/*
+	 *  Initialize the struct se_session pointer
+	 */
+	tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_DIN_PASS |
+						       TARGET_PROT_DOUT_PASS);

TARGET_PROT_D*_PASS incorrectly declares T10 PI support here, which should be TARGET_PROT_NORMAL instead.

Okay.

Quoted 16 lines

<SNIP>

Quoted 13 lines
drivers/xen/xen-scsiback.c new file +13 -0
+
+static int scsiback_port_link(struct se_portal_group *se_tpg,
+			       struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count++;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
Quoted 8 lines

drivers/xen/xen-scsiback.c line 2020

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+	mutex_unlock(&scsiback_mutex);
+
+	return 0;
+}
+

AFAICT, no need to hold scsiback_mutex while incrementing tpg->tv_tpg_port_count.

So there is a guarantee that port_link and port_unlink are never called in parallel?

Quoted 10 lines
Quoted 10 lines
drivers/xen/xen-scsiback.c new file +9 -0
+static void scsiback_port_unlink(struct se_portal_group *se_tpg,
+				  struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 2035

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+	tpg->tv_tpg_port_count--;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	mutex_unlock(&scsiback_mutex);
+}
+

Ditto here.

Juergen

author maintainer
#30 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Nicholas A. Bellinger
Quoted 3 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Thank you very much for your review!

Quoted 43 lines · On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:
Quoted 30 lines

From: Juergen Gross <[email protected]>

Introduces the XEN pvSCSI backend. With pvSCSI it is possible for a XEN domU to issue SCSI commands to a SCSI LUN assigned to that domU. The SCSI commands are passed to the pvSCSI backend in a driver domain (usually Dom0) which is owner of the physical device. This allows e.g. to use SCSI tape drives in a XEN domU.

The code is taken from the pvSCSI implementation in XEN done by Fujitsu based on Linux kernel 2.6.18.

Changes from the original version are:
- port to upstream kernel
- put all code in just one source file
- adapt to Linux style guide
- use target core infrastructure instead doing pure pass-through
- enable module unloading
- support SG-list in grant page(s)
- support task abort
- remove redundant struct backend
- allocate resources dynamically
- correct minor error in scsiback_fast_flush_area
- free allocated resources in case of error during I/O preparation
- remove CDB emulation, now handled by target core infrastructure

Xen related parts

---

<SNIP>

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +0 -0
new file mode 100644
index 0000000..4a0d6e3

<SNIP>

Quoted 13 lines

drivers/xen/xen-scsiback.c line 172

Quoted 9 lines
drivers/xen/xen-scsiback.c new file +9 -0
+struct scsiback_nacl {
+	/* Binary World Wide unique Port Name for pvscsi Initiator port */
+	u64 iport_wwpn;
+	/* ASCII formatted WWPN for Sas Initiator port */
+	char iport_name[VSCSI_NAMELEN];
+	/* Returned by scsiback_make_nodeacl() */
+	struct se_node_acl se_node_acl;
+};
+

Given that this code is similar to how loopback + vhost-scsi function, and uses a (locally) generated nexus for each WWPN endpoint, the scsiback_nacl and associated code will be unused should be be dropped.

Done.

Quoted 73 lines

<SNIP>

Quoted 55 lines
drivers/xen/xen-scsiback.c new file +55 -0
+static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
+			uint32_t resid, struct vscsibk_pend *pending_req)
+{
+	struct vscsiif_response *ring_res;
+	struct vscsibk_info *info = pending_req->info;
+	int notify;
+	struct scsi_sense_hdr sshdr;
+	unsigned long flags;
+	unsigned len;
+
+	spin_lock_irqsave(&info->ring_lock, flags);
+
+	ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
+	info->ring.rsp_prod_pvt++;
+
+	ring_res->rslt   = result;
+	ring_res->rqid   = pending_req->rqid;
+
+	if (sense_buffer != NULL &&
+	    scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
+				 &sshdr)) {
+		len = min_t(unsigned, 8 + sense_buffer[7],
+			    VSCSIIF_SENSE_BUFFERSIZE);
+		memcpy(ring_res->sense_buffer, sense_buffer, len);
+		ring_res->sense_len = len;
+	} else {
+		ring_res->sense_len = 0;
+	}
+
+	ring_res->residual_len = resid;
+
+	RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
+	spin_unlock_irqrestore(&info->ring_lock, flags);
+
+	if (notify)
+		notify_remote_via_irq(info->irq);
+
+	if (pending_req->v2p)
+		kref_put(&pending_req->v2p->kref,
+			 scsiback_free_translation_entry);
+
+	kmem_cache_free(scsiback_cachep, pending_req);
+}
+
+static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
+{
+	struct vscsibk_info *info = pending_req->info;
+	unsigned char *sense_buffer;
+	unsigned int resid;
+	int errors;
+
+	sense_buffer = pending_req->sense_buffer;
+	resid        = pending_req->se_cmd.residual_count;
+	errors       = pending_req->result;
+

drivers/xen/xen-scsiback.c line 415

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+	if (errors && log_print_stat)
+		scsiback_print_status(sense_buffer, errors, pending_req);
+
+	scsiback_fast_flush_area(pending_req);
+	scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
+	scsiback_put(info);
+
+	transport_generic_free_cmd(&pending_req->se_cmd, 0);
+}
+

The usage here of scsiback_do_resp_with_sense() -> kmem_cache_free() for *pending_req, and then invoking transport_generic_free_cmd() with &pending_req->se_cmd is an free after use bug..

Quoted 8 lines

So the way this should work is similar to how loopback currently does things:

- Move the kmem_cache_free() for pending_req from scsiback_do_resp_with_sense() to scsiback_release_cmd()
- Remove the transport_generic_free_cmd() from scsiback_cmd_done()
- Copy what tcm_loop_check_stop_free() does into scsiback_check_stop_free(), and remove target_put_sess_cmd()

Done.

Quoted 14 lines

drivers/xen/xen-scsiback.c line 426

Quoted 12 lines
drivers/xen/xen-scsiback.c new file +11 -0
+static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
+{
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
+	int rc;
+
+	memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
+
+	memset(se_cmd, 0, sizeof(*se_cmd));
+	se_cmd->prot_pto = true;
+

No need to set prot_pto = true, as T10_PI support is not enabled..

Removed.

Quoted 56 lines
Quoted 15 lines
drivers/xen/xen-scsiback.c new file +14 -0
+	scsiback_get(pending_req->info);
+	rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
+			pending_req->sense_buffer, pending_req->v2p->lun,
+			pending_req->data_len, 0,
+			pending_req->sc_data_direction, 0,
+			pending_req->sgl, pending_req->n_sg,
+			NULL, 0, NULL, 0);
+	if (rc < 0) {
+		transport_send_check_condition_and_sense(se_cmd,
+				TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
+		transport_generic_free_cmd(se_cmd, 0);
+	}
+}
+

<SNIP>

Quoted 38 lines
drivers/xen/xen-scsiback.c new file +38 -0
+
+static void scsiback_device_action(struct vscsibk_pend *pending_req,
+	enum tcm_tmreq_table act, int tag)
+{
+	int rc, err = FAILED;
+	struct scsiback_tpg *tpg = pending_req->v2p->tpg;
+	struct se_cmd *se_cmd = &pending_req->se_cmd;
+	struct scsiback_tmr *tmr;
+
+	tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
+	if (!tmr) {
+		pr_err("xen-pvscsi: %s: kmalloc() error\n", __func__);
+		goto out;
+	}
+	init_waitqueue_head(&tmr->tmr_wait);
+
+	transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
+		tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, MSG_SIMPLE_TAG,
+		&pending_req->sense_buffer[0]);
+
+	rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
+	if (rc < 0)
+		goto out;
+
+	se_cmd->se_tmr_req->ref_task_tag = tag;
+
+	if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
+		goto out;
+
+	transport_generic_handle_tmr(se_cmd);
+	wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
+
+	err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
+		SUCCESS : FAILED;
+
+out:
+	if (tmr) {
+		transport_generic_free_cmd(&pending_req->se_cmd, 1);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 721

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+		kfree(tmr);
+	}
+
+	scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
+}

With scsiback_do_resp_with_sense() no longer freeing pending_req directly, this special case will need a kmem_cache_free()

Added.

Quoted 36 lines
Quoted 21 lines
drivers/xen/xen-scsiback.c new file +20 -0
+static int _scsiback_do_cmd_fn(struct vscsibk_info *info)
+{
+	struct vscsiif_back_ring *ring = &info->ring;
+	struct vscsiif_request *ring_req;
+	struct vscsibk_pend *pending_req;
+	RING_IDX rc, rp;
+	int err, more_to_do = 0;
+	uint32_t result;
+	uint8_t act;
+
+	rc = ring->req_cons;
+	rp = ring->sring->req_prod;
+	rmb();	/* guest system is accessing ring, too */
+
+	if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
+		rc = ring->rsp_prod_pvt;
+		pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
+			   info->domid, rp, rc, rp - rc);
+		return -EACCES;
+	}

drivers/xen/xen-scsiback.c line 822

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+
+	while ((rc != rp)) {
+		if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
+			break;
+		pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
+		if (NULL == pending_req) {
+			more_to_do = 1;
+			break;
+		}
+

Ideally this will end up using percpu_ida descriptor pre-allocation via se_sess->sess_tag_poll + se_sess->sess_cmd_map in order to avoid the fast-path memory allocation.

Quoted 2 lines

See commit 4824d3bfb909 for an example of how this was done for vhost-scsi, but should be considered a post-merge optimization..

Okay, put on my todo list.

Quoted 17 lines
Quoted 7 lines
drivers/xen/xen-scsiback.c new file +6 -0
+		ring_req = RING_GET_REQUEST(ring, rc);
+		ring->req_cons = ++rc;
+
+		act = ring_req->act;
+		err = prepare_pending_reqs(info, ring_req, pending_req);
+		if (err) {

drivers/xen/xen-scsiback.c line 838

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+			switch (err) {
+			case -ENODEV:
+				result = DID_NO_CONNECT;
+				break;
+			default:
+				result = DRIVER_ERROR;
+				break;
+			}
+			scsiback_do_resp_with_sense(NULL, result << 24, 0,
+						    pending_req);

kmem_cache_free() added.

Quoted 5 lines
Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+			more_to_do = 1;
+			break;
+		}
+
+		switch (act) {
Quoted 8 lines

drivers/xen/xen-scsiback.c line 848

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+		case VSCSIIF_ACT_SCSI_CDB:
+			if (scsiback_gnttab_data_map(ring_req, pending_req)) {
+				scsiback_fast_flush_area(pending_req);
+				scsiback_do_resp_with_sense(NULL,
+					DRIVER_ERROR << 24, 0, pending_req);

Another special case for scsiback_do_resp_with_sense() that with the recommended changes require a kmem_cache_free() call.

And here.

Quoted 10 lines
Quoted 10 lines
drivers/xen/xen-scsiback.c new file +9 -0
+			} else {
+				scsiback_cmd_exec(pending_req);
+			}
+			break;
+		case VSCSIIF_ACT_SCSI_ABORT:
+			scsiback_device_action(pending_req, TMR_ABORT_TASK,
+				ring_req->ref_rqid);
+			break;
+		case VSCSIIF_ACT_SCSI_RESET:
Quoted 8 lines

drivers/xen/xen-scsiback.c line 863

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+			scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
+			break;
+		default:
+			pr_err_ratelimited("xen-pvscsi: invalid request\n");
+			scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
+						    0, pending_req);

Ditto here.

And here.

Quoted 84 lines
Quoted 14 lines
drivers/xen/xen-scsiback.c new file +13 -0
+			break;
+		}
+
+		/* Yield point for this unbounded loop. */
+		cond_resched();
+	}
+
+	if (RING_HAS_UNCONSUMED_REQUESTS(ring))
+		more_to_do = 1;
+
+	return more_to_do;
+}
+

<SNIP>

Quoted 67 lines
drivers/xen/xen-scsiback.c new file +67 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+	struct scsiback_nacl *nacl;
+
+	nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+	if (!nacl) {
+		pr_err("Unable to allocate struct scsiback_nacl\n");
+		return NULL;
+	}
+
+	return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+			     struct se_node_acl *se_nacl)
+{
+	struct scsiback_nacl *nacl = container_of(se_nacl,
+			struct scsiback_nacl, se_node_acl);
+	kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+	return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+		       struct config_group *group,
+		       const char *name)
+{
+	struct se_node_acl *se_nacl, *se_nacl_new;
+	struct scsiback_nacl *nacl;
+	u64 wwpn = 0;
+	u32 nexus_depth;
+
+	se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+	if (!se_nacl_new)
+		return ERR_PTR(-ENOMEM);
+
+	nexus_depth = 1;
+	/*
+	 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+	 * when converting a NodeACL from demo mode -> explict
+	 */
+	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+				name, nexus_depth);
+	if (IS_ERR(se_nacl)) {
+		scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+		return se_nacl;
+	}
+	/*
+	 * Locate our struct scsiback_nacl and set the FC Nport WWPN
+	 */
+	nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+	nacl->iport_wwpn = wwpn;
+
+	return se_nacl;
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+	struct scsiback_nacl *nacl = container_of(se_acl,
+				struct scsiback_nacl, se_node_acl);
+	core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1582

Quoted 3 lines
drivers/xen/xen-scsiback.c new file +3 -0
+	kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

Deleted.

Quoted 9 lines

drivers/xen/xen-scsiback.c line 1634

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +5 -0
+static int scsiback_check_stop_free(struct se_cmd *se_cmd)
+{
+	return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
+}
+

As mentioned above, scsiback_check_stop_free() should follow what tcm_loop_check_stop_free() does here..

Yep.

Quoted 7 lines

drivers/xen/xen-scsiback.c line 1638

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +4 -0
+static void scsiback_release_cmd(struct se_cmd *se_cmd)
+{
+}
+

Move the kmem_cache_free() for pending_req here..

Done.

Quoted 26 lines
Quoted 26 lines
drivers/xen/xen-scsiback.c new file +25 -0
+static int scsiback_shutdown_session(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static void scsiback_close_session(struct se_session *se_sess)
+{
+}
+
+static u32 scsiback_sess_get_index(struct se_session *se_sess)
+{
+	return 0;
+}
+
+static int scsiback_write_pending(struct se_cmd *se_cmd)
+{
+	/* Go ahead and process the write immediately */
+	target_execute_cmd(se_cmd);
+
+	return 0;
+}
+
+static int scsiback_write_pending_status(struct se_cmd *se_cmd)
+{
+	return 0;
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1669

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+}
+
+static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
+{
+}
+

Safe to drop this no-op too.

Okay.

Quoted 57 lines
Quoted 57 lines
drivers/xen/xen-scsiback.c new file +56 -0
+static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	return pending_req->rqid;
+}
+
+static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
+{
+	return 0;
+}
+
+static int scsiback_queue_data_in(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	pending_req->result = SAM_STAT_GOOD;
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static int scsiback_queue_status(struct se_cmd *se_cmd)
+{
+	struct vscsibk_pend *pending_req = container_of(se_cmd,
+				struct vscsibk_pend, se_cmd);
+
+	if (se_cmd->sense_buffer &&
+	    ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
+	     (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
+		pending_req->result = (DRIVER_SENSE << 24) |
+				      SAM_STAT_CHECK_CONDITION;
+	else
+		pending_req->result = se_cmd->scsi_status;
+
+	scsiback_cmd_done(pending_req);
+	return 0;
+}
+
+static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
+}
+
+static void scsiback_aborted_task(struct se_cmd *se_cmd)
+{
+	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
+	struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
+
+	atomic_set(&tmr->tmr_complete, 1);
+	wake_up(&tmr->tmr_wait);
Quoted 8 lines

drivers/xen/xen-scsiback.c

Quoted 2 lines
drivers/xen/xen-scsiback.c new file +2 -0
+}
+

scsiback_aborted_task() should just return here, instead of the atomic_set + wake_up(). Eg: ABORT_TASK is already calling -> queue_tm_rsp(), and the ->aborted_task() callback is specifically for cleaning up any fabric specific descriptor resources, before ->release_cmd() gets called.

Okay.

Quoted 65 lines
Quoted 65 lines
drivers/xen/xen-scsiback.c new file +64 -0
+static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
+					     char *page)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	ssize_t rb;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return rb;
+}
+
+static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
+					      const char *page, size_t count)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
+						se_tpg);
+	int len;
+
+	if (strlen(page) >= VSCSI_NAMELEN) {
+		pr_err("param alias: %s, exceeds max: %d\n", page,
+			VSCSI_NAMELEN);
+		return -EINVAL;
+	}
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
+	if (tpg->param_alias[len - 1] == '\n')
+		tpg->param_alias[len - 1] = '\0';
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	return count;
+}
+
+TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
+
+static struct configfs_attribute *scsiback_param_attrs[] = {
+	&scsiback_tpg_param_alias.attr,
+	NULL,
+};
+
+static int scsiback_make_nexus(struct scsiback_tpg *tpg,
+				const char *name)
+{
+	struct se_portal_group *se_tpg;
+	struct se_session *se_sess;
+	struct scsiback_nexus *tv_nexus;
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	if (tpg->tpg_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("tpg->tpg_nexus already exists\n");
+		return -EEXIST;
+	}
+	se_tpg = &tpg->se_tpg;
+
+	tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
+	if (!tv_nexus) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_err("Unable to allocate struct scsiback_nexus\n");
+		return -ENOMEM;
+	}
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1796

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+	/*
+	 *  Initialize the struct se_session pointer
+	 */
+	tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_DIN_PASS |
+						       TARGET_PROT_DOUT_PASS);

TARGET_PROT_D*_PASS incorrectly declares T10 PI support here, which should be TARGET_PROT_NORMAL instead.

Okay.

Quoted 16 lines

<SNIP>

Quoted 13 lines
drivers/xen/xen-scsiback.c new file +13 -0
+
+static int scsiback_port_link(struct se_portal_group *se_tpg,
+			       struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count++;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
Quoted 8 lines

drivers/xen/xen-scsiback.c line 2020

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+	mutex_unlock(&scsiback_mutex);
+
+	return 0;
+}
+

AFAICT, no need to hold scsiback_mutex while incrementing tpg->tv_tpg_port_count.

So there is a guarantee that port_link and port_unlink are never called in parallel?

Quoted 10 lines
Quoted 10 lines
drivers/xen/xen-scsiback.c new file +9 -0
+static void scsiback_port_unlink(struct se_portal_group *se_tpg,
+				  struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 2035

Quoted 6 lines
drivers/xen/xen-scsiback.c new file +6 -0
+	tpg->tv_tpg_port_count--;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
+	mutex_unlock(&scsiback_mutex);
+}
+

Ditto here.

Juergen

author maintainer
#24 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 81 lines · Juergen Gross
Quoted 71 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Quoted 67 lines
drivers/xen/xen-scsiback.c new file +67 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+    struct scsiback_nacl *nacl;
+
+    nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+    if (!nacl) {
+        pr_err("Unable to allocate struct scsiback_nacl\n");
+        return NULL;
+    }
+
+    return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+                 struct se_node_acl *se_nacl)
+{
+    struct scsiback_nacl *nacl = container_of(se_nacl,
+            struct scsiback_nacl, se_node_acl);
+    kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+    return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+               struct config_group *group,
+               const char *name)
+{
+    struct se_node_acl *se_nacl, *se_nacl_new;
+    struct scsiback_nacl *nacl;
+    u64 wwpn = 0;
+    u32 nexus_depth;
+
+    se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+    if (!se_nacl_new)
+        return ERR_PTR(-ENOMEM);
+
+    nexus_depth = 1;
+    /*
+     * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+     * when converting a NodeACL from demo mode -> explict
+     */
+    se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+                name, nexus_depth);
+    if (IS_ERR(se_nacl)) {
+        scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+        return se_nacl;
+    }
+    /*
+     * Locate our struct scsiback_nacl and set the FC Nport WWPN
+     */
+    nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+    nacl->iport_wwpn = wwpn;
+
+    return se_nacl;
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+    struct scsiback_nacl *nacl = container_of(se_acl,
+                struct scsiback_nacl, se_node_acl);
+    core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1582

Quoted 3 lines
drivers/xen/xen-scsiback.c new file +3 -0
+    kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

Quoted 1 line

Deleted.

target_fabric_tf_ops_check() complains about missing tpg_alloc_fabric_acl and tpg_release_fabric_acl.

Quoted 8 lines
Quoted 6 lines

drivers/xen/xen-scsiback.c line 1669

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
+{
+}
+

Safe to drop this no-op too.

Okay.

target_fabric_tf_ops_check() wants this to be set, too.

Juergen

author maintainer
#25 lore raw

Re: Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 81 lines · Juergen Gross
Quoted 71 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Quoted 67 lines
drivers/xen/xen-scsiback.c new file +67 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+    struct scsiback_nacl *nacl;
+
+    nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+    if (!nacl) {
+        pr_err("Unable to allocate struct scsiback_nacl\n");
+        return NULL;
+    }
+
+    return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+                 struct se_node_acl *se_nacl)
+{
+    struct scsiback_nacl *nacl = container_of(se_nacl,
+            struct scsiback_nacl, se_node_acl);
+    kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+    return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+               struct config_group *group,
+               const char *name)
+{
+    struct se_node_acl *se_nacl, *se_nacl_new;
+    struct scsiback_nacl *nacl;
+    u64 wwpn = 0;
+    u32 nexus_depth;
+
+    se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+    if (!se_nacl_new)
+        return ERR_PTR(-ENOMEM);
+
+    nexus_depth = 1;
+    /*
+     * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+     * when converting a NodeACL from demo mode -> explict
+     */
+    se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+                name, nexus_depth);
+    if (IS_ERR(se_nacl)) {
+        scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+        return se_nacl;
+    }
+    /*
+     * Locate our struct scsiback_nacl and set the FC Nport WWPN
+     */
+    nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+    nacl->iport_wwpn = wwpn;
+
+    return se_nacl;
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+    struct scsiback_nacl *nacl = container_of(se_acl,
+                struct scsiback_nacl, se_node_acl);
+    core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1582

Quoted 3 lines
drivers/xen/xen-scsiback.c new file +3 -0
+    kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

Quoted 1 line

Deleted.

target_fabric_tf_ops_check() complains about missing tpg_alloc_fabric_acl and tpg_release_fabric_acl.

Quoted 8 lines
Quoted 6 lines

drivers/xen/xen-scsiback.c line 1669

Quoted 4 lines
drivers/xen/xen-scsiback.c new file +4 -0
+static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
+{
+}
+

Safe to drop this no-op too.

Okay.

target_fabric_tf_ops_check() wants this to be set, too.

Juergen

author maintainer
#31 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Nicholas A. Bellinger

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 11 lines
Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+	if (IS_ERR(tv_nexus->tvn_se_sess)) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		kfree(tv_nexus);
+		return -ENOMEM;
+	}
+	se_sess = tv_nexus->tvn_se_sess;
+	/*
+	 * Since we are running in 'demo mode' this call with generate a
+	 * struct se_node_acl for the scsiback struct se_portal_group with
+	 * the SCSI Initiator port name of the passed configfs group 'name'.
+	 */
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines
drivers/xen/xen-scsiback.c new file +8 -0
+	tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
+				se_tpg, (unsigned char *)name);
+	if (!tv_nexus->tvn_se_sess->se_node_acl) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+			 name);
+		goto out;
+	}

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

I suppose the name of the nexus is just for reference and should be stored somewhere to be able to print it in _show_nexus()?

Juergen

author maintainer
#32 lore raw

Re: Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Nicholas A. Bellinger

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 11 lines
Quoted 11 lines
drivers/xen/xen-scsiback.c new file +11 -0
+	if (IS_ERR(tv_nexus->tvn_se_sess)) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		kfree(tv_nexus);
+		return -ENOMEM;
+	}
+	se_sess = tv_nexus->tvn_se_sess;
+	/*
+	 * Since we are running in 'demo mode' this call with generate a
+	 * struct se_node_acl for the scsiback struct se_portal_group with
+	 * the SCSI Initiator port name of the passed configfs group 'name'.
+	 */
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines
drivers/xen/xen-scsiback.c new file +8 -0
+	tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
+				se_tpg, (unsigned char *)name);
+	if (!tv_nexus->tvn_se_sess->se_node_acl) {
+		mutex_unlock(&tpg->tv_tpg_mutex);
+		pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+			 name);
+		goto out;
+	}

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

I suppose the name of the nexus is just for reference and should be stored somewhere to be able to print it in _show_nexus()?

Juergen

author maintainer
#33 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

Quoted 35 lines · Juergen Gross

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 13 lines
Quoted 13 lines
drivers/xen/xen-scsiback.c new file +10 -0
+    if (IS_ERR(tv_nexus->tvn_se_sess)) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        kfree(tv_nexus);
+        return -ENOMEM;
+    }
+    se_sess = tv_nexus->tvn_se_sess;
+    /*
+     * Since we are running in 'demo mode' this call with generate a
+     * struct se_node_acl for the scsiback struct se_portal_group with
+     * the SCSI Initiator port name of the passed configfs group

'name'.

(quoted diff) +2 -0
+     */
+    tv_nexus->tvn_se_sess->se_node_acl =
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines

core_tpg_check_initiator_node_acl(

drivers/xen/xen-scsiback.c new file +7 -0
+                se_tpg, (unsigned char *)name);
+    if (!tv_nexus->tvn_se_sess->se_node_acl) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+             name);
+        goto out;
+    }
Quoted 3 lines

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

Obviously I can't. transport_lookup_cmd_lun() calls right at start spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags); resulting in a page fault...

I guess the hint to just throw away all the node_acl related stuff has to be adjusted somehow...

So: what is needed, what can be removed?

Juergen

author maintainer
#34 lore raw

Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 35 lines · Juergen Gross

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 13 lines
Quoted 13 lines
drivers/xen/xen-scsiback.c new file +10 -0
+    if (IS_ERR(tv_nexus->tvn_se_sess)) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        kfree(tv_nexus);
+        return -ENOMEM;
+    }
+    se_sess = tv_nexus->tvn_se_sess;
+    /*
+     * Since we are running in 'demo mode' this call with generate a
+     * struct se_node_acl for the scsiback struct se_portal_group with
+     * the SCSI Initiator port name of the passed configfs group

'name'.

(quoted diff) +2 -0
+     */
+    tv_nexus->tvn_se_sess->se_node_acl =
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines

core_tpg_check_initiator_node_acl(

drivers/xen/xen-scsiback.c new file +7 -0
+                se_tpg, (unsigned char *)name);
+    if (!tv_nexus->tvn_se_sess->se_node_acl) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+             name);
+        goto out;
+    }
Quoted 3 lines

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

Obviously I can't. transport_lookup_cmd_lun() calls right at start spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags); resulting in a page fault...

I guess the hint to just throw away all the node_acl related stuff has to be adjusted somehow...

So: what is needed, what can be removed?

Juergen

#28 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 1 line · Juergen Gross

On 08/12/2014 11:13 PM, Nicholas A. Bellinger wrote:

<SNIP>

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
drivers/xen/xen-scsiback.c new file +13 -0
+
+static int scsiback_port_link(struct se_portal_group *se_tpg,
+			       struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count++;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
Quoted 8 lines

drivers/xen/xen-scsiback.c line 2020

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+	mutex_unlock(&scsiback_mutex);
+
+	return 0;
+}
+

AFAICT, no need to hold scsiback_mutex while incrementing tpg->tv_tpg_port_count.

Quoted 3 lines

So there is a guarantee that port_link and port_unlink are never called in parallel?

Correct. configfs_symlink() only calls create_link() once type->ct_item_ops->allow_link() -> target_fabric_port_link() -> TFO->fabric_post_link() has successfully completed, effectively preventing configfs_unlink() + subsequent TFO->fabric_pre_unlink() execution until after configfs_symlink() completes.

--nab

#29 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 1 line · Juergen Gross

On 08/12/2014 11:13 PM, Nicholas A. Bellinger wrote:

<SNIP>

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
drivers/xen/xen-scsiback.c new file +13 -0
+
+static int scsiback_port_link(struct se_portal_group *se_tpg,
+			       struct se_lun *lun)
+{
+	struct scsiback_tpg *tpg = container_of(se_tpg,
+				struct scsiback_tpg, se_tpg);
+
+	mutex_lock(&scsiback_mutex);
+
+	mutex_lock(&tpg->tv_tpg_mutex);
+	tpg->tv_tpg_port_count++;
+	mutex_unlock(&tpg->tv_tpg_mutex);
+
Quoted 8 lines

drivers/xen/xen-scsiback.c line 2020

Quoted 5 lines
drivers/xen/xen-scsiback.c new file +5 -0
+	mutex_unlock(&scsiback_mutex);
+
+	return 0;
+}
+

AFAICT, no need to hold scsiback_mutex while incrementing tpg->tv_tpg_port_count.

Quoted 3 lines

So there is a guarantee that port_link and port_unlink are never called in parallel?

Correct. configfs_symlink() only calls create_link() once type->ct_item_ops->allow_link() -> target_fabric_port_link() -> TFO->fabric_post_link() has successfully completed, effectively preventing configfs_unlink() + subsequent TFO->fabric_pre_unlink() execution until after configfs_symlink() completes.

--nab

#26 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 82 lines · Juergen Gross
Quoted 81 lines · Juergen Gross
Quoted 79 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Quoted 60 lines
drivers/xen/xen-scsiback.c new file +60 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+    struct scsiback_nacl *nacl;
+
+    nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+    if (!nacl) {
+        pr_err("Unable to allocate struct scsiback_nacl\n");
+        return NULL;
+    }
+
+    return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+                 struct se_node_acl *se_nacl)
+{
+    struct scsiback_nacl *nacl = container_of(se_nacl,
+            struct scsiback_nacl, se_node_acl);
+    kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+    return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+               struct config_group *group,
+               const char *name)
+{
+    struct se_node_acl *se_nacl, *se_nacl_new;
+    struct scsiback_nacl *nacl;
+    u64 wwpn = 0;
+    u32 nexus_depth;
+
+    se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+    if (!se_nacl_new)
+        return ERR_PTR(-ENOMEM);
+
+    nexus_depth = 1;
+    /*
+     * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+     * when converting a NodeACL from demo mode -> explict
+     */
+    se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+                name, nexus_depth);
+    if (IS_ERR(se_nacl)) {
+        scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+        return se_nacl;
+    }
+    /*
+     * Locate our struct scsiback_nacl and set the FC Nport WWPN
+     */
+    nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+    nacl->iport_wwpn = wwpn;
+
+    return se_nacl;

drivers/xen/xen-scsiback.c line 1629

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+    struct scsiback_nacl *nacl = container_of(se_acl,
+                struct scsiback_nacl, se_node_acl);
+    core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
+    kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

Quoted 5 lines
Quoted 1 line

Deleted.

target_fabric_tf_ops_check() complains about missing tpg_alloc_fabric_acl and tpg_release_fabric_acl.

Sorry, yes, you still need a struct scsiback_nacl that contains struct se_node_acl, along with these two callbacks for allocating + releasing the associated scsiback_nacl memory.

This is required because TFO->tpg_alloc_fabric_acl() is called to allocate struct scsiback_nacl at core_tpg_add_initiator_node_acl() time when TFO->check_demo_mode() == 1 has been set.

Normal fabric NodeACLs (eg: ones not generated in the kernel by demo-mode) hang off configfs, so the TFO->tpg_*_fabric_acl() call is driven from TFO->fabric_make_nodeacl(), which is the part that can safely be dropped here.

--nab

#27 lore raw

Re: Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 82 lines · Juergen Gross
Quoted 81 lines · Juergen Gross
Quoted 79 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

Quoted 60 lines
drivers/xen/xen-scsiback.c new file +60 -0
+static struct se_node_acl *
+scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
+{
+    struct scsiback_nacl *nacl;
+
+    nacl = kzalloc(sizeof(struct scsiback_nacl), GFP_KERNEL);
+    if (!nacl) {
+        pr_err("Unable to allocate struct scsiback_nacl\n");
+        return NULL;
+    }
+
+    return &nacl->se_node_acl;
+}
+
+static void
+scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
+                 struct se_node_acl *se_nacl)
+{
+    struct scsiback_nacl *nacl = container_of(se_nacl,
+            struct scsiback_nacl, se_node_acl);
+    kfree(nacl);
+}
+
+static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
+{
+    return 1;
+}
+
+static struct se_node_acl *
+scsiback_make_nodeacl(struct se_portal_group *se_tpg,
+               struct config_group *group,
+               const char *name)
+{
+    struct se_node_acl *se_nacl, *se_nacl_new;
+    struct scsiback_nacl *nacl;
+    u64 wwpn = 0;
+    u32 nexus_depth;
+
+    se_nacl_new = scsiback_alloc_fabric_acl(se_tpg);
+    if (!se_nacl_new)
+        return ERR_PTR(-ENOMEM);
+
+    nexus_depth = 1;
+    /*
+     * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
+     * when converting a NodeACL from demo mode -> explict
+     */
+    se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
+                name, nexus_depth);
+    if (IS_ERR(se_nacl)) {
+        scsiback_release_fabric_acl(se_tpg, se_nacl_new);
+        return se_nacl;
+    }
+    /*
+     * Locate our struct scsiback_nacl and set the FC Nport WWPN
+     */
+    nacl = container_of(se_nacl, struct scsiback_nacl, se_node_acl);
+    nacl->iport_wwpn = wwpn;
+
+    return se_nacl;

drivers/xen/xen-scsiback.c line 1629

Quoted 10 lines
drivers/xen/xen-scsiback.c new file +10 -0
+}
+
+static void scsiback_drop_nodeacl(struct se_node_acl *se_acl)
+{
+    struct scsiback_nacl *nacl = container_of(se_acl,
+                struct scsiback_nacl, se_node_acl);
+    core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
+    kfree(nacl);
+}
+

As mentioned above, the NodeACL use is unnecessary for this driver so you can safely drop scsiback_make_node_acl() + scsiback_alloc_fabric_acl() + scsiback_drop_nodeacl() + scsiback_release_fabric_acl().

Quoted 5 lines
Quoted 1 line

Deleted.

target_fabric_tf_ops_check() complains about missing tpg_alloc_fabric_acl and tpg_release_fabric_acl.

Sorry, yes, you still need a struct scsiback_nacl that contains struct se_node_acl, along with these two callbacks for allocating + releasing the associated scsiback_nacl memory.

This is required because TFO->tpg_alloc_fabric_acl() is called to allocate struct scsiback_nacl at core_tpg_add_initiator_node_acl() time when TFO->check_demo_mode() == 1 has been set.

Normal fabric NodeACLs (eg: ones not generated in the kernel by demo-mode) hang off configfs, so the TFO->tpg_*_fabric_acl() call is driven from TFO->fabric_make_nodeacl(), which is the part that can safely be dropped here.

--nab

#35 lore raw

Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 40 lines · Juergen Gross
Quoted 35 lines · Juergen Gross

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 13 lines
Quoted 13 lines
drivers/xen/xen-scsiback.c new file +10 -0
+    if (IS_ERR(tv_nexus->tvn_se_sess)) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        kfree(tv_nexus);
+        return -ENOMEM;
+    }
+    se_sess = tv_nexus->tvn_se_sess;
+    /*
+     * Since we are running in 'demo mode' this call with generate a
+     * struct se_node_acl for the scsiback struct se_portal_group with
+     * the SCSI Initiator port name of the passed configfs group

'name'.

(quoted diff) +2 -0
+     */
+    tv_nexus->tvn_se_sess->se_node_acl =
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines

core_tpg_check_initiator_node_acl(

drivers/xen/xen-scsiback.c new file +7 -0
+                se_tpg, (unsigned char *)name);
+    if (!tv_nexus->tvn_se_sess->se_node_acl) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+             name);
+        goto out;
+    }
Quoted 3 lines

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

Quoted 9 lines

Obviously I can't. transport_lookup_cmd_lun() calls right at start spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags); resulting in a page fault...

I guess the hint to just throw away all the node_acl related stuff has to be adjusted somehow...

So: what is needed, what can be removed?

The scsiback_make_nodeacl() + scsiback_drop_nodeacl() configfs callbacks can be safely dropped, along with scisback_ncal->iport_[wwpn,name].

--nab

#38 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Juergen Gross
Quoted 40 lines · Juergen Gross
Quoted 35 lines · Juergen Gross

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 13 lines
Quoted 13 lines
drivers/xen/xen-scsiback.c new file +10 -0
+    if (IS_ERR(tv_nexus->tvn_se_sess)) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        kfree(tv_nexus);
+        return -ENOMEM;
+    }
+    se_sess = tv_nexus->tvn_se_sess;
+    /*
+     * Since we are running in 'demo mode' this call with generate a
+     * struct se_node_acl for the scsiback struct se_portal_group with
+     * the SCSI Initiator port name of the passed configfs group

'name'.

(quoted diff) +2 -0
+     */
+    tv_nexus->tvn_se_sess->se_node_acl =
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines

core_tpg_check_initiator_node_acl(

drivers/xen/xen-scsiback.c new file +7 -0
+                se_tpg, (unsigned char *)name);
+    if (!tv_nexus->tvn_se_sess->se_node_acl) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+             name);
+        goto out;
+    }
Quoted 3 lines

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

Quoted 9 lines

Obviously I can't. transport_lookup_cmd_lun() calls right at start spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags); resulting in a page fault...

I guess the hint to just throw away all the node_acl related stuff has to be adjusted somehow...

So: what is needed, what can be removed?

The scsiback_make_nodeacl() + scsiback_drop_nodeacl() configfs callbacks can be safely dropped, along with scisback_ncal->iport_[wwpn,name].

--nab

author maintainer
#36 lore raw

Re: [Xen-devel] [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Nicholas A. Bellinger
Quoted 45 lines · Nicholas A. Bellinger
Quoted 44 lines · Juergen Gross
Quoted 35 lines · Juergen Gross

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 13 lines
Quoted 13 lines
drivers/xen/xen-scsiback.c new file +10 -0
+    if (IS_ERR(tv_nexus->tvn_se_sess)) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        kfree(tv_nexus);
+        return -ENOMEM;
+    }
+    se_sess = tv_nexus->tvn_se_sess;
+    /*
+     * Since we are running in 'demo mode' this call with generate a
+     * struct se_node_acl for the scsiback struct se_portal_group with
+     * the SCSI Initiator port name of the passed configfs group

'name'.

(quoted diff) +2 -0
+     */
+    tv_nexus->tvn_se_sess->se_node_acl =
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines

core_tpg_check_initiator_node_acl(

drivers/xen/xen-scsiback.c new file +7 -0
+                se_tpg, (unsigned char *)name);
+    if (!tv_nexus->tvn_se_sess->se_node_acl) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+             name);
+        goto out;
+    }
Quoted 3 lines

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

Obviously I can't. transport_lookup_cmd_lun() calls right at start spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags); resulting in a page fault...

Quoted 8 lines
Quoted 5 lines

I guess the hint to just throw away all the node_acl related stuff has to be adjusted somehow...

So: what is needed, what can be removed?

The scsiback_make_nodeacl() + scsiback_drop_nodeacl() configfs callbacks can be safely dropped, along with scisback_ncal->iport_[wwpn,name].

Okay, thanks. This is working.

Juergen

author maintainer
#37 lore raw

Re: [PATCH V4 3/4] Introduce XEN scsiback module

In reply to Nicholas A. Bellinger
Quoted 45 lines · Nicholas A. Bellinger
Quoted 44 lines · Juergen Gross
Quoted 35 lines · Juergen Gross

Nicholas,

just one more question (see below):

Quoted 5 lines · Nicholas A. Bellinger

Hi Juergen & Co,

Finally had a chance to review this code. Comments are inline below..

On Fri, 2014-08-08 at 09:49 +0200, [email protected] wrote:

...

Quoted 13 lines
Quoted 13 lines
drivers/xen/xen-scsiback.c new file +10 -0
+    if (IS_ERR(tv_nexus->tvn_se_sess)) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        kfree(tv_nexus);
+        return -ENOMEM;
+    }
+    se_sess = tv_nexus->tvn_se_sess;
+    /*
+     * Since we are running in 'demo mode' this call with generate a
+     * struct se_node_acl for the scsiback struct se_portal_group with
+     * the SCSI Initiator port name of the passed configfs group

'name'.

(quoted diff) +2 -0
+     */
+    tv_nexus->tvn_se_sess->se_node_acl =
Quoted 8 lines

drivers/xen/xen-scsiback.c line 1815

Quoted 8 lines

core_tpg_check_initiator_node_acl(

drivers/xen/xen-scsiback.c new file +7 -0
+                se_tpg, (unsigned char *)name);
+    if (!tv_nexus->tvn_se_sess->se_node_acl) {
+        mutex_unlock(&tpg->tv_tpg_mutex);
+        pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
+             name);
+        goto out;
+    }
Quoted 3 lines

Can I drop the call to core_tpg_check_initiator_node_acl() as well? Keeping it will result in failing to setup the nexus (which is to be expected IMHO).

Obviously I can't. transport_lookup_cmd_lun() calls right at start spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags); resulting in a page fault...

Quoted 8 lines
Quoted 5 lines

I guess the hint to just throw away all the node_acl related stuff has to be adjusted somehow...

So: what is needed, what can be removed?

The scsiback_make_nodeacl() + scsiback_drop_nodeacl() configfs callbacks can be safely dropped, along with scisback_ncal->iport_[wwpn,name].

Okay, thanks. This is working.

Juergen