Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7510153rwr; Wed, 10 May 2023 09:05:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6TnaQTNni+hwZ0j4D5eMuZCvYFat/9ZZNBEA510/4HDVLiyluc5Ac64dRh4zLy3nfyiZPR X-Received: by 2002:a92:cc48:0:b0:316:f99d:9ea2 with SMTP id t8-20020a92cc48000000b00316f99d9ea2mr14013907ilq.17.1683734728322; Wed, 10 May 2023 09:05:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683734728; cv=none; d=google.com; s=arc-20160816; b=Uj5OEdLhcCQKrsSSoClpkLjbPEl3BCpob0c8aw6paeo2UTWfLPEmuO4NR7q9b6AzfK igLYXgnau1YtpcSxG/D4kgrW/YGHhabJaz80konaA3hLyc08biDiapx1nA0aP3A8h9Qb +JFLIqU5Wc2G3zzdYWhR59sacUkLzEoLPsDsahn1c1DXtNV4lBkrE0mZdS8yxU2zcuyV 2abU8oqf10kF1S6BmvdSFHB7oE5D7c/uaaisgTFLYbLc2zgvMqz4C+wdkV7Ztimt9BmH 0JJpK+byDS84hy2lKOKy0yCV10R4ximhJx5Wa3POnsRmweLFKSOScXRQUMKqyHTkmzYP aYQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=f2kwca3p5lvmK1UKkYNiCNtEhFHRgCfnV1Hv36vabJE=; b=chJEYMa3s9nEVkmZf2N2I97Wdc1NZv8lJ62yXwDmDLJdNXW/WJmF5ZkZZCxZnuE4k5 NU1rVNcPJ+BNSCyqReudgk0pyhoR/ohNhj9CotWxI+MGzapuLk1IWFh7UZTRqLHgP27U hOowjmjhs6BOTNjTEbjXv7ihaDdyWEc5AEldaaXHnfKpPBudpfnkT4J0xToImIify65X 0rPa3DWNr2+7MXujoTeYoIwrJofOss58sUHoSL0s6idi/lrxxvqZcic7rWQGnexD/CkA nQLpcfkeeWi1IHx7NKhZksgmmWTr95cjX6h3lhzi/lxEJ34IDxpcHRQaBU/TlxbfD0og AM6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@pku.edu.cn header.s=dkim header.b=AUAyj4Rh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=pku.edu.cn Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a7-20020a92d347000000b0033559400d92si3714473ilh.92.2023.05.10.09.05.12; Wed, 10 May 2023 09:05:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@pku.edu.cn header.s=dkim header.b=AUAyj4Rh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=pku.edu.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237683AbjEJPld (ORCPT + 99 others); Wed, 10 May 2023 11:41:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237622AbjEJPlZ (ORCPT ); Wed, 10 May 2023 11:41:25 -0400 Received: from pku.edu.cn (mx19.pku.edu.cn [162.105.129.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CF7E63583; Wed, 10 May 2023 08:41:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pku.edu.cn; s=dkim; h=Received:Date:From:To:Cc:Subject: Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=f2kwca3p5lvmK1UKkYNiCNtEhFHR gCfnV1Hv36vabJE=; b=AUAyj4Rh8ryw5jfWK+eYdxwMtpGmL+2cMOMiHRSIKmRO Ux8B+3fGADgsT9+Y57nXL03gP7QhZ/Koli5PlZj447yjYLzMU4pazuL1FRvlYgWL D3bj89QLxyVciAJ05H3DVZ5Q+FwurbUdFHFVvztcxIUAjDbvB1hFuVX9ikdiTHc= Received: from localhost (unknown [10.7.101.92]) by front02 (Coremail) with SMTP id 54FpogCXbjgRu1tkSAlvEw--.9090S2; Wed, 10 May 2023 23:41:10 +0800 (CST) Date: Wed, 10 May 2023 23:41:05 +0800 From: Ruihan Li To: Alan Stern Cc: linux-mm@kvack.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Pasha Tatashin , David Hildenbrand , Matthew Wilcox , Andrew Morton , Christoph Hellwig , Greg Kroah-Hartman , stable@vger.kernel.org, Ruihan Li Subject: Re: [PATCH 2/4] usb: usbfs: Use consistent mmap functions Message-ID: References: <20230510085527.57953-1-lrh2000@pku.edu.cn> <20230510085527.57953-3-lrh2000@pku.edu.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-CM-TRANSID: 54FpogCXbjgRu1tkSAlvEw--.9090S2 X-Coremail-Antispam: 1UD129KBjvJXoWxAr4fXFWUAry5Jr4xtF48Crg_yoW5Xw4xpF W8t3yjkF4YqFyI9r12van8WFyfGwn5KFyUGryIv3sxu3W3Xr1SkFySkFy5ZF12yr10qr1I vFWqyw13u3W5uFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBY1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2 z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vYz4IE04k24V AvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xf McIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7 v_Jr0_Gr1lF7xvr2IY64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E 8cxan2IY04v7MxkIecxEwVCm-wCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26w 4UJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCj r7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6x IIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAI w20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x 0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VU1c4S5UUUUU== X-CM-SenderInfo: yssqiiarrvmko6sn3hxhgxhubq/1tbiAgEHBVPy772BUwAGsm X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Alan, On Wed, May 10, 2023 at 10:38:48AM -0400, Alan Stern wrote: > On Wed, May 10, 2023 at 04:55:25PM +0800, Ruihan Li wrote: > > When hcd->localmem_pool is non-null, it is used to allocate DMA memory. > > In this case, the dma address will be properly returned (in dma_handle), > > and dma_mmap_coherent should be used to map this memory into the user > > space. However, the current implementation uses pfn_remap_range, which > > is supposed to map normal pages (instead of DMA pages). > > > > Instead of repeating the logic in the memory allocation function, this > > patch introduces a more robust solution. To address the previous issue, > > this patch checks the type of allocated memory by testing whether > > dma_handle is properly set. If dma_handle is properly returned, it means > > some DMA pages are allocated and dma_mmap_coherent should be used to map > > them. Otherwise, normal pages are allocated and pfn_remap_range should > > be called. This ensures that the correct mmap functions are used > > consistently, independently with logic details that determine which type > > of memory gets allocated. > > > > Fixes: a0e710a7def4 ("USB: usbfs: fix mmap dma mismatch") > > Cc: stable@vger.kernel.org > > Signed-off-by: Ruihan Li > > --- > > drivers/usb/core/devio.c | 10 ++++++++-- > > 1 file changed, 8 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c > > index b4cf9e860..5067030b7 100644 > > --- a/drivers/usb/core/devio.c > > +++ b/drivers/usb/core/devio.c > > @@ -235,7 +235,7 @@ static int usbdev_mmap(struct file *file, struct vm_area_struct *vma) > > size_t size = vma->vm_end - vma->vm_start; > > void *mem; > > unsigned long flags; > > - dma_addr_t dma_handle; > > + dma_addr_t dma_handle = DMA_MAPPING_ERROR; > > int ret; > > > > ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory)); > > @@ -265,7 +265,13 @@ static int usbdev_mmap(struct file *file, struct vm_area_struct *vma) > > usbm->vma_use_count = 1; > > INIT_LIST_HEAD(&usbm->memlist); > > > > - if (hcd->localmem_pool || !hcd_uses_dma(hcd)) { > > + /* In DMA-unavailable cases, hcd_buffer_alloc_pages allocates > > + * normal pages and assigns DMA_MAPPING_ERROR to dma_handle. Check > > + * whether we are in such cases, and then use remap_pfn_range (or > > + * dma_mmap_coherent) to map normal (or DMA) pages into the user > > + * space, respectively. > > + */ > > Another stylistic issue. For multi-line comments, the format we use is: > > /* > * Blah, blah, blah > * Blah, blah, blah > */ > > Alan Stern Yeah, I am pretty sure it is another style difference with the net subsystem. Again, in the next version, I'll follow the coding style that you have pointed out. Thanks, Ruihan Li