Skip to content

Commit 4744356

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Move vevent memory allocation outside spinlock
The veventq memory allocation happens inside the spinlock. Given its depth is decided by the user space, this leaves a vulnerability, where userspace can allocate large queues to exhaust atomic memory reserves. Move the allocation outside the spinlock and use GFP_NOWAIT, which can fail fast under memory pressure without dipping into the GFP_ATOMIC reserves or direct-reclaiming from the threaded IRQ handler. On allocation failure, queue the lost_events_header (so userspace learns of the drop) and return -ENOMEM so the caller learns of the kernel-side memory pressure. This is intentionally distinct from the queue-overflow path, which also queues the lost_events_header but returns 0: a full queue is an expected userspace-pacing condition rather than a kernel error. A subsequent change will cap the upper bound of the veventq_depth. Fixes: e36ba5a ("iommufd: Add IOMMUFD_OBJ_VEVENTQ and IOMMUFD_CMD_VEVENTQ_ALLOC") Link: https://patch.msgid.link/r/5ff36b5d80f7f6299f851be532a5195c1d2f1dae.1779408671.git.nicolinc@nvidia.com Cc: stable@vger.kernel.org Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 85345be commit 4744356

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

drivers/iommu/iommufd/driver.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,18 @@ int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
149149
goto out_unlock_veventqs;
150150
}
151151

152-
spin_lock(&veventq->common.lock);
153-
if (veventq->num_events == veventq->depth) {
152+
/* Pre-allocate to avoid GFP_ATOMIC; use GFP_NOWAIT to avoid sleeping */
153+
vevent = kzalloc_flex(*vevent, event_data, data_len, GFP_NOWAIT);
154+
if (!vevent) {
155+
spin_lock(&veventq->common.lock);
154156
vevent = &veventq->lost_events_header;
157+
rc = -ENOMEM;
155158
goto out_set_header;
156159
}
157160

158-
vevent = kzalloc_flex(*vevent, event_data, data_len, GFP_ATOMIC);
159-
if (!vevent) {
160-
rc = -ENOMEM;
161+
spin_lock(&veventq->common.lock);
162+
if (veventq->num_events == veventq->depth) {
163+
kfree(vevent);
161164
vevent = &veventq->lost_events_header;
162165
goto out_set_header;
163166
}

0 commit comments

Comments
 (0)