Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp430131ybc; Fri, 22 Nov 2019 07:28:35 -0800 (PST) X-Google-Smtp-Source: APXvYqyn1X3PdG6b+SutBUc38EMoqSvn0Z9Ak23VqbYa5qZ2L9rWKdoKajAoeKWxDu4DDpj6qte6 X-Received: by 2002:a17:906:d9db:: with SMTP id qk27mr6307153ejb.309.1574436515838; Fri, 22 Nov 2019 07:28:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574436515; cv=none; d=google.com; s=arc-20160816; b=q+kG4nYSw1Dg7Y4ObKytcRFnIRctDfiWT0+HmSn16QiOb7rlRAmkEXdPiGJ3gu0NS6 +VjR/1EYPHYMOud69pZhb1RNZK9Lv5pLH0MB+1lffiATG6TxTn4vb/Q/f0HKXi+ZpxyE 7C6O9x56e7ukm+l2Ufv05od+h/dtnYIfyZwAUSpTX6eoorPc3/xT7LuVO4ZJFK9H9Yie Cc5H8mvtY+YsZESPLTD8qYpVkGMzFBzafOvJwQeFCfsOcRzyP1oA5+tJj70MiF6YlpoQ Eij6f3g7A8eglFPzyuFAGQ27UormVRj/Zu3xnG9GAr5FmAaEawk6vYFqh4RC1VDdBR8r JG/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date; bh=E8d2wyRy8O1RX9iQ528RFWt78WAFTnJEzYag6YbcgMU=; b=uclX+aXLXVYxsQfLDqqypqxLShvhWfu9W7TkLIvGygMmeJGNnm3CdJZK7lS0P89QAt KEt5FANYBZvzZkc9DQ3NW9LBWLLLxGRFZR0G7wriSPq5pIN67LX/egC3HJl4j22zdEfw a4hpVj2o3lQM/Sluq/6dJYEtEoTb29eHxBFsGnCY2/YQFu/CSSN9hATQ9vDggmrL7dVw dBT2LyG4Oh+mVAU0BD1jZkbuyd26YffomE90R7c8Z301vYa6G278JhVFhQ0MZDiUm9HC FFZ06IO1DBOlVGWr3FDp4BA2eatJyqcApryGGJRNL1gs+o2s1LQ+yUBCVRPkkOQWxMhE lGKw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ci3si4474980ejb.37.2019.11.22.07.28.11; Fri, 22 Nov 2019 07:28:35 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726822AbfKVP1L (ORCPT + 99 others); Fri, 22 Nov 2019 10:27:11 -0500 Received: from iolanthe.rowland.org ([192.131.102.54]:59318 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1726100AbfKVP1L (ORCPT ); Fri, 22 Nov 2019 10:27:11 -0500 Received: (qmail 2667 invoked by uid 2102); 22 Nov 2019 10:27:10 -0500 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 22 Nov 2019 10:27:10 -0500 Date: Fri, 22 Nov 2019 10:27:10 -0500 (EST) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Pete Zaitcev cc: syzbot , , , , , , Kernel development list , USB list , , , Subject: Re: possible deadlock in mon_bin_vma_fault In-Reply-To: <20191121173825.1527c3a5@suzdal.zaitcev.lan> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 21 Nov 2019, Pete Zaitcev wrote: > On Thu, 21 Nov 2019 11:20:20 -0500 (EST) > Alan Stern wrote: > > > On Thu, 21 Nov 2019, Pete Zaitcev wrote: > > > > > Anyway... If you are looking at it too, what do you think about not using > > > any locks in mon_bin_vma_fault() at all? Isn't it valid? I think I tried > > > to be "safe", but it only uses things that are constants unless we're > > > opening and closing; a process cannot make page faults unless it has > > > some thing mapped; and that is only possible if device is open and stays > > > open. Can you find a hole in this reasoning? > > > > I think you're right. [...] > > How about the appended patch, then? You like? > > Do you happen to know how to refer to a syzbot report in a commit message? As Dmitry mentioned, you should put the Reported-by: line from the original syzbot bug report (see ) in the patch. > -- Pete > > commit 628f3bbf37eee21cce4cfbfaa6a796b129d7736d > Author: Pete Zaitcev > Date: Thu Nov 21 17:24:00 2019 -0600 > > usb: Fix a deadlock in usbmon between mmap and read > > Signed-off-by: Pete Zaitcev > > diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c > index ac2b4fcc265f..fb7df9810bad 100644 > --- a/drivers/usb/mon/mon_bin.c > +++ b/drivers/usb/mon/mon_bin.c > @@ -1039,12 +1039,18 @@ static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg > > mutex_lock(&rp->fetch_lock); > spin_lock_irqsave(&rp->b_lock, flags); > - mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE); > - kfree(rp->b_vec); > - rp->b_vec = vec; > - rp->b_size = size; > - rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0; > - rp->cnt_lost = 0; > + if (rp->mmap_active) { > + mon_free_buff(vec, size/CHUNK_SIZE); > + kfree(vec); > + ret = -EBUSY; It would be more elegant to do the rp->mmap_active test before calling kcalloc and mon_alloc_buf. But of course that's a pretty minor thing. > + } else { > + mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE); > + kfree(rp->b_vec); > + rp->b_vec = vec; > + rp->b_size = size; > + rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0; > + rp->cnt_lost = 0; > + } > spin_unlock_irqrestore(&rp->b_lock, flags); > mutex_unlock(&rp->fetch_lock); > } > @@ -1093,11 +1099,11 @@ static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg > return ret; > if (put_user(ret, &uptr->nfetch)) > return -EFAULT; > - ret = 0; What's the reason for this change? > } > break; > > - case MON_IOCG_STATS: { > + case MON_IOCG_STATS: > + { And this one? This disagrees with the usual kernel style. > struct mon_bin_stats __user *sp; > unsigned int nevents; > unsigned int ndropped; > @@ -1113,7 +1119,6 @@ static long mon_bin_ioctl(struct file *file, unsigned int cmd, unsigned long arg > return -EFAULT; > if (put_user(nevents, &sp->queued)) > return -EFAULT; > - > } > break; > > @@ -1216,13 +1221,21 @@ mon_bin_poll(struct file *file, struct poll_table_struct *wait) > static void mon_bin_vma_open(struct vm_area_struct *vma) > { > struct mon_reader_bin *rp = vma->vm_private_data; > + unsigned long flags; > + > + spin_lock_irqsave(&rp->b_lock, flags); > rp->mmap_active++; > + spin_unlock_irqrestore(&rp->b_lock, flags); > } > > static void mon_bin_vma_close(struct vm_area_struct *vma) > { > + unsigned long flags; > + > struct mon_reader_bin *rp = vma->vm_private_data; > + spin_lock_irqsave(&rp->b_lock, flags); > rp->mmap_active--; > + spin_unlock_irqrestore(&rp->b_lock, flags); > } > > /* > @@ -1234,16 +1247,12 @@ static vm_fault_t mon_bin_vma_fault(struct vm_fault *vmf) > unsigned long offset, chunk_idx; > struct page *pageptr; > > - mutex_lock(&rp->fetch_lock); > offset = vmf->pgoff << PAGE_SHIFT; > - if (offset >= rp->b_size) { > - mutex_unlock(&rp->fetch_lock); > + if (offset >= rp->b_size) > return VM_FAULT_SIGBUS; > - } > chunk_idx = offset / CHUNK_SIZE; > pageptr = rp->b_vec[chunk_idx].pg; > get_page(pageptr); > - mutex_unlock(&rp->fetch_lock); > vmf->page = pageptr; > return 0; > } Apart from the items mentioned above, this looks right to me. Alan Stern