Skip to content

Commit dab7490

Browse files
hghimirambrost05
authored andcommitted
drm/gpusvm: Make drm_gpusvm_for_each_* macros public
The drm_gpusvm_for_each_notifier, drm_gpusvm_for_each_notifier_safe and drm_gpusvm_for_each_range_safe macros are useful for locating notifiers and ranges within a user-specified range. By making these macros public, we enable broader access and utility for developers who need to leverage them in their implementations. v2 (Matthew Brost) - drop inline __drm_gpusvm_range_find - /s/notifier_iter_first/drm_gpusvm_notifier_find Signed-off-by: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com> Reviewed-by: Matthew Brost <matthew.brost@intel.com> Signed-off-by: Matthew Brost <matthew.brost@intel.com> Link: https://lore.kernel.org/r/20250819162058.2777306-5-himal.prasad.ghimiray@intel.com
1 parent baf1638 commit dab7490

2 files changed

Lines changed: 95 additions & 97 deletions

File tree

drivers/gpu/drm/drm_gpusvm.c

Lines changed: 25 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -271,107 +271,50 @@ npages_in_range(unsigned long start, unsigned long end)
271271
}
272272

273273
/**
274-
* drm_gpusvm_range_find() - Find GPU SVM range from GPU SVM notifier
275-
* @notifier: Pointer to the GPU SVM notifier structure.
276-
* @start: Start address of the range
277-
* @end: End address of the range
274+
* drm_gpusvm_notifier_find() - Find GPU SVM notifier from GPU SVM
275+
* @gpusvm: Pointer to the GPU SVM structure.
276+
* @start: Start address of the notifier
277+
* @end: End address of the notifier
278278
*
279-
* Return: A pointer to the drm_gpusvm_range if found or NULL
279+
* Return: A pointer to the drm_gpusvm_notifier if found or NULL
280280
*/
281-
struct drm_gpusvm_range *
282-
drm_gpusvm_range_find(struct drm_gpusvm_notifier *notifier, unsigned long start,
283-
unsigned long end)
281+
struct drm_gpusvm_notifier *
282+
drm_gpusvm_notifier_find(struct drm_gpusvm *gpusvm, unsigned long start,
283+
unsigned long end)
284284
{
285285
struct interval_tree_node *itree;
286286

287-
itree = interval_tree_iter_first(&notifier->root, start, end - 1);
287+
itree = interval_tree_iter_first(&gpusvm->root, start, end - 1);
288288

289289
if (itree)
290-
return container_of(itree, struct drm_gpusvm_range, itree);
290+
return container_of(itree, struct drm_gpusvm_notifier, itree);
291291
else
292292
return NULL;
293293
}
294-
EXPORT_SYMBOL_GPL(drm_gpusvm_range_find);
294+
EXPORT_SYMBOL_GPL(drm_gpusvm_notifier_find);
295295

