Skip to content

Commit 459f213

Browse files
Andy GroverNicholas Bellinger
authored andcommitted
target: Allocate aptpl_buf inside update_and_write_aptpl()
Instead of taking the buffer and length, update_and_write_aptpl() will allocate the buffer as needed, and then free it. Instead, the function takes an 'aptpl' boolean parameter. This enables us to remove memory alloc/frees from struct t10_pr_registration and other spots. There is a slight loss of functionality because each callsite doesn't get its own pr_debug any more, but this info can be cleaned via ftrace if necessary and I think the shorter code is worth it. Signed-off-by: Andy Grover <agrover@redhat.com> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 63e0334 commit 459f213

2 files changed

Lines changed: 48 additions & 157 deletions

File tree

‎drivers/target/target_core_pr.c‎

Lines changed: 48 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,6 @@ static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
606606
return NULL;
607607
}
608608

609-
pr_reg->pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_ATOMIC);
610-
if (!pr_reg->pr_aptpl_buf) {
611-
pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
612-
kmem_cache_free(t10_pr_reg_cache, pr_reg);
613-
return NULL;
614-
}
615-
616609
INIT_LIST_HEAD(&pr_reg->pr_reg_list);
617610
INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
618611
INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
@@ -803,7 +796,6 @@ int core_scsi3_alloc_aptpl_registration(
803796
pr_err("Unable to allocate struct t10_pr_registration\n");
804797
return -ENOMEM;
805798
}
806-
pr_reg->pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL);
807799

808800
INIT_LIST_HEAD(&pr_reg->pr_reg_list);
809801
INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
@@ -1272,7 +1264,6 @@ static void __core_scsi3_free_registration(
12721264
if (!preempt_and_abort_list) {
12731265
pr_reg->pr_reg_deve = NULL;
12741266
pr_reg->pr_reg_nacl = NULL;
1275-
kfree(pr_reg->pr_aptpl_buf);
12761267
kmem_cache_free(t10_pr_reg_cache, pr_reg);
12771268
return;
12781269
}
@@ -1341,7 +1332,6 @@ void core_scsi3_free_all_registrations(
13411332
list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
13421333
pr_reg_aptpl_list) {
13431334
list_del(&pr_reg->pr_reg_aptpl_list);
1344-
kfree(pr_reg->pr_aptpl_buf);
13451335
kmem_cache_free(t10_pr_reg_cache, pr_reg);
13461336
}
13471337
spin_unlock(&pr_tmpl->aptpl_reg_lock);
@@ -1814,7 +1804,6 @@ core_scsi3_decode_spec_i_port(
18141804
kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
18151805
}
18161806

1817-
kfree(dest_pr_reg->pr_aptpl_buf);
18181807
kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
18191808

18201809
if (dest_local_nexus)
@@ -1840,8 +1829,6 @@ static int core_scsi3_update_aptpl_buf(
18401829
int reg_count = 0;
18411830
int ret = 0;
18421831

1843-
memset(buf, 0, pr_aptpl_buf_len);
1844-
18451832
spin_lock(&dev->dev_reservation_lock);
18461833
spin_lock(&dev->t10_pr.registration_lock);
18471834
/*
@@ -1965,31 +1952,45 @@ static int __core_scsi3_write_aptpl_to_file(
19651952
return ret ? -EIO : 0;
19661953
}
19671954

1968-
static int
1969-
core_scsi3_update_and_write_aptpl(struct se_device *dev, unsigned char *in_buf,
1970-
u32 in_pr_aptpl_buf_len)
1955+
/*
1956+
* Clear the APTPL metadata if APTPL has been disabled, otherwise
1957+
* write out the updated metadata to struct file for this SCSI device.
1958+
*/
1959+
static int core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
19711960
{
1972-
unsigned char null_buf[64], *buf;
1973-
int ret;
1961+
int ret = 0;
19741962

1975-
/*
1976-
* Can be called with a NULL pointer from PROUT service action CLEAR
1977-
*/
1978-
if (!in_buf) {
1979-
snprintf(null_buf, 64, "No Registrations or Reservations\n");
1980-
buf = null_buf;
1963+
if (!aptpl) {
1964+
char *null_buf = "No Registrations or Reservations\n";
1965+
1966+
ret = __core_scsi3_write_aptpl_to_file(dev, null_buf);
1967+
dev->t10_pr.pr_aptpl_active = 0;
1968+
pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
19811969
} else {
1982-
ret = core_scsi3_update_aptpl_buf(dev, in_buf, in_pr_aptpl_buf_len);
1983-
if (ret != 0)
1970+
int ret;
1971+
unsigned char *buf;
1972+
1973+
buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL);
1974+
if (!buf)
1975+
return -ENOMEM;
1976+
1977+
ret = core_scsi3_update_aptpl_buf(dev, buf, PR_APTPL_BUF_LEN);
1978+
if (ret < 0) {
1979+
kfree(buf);
19841980
return ret;
1985-
buf = in_buf;
1981+
}
1982+
1983+
ret = __core_scsi3_write_aptpl_to_file(dev, buf);
1984+
if (ret != 0) {
1985+
pr_err("SPC-3 PR: Could not update APTPL\n");
1986+
} else {
1987+
dev->t10_pr.pr_aptpl_active = 1;
1988+
pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
1989+
}
1990+
kfree(buf);
19861991
}
19871992

1988-
/*
1989-
* __core_scsi3_write_aptpl_to_file() will call strlen()
1990-
* on the passed buf to determine pr_aptpl_buf_len.
1991-
*/
1992-
return __core_scsi3_write_aptpl_to_file(dev, buf);
1993+
return ret;
19931994
}
19941995

19951996
static sense_reason_t
@@ -2003,8 +2004,6 @@ core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
20032004
struct se_portal_group *se_tpg;
20042005
struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
20052006
struct t10_reservation *pr_tmpl = &dev->t10_pr;
2006-
/* Used for APTPL metadata w/ UNREGISTER */
2007-
unsigned char *pr_aptpl_buf = NULL;
20082007
unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
20092008
sense_reason_t ret = TCM_NO_SENSE;
20102009
int pr_holder = 0, type;
@@ -2066,31 +2065,8 @@ core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
20662065
if (ret != 0)
20672066
return ret;
20682067
}
2069-
/*
2070-
* Nothing left to do for the APTPL=0 case.
2071-
*/
2072-
if (!aptpl) {
2073-
pr_tmpl->pr_aptpl_active = 0;
2074-
core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2075-
pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2076-
" REGISTER\n");
2077-
return 0;
2078-
}
2079-
/*
2080-
* Locate the newly allocated local I_T Nexus *pr_reg, and
2081-
* update the APTPL metadata information using its
2082-
* preallocated *pr_reg->pr_aptpl_buf.
2083-
*/
2084-
pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2085-
se_sess->se_node_acl, se_sess);
2086-
2087-
if (core_scsi3_update_and_write_aptpl(cmd->se_dev,
2088-
pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
2089-
pr_tmpl->pr_aptpl_active = 1;
2090-
pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2091-
}
20922068

2093-
goto out_put_pr_reg;
2069+
return core_scsi3_update_and_write_aptpl(dev, aptpl);
20942070
}
20952071

