Skip to content

Commit c1edd3b

Browse files
Frederic Barratgregkh
authored andcommitted
cxl: Fix possible deadlock when processing page faults from cxllib
commit ad7b4e8 upstream. 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: stable@vger.kernel.org # 4.13+ Signed-off-by: Frederic Barrat <fbarrat@linux.vnet.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 156b45e commit c1edd3b

1 file changed

Lines changed: 55 additions & 30 deletions

File tree

‎drivers/misc/cxl/cxllib.c‎

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

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

220217
down_read(&mm->mmap_sem);
221218

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

243274
rc = cxl_handle_mm_fault(mm, flags, dar);
244-
if (rc) {
245-
pr_err("cxl_handle_mm_fault failed %d", rc);
246-
rc = -EFAULT;
247-
goto out;
248-
}
275+
if (rc)
276+
return -EFAULT;
249277
}
250-
rc = 0;
251-
out:
252-
up_read(&mm->mmap_sem);
253-
return rc;
278+
return 0;
254279
}
255280
EXPORT_SYMBOL_GPL(cxllib_handle_fault);

0 commit comments

Comments
 (0)