296296
/**
297-
* drm_gpusvm_for_each_range_safe() - Safely iterate over GPU SVM ranges in a notifier
298-
* @range__: Iterator variable for the ranges
299-
* @next__: Iterator variable for the ranges temporay storage
300-
* @notifier__: Pointer to the GPU SVM notifier
301-
* @start__: Start address of the range
302-
* @end__: End address of the range
303-
*
304-
* This macro is used to iterate over GPU SVM ranges in a notifier while
305-
* removing ranges from it.
306-
*/
307-
#define drm_gpusvm_for_each_range_safe(range__, next__, notifier__, start__, end__) \
308-
for ((range__) = drm_gpusvm_range_find((notifier__), (start__), (end__)), \
309-
(next__) = __drm_gpusvm_range_next(range__); \
310-
(range__) && (drm_gpusvm_range_start(range__) < (end__)); \
311-
(range__) = (next__), (next__) = __drm_gpusvm_range_next(range__))
312-
313-
/**
314-
* __drm_gpusvm_notifier_next() - get the next drm_gpusvm_notifier in the list
315-
* @notifier: a pointer to the current drm_gpusvm_notifier
297+
* drm_gpusvm_range_find() - Find GPU SVM range from GPU SVM notifier
298+
* @notifier: Pointer to the GPU SVM notifier structure.
299+
* @start: Start address of the range
300+
* @end: End address of the range
316301
*
317-
* Return: A pointer to the next drm_gpusvm_notifier if available, or NULL if
318-
* the current notifier is the last one or if the input notifier is
319-
* NULL.
302+
* Return: A pointer to the drm_gpusvm_range if found or NULL
320303
*/
321-
static struct drm_gpusvm_notifier *
322-
__drm_gpusvm_notifier_next(struct drm_gpusvm_notifier *notifier)
323-
{
324-
if (notifier && !list_is_last(&notifier->entry,
325-
&notifier->gpusvm->notifier_list))
326-
return list_next_entry(notifier, entry);
327-
328-
return NULL;
329-
}
330-
331-
static struct drm_gpusvm_notifier *
332-
notifier_iter_first(struct rb_root_cached *root, unsigned long start,
333-
unsigned long last)
304+
struct drm_gpusvm_range *
305+
drm_gpusvm_range_find(struct drm_gpusvm_notifier *notifier, unsigned long start,
306+
unsigned long end)
334307
{
335308
struct interval_tree_node *itree;
336309

337-
itree = interval_tree_iter_first(root, start, last);
310+
itree = interval_tree_iter_first(&notifier->root, start, end - 1);
338311

339312
if (itree)
340-
return container_of(itree, struct drm_gpusvm_notifier, itree);
313+
return container_of(itree, struct drm_gpusvm_range, itree);
341314
else
342315
return NULL;
343316
}
344-
345-
/**
346-
* drm_gpusvm_for_each_notifier() - Iterate over GPU SVM notifiers in a gpusvm
347-
* @notifier__: Iterator variable for the notifiers
348-
* @notifier__: Pointer to the GPU SVM notifier
349-
* @start__: Start address of the notifier
350-
* @end__: End address of the notifier
351-
*
352-
* This macro is used to iterate over GPU SVM notifiers in a gpusvm.
353-
*/
354-
#define drm_gpusvm_for_each_notifier(notifier__, gpusvm__, start__, end__) \
355-
for ((notifier__) = notifier_iter_first(&(gpusvm__)->root, (start__), (end__) - 1); \
356-
(notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__)); \
357-
(notifier__) = __drm_gpusvm_notifier_next(notifier__))
358-
359-
/**
360-
* drm_gpusvm_for_each_notifier_safe() - Safely iterate over GPU SVM notifiers in a gpusvm
361-
* @notifier__: Iterator variable for the notifiers
362-
* @next__: Iterator variable for the notifiers temporay storage
363-
* @notifier__: Pointer to the GPU SVM notifier
364-
* @start__: Start address of the notifier
365-
* @end__: End address of the notifier
366-
*
367-
* This macro is used to iterate over GPU SVM notifiers in a gpusvm while
368-
* removing notifiers from it.
369-
*/
370-
#define drm_gpusvm_for_each_notifier_safe(notifier__, next__, gpusvm__, start__, end__) \
371-
for ((notifier__) = notifier_iter_first(&(gpusvm__)->root, (start__), (end__) - 1), \
372-
(next__) = __drm_gpusvm_notifier_next(notifier__); \
373-
(notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__)); \
374-
(notifier__) = (next__), (next__) = __drm_gpusvm_notifier_next(notifier__))
317+
EXPORT_SYMBOL_GPL(drm_gpusvm_range_find);
375318

