xref: /openbmc/linux/block/blk-sysfs.c (revision 79f08d9e)
1 /*
2  * Functions related to sysfs handling
3  */
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/module.h>
7 #include <linux/bio.h>
8 #include <linux/blkdev.h>
9 #include <linux/blktrace_api.h>
10 #include <linux/blk-mq.h>
11 
12 #include "blk.h"
13 #include "blk-cgroup.h"
14 
15 struct queue_sysfs_entry {
16 	struct attribute attr;
17 	ssize_t (*show)(struct request_queue *, char *);
18 	ssize_t (*store)(struct request_queue *, const char *, size_t);
19 };
20 
21 static ssize_t
22 queue_var_show(unsigned long var, char *page)
23 {
24 	return sprintf(page, "%lu\n", var);
25 }
26 
27 static ssize_t
28 queue_var_store(unsigned long *var, const char *page, size_t count)
29 {
30 	int err;
31 	unsigned long v;
32 
33 	err = kstrtoul(page, 10, &v);
34 	if (err || v > UINT_MAX)
35 		return -EINVAL;
36 
37 	*var = v;
38 
39 	return count;
40 }
41 
42 static ssize_t queue_requests_show(struct request_queue *q, char *page)
43 {
44 	return queue_var_show(q->nr_requests, (page));
45 }
46 
47 static ssize_t
48 queue_requests_store(struct request_queue *q, const char *page, size_t count)
49 {
50 	struct request_list *rl;
51 	unsigned long nr;
52 	int ret;
53 
54 	if (!q->request_fn)
55 		return -EINVAL;
56 
57 	ret = queue_var_store(&nr, page, count);
58 	if (ret < 0)
59 		return ret;
60 
61 	if (nr < BLKDEV_MIN_RQ)
62 		nr = BLKDEV_MIN_RQ;
63 
64 	spin_lock_irq(q->queue_lock);
65 	q->nr_requests = nr;
66 	blk_queue_congestion_threshold(q);
67 
68 	/* congestion isn't cgroup aware and follows root blkcg for now */
69 	rl = &q->root_rl;
70 
71 	if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
72 		blk_set_queue_congested(q, BLK_RW_SYNC);
73 	else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
74 		blk_clear_queue_congested(q, BLK_RW_SYNC);
75 
76 	if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
77 		blk_set_queue_congested(q, BLK_RW_ASYNC);
78 	else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
79 		blk_clear_queue_congested(q, BLK_RW_ASYNC);
80 
81 	blk_queue_for_each_rl(rl, q) {
82 		if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
83 			blk_set_rl_full(rl, BLK_RW_SYNC);
84 		} else {
85 			blk_clear_rl_full(rl, BLK_RW_SYNC);
86 			wake_up(&rl->wait[BLK_RW_SYNC]);
87 		}
88 
89 		if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
90 			blk_set_rl_full(rl, BLK_RW_ASYNC);
91 		} else {
92 			blk_clear_rl_full(rl, BLK_RW_ASYNC);
93 			wake_up(&rl->wait[BLK_RW_ASYNC]);
94 		}
95 	}
96 
97 	spin_unlock_irq(q->queue_lock);
98 	return ret;
99 }
100 
101 static ssize_t queue_ra_show(struct request_queue *q, char *page)
102 {
103 	unsigned long ra_kb = q->backing_dev_info.ra_pages <<
104 					(PAGE_CACHE_SHIFT - 10);
105 
106 	return queue_var_show(ra_kb, (page));
107 }
108 
109 static ssize_t
110 queue_ra_store(struct request_queue *q, const char *page, size_t count)
111 {
112 	unsigned long ra_kb;
113 	ssize_t ret = queue_var_store(&ra_kb, page, count);
114 
115 	if (ret < 0)
116 		return ret;
117 
118 	q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
119 
120 	return ret;
121 }
122 
123 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
124 {
125 	int max_sectors_kb = queue_max_sectors(q) >> 1;
126 
127 	return queue_var_show(max_sectors_kb, (page));
128 }
129 
130 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
131 {
132 	return queue_var_show(queue_max_segments(q), (page));
133 }
134 
135 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
136 {
137 	return queue_var_show(q->limits.max_integrity_segments, (page));
138 }
139 
140 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
141 {
142 	if (blk_queue_cluster(q))
143 		return queue_var_show(queue_max_segment_size(q), (page));
144 
145 	return queue_var_show(PAGE_CACHE_SIZE, (page));
146 }
147 
148 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
149 {
150 	return queue_var_show(queue_logical_block_size(q), page);
151 }
152 
153 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
154 {
155 	return queue_var_show(queue_physical_block_size(q), page);
156 }
157 
158 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
159 {
160 	return queue_var_show(queue_io_min(q), page);
161 }
162 
163 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
164 {
165 	return queue_var_show(queue_io_opt(q), page);
166 }
167 
168 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
169 {
170 	return queue_var_show(q->limits.discard_granularity, page);
171 }
172 
173 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
174 {
175 	return sprintf(page, "%llu\n",
176 		       (unsigned long long)q->limits.max_discard_sectors << 9);
177 }
178 
179 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
180 {
181 	return queue_var_show(queue_discard_zeroes_data(q), page);
182 }
183 
184 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
185 {
186 	return sprintf(page, "%llu\n",
187 		(unsigned long long)q->limits.max_write_same_sectors << 9);
188 }
189 
190 
191 static ssize_t
192 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
193 {
194 	unsigned long max_sectors_kb,
195 		max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
196 			page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
197 	ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
198 
199 	if (ret < 0)
200 		return ret;
201 
202 	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
203 		return -EINVAL;
204 
205 	spin_lock_irq(q->queue_lock);
206 	q->limits.max_sectors = max_sectors_kb << 1;
207 	spin_unlock_irq(q->queue_lock);
208 
209 	return ret;
210 }
211 
212 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
213 {
214 	int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
215 
216 	return queue_var_show(max_hw_sectors_kb, (page));
217 }
218 
219 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)				\
220 static ssize_t								\
221 queue_show_##name(struct request_queue *q, char *page)			\
222 {									\
223 	int bit;							\
224 	bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);		\
225 	return queue_var_show(neg ? !bit : bit, page);			\
226 }									\
227 static ssize_t								\
228 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
229 {									\
230 	unsigned long val;						\
231 	ssize_t ret;							\
232 	ret = queue_var_store(&val, page, count);			\
233 	if (ret < 0)							\
234 		 return ret;						\
235 	if (neg)							\
236 		val = !val;						\
237 									\
238 	spin_lock_irq(q->queue_lock);					\
239 	if (val)							\
240 		queue_flag_set(QUEUE_FLAG_##flag, q);			\
241 	else								\
242 		queue_flag_clear(QUEUE_FLAG_##flag, q);			\
243 	spin_unlock_irq(q->queue_lock);					\
244 	return ret;							\
245 }
246 
247 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
248 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
249 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
250 #undef QUEUE_SYSFS_BIT_FNS
251 
252 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
253 {
254 	return queue_var_show((blk_queue_nomerges(q) << 1) |
255 			       blk_queue_noxmerges(q), page);
256 }
257 
258 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
259 				    size_t count)
260 {
261 	unsigned long nm;
262 	ssize_t ret = queue_var_store(&nm, page, count);
263 
264 	if (ret < 0)
265 		return ret;
266 
267 	spin_lock_irq(q->queue_lock);
268 	queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
269 	queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
270 	if (nm == 2)
271 		queue_flag_set(QUEUE_FLAG_NOMERGES, q);
272 	else if (nm)
273 		queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
274 	spin_unlock_irq(q->queue_lock);
275 
276 	return ret;
277 }
278 
279 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
280 {
281 	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
282 	bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
283 
284 	return queue_var_show(set << force, page);
285 }
286 
287 static ssize_t
288 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
289 {
290 	ssize_t ret = -EINVAL;
291 #ifdef CONFIG_SMP
292 	unsigned long val;
293 
294 	ret = queue_var_store(&val, page, count);
295 	if (ret < 0)
296 		return ret;
297 
298 	spin_lock_irq(q->queue_lock);
299 	if (val == 2) {
300 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
301 		queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
302 	} else if (val == 1) {
303 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
304 		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
305 	} else if (val == 0) {
306 		queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
307 		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
308 	}
309 	spin_unlock_irq(q->queue_lock);
310 #endif
311 	return ret;
312 }
313 
314 static struct queue_sysfs_entry queue_requests_entry = {
315 	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
316 	.show = queue_requests_show,
317 	.store = queue_requests_store,
318 };
319 
320 static struct queue_sysfs_entry queue_ra_entry = {
321 	.attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
322 	.show = queue_ra_show,
323 	.store = queue_ra_store,
324 };
325 
326 static struct queue_sysfs_entry queue_max_sectors_entry = {
327 	.attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
328 	.show = queue_max_sectors_show,
329 	.store = queue_max_sectors_store,
330 };
331 
332 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
333 	.attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
334 	.show = queue_max_hw_sectors_show,
335 };
336 
337 static struct queue_sysfs_entry queue_max_segments_entry = {
338 	.attr = {.name = "max_segments", .mode = S_IRUGO },
339 	.show = queue_max_segments_show,
340 };
341 
342 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
343 	.attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
344 	.show = queue_max_integrity_segments_show,
345 };
346 
347 static struct queue_sysfs_entry queue_max_segment_size_entry = {
348 	.attr = {.name = "max_segment_size", .mode = S_IRUGO },
349 	.show = queue_max_segment_size_show,
350 };
351 
352 static struct queue_sysfs_entry queue_iosched_entry = {
353 	.attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
354 	.show = elv_iosched_show,
355 	.store = elv_iosched_store,
356 };
357 
358 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
359 	.attr = {.name = "hw_sector_size", .mode = S_IRUGO },
360 	.show = queue_logical_block_size_show,
361 };
362 
363 static struct queue_sysfs_entry queue_logical_block_size_entry = {
364 	.attr = {.name = "logical_block_size", .mode = S_IRUGO },
365 	.show = queue_logical_block_size_show,
366 };
367 
368 static struct queue_sysfs_entry queue_physical_block_size_entry = {
369 	.attr = {.name = "physical_block_size", .mode = S_IRUGO },
370 	.show = queue_physical_block_size_show,
371 };
372 
373 static struct queue_sysfs_entry queue_io_min_entry = {
374 	.attr = {.name = "minimum_io_size", .mode = S_IRUGO },
375 	.show = queue_io_min_show,
376 };
377 
378 static struct queue_sysfs_entry queue_io_opt_entry = {
379 	.attr = {.name = "optimal_io_size", .mode = S_IRUGO },
380 	.show = queue_io_opt_show,
381 };
382 
383 static struct queue_sysfs_entry queue_discard_granularity_entry = {
384 	.attr = {.name = "discard_granularity", .mode = S_IRUGO },
385 	.show = queue_discard_granularity_show,
386 };
387 
388 static struct queue_sysfs_entry queue_discard_max_entry = {
389 	.attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
390 	.show = queue_discard_max_show,
391 };
392 
393 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
394 	.attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
395 	.show = queue_discard_zeroes_data_show,
396 };
397 
398 static struct queue_sysfs_entry queue_write_same_max_entry = {
399 	.attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
400 	.show = queue_write_same_max_show,
401 };
402 
403 static struct queue_sysfs_entry queue_nonrot_entry = {
404 	.attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
405 	.show = queue_show_nonrot,
406 	.store = queue_store_nonrot,
407 };
408 
409 static struct queue_sysfs_entry queue_nomerges_entry = {
410 	.attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
411 	.show = queue_nomerges_show,
412 	.store = queue_nomerges_store,
413 };
414 
415 static struct queue_sysfs_entry queue_rq_affinity_entry = {
416 	.attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
417 	.show = queue_rq_affinity_show,
418 	.store = queue_rq_affinity_store,
419 };
420 
421 static struct queue_sysfs_entry queue_iostats_entry = {
422 	.attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
423 	.show = queue_show_iostats,
424 	.store = queue_store_iostats,
425 };
426 
427 static struct queue_sysfs_entry queue_random_entry = {
428 	.attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
429 	.show = queue_show_random,
430 	.store = queue_store_random,
431 };
432 
433 static struct attribute *default_attrs[] = {
434 	&queue_requests_entry.attr,
435 	&queue_ra_entry.attr,
436 	&queue_max_hw_sectors_entry.attr,
437 	&queue_max_sectors_entry.attr,
438 	&queue_max_segments_entry.attr,
439 	&queue_max_integrity_segments_entry.attr,
440 	&queue_max_segment_size_entry.attr,
441 	&queue_iosched_entry.attr,
442 	&queue_hw_sector_size_entry.attr,
443 	&queue_logical_block_size_entry.attr,
444 	&queue_physical_block_size_entry.attr,
445 	&queue_io_min_entry.attr,
446 	&queue_io_opt_entry.attr,
447 	&queue_discard_granularity_entry.attr,
448 	&queue_discard_max_entry.attr,
449 	&queue_discard_zeroes_data_entry.attr,
450 	&queue_write_same_max_entry.attr,
451 	&queue_nonrot_entry.attr,
452 	&queue_nomerges_entry.attr,
453 	&queue_rq_affinity_entry.attr,
454 	&queue_iostats_entry.attr,
455 	&queue_random_entry.attr,
456 	NULL,
457 };
458 
459 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
460 
461 static ssize_t
462 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
463 {
464 	struct queue_sysfs_entry *entry = to_queue(attr);
465 	struct request_queue *q =
466 		container_of(kobj, struct request_queue, kobj);
467 	ssize_t res;
468 
469 	if (!entry->show)
470 		return -EIO;
471 	mutex_lock(&q->sysfs_lock);
472 	if (blk_queue_dying(q)) {
473 		mutex_unlock(&q->sysfs_lock);
474 		return -ENOENT;
475 	}
476 	res = entry->show(q, page);
477 	mutex_unlock(&q->sysfs_lock);
478 	return res;
479 }
480 
481 static ssize_t
482 queue_attr_store(struct kobject *kobj, struct attribute *attr,
483 		    const char *page, size_t length)
484 {
485 	struct queue_sysfs_entry *entry = to_queue(attr);
486 	struct request_queue *q;
487 	ssize_t res;
488 
489 	if (!entry->store)
490 		return -EIO;
491 
492 	q = container_of(kobj, struct request_queue, kobj);
493 	mutex_lock(&q->sysfs_lock);
494 	if (blk_queue_dying(q)) {
495 		mutex_unlock(&q->sysfs_lock);
496 		return -ENOENT;
497 	}
498 	res = entry->store(q, page, length);
499 	mutex_unlock(&q->sysfs_lock);
500 	return res;
501 }
502 
503 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
504 {
505 	struct request_queue *q = container_of(rcu_head, struct request_queue,
506 					       rcu_head);
507 	kmem_cache_free(blk_requestq_cachep, q);
508 }
509 
510 /**
511  * blk_release_queue: - release a &struct request_queue when it is no longer needed
512  * @kobj:    the kobj belonging to the request queue to be released
513  *
514  * Description:
515  *     blk_release_queue is the pair to blk_init_queue() or
516  *     blk_queue_make_request().  It should be called when a request queue is
517  *     being released; typically when a block device is being de-registered.
518  *     Currently, its primary task it to free all the &struct request
519  *     structures that were allocated to the queue and the queue itself.
520  *
521  * Caveat:
522  *     Hopefully the low level driver will have finished any
523  *     outstanding requests first...
524  **/
525 static void blk_release_queue(struct kobject *kobj)
526 {
527 	struct request_queue *q =
528 		container_of(kobj, struct request_queue, kobj);
529 
530 	blk_sync_queue(q);
531 
532 	blkcg_exit_queue(q);
533 
534 	if (q->elevator) {
535 		spin_lock_irq(q->queue_lock);
536 		ioc_clear_queue(q);
537 		spin_unlock_irq(q->queue_lock);
538 		elevator_exit(q->elevator);
539 	}
540 
541 	blk_exit_rl(&q->root_rl);
542 
543 	if (q->queue_tags)
544 		__blk_queue_free_tags(q);
545 
546 	percpu_counter_destroy(&q->mq_usage_counter);
547 
548 	if (q->mq_ops)
549 		blk_mq_free_queue(q);
550 
551 	blk_trace_shutdown(q);
552 
553 	bdi_destroy(&q->backing_dev_info);
554 
555 	ida_simple_remove(&blk_queue_ida, q->id);
556 	call_rcu(&q->rcu_head, blk_free_queue_rcu);
557 }
558 
559 static const struct sysfs_ops queue_sysfs_ops = {
560 	.show	= queue_attr_show,
561 	.store	= queue_attr_store,
562 };
563 
564 struct kobj_type blk_queue_ktype = {
565 	.sysfs_ops	= &queue_sysfs_ops,
566 	.default_attrs	= default_attrs,
567 	.release	= blk_release_queue,
568 };
569 
570 int blk_register_queue(struct gendisk *disk)
571 {
572 	int ret;
573 	struct device *dev = disk_to_dev(disk);
574 	struct request_queue *q = disk->queue;
575 
576 	if (WARN_ON(!q))
577 		return -ENXIO;
578 
579 	/*
580 	 * Initialization must be complete by now.  Finish the initial
581 	 * bypass from queue allocation.
582 	 */
583 	blk_queue_bypass_end(q);
584 	queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
585 
586 	ret = blk_trace_init_sysfs(dev);
587 	if (ret)
588 		return ret;
589 
590 	ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
591 	if (ret < 0) {
592 		blk_trace_remove_sysfs(dev);
593 		return ret;
594 	}
595 
596 	kobject_uevent(&q->kobj, KOBJ_ADD);
597 
598 	if (q->mq_ops)
599 		blk_mq_register_disk(disk);
600 
601 	if (!q->request_fn)
602 		return 0;
603 
604 	ret = elv_register_queue(q);
605 	if (ret) {
606 		kobject_uevent(&q->kobj, KOBJ_REMOVE);
607 		kobject_del(&q->kobj);
608 		blk_trace_remove_sysfs(dev);
609 		kobject_put(&dev->kobj);
610 		return ret;
611 	}
612 
613 	return 0;
614 }
615 
616 void blk_unregister_queue(struct gendisk *disk)
617 {
618 	struct request_queue *q = disk->queue;
619 
620 	if (WARN_ON(!q))
621 		return;
622 
623 	if (q->mq_ops)
624 		blk_mq_unregister_disk(disk);
625 
626 	if (q->request_fn)
627 		elv_unregister_queue(q);
628 
629 	kobject_uevent(&q->kobj, KOBJ_REMOVE);
630 	kobject_del(&q->kobj);
631 	blk_trace_remove_sysfs(disk_to_dev(disk));
632 	kobject_put(&disk_to_dev(disk)->kobj);
633 }
634