Skip to content

Commit d9ea7a3

Browse files
zhijianli88liuw
authored andcommitted
hv: vmbus: Convert sprintf() family to sysfs_emit() family
Per filesystems/sysfs.rst, show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space. Coccinelle complains that there are still a couple of functions that use snprintf(). Convert them to sysfs_emit(). sprintf() and scnprintf() will be converted as well if these files have such abused cases. This patch is generated by make coccicheck M=<path/to/file> MODE=patch \ COCCI=scripts/coccinelle/api/device_attr_show.cocci No functional change intended. CC: "K. Y. Srinivasan" <[email protected]> CC: Haiyang Zhang <[email protected]> CC: Wei Liu <[email protected]> CC: Dexuan Cui <[email protected]> CC: [email protected] Signed-off-by: Li Zhijian <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]> Message-ID: <[email protected]>
1 parent 1f1dc44 commit d9ea7a3

File tree

1 file changed

+42
-52
lines changed

1 file changed

+42
-52
lines changed

drivers/hv/vmbus_drv.c

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
131131

132132
if (!hv_dev->channel)
133133
return -ENODEV;
134-
return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
134+
return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
135135
}
136136
static DEVICE_ATTR_RO(id);
137137

@@ -142,7 +142,7 @@ static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
142142

143143
if (!hv_dev->channel)
144144
return -ENODEV;
145-
return sprintf(buf, "%d\n", hv_dev->channel->state);
145+
return sysfs_emit(buf, "%d\n", hv_dev->channel->state);
146146
}
147147
static DEVICE_ATTR_RO(state);
148148

@@ -153,7 +153,7 @@ static ssize_t monitor_id_show(struct device *dev,
153153

154154
if (!hv_dev->channel)
155155
return -ENODEV;
156-
return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
156+
return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
157157
}
158158
static DEVICE_ATTR_RO(monitor_id);
159159

@@ -164,8 +164,8 @@ static ssize_t class_id_show(struct device *dev,
164164

165165
if (!hv_dev->channel)
166166
return -ENODEV;
167-
return sprintf(buf, "{%pUl}\n",
168-
&hv_dev->channel->offermsg.offer.if_type);
167+
return sysfs_emit(buf, "{%pUl}\n",
168+
&hv_dev->channel->offermsg.offer.if_type);
169169
}
170170
static DEVICE_ATTR_RO(class_id);
171171

@@ -176,8 +176,8 @@ static ssize_t device_id_show(struct device *dev,
176176

177177
if (!hv_dev->channel)
178178
return -ENODEV;
179-
return sprintf(buf, "{%pUl}\n",
180-
&hv_dev->channel->offermsg.offer.if_instance);
179+
return sysfs_emit(buf, "{%pUl}\n",
180+
&hv_dev->channel->offermsg.offer.if_instance);
181181
}
182182
static DEVICE_ATTR_RO(device_id);
183183

@@ -186,7 +186,7 @@ static ssize_t modalias_show(struct device *dev,
186186
{
187187
struct hv_device *hv_dev = device_to_hv_device(dev);
188188

189-
return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
189+
return sysfs_emit(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
190190
}
191191
static DEVICE_ATTR_RO(modalias);
192192

@@ -199,7 +199,7 @@ static ssize_t numa_node_show(struct device *dev,
199199
if (!hv_dev->channel)
200200
return -ENODEV;
201201

202-
return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
202+
return sysfs_emit(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
203203
}
204204
static DEVICE_ATTR_RO(numa_node);
205205
#endif
@@ -212,9 +212,8 @@ static ssize_t server_monitor_pending_show(struct device *dev,
212212

213213
if (!hv_dev->channel)
214214
return -ENODEV;
215-
return sprintf(buf, "%d\n",
216-
channel_pending(hv_dev->channel,
217-
vmbus_connection.monitor_pages[0]));
215+
return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel,
216+
vmbus_connection.monitor_pages[0]));
218217
}
219218
static DEVICE_ATTR_RO(server_monitor_pending);
220219

@@ -226,9 +225,8 @@ static ssize_t client_monitor_pending_show(struct device *dev,
226225

227226
if (!hv_dev->channel)
228227
return -ENODEV;
229-
return sprintf(buf, "%d\n",
230-
channel_pending(hv_dev->channel,
231-
vmbus_connection.monitor_pages[1]));
228+
return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel,
229+
vmbus_connection.monitor_pages[1]));
232230
}
233231
static DEVICE_ATTR_RO(client_monitor_pending);
234232

