From patchwork Fri Jul 21 15:24:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13322148 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A90FC04A6A for ; Fri, 21 Jul 2023 15:24:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231268AbjGUPYs (ORCPT ); Fri, 21 Jul 2023 11:24:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231124AbjGUPYn (ORCPT ); Fri, 21 Jul 2023 11:24:43 -0400 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAD9C30DF; Fri, 21 Jul 2023 08:24:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1689953074; x=1690557874; i=deller@gmx.de; bh=dc9kWra9ypygI1Is19IALpuSbGOBe3lX2QdPVOp/v9s=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=kEGesIdm30Ffy2+8IJH8B0kNiqfLnoJ+El+teymShR95a9ipVPFKB+E2Qwb6DK5cVb/0+lc FyApxSS3xTXNLLJ9KjGo41qua6lbYoaOZPllvmuHVqrlpct+DxKB/WAAkMA/kUfE6BiQg83E7 Kt0xCn7D1Dg5/RGzfnKkZnoowLQJIhZ6DpRCpPvu3i0ORKXajtStORMk204iRnwwEOtQaBrfR 5ILLpsrzJi9BUL4l5js+UB2VVLjKKI7Oj5y/6hbw/fwH6tvLLeAJGG1ac7DUEJ1x6B06zd+vZ HCLB+K5h5pESlt/NnZPqx+ov+fNtVY2h4ly398YBGMBg/lUPdsZQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.144.189]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N6bjy-1pqLbI0UhS-0185JI; Fri, 21 Jul 2023 17:24:34 +0200 From: Helge Deller To: linux-kernel@vger.kernel.org, io-uring@vger.kernel.org, Jens Axboe , linux-ia64@vger.kernel.org, Jiri Slaby , linux-parisc@vger.kernel.org Cc: Helge Deller , matoro Subject: [PATCH 1/2] io_uring: Fix io_uring mmap() by using architecture-provided get_unmapped_area() Date: Fri, 21 Jul 2023 17:24:31 +0200 Message-ID: <20230721152432.196382-2-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230721152432.196382-1-deller@gmx.de> References: <20230721152432.196382-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:kzPNfBubwIAm6jIU5x6btA4ZDwGK76xM124wZLXWVQgpeipS7tt V6B0QnlOj4xGHa+h/F4yPu/XeG+TU0Q2XC1Hz2ibjmbbI+pAF1c1TkXstQ7vVLyiRY/o6bi DWisgLSUiKjHQwT/v5ZR8gyaQ1R1OflBcQtMIGqQJmDE33Ka4u/iZLzzr7TsmjOT8rmL7yb htP5pUPZLpy1sRSm/rIDw== UI-OutboundReport: notjunk:1;M01:P0:9tu16ombaCA=;2zqAQ/W++1GcM2Vdx2AGC1WGpMb TLOz/kD34B/esUBUKS4B8S3SXoo3ekfNnZ7Utq/KVd+gLPdtz6GRsVhdJVyNYo8ZiLmVtf04q R78ojvTz5Gji0Q1blkEy+EYN6DwuIWq5yQUWAKskLx4YJh9N76YWGQ+eGawjltTC218rBq0Cu K6QBBYIKTTmIbkgSL4XuCT6npkm++tiUiY/SCAZWd49R5oF0+ZqRPTQIRiiKwrMfAPBwAe3SG xlAyXgKFyvhwmBY/2vXnr+98Yr3N1uzUuMq2T8Tx2sCl/uL0dDlWSlx5qbCchkShiCHfpsGWS k/wSAqtFSpdDgZCLEv8rrb65rsv+q7tUzPgrypBB64IKuOSmuYrM1cvKx98HDo+FtEQFzP4Zd YzgknjaQgF3uN2wWnu+vg96QIBDZ9tYSTJ3QOIIQ3jW4OEq0aFeayjIyxsm+KX2ba0Z9AOyM+ VnVMoFiCDjMdbMRb7Mw2kmpeWYAb+mZrfwn+1knB1SP1RlOqLtGEntvslo7quxNZOjDuhNTAr 3bjqYnnPDVPyvPxcpRY+xJiDjj9yLXiXjdWvA+ygbB43SZimIDbDxznSO+I7dwAAOa4uqYRU0 ZJKJYZ11uniRHeMwDL9NAsRFNKpP5mv+cZkjad9/yXSVagmXEULI2EkIrOYwKEZyM82bHz3OX 9vqXk+9/6KtweWwC/r0kk3xQBR5xbbpb+Yp2V9m9sW4d6Bap00Gj/sZfPUS8ZbPgadyZuIAbh +ALE9N/GC1QgmUnaignDPsRx+3y5TiB0IBazpb+BjZjxLUv8mY5WTuMqVG2z/6UCeJUf/8S6y +etfQkNiyM01J0UuDNRwZz/VkHaxK0e3w4xqqWcQ86Ap0G6FATB0X9MDy0xCMBNbYzq/xkGjB 8a35hL/ABjMc/IAUMH5q4H/NuQpVa+OD3U+hkN2YP6AqIiw9uSaPT6l6OzqppmVj0mGqlcl4a NFtUug== Precedence: bulk List-ID: X-Mailing-List: linux-parisc@vger.kernel.org The io_uring testcase is broken on IA-64 since commit d808459b2e31 ("io_uring: Adjust mapping wrt architecture aliasing requirements"). The reason is, that this commit introduced an own architecture independend get_unmapped_area() search algorithm which finds on IA-64 a memory region which is outside of the regular memory region used for shared userspace mappings and which can't be used on that platform due to aliasing. To avoid similar problems on IA-64 and other platforms in the future, it's better to switch back to the architecture-provided get_unmapped_area() function and adjust the needed input parameters before the call. Beside fixing the issue, the function now becomes easier to understand and maintain. This patch has been successfully tested with the io_uring testcase on physical x86-64, ppc64le, IA-64 and PA-RISC machines. On PA-RISC the LTP mmmap testcases did not report any regressions. Signed-off-by: Helge Deller Cc: Jens Axboe Reported-by: matoro Fixes: d808459b2e31 ("io_uring: Adjust mapping wrt architecture aliasing requirements") Tested-by: Jiri Slaby --- arch/parisc/kernel/sys_parisc.c | 15 ++++++++---- io_uring/io_uring.c | 42 +++++++++++++-------------------- 2 files changed, 27 insertions(+), 30 deletions(-) -- 2.41.0 diff --git a/arch/parisc/kernel/sys_parisc.c b/arch/parisc/kernel/sys_parisc.c index 39acccabf2ed..465b7cb9d44f 100644 --- a/arch/parisc/kernel/sys_parisc.c +++ b/arch/parisc/kernel/sys_parisc.c @@ -26,12 +26,17 @@ #include /* - * Construct an artificial page offset for the mapping based on the physical + * Construct an artificial page offset for the mapping based on the virtual * address of the kernel file mapping variable. + * If filp is zero the calculated pgoff value aliases the memory of the given + * address. This is useful for io_uring where the mapping shall alias a kernel + * address and a userspace adress where both the kernel and the userspace + * access the same memory region. */ -#define GET_FILP_PGOFF(filp) \ - (filp ? (((unsigned long) filp->f_mapping) >> 8) \ - & ((SHM_COLOUR-1) >> PAGE_SHIFT) : 0UL) +#define GET_FILP_PGOFF(filp, addr) \ + ((filp ? (((unsigned long) filp->f_mapping) >> 8) \ + & ((SHM_COLOUR-1) >> PAGE_SHIFT) : 0UL) \ + + (addr >> PAGE_SHIFT)) static unsigned long shared_align_offset(unsigned long filp_pgoff, unsigned long pgoff) @@ -111,7 +116,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp, do_color_align = 0; if (filp || (flags & MAP_SHARED)) do_color_align = 1; - filp_pgoff = GET_FILP_PGOFF(filp); + filp_pgoff = GET_FILP_PGOFF(filp, addr); if (flags & MAP_FIXED) { /* Even MAP_FIXED mappings must reside within TASK_SIZE */ diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 3bca7a79efda..227e50aa9af0 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3398,8 +3398,6 @@ static unsigned long io_uring_mmu_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { - const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); - struct vm_unmapped_area_info info; void *ptr; /* @@ -3414,32 +3412,26 @@ static unsigned long io_uring_mmu_get_unmapped_area(struct file *filp, if (IS_ERR(ptr)) return -ENOMEM; - info.flags = VM_UNMAPPED_AREA_TOPDOWN; - info.length = len; - info.low_limit = max(PAGE_SIZE, mmap_min_addr); - info.high_limit = arch_get_mmap_base(addr, current->mm->mmap_base); + /* + * Some architectures have strong cache aliasing requirements. + * For such architectures we need a coherent mapping which aliases + * kernel memory *and* userspace memory. To achieve that: + * - use a NULL file pointer to reference physical memory, and + * - use the kernel virtual address of the shared io_uring context + * (instead of the userspace-provided address, which has to be 0UL + * anyway). + * For architectures without such aliasing requirements, the + * architecture will return any suitable mapping because addr is 0. + */ + filp = NULL; + flags |= MAP_SHARED; + pgoff = 0; /* has been translated to ptr above */ #ifdef SHM_COLOUR - info.align_mask = PAGE_MASK & (SHM_COLOUR - 1UL); + addr = (uintptr_t) ptr; #else - info.align_mask = PAGE_MASK & (SHMLBA - 1UL); + addr = 0UL; #endif - info.align_offset = (unsigned long) ptr; - - /* - * A failed mmap() very likely causes application failure, - * so fall back to the bottom-up function here. This scenario - * can happen with large stack limits and large mmap() - * allocations. - */ - addr = vm_unmapped_area(&info); - if (offset_in_page(addr)) { - info.flags = 0; - info.low_limit = TASK_UNMAPPED_BASE; - info.high_limit = mmap_end; - addr = vm_unmapped_area(&info); - } - - return addr; + return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); } #else /* !CONFIG_MMU */ From patchwork Fri Jul 21 15:24:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13322147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DD8BC001E0 for ; Fri, 21 Jul 2023 15:24:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230475AbjGUPYr (ORCPT ); Fri, 21 Jul 2023 11:24:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230344AbjGUPYn (ORCPT ); Fri, 21 Jul 2023 11:24:43 -0400 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8086D30DB; Fri, 21 Jul 2023 08:24:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1689953074; x=1690557874; i=deller@gmx.de; bh=A7WxgBjz3zo2CGF0QZzRQfzfUsKrkF2oGa6/wHukdFM=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Ko2qdlYuXReozta8VnE/apNE4ymC4iZTiFxjjaTrWqbv3JfCPN118kMJWChmEnhlS24yB3N 1t8V9dVHuKJxQQIEvdgeGzMphReVYqX3QZ3C0WvhfnYS7SKhc+sRPYJPIDHvStB1RxMlMfEOH ZcaBANg+jJmd6S1FLrcpEWyjCN2bIl7Sa+2i4ikkkomVs4U8C7szAvfiUxuihA+dxplk7o8F0 Y0REMm9pz92bAWA+1qBDiEOTGAFQQtAqo7HwZS8KR2NL1ikeglPK9IQVkCywUGfYaP3VqPE6g vXgGkF28uQawUqqWZaqnzBFXmUajHZUGJuguXvdQ1THzu2ef6ODg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.144.189]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MplXp-1pZVPD1kfV-00q8gd; Fri, 21 Jul 2023 17:24:34 +0200 From: Helge Deller To: linux-kernel@vger.kernel.org, io-uring@vger.kernel.org, Jens Axboe , linux-ia64@vger.kernel.org, Jiri Slaby , linux-parisc@vger.kernel.org Cc: Helge Deller , matoro , Andrew Morton Subject: [PATCH 2/2] ia64: mmap: Consider pgoff when searching for free mapping Date: Fri, 21 Jul 2023 17:24:32 +0200 Message-ID: <20230721152432.196382-3-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230721152432.196382-1-deller@gmx.de> References: <20230721152432.196382-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:BplPTINVd0OdLsIUOtIgdkny/5qsRkSo0WcblPhJqu+oYHGcw71 bySZIEIKmJcNLmjhb0oyHAQr7vH8bDvcfn3G9AiBlVHH66N8vx2stCFdAQ0FpCCx/6LRvup fNkM5mAgT11aCqfJH9ZXj7iiqcRE/77E62bTUUtTpZBy0aU9iR2eMAqkVpwUxqNvjTwas5V MOOSvn7rsNQyWiClPxdEw== UI-OutboundReport: notjunk:1;M01:P0:AYbJDJWuZLQ=;AAsXgFhQ1l5zYV5mMqehdTP+Z69 OK5Ws0ozn2VQuyZl3r6VhNTq190tOl0PsnFHiQo8EMfGxNF14Jeqq/NlM3oZsmJiWTmIiAtQM Cq4tK1VwMSsfTJGepWQgKeZqbMD98AaA30FoUvoLeyUe0APn0mykrO5eeWc3f58pcrNA/xbaA F6RN8Mn70+QFHMzOgqmNb3yzwpBNgjrGdkJm5fx6lnn1oPJUX9Uy2YJv8DqNxyvMJhGYrV4Xy fudinA+6wZ5BNMnyJuxoR+MLjfpkI1+HW76LGjPr1AC1VjmypYN0BigTDnCAr/nqbTSVaIOzV j6j4G5qmoISfY/fvPZr4tuca24UvOwBY0IrL0wRSnA+OrfOkAvkxNxjrmIpFOM63ZJi++mYuS 0MYgIeCMpkH2XvKGY+WWxlkflD6i87susUQp19Lwi4PB165Ap7MNokr63eE/n2DwiIsc0x7Ey KOx9dRGzUkRINyL/FNtUSBV/646Pb9Lql5aQa2XaY0HcVs/wcDgLwx/uMTjQjHcR3eC+sd/6R /KTmSIACgxjHYdStmASVhBa42RdXxOF6daYUCimJ5iZD4bMrtG/nTPCx/cd67JZUSr2ah+BCE wo1j9j4IQ+5nO61nhzC9aN0Pfu6WAIFqLrI4rCA4SvIpIQl3cSVlm0eLKPZ2Q95RRMepx+WX4 nW2jVZ9PIaLD3am2rpQKWd0lHxpPTlFQHy2pvwRmcBoi7K2XuH/XBONRZF7gFLnxZV0ifoA+Q 2qmv6oSRNmXrTrMhlR4j8xCn+h4HwCBwPRAZBIMc/V1WMhPWlT6CpKU4ErjOWuUs5BfNEYUZ9 ikmLLPIHkkWL55i93IKTiv9jkXTQrIq5mh19XnKVJpYNxAv71KYuJYcV312/IPpepC4CW8rsj kArwN2qAlQbBDUS9K7k+XvtCdiMv0W2nNDaizDkXthmsD1au7asBxLX/LAUxtNieo/moaxEIv SA90T0opNeszWvzo9N6wrK7IIh4= Precedence: bulk List-ID: X-Mailing-List: linux-parisc@vger.kernel.org IA64 is the only architecture which does not consider the pgoff value when searching for a possible free memory region with vm_unmapped_area(). Adding this seems to have no negative side effect on IA64, so add it now to make IA64 consistent with all other architectures. Signed-off-by: Helge Deller Tested-by: matoro Cc: Andrew Morton Cc: linux-ia64@vger.kernel.org --- arch/ia64/kernel/sys_ia64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.41.0 diff --git a/arch/ia64/kernel/sys_ia64.c b/arch/ia64/kernel/sys_ia64.c index 6e948d015332..eb561cc93632 100644 --- a/arch/ia64/kernel/sys_ia64.c +++ b/arch/ia64/kernel/sys_ia64.c @@ -63,7 +63,7 @@ arch_get_unmapped_area (struct file *filp, unsigned long addr, unsigned long len info.low_limit = addr; info.high_limit = TASK_SIZE; info.align_mask = align_mask; - info.align_offset = 0; + info.align_offset = pgoff << PAGE_SHIFT; return vm_unmapped_area(&info); }