Skip to content

Commit 948b3ed

Browse files
JoePerchesgregkh
authored andcommitted
drivers core: Miscellaneous changes for sysfs_emit
Change additional instances that could use sysfs_emit and sysfs_emit_at that the coccinelle script could not convert. o macros creating show functions with ## concatenation o unbound sprintf uses with buf+len for start of output to sysfs_emit_at o returns with ?: tests and sprintf to sysfs_emit o sysfs output with struct class * not struct device * arguments Miscellanea: o remove unnecessary initializations around these changes o consistently use int len for return length of show functions o use octal permissions and not S_<FOO> o rename a few show function names so DEVICE_ATTR_<FOO> can be used o use DEVICE_ATTR_ADMIN_RO where appropriate o consistently use const char *output for strings o checkpatch/style neatening Signed-off-by: Joe Perches <[email protected]> Link: https://lore.kernel.org/r/8bc24444fe2049a9b2de6127389b57edfdfe324d.1600285923.git.joe@perches.com Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 27275d3 commit 948b3ed

File tree

15 files changed

+308
-267
lines changed

15 files changed

+308
-267
lines changed

drivers/base/bus.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
229229

230230
static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
231231
{
232-
return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
232+
return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe);
233233
}
234234

235235
static ssize_t drivers_autoprobe_store(struct bus_type *bus,

drivers/base/cacheinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ static ssize_t file_name##_show(struct device *dev, \
362362
struct device_attribute *attr, char *buf) \
363363
{ \
364364
struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
365-
return sprintf(buf, "%u\n", this_leaf->object); \
365+
return sysfs_emit(buf, "%u\n", this_leaf->object); \
366366
}
367367

368368
show_one(id, id);

drivers/base/class.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ ssize_t show_class_attr_string(struct class *class,
478478
struct class_attribute_string *cs;
479479

480480
cs = container_of(attr, struct class_attribute_string, attr);
481-
return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
481+
return sysfs_emit(buf, "%s\n", cs->str);
482482
}
483483

484484
EXPORT_SYMBOL_GPL(show_class_attr_string);

drivers/base/core.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,35 @@ void device_pm_move_to_tail(struct device *dev)
240240
#define to_devlink(dev) container_of((dev), struct device_link, link_dev)
241241

242242
static ssize_t status_show(struct device *dev,
243-
struct device_attribute *attr, char *buf)
243+
struct device_attribute *attr, char *buf)
244244
{
245-
char *status;
245+
const char *output;
246246

247247
switch (to_devlink(dev)->status) {
248248
case DL_STATE_NONE:
249-
status = "not tracked"; break;
249+
output = "not tracked";
250+
break;
250251
case DL_STATE_DORMANT:
251-
status = "dormant"; break;
252+
output = "dormant";
253+
break;
252254
case DL_STATE_AVAILABLE:
253-
status = "available"; break;
255+
output = "available";
256+
break;
254257
case DL_STATE_CONSUMER_PROBE:
255-
status = "consumer probing"; break;
258+
output = "consumer probing";
259+
break;
256260
case DL_STATE_ACTIVE:
257-
status = "active"; break;
261+
output = "active";
262+
break;
258263
case DL_STATE_SUPPLIER_UNBIND:
259-
status = "supplier unbinding"; break;
264+
output = "supplier unbinding";
265+
break;
260266
default:
261-
status = "unknown"; break;
267+
output = "unknown";
268+
break;
262269
}
263-
return sysfs_emit(buf, "%s\n", status);
270+
271+
return sysfs_emit(buf, "%s\n", output);
264272
}
265273
static DEVICE_ATTR_RO(status);
266274

@@ -1934,7 +1942,7 @@ static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
19341942
struct kset *kset;
19351943
struct kobj_uevent_env *env = NULL;
19361944
int i;
1937-
size_t count = 0;
1945+
int len = 0;
19381946
int retval;
19391947

19401948
/* search the kset, the device belongs to */
@@ -1964,10 +1972,10 @@ static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
19641972

19651973
/* copy keys to file */
19661974
for (i = 0; i < env->envp_idx; i++)
1967-
count += sprintf(&buf[count], "%s\n", env->envp[i]);
1975+
len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
19681976
out:
19691977
kfree(env);
1970-
return count;
1978+
return len;
19711979
}
19721980