@@ -240,9 +238,8 @@ static ssize_t server_monitor_latency_show(struct device *dev,
240238

241239
if (!hv_dev->channel)
242240
return -ENODEV;
243-
return sprintf(buf, "%d\n",
244-
channel_latency(hv_dev->channel,
245-
vmbus_connection.monitor_pages[0]));
241+
return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel,
242+
vmbus_connection.monitor_pages[0]));
246243
}
247244
static DEVICE_ATTR_RO(server_monitor_latency);
248245

@@ -254,9 +251,8 @@ static ssize_t client_monitor_latency_show(struct device *dev,
254251

255252
if (!hv_dev->channel)
256253
return -ENODEV;
257-
return sprintf(buf, "%d\n",
258-
channel_latency(hv_dev->channel,
259-
vmbus_connection.monitor_pages[1]));
254+
return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel,
255+
vmbus_connection.monitor_pages[1]));
260256
}
261257
static DEVICE_ATTR_RO(client_monitor_latency);
262258

@@ -268,9 +264,8 @@ static ssize_t server_monitor_conn_id_show(struct device *dev,
268264

269265
if (!hv_dev->channel)
270266
return -ENODEV;
271-
return sprintf(buf, "%d\n",
272-
channel_conn_id(hv_dev->channel,
273-
vmbus_connection.monitor_pages[0]));
267+
return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel,
268+
vmbus_connection.monitor_pages[0]));
274269
}
275270
static DEVICE_ATTR_RO(server_monitor_conn_id);
276271

@@ -282,9 +277,8 @@ static ssize_t client_monitor_conn_id_show(struct device *dev,
282277

283278
if (!hv_dev->channel)
284279
return -ENODEV;
285-
return sprintf(buf, "%d\n",
286-
channel_conn_id(hv_dev->channel,
287-
vmbus_connection.monitor_pages[1]));
280+
return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel,
281+
vmbus_connection.monitor_pages[1]));
288282
}
289283
static DEVICE_ATTR_RO(client_monitor_conn_id);
290284

@@ -303,7 +297,7 @@ static ssize_t out_intr_mask_show(struct device *dev,
303297
if (ret < 0)
304298
return ret;
305299

306-
return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
300+
return sysfs_emit(buf, "%d\n", outbound.current_interrupt_mask);
307301
}
308302
static DEVICE_ATTR_RO(out_intr_mask);
309303

@@ -321,7 +315,7 @@ static ssize_t out_read_index_show(struct device *dev,
321315
&outbound);
322316
if (ret < 0)
323317
return ret;
324-
return sprintf(buf, "%d\n", outbound.current_read_index);
318+
return sysfs_emit(buf, "%d\n", outbound.current_read_index);
325319
}
326320
static DEVICE_ATTR_RO(out_read_index);
327321

@@ -340,7 +334,7 @@ static ssize_t out_write_index_show(struct device *dev,
340334
&outbound);
341335
if (ret < 0)
342336
return ret;
343-
return sprintf(buf, "%d\n", outbound.current_write_index);
337+
return sysfs_emit(buf, "%d\n", outbound.current_write_index);
344338
}
345339
static DEVICE_ATTR_RO(out_write_index);
346340

@@ -359,7 +353,7 @@ static ssize_t out_read_bytes_avail_show(struct device *dev,
359353
&outbound);
360354
if (ret < 0)
361355
return ret;
362-
return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
356+
return sysfs_emit(buf, "%d\n", outbound.bytes_avail_toread);
363357
}
364358
static DEVICE_ATTR_RO(out_read_bytes_avail);
365359

@@ -378,7 +372,7 @@ static ssize_t out_write_bytes_avail_show(struct device *dev,
378372
&outbound);
379373
if (ret < 0)
380374
return ret;
381-
return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
375+
return sysfs_emit(buf, "%d\n", outbound.bytes_avail_towrite);
382376
}
383377
static DEVICE_ATTR_RO(out_write_bytes_avail);
384378

@@ -396,7 +390,7 @@ static ssize_t in_intr_mask_show(struct device *dev,
396390
if (ret < 0)
397391
return ret;
398392

399-
return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
393+
return sysfs_emit(buf, "%d\n", inbound.current_interrupt_mask);
400394
}
401395
static DEVICE_ATTR_RO(in_intr_mask);
402396

