Skip to content

Commit ad7b4e8

Browse files
Frederic Barratmpe
authored andcommitted
cxl: Fix possible deadlock when processing page faults from cxllib
cxllib_handle_fault() is called by an external driver when it needs to have the host resolve page faults for a buffer. The buffer can cover several pages and VMAs. The function iterates over all the pages used by the buffer, based on the page size of the VMA. To ensure some stability while processing the faults, the thread T1 grabs the mm->mmap_sem semaphore with read access (R1). However, when processing a page fault for a single page, one of the underlying functions, copro_handle_mm_fault(), also grabs the same semaphore with read access (R2). So the thread T1 takes the semaphore twice. If another thread T2 tries to access the semaphore in write mode W1 (say, because it wants to allocate memory and calls 'brk'), then that thread T2 will have to wait because there's a reader (R1). If the thread T1 is processing a new page at that time, it won't get an automatic grant at R2, because there's now a writer thread waiting (T2). And we have a deadlock. The timeline is: 1. thread T1 owns the semaphore with read access R1 2. thread T2 requests write access W1 and waits 3. thread T1 requests read access R2 and waits The fix is for the thread T1 to release the semaphore R1 once it got the information it needs from the current VMA. The address space/VMAs could evolve while T1 iterates over the full buffer, but in the unlikely case where T1 misses a page, the external driver will raise a new page fault when retrying the memory access. Fixes: 3ced8d7 ("cxl: Export library to support IBM XSL") Cc: [email protected] # 4.13+ Signed-off-by: Frederic Barrat <[email protected]> Signed-off-by: Michael Ellerman <[email protected]>
1 parent 5d6a03e commit ad7b4e8

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

drivers/misc/cxl/cxllib.c

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -208,49 +208,74 @@ int cxllib_get_PE_attributes(struct task_struct *task,
208208
}
209209
EXPORT_SYMBOL_GPL(cxllib_get_PE_attributes);
210210

211-
int cxllib_handle_fault(struct mm_struct *mm, u64 addr, u64 size, u64 flags)
211+
static int get_vma_info(struct mm_struct *mm, u64 addr,
212+
u64 *vma_start, u64 *vma_end,
213+
unsigned long *page_size)
212214
{
213-
int rc;
214-
u64 dar;
215215
struct vm_area_struct *vma = NULL;
216-
unsigned long page_size;
217-
218-
if (mm == NULL)
219-
return -EFAULT;
216+
int rc = 0;
220217

221218
down_read(&mm->mmap_sem);
222219

223220
vma = find_vma(mm, addr);
224221
if (!vma) {
225-
pr_err("Can't find vma for addr %016llx\n", addr);
226222
rc = -EFAULT;
227223
goto out;
228224
}
229-
/* get the size of the pages allocated */
230-
page_size = vma_kernel_pagesize(vma);
231-
232-
for (dar = (addr & ~(page_size - 1)); dar < (addr + size); dar += page_size) {
233-
if (dar < vma->vm_start || dar >= vma->vm_end) {
234-
vma = find_vma(mm, addr);
235-
if (!vma) {
236-
pr_err("Can't find vma for addr %016llx\n", addr);
237-
rc = -EFAULT;
238-
goto out;
239-
}
240-
/* get the size of the pages allocated */
241-
page_size = vma_kernel_pagesize(vma);
225+
*page_size = vma_kernel_pagesize(vma);
226+
*vma_start = vma->vm_start;
227+
*vma_end = vma->vm_end;
228+
out:
229+
up_read(&mm->mmap_sem);
230+
return rc;
231+
}
232+
233+
int cxllib_handle_fault(struct mm_struct *mm, u64 addr, u64 size, u64 flags)
234+
{
235+
int rc;
236+
u64 dar, vma_start, vma_end;
237+
unsigned long page_size;
238+
239+
if (mm == NULL)
240+
return -EFAULT;
241+
242+
/*
243+
* The buffer we have to process can extend over several pages
244+
* and may also cover several VMAs.
245+
* We iterate over all the pages. The page size could vary
246+
* between VMAs.
247+
*/
248+
rc = get_vma_info(mm, addr, &vma_start, &vma_end, &page_size);
249+
if (rc)
250+
return rc;
251+
252+
for (dar = (addr & ~(page_size - 1)); dar < (addr + size);
253+
dar += page_size) {
254+
if (dar < vma_start || dar >= vma_end) {
255+
/*
256+
* We don't hold the mm->mmap_sem semaphore
257+
* while iterating, since the semaphore is
258+
* required by one of the lower-level page
259+
* fault processing functions and it could
260+
* create a deadlock.
261+
*
262+
* It means the VMAs can be altered between 2
263+
* loop iterations and we could theoretically
264+
* miss a page (however unlikely). But that's
265+
* not really a problem, as the driver will
266+
* retry access, get another page fault on the
267+
* missing page and call us again.
268+
*/
269+
rc = get_vma_info(mm, dar, &vma_start, &vma_end,
270+
&page_size);
271+
if (rc)
272+
return rc;
242273
}
243274

244275
rc = cxl_handle_mm_fault(mm, flags, dar);
245-
if (rc) {
246-
pr_err("cxl_handle_mm_fault failed %d", rc);
247-
rc = -EFAULT;
248-
goto out;
249-
}
276+
if (rc)
277+
return -EFAULT;
250278
}
251-
rc = 0;
252-
out:
253-
up_read(&mm->mmap_sem);
254-
return rc;
279+
return 0;
255280
}
256281
EXPORT_SYMBOL_GPL(cxllib_handle_fault);

0 commit comments

Comments
 (0)