19731981
static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,

drivers/base/cpu.c

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ EXPORT_SYMBOL_GPL(cpu_subsys);
139139
#ifdef CONFIG_KEXEC
140140
#include <linux/kexec.h>
141141

142-
static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr,
142+
static ssize_t crash_notes_show(struct device *dev,
143+
struct device_attribute *attr,
143144
char *buf)
144145
{
145146
struct cpu *cpu = container_of(dev, struct cpu, dev);
146-
ssize_t rc;
147147
unsigned long long addr;
148148
int cpunum;
149149

@@ -156,21 +156,18 @@ static ssize_t show_crash_notes(struct device *dev, struct device_attribute *att
156156
* operation should be safe. No locking required.
157157
*/
158158
addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
159-
rc = sysfs_emit(buf, "%Lx\n", addr);
160-
return rc;
159+
160+
return sysfs_emit(buf, "%llx\n", addr);
161161
}
162-
static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL);
162+
static DEVICE_ATTR_ADMIN_RO(crash_notes);
163163

164-
static ssize_t show_crash_notes_size(struct device *dev,
164+
static ssize_t crash_notes_size_show(struct device *dev,
165165
struct device_attribute *attr,
166166
char *buf)
167167
{
168-
ssize_t rc;
169-
170-
rc = sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
171-
return rc;
168+
return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
172169
}
173-
static DEVICE_ATTR(crash_notes_size, 0400, show_crash_notes_size, NULL);
170+
static DEVICE_ATTR_ADMIN_RO(crash_notes_size);
174171

175172
static struct attribute *crash_note_cpu_attrs[] = {
176173
&dev_attr_crash_notes.attr,
@@ -241,55 +238,55 @@ unsigned int total_cpus;
241238
static ssize_t print_cpus_offline(struct device *dev,
242239
struct device_attribute *attr, char *buf)
243240
{
244-
int n = 0, len = PAGE_SIZE-2;
241+
int len = 0;
245242
cpumask_var_t offline;
246243

247244
/* display offline cpus < nr_cpu_ids */
248245
if (!alloc_cpumask_var(&offline, GFP_KERNEL))
249246
return -ENOMEM;
250247
cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
251-
n = scnprintf(buf, len, "%*pbl", cpumask_pr_args(offline));
248+
len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
252249
free_cpumask_var(offline);
253250

254251
/* display offline cpus >= nr_cpu_ids */
255252
if (total_cpus && nr_cpu_ids < total_cpus) {
256-
if (n && n < len)
257-
buf[n++] = ',';
253+
len += sysfs_emit_at(buf, len, ",");
258254

259255
if (nr_cpu_ids == total_cpus-1)
260-
n += scnprintf(&buf[n], len - n, "%u", nr_cpu_ids);
256+
len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids);
261257
else
262-
n += scnprintf(&buf[n], len - n, "%u-%d",
263-
nr_cpu_ids, total_cpus-1);
258+
len += sysfs_emit_at(buf, len, "%u-%d",
259+
nr_cpu_ids, total_cpus - 1);
264260
}
265261

266-
n += scnprintf(&buf[n], len - n, "\n");
267-
return n;
262+
len += sysfs_emit_at(buf, len, "\n");
263+
264+
return len;
268265
}
269266
static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
270267

271268
static ssize_t print_cpus_isolated(struct device *dev,
272269
struct device_attribute *attr, char *buf)
273270
{
274-
int n;
271+
int len;
275272
cpumask_var_t isolated;
276273

277274
if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
278275
return -ENOMEM;
279276

280277
cpumask_andnot(isolated, cpu_possible_mask,
281278
housekeeping_cpumask(HK_FLAG_DOMAIN));
282-
n = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
279+
len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
283280

284281
free_cpumask_var(isolated);
285282

286-
return n;
283+
return len;
287284
}
288285
static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
289286

290287
#ifdef CONFIG_NO_HZ_FULL
291288
static ssize_t print_cpus_nohz_full(struct device *dev,
292-
struct device_attribute *attr, char *buf)
289+
struct device_attribute *attr, char *buf)
293290
{
294291
return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
295292
}
@@ -320,22 +317,23 @@ static ssize_t print_cpu_modalias(struct device *dev,
320317
struct device_attribute *attr,
321318
char *buf)
322319
{
323-
ssize_t n;
320+
int len = 0;
324321
u32 i;
325322

326-
n = sysfs_emit(buf, "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
327-
CPU_FEATURE_TYPEVAL);
323+
len += sysfs_emit_at(buf, len,
324+
"cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
325+
CPU_FEATURE_TYPEVAL);
328326

329327
for (i = 0; i < MAX_CPU_FEATURES; i++)
330328
if (cpu_have_feature(i)) {
331-
if (PAGE_SIZE < n + sizeof(",XXXX\n")) {
329+
if (len + sizeof(",XXXX\n") >= PAGE_SIZE) {
332330
WARN(1, "CPU features overflow page\n");
333331
break;
334332
}
335-
n += sprintf(&buf[n], ",%04X", i);
333+
len += sysfs_emit_at(buf, len, ",%04X", i);
336334
}
337-
buf[n++] = '\n';
338-
return n;
335+
len += sysfs_emit_at(buf, len, "\n");
336+
return len;
339337
}
340338

341339
static int cpu_uevent(struct device *dev, struct kobj_uevent_env *env)
@@ -557,7 +555,7 @@ ssize_t __weak cpu_show_tsx_async_abort(struct device *dev,
557555
}
558556

559557
ssize_t __weak cpu_show_itlb_multihit(struct device *dev,
560-
struct device_attribute *attr, char *buf)
558+
struct device_attribute *attr, char *buf)
561559
{
562560
return sysfs_emit(buf, "Not affected\n");
563561
}

