Skip to content

Commit 1c9dc07

Browse files
yiliu1765awilliam
authored andcommitted
iommufd: Add iommufd_ctx_from_fd()
It's common to get a reference to the iommufd context from a given file descriptor. So adds an API for it. Existing users of this API are compiled only when IOMMUFD is enabled, so no need to have a stub for the IOMMUFD disabled case. Tested-by: Yanting Jiang <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Yi Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 5c6de3e commit 1c9dc07

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

drivers/iommu/iommufd/main.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,30 @@ struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
377377
}
378378
EXPORT_SYMBOL_NS_GPL(iommufd_ctx_from_file, IOMMUFD);
379379

380+
/**
381+
* iommufd_ctx_from_fd - Acquires a reference to the iommufd context
382+
* @fd: File descriptor to obtain the reference from
383+
*
384+
* Returns a pointer to the iommufd_ctx, otherwise ERR_PTR. On success
385+
* the caller is responsible to call iommufd_ctx_put().
386+
*/
387+
struct iommufd_ctx *iommufd_ctx_from_fd(int fd)
388+
{
389+
struct file *file;
390+
391+
file = fget(fd);
392+
if (!file)
393+
return ERR_PTR(-EBADF);
394+
395+
if (file->f_op != &iommufd_fops) {
396+
fput(file);
397+
return ERR_PTR(-EBADFD);
398+
}
399+
/* fget is the same as iommufd_ctx_get() */
400+
return file->private_data;
401+
}
402+
EXPORT_SYMBOL_NS_GPL(iommufd_ctx_from_fd, IOMMUFD);
403+
380404
/**
381405
* iommufd_ctx_put - Put back a reference
382406
* @ictx: Context to put back

include/linux/iommufd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void iommufd_ctx_get(struct iommufd_ctx *ictx);
5454

5555
#if IS_ENABLED(CONFIG_IOMMUFD)
5656
struct iommufd_ctx *iommufd_ctx_from_file(struct file *file);
57+
struct iommufd_ctx *iommufd_ctx_from_fd(int fd);
5758
void iommufd_ctx_put(struct iommufd_ctx *ictx);
5859
bool iommufd_ctx_has_group(struct iommufd_ctx *ictx, struct iommu_group *group);
5960

0 commit comments

Comments
 (0)