20962072
/*
@@ -2130,19 +2106,6 @@ core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
21302106
goto out_put_pr_reg;
21312107
}
21322108

2133-
/*
2134-
* Allocate APTPL metadata buffer used for UNREGISTER ops
2135-
*/
2136-
if (aptpl) {
2137-
pr_aptpl_buf = kzalloc(PR_APTPL_BUF_LEN, GFP_KERNEL);
2138-
if (!pr_aptpl_buf) {
2139-
pr_err("Unable to allocate"
2140-
" pr_aptpl_buf\n");
2141-
ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2142-
goto out_put_pr_reg;
2143-
}
2144-
}
2145-
21462109
/*
21472110
* sa_res_key=0 Unregister Reservation Key for registered I_T
21482111
* Nexus sa_res_key=1 Change Reservation Key for registered I_T
@@ -2152,7 +2115,6 @@ core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
21522115
pr_holder = core_scsi3_check_implict_release(
21532116
cmd->se_dev, pr_reg);
21542117
if (pr_holder < 0) {
2155-
kfree(pr_aptpl_buf);
21562118
ret = TCM_RESERVATION_CONFLICT;
21572119
goto out_put_pr_reg;
21582120
}
@@ -2214,21 +2176,8 @@ core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
22142176
}
22152177
spin_unlock(&pr_tmpl->registration_lock);
22162178

2217-
if (!aptpl) {
2218-
pr_tmpl->pr_aptpl_active = 0;
2219-
core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2220-
pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2221-
" for UNREGISTER\n");
2222-
return 0;
2223-
}
2224-
2225-
if (!core_scsi3_update_and_write_aptpl(dev, pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
2226-
pr_tmpl->pr_aptpl_active = 1;
2227-
pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2228-
" for UNREGISTER\n");
2229-
}
2230-
2231-
goto out_free_aptpl_buf;
2179+
ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2180+
goto out_put_pr_reg;
22322181
}
22332182

22342183
/*
@@ -2245,24 +2194,8 @@ core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
22452194
pr_reg->pr_reg_nacl->initiatorname,
22462195
pr_reg->pr_res_key, pr_reg->pr_res_generation);
22472196

2248-
if (!aptpl) {
2249-
pr_tmpl->pr_aptpl_active = 0;
2250-
core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2251-
pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2252-
" for REGISTER\n");
2253-
ret = 0;
2254-
goto out_put_pr_reg;
2255-
}
2197+
ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
22562198

2257-
if (!core_scsi3_update_and_write_aptpl(dev, pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
2258-
pr_tmpl->pr_aptpl_active = 1;
2259-
pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2260-
" for REGISTER\n");
2261-
}
2262-
2263-
out_free_aptpl_buf:
2264-
kfree(pr_aptpl_buf);
2265-
ret = 0;
22662199
out_put_pr_reg:
22672200
core_scsi3_put_pr_reg(pr_reg);
22682201
return ret;
@@ -2437,13 +2370,8 @@ core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
24372370
i_buf);
24382371
spin_unlock(&dev->dev_reservation_lock);
24392372

2440-
if (pr_tmpl->pr_aptpl_active) {
2441-
if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
2442-
pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
2443-
pr_debug("SPC-3 PR: Updated APTPL metadata"
2444-
" for RESERVE\n");
2445-
}
2446-
}
2373+
if (pr_tmpl->pr_aptpl_active)
2374+
core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
24472375

24482376
ret = 0;
24492377
out_put_pr_reg:
@@ -2657,12 +2585,9 @@ core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
26572585
spin_unlock(&pr_tmpl->registration_lock);
26582586

26592587
write_aptpl:
2660-
if (pr_tmpl->pr_aptpl_active) {
2661-
if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
2662-
pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
2663-
pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2664-
}
2665-
}
2588+
if (pr_tmpl->pr_aptpl_active)
2589+
core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2590+
26662591
out_put_pr_reg:
26672592
core_scsi3_put_pr_reg(pr_reg);
26682593
return ret;
@@ -2746,11 +2671,7 @@ core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
27462671
pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
27472672
cmd->se_tfo->get_fabric_name());
27482673

2749-
if (pr_tmpl->pr_aptpl_active) {
2750-
core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2751-
pr_debug("SPC-3 PR: Updated APTPL metadata"
2752-
" for CLEAR\n");
2753-
}
2674+
core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
27542675

27552676
core_scsi3_pr_generation(dev);
27562677
return 0;
@@ -2822,7 +2743,6 @@ static void core_scsi3_release_preempt_and_abort(
28222743

28232744
pr_reg->pr_reg_deve = NULL;
28242745
pr_reg->pr_reg_nacl = NULL;
2825-
kfree(pr_reg->pr_aptpl_buf);
28262746
kmem_cache_free(t10_pr_reg_cache, pr_reg);
28272747
}
28282748
}
@@ -2984,14 +2904,8 @@ core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
29842904
}
29852905
spin_unlock(&dev->dev_reservation_lock);
29862906

2987-
if (pr_tmpl->pr_aptpl_active) {
2988-
if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
2989-
pr_reg_n->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
2990-
pr_debug("SPC-3 PR: Updated APTPL"
2991-
" metadata for PREEMPT%s\n", (preempt_type == PREEMPT_AND_ABORT) ?
2992-
"_AND_ABORT" : "");
2993-
}
2994-
}
2907+
if (pr_tmpl->pr_aptpl_active)
2908+
core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
29952909

29962910
core_scsi3_put_pr_reg(pr_reg_n);
29972911
core_scsi3_pr_generation(cmd->se_dev);
@@ -3119,13 +3033,8 @@ core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
31193033
pr_reg_n);
31203034
}
31213035

3122-
if (pr_tmpl->pr_aptpl_active) {
3123-
if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
3124-
pr_reg_n->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
3125-
pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3126-
"%s\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
3127-
}
3128-
}
3036+
if (pr_tmpl->pr_aptpl_active)
3037+
core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
31293038

31303039
core_scsi3_put_pr_reg(pr_reg_n);
31313040
core_scsi3_pr_generation(cmd->se_dev);
@@ -3552,23 +3461,7 @@ core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
35523461
} else
35533462
core_scsi3_put_pr_reg(pr_reg);
35543463

3555-
/*
3556-
* Clear the APTPL metadata if APTPL has been disabled, otherwise
3557-
* write out the updated metadata to struct file for this SCSI device.
3558-
*/
3559-
if (!aptpl) {
3560-
pr_tmpl->pr_aptpl_active = 0;
3561-
core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3562-
pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3563-
" REGISTER_AND_MOVE\n");
3564-
} else {
3565-
pr_tmpl->pr_aptpl_active = 1;
3566-
if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
3567-
dest_pr_reg->pr_aptpl_buf, PR_APTPL_BUF_LEN)) {
3568-
pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3569-
" REGISTER_AND_MOVE\n");
3570-
}
3571-
}
3464+
core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
35723465

35733466
transport_kunmap_data_sg(cmd);
35743467

‎include/target/target_core_base.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,6 @@ struct t10_pr_registration {
339339
/* Used during APTPL metadata reading */
340340
#define PR_APTPL_MAX_TPORT_LEN 256
341341
unsigned char pr_tport[PR_APTPL_MAX_TPORT_LEN];
342-
/* For writing out live meta data */
343-
unsigned char *pr_aptpl_buf;
344342
u16 pr_aptpl_rpti;
345343
u16 pr_reg_tpgt;
346344
/* Reservation effects all target ports */

0 commit comments

Comments
 (0)