xref: /openbmc/linux/block/blk-ia-ranges.c (revision 5f622417)
1a2247f19SDamien Le Moal // SPDX-License-Identifier: GPL-2.0
2a2247f19SDamien Le Moal /*
3a2247f19SDamien Le Moal  *  Block device concurrent positioning ranges.
4a2247f19SDamien Le Moal  *
5a2247f19SDamien Le Moal  *  Copyright (C) 2021 Western Digital Corporation or its Affiliates.
6a2247f19SDamien Le Moal  */
7a2247f19SDamien Le Moal #include <linux/kernel.h>
8a2247f19SDamien Le Moal #include <linux/blkdev.h>
9a2247f19SDamien Le Moal #include <linux/slab.h>
10a2247f19SDamien Le Moal #include <linux/init.h>
11a2247f19SDamien Le Moal 
12a2247f19SDamien Le Moal #include "blk.h"
13a2247f19SDamien Le Moal 
14a2247f19SDamien Le Moal static ssize_t
blk_ia_range_sector_show(struct blk_independent_access_range * iar,char * buf)15a2247f19SDamien Le Moal blk_ia_range_sector_show(struct blk_independent_access_range *iar,
16a2247f19SDamien Le Moal 			 char *buf)
17a2247f19SDamien Le Moal {
18a2247f19SDamien Le Moal 	return sprintf(buf, "%llu\n", iar->sector);
19a2247f19SDamien Le Moal }
20a2247f19SDamien Le Moal 
21a2247f19SDamien Le Moal static ssize_t
blk_ia_range_nr_sectors_show(struct blk_independent_access_range * iar,char * buf)22a2247f19SDamien Le Moal blk_ia_range_nr_sectors_show(struct blk_independent_access_range *iar,
23a2247f19SDamien Le Moal 			     char *buf)
24a2247f19SDamien Le Moal {
25a2247f19SDamien Le Moal 	return sprintf(buf, "%llu\n", iar->nr_sectors);
26a2247f19SDamien Le Moal }
27a2247f19SDamien Le Moal 
28a2247f19SDamien Le Moal struct blk_ia_range_sysfs_entry {
29a2247f19SDamien Le Moal 	struct attribute attr;
30a2247f19SDamien Le Moal 	ssize_t (*show)(struct blk_independent_access_range *iar, char *buf);
31a2247f19SDamien Le Moal };
32a2247f19SDamien Le Moal 
33a2247f19SDamien Le Moal static struct blk_ia_range_sysfs_entry blk_ia_range_sector_entry = {
34a2247f19SDamien Le Moal 	.attr = { .name = "sector", .mode = 0444 },
35a2247f19SDamien Le Moal 	.show = blk_ia_range_sector_show,
36a2247f19SDamien Le Moal };
37a2247f19SDamien Le Moal 
38a2247f19SDamien Le Moal static struct blk_ia_range_sysfs_entry blk_ia_range_nr_sectors_entry = {
39a2247f19SDamien Le Moal 	.attr = { .name = "nr_sectors", .mode = 0444 },
40a2247f19SDamien Le Moal 	.show = blk_ia_range_nr_sectors_show,
41a2247f19SDamien Le Moal };
42a2247f19SDamien Le Moal 
43a2247f19SDamien Le Moal static struct attribute *blk_ia_range_attrs[] = {
44a2247f19SDamien Le Moal 	&blk_ia_range_sector_entry.attr,
45a2247f19SDamien Le Moal 	&blk_ia_range_nr_sectors_entry.attr,
46a2247f19SDamien Le Moal 	NULL,
47a2247f19SDamien Le Moal };
48a2247f19SDamien Le Moal ATTRIBUTE_GROUPS(blk_ia_range);
49a2247f19SDamien Le Moal 
blk_ia_range_sysfs_show(struct kobject * kobj,struct attribute * attr,char * buf)50a2247f19SDamien Le Moal static ssize_t blk_ia_range_sysfs_show(struct kobject *kobj,
51a2247f19SDamien Le Moal 				      struct attribute *attr, char *buf)
52a2247f19SDamien Le Moal {
53a2247f19SDamien Le Moal 	struct blk_ia_range_sysfs_entry *entry =
54a2247f19SDamien Le Moal 		container_of(attr, struct blk_ia_range_sysfs_entry, attr);
55a2247f19SDamien Le Moal 	struct blk_independent_access_range *iar =
56a2247f19SDamien Le Moal 		container_of(kobj, struct blk_independent_access_range, kobj);
57a2247f19SDamien Le Moal 
5841e46b3cSDamien Le Moal 	return entry->show(iar, buf);
59a2247f19SDamien Le Moal }
60a2247f19SDamien Le Moal 
61a2247f19SDamien Le Moal static const struct sysfs_ops blk_ia_range_sysfs_ops = {
62a2247f19SDamien Le Moal 	.show	= blk_ia_range_sysfs_show,
63a2247f19SDamien Le Moal };
64a2247f19SDamien Le Moal 
65a2247f19SDamien Le Moal /*
66a2247f19SDamien Le Moal  * Independent access range entries are not freed individually, but alltogether
67a2247f19SDamien Le Moal  * with struct blk_independent_access_ranges and its array of ranges. Since
68a2247f19SDamien Le Moal  * kobject_add() takes a reference on the parent kobject contained in
69a2247f19SDamien Le Moal  * struct blk_independent_access_ranges, the array of independent access range
70a2247f19SDamien Le Moal  * entries cannot be freed until kobject_del() is called for all entries.
71a2247f19SDamien Le Moal  * So we do not need to do anything here, but still need this no-op release
72a2247f19SDamien Le Moal  * operation to avoid complaints from the kobject code.
73a2247f19SDamien Le Moal  */
blk_ia_range_sysfs_nop_release(struct kobject * kobj)74a2247f19SDamien Le Moal static void blk_ia_range_sysfs_nop_release(struct kobject *kobj)
75a2247f19SDamien Le Moal {
76a2247f19SDamien Le Moal }
77a2247f19SDamien Le Moal 
78*5f622417SThomas Weißschuh static const struct kobj_type blk_ia_range_ktype = {
79a2247f19SDamien Le Moal 	.sysfs_ops	= &blk_ia_range_sysfs_ops,
80a2247f19SDamien Le Moal 	.default_groups	= blk_ia_range_groups,
81a2247f19SDamien Le Moal 	.release	= blk_ia_range_sysfs_nop_release,
82a2247f19SDamien Le Moal };
83a2247f19SDamien Le Moal 
84a2247f19SDamien Le Moal /*
85a2247f19SDamien Le Moal  * This will be executed only after all independent access range entries are
86a2247f19SDamien Le Moal  * removed with kobject_del(), at which point, it is safe to free everything,
87a2247f19SDamien Le Moal  * including the array of ranges.
88a2247f19SDamien Le Moal  */
blk_ia_ranges_sysfs_release(struct kobject * kobj)89a2247f19SDamien Le Moal static void blk_ia_ranges_sysfs_release(struct kobject *kobj)
90a2247f19SDamien Le Moal {
91a2247f19SDamien Le Moal 	struct blk_independent_access_ranges *iars =
92a2247f19SDamien Le Moal 		container_of(kobj, struct blk_independent_access_ranges, kobj);
93a2247f19SDamien Le Moal 
94a2247f19SDamien Le Moal 	kfree(iars);
95a2247f19SDamien Le Moal }
96a2247f19SDamien Le Moal 
97*5f622417SThomas Weißschuh static const struct kobj_type blk_ia_ranges_ktype = {
98a2247f19SDamien Le Moal 	.release	= blk_ia_ranges_sysfs_release,
99a2247f19SDamien Le Moal };
100a2247f19SDamien Le Moal 
101a2247f19SDamien Le Moal /**
102438cd742SJens Axboe  * disk_register_independent_access_ranges - register with sysfs a set of
103438cd742SJens Axboe  *		independent access ranges
104a2247f19SDamien Le Moal  * @disk:	Target disk
105a2247f19SDamien Le Moal  *
106a2247f19SDamien Le Moal  * Register with sysfs a set of independent access ranges for @disk.
107a2247f19SDamien Le Moal  */
disk_register_independent_access_ranges(struct gendisk * disk)10822d0c408SChristoph Hellwig int disk_register_independent_access_ranges(struct gendisk *disk)
109a2247f19SDamien Le Moal {
11022d0c408SChristoph Hellwig 	struct blk_independent_access_ranges *iars = disk->ia_ranges;
111a2247f19SDamien Le Moal 	struct request_queue *q = disk->queue;
112a2247f19SDamien Le Moal 	int i, ret;
113a2247f19SDamien Le Moal 
114a2247f19SDamien Le Moal 	lockdep_assert_held(&q->sysfs_dir_lock);
115a2247f19SDamien Le Moal 	lockdep_assert_held(&q->sysfs_lock);
116a2247f19SDamien Le Moal 
117a2247f19SDamien Le Moal 	if (!iars)
118a2247f19SDamien Le Moal 		return 0;
119a2247f19SDamien Le Moal 
120a2247f19SDamien Le Moal 	/*
121a2247f19SDamien Le Moal 	 * At this point, iars is the new set of sector access ranges that needs
122a2247f19SDamien Le Moal 	 * to be registered with sysfs.
123a2247f19SDamien Le Moal 	 */
124a2247f19SDamien Le Moal 	WARN_ON(iars->sysfs_registered);
125a2247f19SDamien Le Moal 	ret = kobject_init_and_add(&iars->kobj, &blk_ia_ranges_ktype,
1262bd85221SChristoph Hellwig 				   &disk->queue_kobj, "%s",
1272bd85221SChristoph Hellwig 				   "independent_access_ranges");
128a2247f19SDamien Le Moal 	if (ret) {
1296a27d28cSChristoph Hellwig 		disk->ia_ranges = NULL;
13083114df3SMiaoqian Lin 		kobject_put(&iars->kobj);
131a2247f19SDamien Le Moal 		return ret;
132a2247f19SDamien Le Moal 	}
133a2247f19SDamien Le Moal 
134a2247f19SDamien Le Moal 	for (i = 0; i < iars->nr_ia_ranges; i++) {
135a2247f19SDamien Le Moal 		ret = kobject_init_and_add(&iars->ia_range[i].kobj,
136a2247f19SDamien Le Moal 					   &blk_ia_range_ktype, &iars->kobj,
137a2247f19SDamien Le Moal 					   "%d", i);
138a2247f19SDamien Le Moal 		if (ret) {
139a2247f19SDamien Le Moal 			while (--i >= 0)
140a2247f19SDamien Le Moal 				kobject_del(&iars->ia_range[i].kobj);
141a2247f19SDamien Le Moal 			kobject_del(&iars->kobj);
142a2247f19SDamien Le Moal 			kobject_put(&iars->kobj);
143a2247f19SDamien Le Moal 			return ret;
144a2247f19SDamien Le Moal 		}
145a2247f19SDamien Le Moal 	}
146a2247f19SDamien Le Moal 
147a2247f19SDamien Le Moal 	iars->sysfs_registered = true;
148a2247f19SDamien Le Moal 
149a2247f19SDamien Le Moal 	return 0;
150a2247f19SDamien Le Moal }
151a2247f19SDamien Le Moal 
disk_unregister_independent_access_ranges(struct gendisk * disk)152a2247f19SDamien Le Moal void disk_unregister_independent_access_ranges(struct gendisk *disk)
153a2247f19SDamien Le Moal {
154a2247f19SDamien Le Moal 	struct request_queue *q = disk->queue;
1556a27d28cSChristoph Hellwig 	struct blk_independent_access_ranges *iars = disk->ia_ranges;
156a2247f19SDamien Le Moal 	int i;
157a2247f19SDamien Le Moal 
158a2247f19SDamien Le Moal 	lockdep_assert_held(&q->sysfs_dir_lock);
159a2247f19SDamien Le Moal 	lockdep_assert_held(&q->sysfs_lock);
160a2247f19SDamien Le Moal 
161a2247f19SDamien Le Moal 	if (!iars)
162a2247f19SDamien Le Moal 		return;
163a2247f19SDamien Le Moal 
164a2247f19SDamien Le Moal 	if (iars->sysfs_registered) {
165a2247f19SDamien Le Moal 		for (i = 0; i < iars->nr_ia_ranges; i++)
166a2247f19SDamien Le Moal 			kobject_del(&iars->ia_range[i].kobj);
167a2247f19SDamien Le Moal 		kobject_del(&iars->kobj);
168a2247f19SDamien Le Moal 		kobject_put(&iars->kobj);
169a2247f19SDamien Le Moal 	} else {
170a2247f19SDamien Le Moal 		kfree(iars);
171a2247f19SDamien Le Moal 	}
172a2247f19SDamien Le Moal 
1736a27d28cSChristoph Hellwig 	disk->ia_ranges = NULL;
174a2247f19SDamien Le Moal }
175a2247f19SDamien Le Moal 
176a2247f19SDamien Le Moal static struct blk_independent_access_range *
disk_find_ia_range(struct blk_independent_access_ranges * iars,sector_t sector)177a2247f19SDamien Le Moal disk_find_ia_range(struct blk_independent_access_ranges *iars,
178a2247f19SDamien Le Moal 		  sector_t sector)
179a2247f19SDamien Le Moal {
180a2247f19SDamien Le Moal 	struct blk_independent_access_range *iar;
181a2247f19SDamien Le Moal 	int i;
182a2247f19SDamien Le Moal 
183a2247f19SDamien Le Moal 	for (i = 0; i < iars->nr_ia_ranges; i++) {
184a2247f19SDamien Le Moal 		iar = &iars->ia_range[i];
185a2247f19SDamien Le Moal 		if (sector >= iar->sector &&
186a2247f19SDamien Le Moal 		    sector < iar->sector + iar->nr_sectors)
187a2247f19SDamien Le Moal 			return iar;
188a2247f19SDamien Le Moal 	}
189a2247f19SDamien Le Moal 
190a2247f19SDamien Le Moal 	return NULL;
191a2247f19SDamien Le Moal }
192a2247f19SDamien Le Moal 
disk_check_ia_ranges(struct gendisk * disk,struct blk_independent_access_ranges * iars)193a2247f19SDamien Le Moal static bool disk_check_ia_ranges(struct gendisk *disk,
194a2247f19SDamien Le Moal 				struct blk_independent_access_ranges *iars)
195a2247f19SDamien Le Moal {
196a2247f19SDamien Le Moal 	struct blk_independent_access_range *iar, *tmp;
197a2247f19SDamien Le Moal 	sector_t capacity = get_capacity(disk);
198a2247f19SDamien Le Moal 	sector_t sector = 0;
199a2247f19SDamien Le Moal 	int i;
200a2247f19SDamien Le Moal 
20122d0c408SChristoph Hellwig 	if (WARN_ON_ONCE(!iars->nr_ia_ranges))
20222d0c408SChristoph Hellwig 		return false;
20322d0c408SChristoph Hellwig 
204a2247f19SDamien Le Moal 	/*
205a2247f19SDamien Le Moal 	 * While sorting the ranges in increasing LBA order, check that the
206a2247f19SDamien Le Moal 	 * ranges do not overlap, that there are no sector holes and that all
207a2247f19SDamien Le Moal 	 * sectors belong to one range.
208a2247f19SDamien Le Moal 	 */
209a2247f19SDamien Le Moal 	for (i = 0; i < iars->nr_ia_ranges; i++) {
210a2247f19SDamien Le Moal 		tmp = disk_find_ia_range(iars, sector);
211a2247f19SDamien Le Moal 		if (!tmp || tmp->sector != sector) {
212a2247f19SDamien Le Moal 			pr_warn("Invalid non-contiguous independent access ranges\n");
213a2247f19SDamien Le Moal 			return false;
214a2247f19SDamien Le Moal 		}
215a2247f19SDamien Le Moal 
216a2247f19SDamien Le Moal 		iar = &iars->ia_range[i];
217a2247f19SDamien Le Moal 		if (tmp != iar) {
218a2247f19SDamien Le Moal 			swap(iar->sector, tmp->sector);
219a2247f19SDamien Le Moal 			swap(iar->nr_sectors, tmp->nr_sectors);
220a2247f19SDamien Le Moal 		}
221a2247f19SDamien Le Moal 
222a2247f19SDamien Le Moal 		sector += iar->nr_sectors;
223a2247f19SDamien Le Moal 	}
224a2247f19SDamien Le Moal 
225a2247f19SDamien Le Moal 	if (sector != capacity) {
226a2247f19SDamien Le Moal 		pr_warn("Independent access ranges do not match disk capacity\n");
227a2247f19SDamien Le Moal 		return false;
228a2247f19SDamien Le Moal 	}
229a2247f19SDamien Le Moal 
230a2247f19SDamien Le Moal 	return true;
231a2247f19SDamien Le Moal }
232a2247f19SDamien Le Moal 
disk_ia_ranges_changed(struct gendisk * disk,struct blk_independent_access_ranges * new)233a2247f19SDamien Le Moal static bool disk_ia_ranges_changed(struct gendisk *disk,
234a2247f19SDamien Le Moal 				   struct blk_independent_access_ranges *new)
235a2247f19SDamien Le Moal {
2366a27d28cSChristoph Hellwig 	struct blk_independent_access_ranges *old = disk->ia_ranges;
237a2247f19SDamien Le Moal 	int i;
238a2247f19SDamien Le Moal 
239a2247f19SDamien Le Moal 	if (!old)
240a2247f19SDamien Le Moal 		return true;
241a2247f19SDamien Le Moal 
242a2247f19SDamien Le Moal 	if (old->nr_ia_ranges != new->nr_ia_ranges)
243a2247f19SDamien Le Moal 		return true;
244a2247f19SDamien Le Moal 
245a2247f19SDamien Le Moal 	for (i = 0; i < old->nr_ia_ranges; i++) {
246a2247f19SDamien Le Moal 		if (new->ia_range[i].sector != old->ia_range[i].sector ||
247a2247f19SDamien Le Moal 		    new->ia_range[i].nr_sectors != old->ia_range[i].nr_sectors)
248a2247f19SDamien Le Moal 			return true;
249a2247f19SDamien Le Moal 	}
250a2247f19SDamien Le Moal 
251a2247f19SDamien Le Moal 	return false;
252a2247f19SDamien Le Moal }
253a2247f19SDamien Le Moal 
254a2247f19SDamien Le Moal /**
255a2247f19SDamien Le Moal  * disk_alloc_independent_access_ranges - Allocate an independent access ranges
256a2247f19SDamien Le Moal  *                                        data structure
257a2247f19SDamien Le Moal  * @disk:		target disk
258a2247f19SDamien Le Moal  * @nr_ia_ranges:	Number of independent access ranges
259a2247f19SDamien Le Moal  *
260a2247f19SDamien Le Moal  * Allocate a struct blk_independent_access_ranges structure with @nr_ia_ranges
261a2247f19SDamien Le Moal  * access range descriptors.
262a2247f19SDamien Le Moal  */
263a2247f19SDamien Le Moal struct blk_independent_access_ranges *
disk_alloc_independent_access_ranges(struct gendisk * disk,int nr_ia_ranges)264a2247f19SDamien Le Moal disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges)
265a2247f19SDamien Le Moal {
266a2247f19SDamien Le Moal 	struct blk_independent_access_ranges *iars;
267a2247f19SDamien Le Moal 
268a2247f19SDamien Le Moal 	iars = kzalloc_node(struct_size(iars, ia_range, nr_ia_ranges),
269a2247f19SDamien Le Moal 			    GFP_KERNEL, disk->queue->node);
270a2247f19SDamien Le Moal 	if (iars)
271a2247f19SDamien Le Moal 		iars->nr_ia_ranges = nr_ia_ranges;
272a2247f19SDamien Le Moal 	return iars;
273a2247f19SDamien Le Moal }
274a2247f19SDamien Le Moal EXPORT_SYMBOL_GPL(disk_alloc_independent_access_ranges);
275a2247f19SDamien Le Moal 
276a2247f19SDamien Le Moal /**
277a2247f19SDamien Le Moal  * disk_set_independent_access_ranges - Set a disk independent access ranges
278a2247f19SDamien Le Moal  * @disk:	target disk
279a2247f19SDamien Le Moal  * @iars:	independent access ranges structure
280a2247f19SDamien Le Moal  *
281a2247f19SDamien Le Moal  * Set the independent access ranges information of the request queue
282a2247f19SDamien Le Moal  * of @disk to @iars. If @iars is NULL and the independent access ranges
283a2247f19SDamien Le Moal  * structure already set is cleared. If there are no differences between
284a2247f19SDamien Le Moal  * @iars and the independent access ranges structure already set, @iars
285a2247f19SDamien Le Moal  * is freed.
286a2247f19SDamien Le Moal  */
disk_set_independent_access_ranges(struct gendisk * disk,struct blk_independent_access_ranges * iars)287a2247f19SDamien Le Moal void disk_set_independent_access_ranges(struct gendisk *disk,
288a2247f19SDamien Le Moal 				struct blk_independent_access_ranges *iars)
289a2247f19SDamien Le Moal {
290a2247f19SDamien Le Moal 	struct request_queue *q = disk->queue;
291a2247f19SDamien Le Moal 
292a2247f19SDamien Le Moal 	mutex_lock(&q->sysfs_dir_lock);
293a2247f19SDamien Le Moal 	mutex_lock(&q->sysfs_lock);
29422d0c408SChristoph Hellwig 	if (iars && !disk_check_ia_ranges(disk, iars)) {
295a2247f19SDamien Le Moal 		kfree(iars);
296a2247f19SDamien Le Moal 		iars = NULL;
297a2247f19SDamien Le Moal 	}
29822d0c408SChristoph Hellwig 	if (iars && !disk_ia_ranges_changed(disk, iars)) {
299a2247f19SDamien Le Moal 		kfree(iars);
300a2247f19SDamien Le Moal 		goto unlock;
301a2247f19SDamien Le Moal 	}
302a2247f19SDamien Le Moal 
303a2247f19SDamien Le Moal 	/*
304a2247f19SDamien Le Moal 	 * This may be called for a registered queue. E.g. during a device
305a2247f19SDamien Le Moal 	 * revalidation. If that is the case, we need to unregister the old
306a2247f19SDamien Le Moal 	 * set of independent access ranges and register the new set. If the
307a2247f19SDamien Le Moal 	 * queue is not registered, registration of the device request queue
30822d0c408SChristoph Hellwig 	 * will register the independent access ranges.
309a2247f19SDamien Le Moal 	 */
31022d0c408SChristoph Hellwig 	disk_unregister_independent_access_ranges(disk);
31122d0c408SChristoph Hellwig 	disk->ia_ranges = iars;
31222d0c408SChristoph Hellwig 	if (blk_queue_registered(q))
31322d0c408SChristoph Hellwig 		disk_register_independent_access_ranges(disk);
314a2247f19SDamien Le Moal unlock:
315a2247f19SDamien Le Moal 	mutex_unlock(&q->sysfs_lock);
316a2247f19SDamien Le Moal 	mutex_unlock(&q->sysfs_dir_lock);
317a2247f19SDamien Le Moal }
318a2247f19SDamien Le Moal EXPORT_SYMBOL_GPL(disk_set_independent_access_ranges);
319