@@ -414,7 +408,7 @@ static ssize_t in_read_index_show(struct device *dev,
414408
if (ret < 0)
415409
return ret;
416410

417-
return sprintf(buf, "%d\n", inbound.current_read_index);
411+
return sysfs_emit(buf, "%d\n", inbound.current_read_index);
418412
}
419413
static DEVICE_ATTR_RO(in_read_index);
420414

@@ -432,7 +426,7 @@ static ssize_t in_write_index_show(struct device *dev,
432426
if (ret < 0)
433427
return ret;
434428

435-
return sprintf(buf, "%d\n", inbound.current_write_index);
429+
return sysfs_emit(buf, "%d\n", inbound.current_write_index);
436430
}
437431
static DEVICE_ATTR_RO(in_write_index);
438432

@@ -451,7 +445,7 @@ static ssize_t in_read_bytes_avail_show(struct device *dev,
451445
if (ret < 0)
452446
return ret;
453447

454-
return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
448+
return sysfs_emit(buf, "%d\n", inbound.bytes_avail_toread);
455449
}
456450
static DEVICE_ATTR_RO(in_read_bytes_avail);
457451

@@ -470,7 +464,7 @@ static ssize_t in_write_bytes_avail_show(struct device *dev,
470464
if (ret < 0)
471465
return ret;
472466

473-
return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
467+
return sysfs_emit(buf, "%d\n", inbound.bytes_avail_towrite);
474468
}
475469
static DEVICE_ATTR_RO(in_write_bytes_avail);
476470

@@ -480,33 +474,29 @@ static ssize_t channel_vp_mapping_show(struct device *dev,
480474
{
481475
struct hv_device *hv_dev = device_to_hv_device(dev);
482476
struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
483-
int buf_size = PAGE_SIZE, n_written, tot_written;
477+
int n_written;
484478
struct list_head *cur;
485479

486480
if (!channel)
487481
return -ENODEV;
488482

489483
mutex_lock(&vmbus_connection.channel_mutex);
490484

491-
tot_written = snprintf(buf, buf_size, "%u:%u\n",
492-
channel->offermsg.child_relid, channel->target_cpu);
485+
n_written = sysfs_emit(buf, "%u:%u\n",
486+
channel->offermsg.child_relid,
487+
channel->target_cpu);
493488

494489
list_for_each(cur, &channel->sc_list) {
495-
if (tot_written >= buf_size - 1)
496-
break;
497490

498491
cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
499-
n_written = scnprintf(buf + tot_written,
500-
buf_size - tot_written,
501-
"%u:%u\n",
502-
cur_sc->offermsg.child_relid,
503-
cur_sc->target_cpu);
504-
tot_written += n_written;
492+
n_written += sysfs_emit_at(buf, n_written, "%u:%u\n",
493+
cur_sc->offermsg.child_relid,
494+
cur_sc->target_cpu);
505495
}
506496

507497
mutex_unlock(&vmbus_connection.channel_mutex);
508498

509-
return tot_written;
499+
return n_written;
510500
}
511501
static DEVICE_ATTR_RO(channel_vp_mapping);
512502

@@ -516,7 +506,7 @@ static ssize_t vendor_show(struct device *dev,
516506
{
517507
struct hv_device *hv_dev = device_to_hv_device(dev);
518508

519-
return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
509+
return sysfs_emit(buf, "0x%x\n", hv_dev->vendor_id);
520510
}
521511
static DEVICE_ATTR_RO(vendor);
522512

@@ -526,7 +516,7 @@ static ssize_t device_show(struct device *dev,
526516
{
527517
struct hv_device *hv_dev = device_to_hv_device(dev);
528518

529-
return sprintf(buf, "0x%x\n", hv_dev->device_id);
519+
return sysfs_emit(buf, "0x%x\n", hv_dev->device_id);
530520
}
531521
static DEVICE_ATTR_RO(device);
532522

@@ -551,7 +541,7 @@ static ssize_t driver_override_show(struct device *dev,
551541
ssize_t len;
552542

553543
device_lock(dev);
554-
len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
544+
len = sysfs_emit(buf, "%s\n", hv_dev->driver_override);
555545
device_unlock(dev);
556546

557547
return len;

0 commit comments

Comments
 (0)