drivers/base/dd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ static ssize_t state_synced_show(struct device *dev,
486486
device_lock(dev);
487487
val = dev->state_synced;
488488
device_unlock(dev);
489+
489490
return sysfs_emit(buf, "%u\n", val);
490491
}
491492
static DEVICE_ATTR_RO(state_synced);

drivers/base/devcoredump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static int devcd_free(struct device *dev, void *data)
123123
static ssize_t disabled_show(struct class *class, struct class_attribute *attr,
124124
char *buf)
125125
{
126-
return sprintf(buf, "%d\n", devcd_disabled);
126+
return sysfs_emit(buf, "%d\n", devcd_disabled);
127127
}
128128

129129
static ssize_t disabled_store(struct class *class, struct class_attribute *attr,

drivers/base/firmware_loader/fallback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void kill_pending_fw_fallback_reqs(bool only_kill_custom)
124124
static ssize_t timeout_show(struct class *class, struct class_attribute *attr,
125125
char *buf)
126126
{
127-
return sprintf(buf, "%d\n", __firmware_loading_timeout());
127+
return sysfs_emit(buf, "%d\n", __firmware_loading_timeout());
128128
}
129129

130130
/**

drivers/base/memory.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static ssize_t phys_index_show(struct device *dev,
119119
unsigned long phys_index;
120120

121121
phys_index = mem->start_section_nr / sections_per_block;
122+
122123
return sysfs_emit(buf, "%08lx\n", phys_index);
123124
}
124125

@@ -301,6 +302,7 @@ static ssize_t phys_device_show(struct device *dev,
301302
struct device_attribute *attr, char *buf)
302303
{
303304
struct memory_block *mem = to_memory_block(dev);
305+
304306
return sysfs_emit(buf, "%d\n", mem->phys_device);
305307
}
306308

@@ -314,6 +316,7 @@ static int print_allowed_zone(char *buf, int len, int nid,
314316
zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
315317
if (zone == default_zone)
316318
return 0;
319+
317320
return sysfs_emit_at(buf, len, " %s", zone->name);
318321
}
319322

@@ -354,7 +357,7 @@ static ssize_t valid_zones_show(struct device *dev,
354357
len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages,
355358
MMOP_ONLINE_MOVABLE, default_zone);
356359
out:
357-
len += sysfs_emit_at(buf, len, "%s", "\n");
360+
len += sysfs_emit_at(buf, len, "\n");
358361
return len;
359362
}
360363
static DEVICE_ATTR_RO(valid_zones);

0 commit comments

Comments
 (0)