Skip to content

Commit

Permalink
scsi: target: Convert target drivers to use sbitmap
Browse files Browse the repository at this point in the history
The sbitmap and the percpu_ida perform essentially the same task,
allocating tags for commands.  The sbitmap outperforms the percpu_ida as
documented here: https://lkml.org/lkml/2014/4/22/553

The sbitmap interface is a little harder to use, but being able to remove
the percpu_ida code and getting better performance justifies the additional
complexity.

Signed-off-by: Matthew Wilcox <willy@infradead.org>
Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com>	# f_tcm
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
  • Loading branch information
Matthew Wilcox authored and Martin K. Petersen committed Jun 20, 2018
1 parent 83c2b54 commit 10e9cbb
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 24 deletions.
10 changes: 6 additions & 4 deletions drivers/scsi/qla2xxx/qla_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -4277,9 +4277,9 @@ static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
{
struct se_session *se_sess = sess->se_sess;
struct qla_tgt_cmd *cmd;
int tag;
int tag, cpu;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0)
return NULL;

Expand All @@ -4292,6 +4292,7 @@ static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
qlt_incr_num_pend_cmds(vha);
cmd->vha = vha;
cmd->se_cmd.map_tag = tag;
cmd->se_cmd.map_cpu = cpu;
cmd->sess = sess;
cmd->loop_id = sess->loop_id;
cmd->conf_compl_supported = sess->conf_compl_supported;
Expand Down Expand Up @@ -5294,7 +5295,7 @@ qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
struct fc_port *sess;
struct se_session *se_sess;
struct qla_tgt_cmd *cmd;
int tag;
int tag, cpu;
unsigned long flags;

if (unlikely(tgt->tgt_stop)) {
Expand Down Expand Up @@ -5326,7 +5327,7 @@ qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,

se_sess = sess->se_sess;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0)
return;

Expand Down Expand Up @@ -5357,6 +5358,7 @@ qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
cmd->reset_count = ha->base_qpair->chip_reset;
cmd->q_full = 1;
cmd->qpair = ha->base_qpair;
cmd->se_cmd.map_cpu = cpu;

if (qfull) {
cmd->q_full = 1;
Expand Down
33 changes: 30 additions & 3 deletions drivers/target/iscsi/iscsi_target_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
******************************************************************************/

#include <linux/list.h>
#include <linux/percpu_ida.h>
#include <linux/sched/signal.h>
#include <net/ipv6.h> /* ipv6_addr_equal() */
#include <scsi/scsi_tcq.h>
#include <scsi/iscsi_proto.h>
Expand Down Expand Up @@ -147,6 +147,30 @@ void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
spin_unlock_bh(&cmd->r2t_lock);
}

static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup)
{
int tag = -1;
DEFINE_WAIT(wait);
struct sbq_wait_state *ws;

if (state == TASK_RUNNING)
return tag;

ws = &se_sess->sess_tag_pool.ws[0];
for (;;) {
prepare_to_wait_exclusive(&ws->wait, &wait, state);
if (signal_pending_state(state, current))
break;
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, cpup);
if (tag >= 0)
break;
schedule();
}

finish_wait(&ws->wait, &wait);
return tag;
}

