Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2045773rwd; Mon, 15 May 2023 06:39:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6hyltP8P9YR49tEHg82yXMblQBdNtPVXf5+pqtryb74wAtC2EBKYGHZRlVgm4FBObYW61G X-Received: by 2002:a17:90a:eb15:b0:23d:286:47d3 with SMTP id j21-20020a17090aeb1500b0023d028647d3mr33806450pjz.40.1684157964747; Mon, 15 May 2023 06:39:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684157964; cv=none; d=google.com; s=arc-20160816; b=U6e8nKsf1S7RAYNi49SA/kNyfBslOosAoL8ulmeTw7GUI6/imYEFWQY+BkHrMylDCg N62QbPEyuGOgMTD8Xav6MtXQNGMBPZCWnyk338wAGElAgYQIBfaaNkl+PJYIH0QOS361 U2ElCH4WC0MZENQjVp6PleXc1MerKAUkhmAqZ/VbpcDYGBNXuqryU2CFZ0H4k4KcyDh9 fzFVucT160wFjGuh/XjY/u/5xyt9nyEgXSU3EwZqqMjgfR3c2DbsopsZIfSxieuBFliI S70ePK3dtgfcHeCBFM/RBAMTNuxonoO0xgufx8l58HzICa2segjzBM7a/Ns377yrDKIN AUog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hM6wTF99Qb2+CWCASa87DqYXF4HjiX77nILI5iBwlYI=; b=rgRws+/+1v2dGZzxIA1Hj6CjGc/r+DnRERRWvD2oim29X1H4OIHQJx6bxX0XgzeWH5 dFKON7VpU04Sgd+FB3hmbt7/sD1//qdtGiQ1Yyz6IHIU10+aH/E0m43TAs9+pxEWCO1/ YHrB1I1wvg/mhl7c4hVhSpEV3l6Bk6xt/e1lqd3qlt0tMzmTwPfTZI7H1RkbDOU8dZxP cfjjHm1ML+QYl/H9vTbMgAQ29FA3MfBiz0yAzTUtNsLB8j5NzPLoUKms1jD3P1Y/S2vW 879Zrt70AsnNptRBralZ7fKUFOQXmGCPNBaG2bDwnx5EwmoqWQ6spEvAeECpW9qKJQcm o5/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@pku.edu.cn header.s=dkim header.b=HnCW9Ni1; 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 f15-20020a17090ac28f00b002477fad79d5si28889782pjt.63.2023.05.15.06.39.12; Mon, 15 May 2023 06:39:24 -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=HnCW9Ni1; 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 S242331AbjEONLS (ORCPT + 99 others); Mon, 15 May 2023 09:11:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242258AbjEONKr (ORCPT ); Mon, 15 May 2023 09:10:47 -0400 Received: from pku.edu.cn (mx19.pku.edu.cn [162.105.129.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E00461FF3; Mon, 15 May 2023 06:10:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pku.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-Id:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=hM6wTF99Qb2+CWCASa87DqYXF4HjiX77nI LI5iBwlYI=; b=HnCW9Ni1T0Tk+222800B1cmPfEly+gG4RGJeMSMFYlHY414w8v BuDsdMB+SoMr0TsFPwjHXOkbUFnA5m7Ff7hb9q5n2+yd3zD9xr8PUJMrbZ1fR+cw jk52PpM3hfOhed4KHvHJJL93lVBKRXU7e1bWVmSsXCjA1a15ZeYc4aAYg= Received: from localhost.localdomain (unknown [10.7.98.243]) by front02 (Coremail) with SMTP id 54FpogAnLDgqL2JkVboyFA--.10053S4; Mon, 15 May 2023 21:10:07 +0800 (CST) From: Ruihan Li To: linux-mm@kvack.org, linux-usb@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Pasha Tatashin , David Hildenbrand , Matthew Wilcox , Andrew Morton , Christoph Hellwig , Alan Stern , Greg Kroah-Hartman , Ruihan Li , stable@vger.kernel.org Subject: [PATCH v2 2/4] usb: usbfs: Use consistent mmap functions Date: Mon, 15 May 2023 21:09:56 +0800 Message-Id: <20230515130958.32471-3-lrh2000@pku.edu.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230515130958.32471-1-lrh2000@pku.edu.cn> References: <20230515130958.32471-1-lrh2000@pku.edu.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: 54FpogAnLDgqL2JkVboyFA--.10053S4 X-Coremail-Antispam: 1UD129KBjvJXoW7tFyUZryUCFWrJr47Zw4kCrg_yoW8KF13pF WrK3yYkr40gF1Svr1q9an5WF95Gwn5KFyUWrW2v3s8u3W3Jr1SkFySya4Yqr17tr1kXr1f tFWqyw13C3W5uFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBI1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2 z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vYz4IE04k24V AvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xf McIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7 v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF 7I0E8cxan2IY04v7MxkIecxEwVCm-wCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6c x26w4UJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2Iq xVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42 IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY 6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aV CY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbHa0DUUUUU== X-CM-SenderInfo: yssqiiarrvmko6sn3hxhgxhubq/1tbiAgEMBVPy77495QAAsi 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 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 When hcd->localmem_pool is non-null, localmem_pool 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 repeating the logic in the memory allocation function, this patch introduces a more robust solution. Here, the type of allocated memory is checked 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 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c index 3936ca7f7..fcf68818e 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,14 @@ 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. + */ + if (dma_handle == DMA_MAPPING_ERROR) { if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(usbm->mem) >> PAGE_SHIFT, size, vma->vm_page_prot) < 0) { -- 2.40.1