376319
/**
377320
* drm_gpusvm_notifier_invalidate() - Invalidate a GPU SVM notifier.
@@ -472,22 +415,6 @@ int drm_gpusvm_init(struct drm_gpusvm *gpusvm,
472415
}
473416
EXPORT_SYMBOL_GPL(drm_gpusvm_init);
474417

475-
/**
476-
* drm_gpusvm_notifier_find() - Find GPU SVM notifier
477-
* @gpusvm: Pointer to the GPU SVM structure
478-
* @fault_addr: Fault address
479-
*
480-
* This function finds the GPU SVM notifier associated with the fault address.
481-
*
482-
* Return: Pointer to the GPU SVM notifier on success, NULL otherwise.
483-
*/
484-
static struct drm_gpusvm_notifier *
485-
drm_gpusvm_notifier_find(struct drm_gpusvm *gpusvm,
486-
unsigned long fault_addr)
487-
{
488-
return notifier_iter_first(&gpusvm->root, fault_addr, fault_addr + 1);
489-
}
490-
491418
/**
492419
* to_drm_gpusvm_notifier() - retrieve the container struct for a given rbtree node
493420
* @node: a pointer to the rbtree node embedded within a drm_gpusvm_notifier struct
@@ -943,7 +870,7 @@ drm_gpusvm_range_find_or_insert(struct drm_gpusvm *gpusvm,
943870
if (!mmget_not_zero(mm))
944871
return ERR_PTR(-EFAULT);
945872

946-
notifier = drm_gpusvm_notifier_find(gpusvm, fault_addr);
873+
notifier = drm_gpusvm_notifier_find(gpusvm, fault_addr, fault_addr + 1);
947874
if (!notifier) {
948875
notifier = drm_gpusvm_notifier_alloc(gpusvm, fault_addr);
949876
if (IS_ERR(notifier)) {
@@ -1107,7 +1034,8 @@ void drm_gpusvm_range_remove(struct drm_gpusvm *gpusvm,
11071034
drm_gpusvm_driver_lock_held(gpusvm);
11081035

11091036
notifier = drm_gpusvm_notifier_find(gpusvm,
1110-
drm_gpusvm_range_start(range));
1037+
drm_gpusvm_range_start(range),
1038+
drm_gpusvm_range_start(range) + 1);
11111039
if (WARN_ON_ONCE(!notifier))
11121040
return;
11131041

include/drm/drm_gpusvm.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ void drm_gpusvm_range_unmap_pages(struct drm_gpusvm *gpusvm,
282282
bool drm_gpusvm_has_mapping(struct drm_gpusvm *gpusvm, unsigned long start,
283283
unsigned long end);
284284

285+
struct drm_gpusvm_notifier *
286+
drm_gpusvm_notifier_find(struct drm_gpusvm *gpusvm, unsigned long start,
287+
unsigned long end);
288+
285289
struct drm_gpusvm_range *
286290
drm_gpusvm_range_find(struct drm_gpusvm_notifier *notifier, unsigned long start,
287291
unsigned long end);
@@ -434,4 +438,70 @@ __drm_gpusvm_range_next(struct drm_gpusvm_range *range)
434438
(range__) && (drm_gpusvm_range_start(range__) < (end__)); \
435439
(range__) = __drm_gpusvm_range_next(range__))
436440

441+
/**
442+
* drm_gpusvm_for_each_range_safe() - Safely iterate over GPU SVM ranges in a notifier
443+
* @range__: Iterator variable for the ranges
444+
* @next__: Iterator variable for the ranges temporay storage
445+
* @notifier__: Pointer to the GPU SVM notifier
446+
* @start__: Start address of the range
447+
* @end__: End address of the range
448+
*
449+
* This macro is used to iterate over GPU SVM ranges in a notifier while
450+
* removing ranges from it.
451+
*/
452+
#define drm_gpusvm_for_each_range_safe(range__, next__, notifier__, start__, end__) \
453+
for ((range__) = drm_gpusvm_range_find((notifier__), (start__), (end__)), \
454+
(next__) = __drm_gpusvm_range_next(range__); \
455+
(range__) && (drm_gpusvm_range_start(range__) < (end__)); \
456+
(range__) = (next__), (next__) = __drm_gpusvm_range_next(range__))
457+
458+
/**
459+
* __drm_gpusvm_notifier_next() - get the next drm_gpusvm_notifier in the list
460+
* @notifier: a pointer to the current drm_gpusvm_notifier
461+
*
462+
* Return: A pointer to the next drm_gpusvm_notifier if available, or NULL if
463+
* the current notifier is the last one or if the input notifier is
464+
* NULL.
465+
*/
466+
static inline struct drm_gpusvm_notifier *
467+
__drm_gpusvm_notifier_next(struct drm_gpusvm_notifier *notifier)
468+
{
469+
if (notifier && !list_is_last(&notifier->entry,
470+
&notifier->gpusvm->notifier_list))
471+
return list_next_entry(notifier, entry);
472+
473+
return NULL;
474+
}
475+
476+
/**
477+
* drm_gpusvm_for_each_notifier() - Iterate over GPU SVM notifiers in a gpusvm
478+
* @notifier__: Iterator variable for the notifiers
479+
* @gpusvm__: Pointer to the GPU SVM notifier
480+
* @start__: Start address of the notifier
481+
* @end__: End address of the notifier
482+
*
483+
* This macro is used to iterate over GPU SVM notifiers in a gpusvm.
484+
*/
485+
#define drm_gpusvm_for_each_notifier(notifier__, gpusvm__, start__, end__) \
486+
for ((notifier__) = drm_gpusvm_notifier_find((gpusvm__), (start__), (end__)); \
487+
(notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__)); \
488+
(notifier__) = __drm_gpusvm_notifier_next(notifier__))
489+
490+
/**
491+
* drm_gpusvm_for_each_notifier_safe() - Safely iterate over GPU SVM notifiers in a gpusvm
492+
* @notifier__: Iterator variable for the notifiers
493+
* @next__: Iterator variable for the notifiers temporay storage
494+
* @gpusvm__: Pointer to the GPU SVM notifier
495+
* @start__: Start address of the notifier
496+
* @end__: End address of the notifier
497+
*
498+
* This macro is used to iterate over GPU SVM notifiers in a gpusvm while
499+
* removing notifiers from it.
500+
*/
501+
#define drm_gpusvm_for_each_notifier_safe(notifier__, next__, gpusvm__, start__, end__) \
502+
for ((notifier__) = drm_gpusvm_notifier_find((gpusvm__), (start__), (end__)), \
503+
(next__) = __drm_gpusvm_notifier_next(notifier__); \
504+
(notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__)); \
505+
(notifier__) = (next__), (next__) = __drm_gpusvm_notifier_next(notifier__))
506+
437507
#endif /* __DRM_GPUSVM_H__ */

0 commit comments

Comments
 (0)