/*
* May be called from software interrupt (timer) context for allocating
* iSCSI NopINs.
Expand All @@ -155,9 +179,11 @@ struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
{
struct iscsi_cmd *cmd;
struct se_session *se_sess = conn->sess->se_sess;
int size, tag;
int size, tag, cpu;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, state);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0)
tag = iscsit_wait_for_tag(se_sess, state, &cpu);
if (tag < 0)
return NULL;

Expand All @@ -166,6 +192,7 @@ struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
memset(cmd, 0, size);

cmd->se_cmd.map_tag = tag;
cmd->se_cmd.map_cpu = cpu;
cmd->conn = conn;
cmd->data_direction = DMA_NONE;
INIT_LIST_HEAD(&cmd->i_conn_node);
Expand Down
5 changes: 3 additions & 2 deletions drivers/target/sbp/sbp_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,15 +926,16 @@ static struct sbp_target_request *sbp_mgt_get_req(struct sbp_session *sess,
{
struct se_session *se_sess = sess->se_sess;
struct sbp_target_request *req;
int tag;
int tag, cpu;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0)
return ERR_PTR(-ENOMEM);

req = &((struct sbp_target_request *)se_sess->sess_cmd_map)[tag];
memset(req, 0, sizeof(*req));
req->se_cmd.map_tag = tag;
req->se_cmd.map_cpu = cpu;
req->se_cmd.tag = next_orb;

return req;
Expand Down
5 changes: 3 additions & 2 deletions drivers/target/target_core_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ int transport_alloc_session_tags(struct se_session *se_sess,
}
}

rc = percpu_ida_init(&se_sess->sess_tag_pool, tag_num);
rc = sbitmap_queue_init_node(&se_sess->sess_tag_pool, tag_num, -1,
false, GFP_KERNEL, NUMA_NO_NODE);
if (rc < 0) {
pr_err("Unable to init se_sess->sess_tag_pool,"
" tag_num: %u\n", tag_num);
Expand Down Expand Up @@ -547,7 +548,7 @@ void transport_free_session(struct se_session *se_sess)
target_put_nacl(se_nacl);
}
if (se_sess->sess_cmd_map) {
percpu_ida_destroy(&se_sess->sess_tag_pool);
sbitmap_queue_free(&se_sess->sess_tag_pool);
kvfree(se_sess->sess_cmd_map);
}
kmem_cache_free(se_sess_cache, se_sess);
Expand Down
6 changes: 3 additions & 3 deletions drivers/target/tcm_fc/tfc_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <linux/configfs.h>
#include <linux/ctype.h>
#include <linux/hash.h>
#include <linux/percpu_ida.h>
#include <asm/unaligned.h>
#include <scsi/scsi_tcq.h>
#include <scsi/libfc.h>
Expand Down Expand Up @@ -448,16 +447,17 @@ static void ft_recv_cmd(struct ft_sess *sess, struct fc_frame *fp)
struct ft_cmd *cmd;
struct fc_lport *lport = sess->tport->lport;
struct se_session *se_sess = sess->se_sess;
int tag;
int tag, cpu;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0)
goto busy;

cmd = &((struct ft_cmd *)se_sess->sess_cmd_map)[tag];
memset(cmd, 0, sizeof(struct ft_cmd));

cmd->se_cmd.map_tag = tag;
cmd->se_cmd.map_cpu = cpu;
cmd->sess = sess;
cmd->seq = fc_seq_assign(lport, fp);
if (!cmd->seq) {
Expand Down
5 changes: 3 additions & 2 deletions drivers/usb/gadget/function/f_tcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,15 +1071,16 @@ static struct usbg_cmd *usbg_get_cmd(struct f_uas *fu,
{
struct se_session *se_sess = tv_nexus->tvn_se_sess;
struct usbg_cmd *cmd;
int tag;
int tag, cpu;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0)
return ERR_PTR(-ENOMEM);

cmd = &((struct usbg_cmd *)se_sess->sess_cmd_map)[tag];
memset(cmd, 0, sizeof(*cmd));
cmd->se_cmd.map_tag = tag;
cmd->se_cmd.map_cpu = cpu;
cmd->se_cmd.tag = cmd->tag = scsi_tag;
cmd->fu = fu;

Expand Down
6 changes: 3 additions & 3 deletions drivers/vhost/scsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <linux/virtio_scsi.h>
#include <linux/llist.h>
#include <linux/bitmap.h>
#include <linux/percpu_ida.h>

#include "vhost.h"

Expand Down Expand Up @@ -567,7 +566,7 @@ vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
struct se_session *se_sess;
struct scatterlist *sg, *prot_sg;
struct page **pages;
int tag;
int tag, cpu;

tv_nexus = tpg->tpg_nexus;
if (!tv_nexus) {
Expand All @@ -576,7 +575,7 @@ vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
}
se_sess = tv_nexus->tvn_se_sess;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0) {
pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
return ERR_PTR(-ENOMEM);
Expand All @@ -591,6 +590,7 @@ vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
cmd->tvc_prot_sgl = prot_sg;
cmd->tvc_upages = pages;
cmd->tvc_se_cmd.map_tag = tag;
cmd->tvc_se_cmd.map_cpu = cpu;
cmd->tvc_tag = scsi_tag;
cmd->tvc_lun = lun;
cmd->tvc_task_attr = task_attr;
Expand Down
5 changes: 3 additions & 2 deletions drivers/xen/xen-scsiback.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,9 @@ static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring
struct scsiback_nexus *nexus = tpg->tpg_nexus;
struct se_session *se_sess = nexus->tvn_se_sess;
struct vscsibk_pend *req;
int tag, i;
int tag, cpu, i;

tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
if (tag < 0) {
pr_err("Unable to obtain tag for vscsiif_request\n");
return ERR_PTR(-ENOMEM);
Expand All @@ -665,6 +665,7 @@ static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring
req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
memset(req, 0, sizeof(*req));
req->se_cmd.map_tag = tag;
req->se_cmd.map_cpu = cpu;

for (i = 0; i < VSCSI_MAX_GRANTS; i++)
req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
Expand Down
1 change: 1 addition & 0 deletions include/target/iscsi/iscsi_target_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <linux/dma-direction.h> /* enum dma_data_direction */
#include <linux/list.h> /* struct list_head */
#include <linux/sched.h>
#include <linux/socket.h> /* struct sockaddr_storage */
#include <linux/types.h> /* u8 */
#include <scsi/iscsi_proto.h> /* itt_t */
Expand Down
7 changes: 4 additions & 3 deletions include/target/target_core_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <linux/configfs.h> /* struct config_group */
#include <linux/dma-direction.h> /* enum dma_data_direction */
#include <linux/percpu_ida.h> /* struct percpu_ida */
#include <linux/sbitmap.h>
#include <linux/percpu-refcount.h>
#include <linux/semaphore.h> /* struct semaphore */
#include <linux/completion.h>
Expand Down Expand Up @@ -455,6 +455,7 @@ struct se_cmd {
int sam_task_attr;
/* Used for se_sess->sess_tag_pool */
unsigned int map_tag;
int map_cpu;
/* Transport protocol dependent state, see transport_state_table */
enum transport_state_table t_state;
/* See se_cmd_flags_table */
Expand Down Expand Up @@ -608,7 +609,7 @@ struct se_session {
struct list_head sess_wait_list;
spinlock_t sess_cmd_lock;
void *sess_cmd_map;
struct percpu_ida sess_tag_pool;
struct sbitmap_queue sess_tag_pool;
};

struct se_device;
Expand Down Expand Up @@ -936,7 +937,7 @@ static inline void atomic_dec_mb(atomic_t *v)

static inline void target_free_tag(struct se_session *sess, struct se_cmd *cmd)
{
percpu_ida_free(&sess->sess_tag_pool, cmd->map_tag);
sbitmap_queue_clear(&sess->sess_tag_pool, cmd->map_tag, cmd->map_cpu);
}

#endif /* TARGET_CORE_BASE_H */

0 comments on commit 10e9cbb

Please sign in to comment.