From patchwork Wed May 11 12:58:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12846159 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 14B5CC4332F for ; Wed, 11 May 2022 12:58:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.326804.549440 (Exim 4.92) (envelope-from ) id 1nolv3-0002Dl-7v; Wed, 11 May 2022 12:58:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 326804.549440; Wed, 11 May 2022 12:58:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nolv3-0002De-59; Wed, 11 May 2022 12:58:21 +0000 Received: by outflank-mailman (input) for mailman id 326804; Wed, 11 May 2022 12:58:19 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nolv0-0002DF-Ak for xen-devel@lists.xenproject.org; Wed, 11 May 2022 12:58:19 +0000 Received: from bombadil.infradead.org (bombadil.infradead.org [2607:7c80:54:e::133]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 05e66253-d12a-11ec-a406-831a346695d4; Wed, 11 May 2022 14:58:16 +0200 (CEST) Received: from [2001:4bb8:184:7881:740a:25f7:2c13:ef7a] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nolut-006reA-JL; Wed, 11 May 2022 12:58:12 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 05e66253-d12a-11ec-a406-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=VBAca1xXxndzNfh1X1k86JeV9qFeb8QaWGR+x5euRpI=; b=VBMFfZIBGc4yLHRm2Wa5peBxYo L7lkpBKL9qmRp7GdsRzDbo2hOxkLdrUFWSDWJAWKxk7QsvkAo2WDU1HButK/AFoRRG2iI+xRaqzne eJpGCLr8lqE1G4UklNZu5W5Via4KqfQygzftAqjThgZhRiLJT9nOUNEt91aPkno5EGtYNQKITCBm9 0qhtN7kBThU6Wf12wDMqazMp94VKvBm1Bh2nyagV+tLVCyBBcz19ufT9rcxQ1nGaInv4lWj1hvQ9c ORWQ8Tf+T/53asn71Frgu3Zufwh+iDGXlg9wwgJIvAusQNxKo6BMaE2N49S38haolif8C8QpTZGf3 yN2fiQ/g==; From: Christoph Hellwig To: iommu@lists.linux-foundation.org Cc: Stefano Stabellini , Boris Ostrovsky , Conor.Dooley@microchip.com, xen-devel@lists.xenproject.org Subject: [PATCH 1/3] swiotlb: don't panic when the swiotlb buffer can't be allocated Date: Wed, 11 May 2022 14:58:03 +0200 Message-Id: <20220511125805.1377025-2-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511125805.1377025-1-hch@lst.de> References: <20220511125805.1377025-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html For historical reasons the switlb code paniced when the metadata could not be allocated, but just printed a warning when the actual main swiotlb buffer could not be allocated. Restore this somewhat unexpected behavior as changing it caused a boot failure on the Microchip RISC-V PolarFire SoC Icicle kit. Fixes: 6424e31b1c05 ("swiotlb: remove swiotlb_init_with_tbl and swiotlb_init_late_with_tbl") Reported-by: Conor Dooley Signed-off-by: Christoph Hellwig Tested-by: Conor Dooley Reviewed-by: Stefano Stabellini Acked-by: Conor Dooley --- kernel/dma/swiotlb.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index e2ef0864eb1e5..3e992a308c8a1 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -254,8 +254,10 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, tlb = memblock_alloc(bytes, PAGE_SIZE); else tlb = memblock_alloc_low(bytes, PAGE_SIZE); - if (!tlb) - panic("%s: failed to allocate tlb structure\n", __func__); + if (!tlb) { + pr_warn("%s: failed to allocate tlb structure\n", __func__); + return; + } if (remap && remap(tlb, nslabs) < 0) { memblock_free(tlb, PAGE_ALIGN(bytes)); From patchwork Wed May 11 12:58:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12846158 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 07B8EC433EF for ; Wed, 11 May 2022 12:58:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.326807.549474 (Exim 4.92) (envelope-from ) id 1nolv6-0002ym-31; Wed, 11 May 2022 12:58:24 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 326807.549474; Wed, 11 May 2022 12:58:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nolv5-0002yf-VW; Wed, 11 May 2022 12:58:23 +0000 Received: by outflank-mailman (input) for mailman id 326807; Wed, 11 May 2022 12:58:22 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nolv4-0002cJ-PT for xen-devel@lists.xenproject.org; Wed, 11 May 2022 12:58:22 +0000 Received: from bombadil.infradead.org (bombadil.infradead.org [2607:7c80:54:e::133]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 06e89449-d12a-11ec-8fc4-03012f2f19d4; Wed, 11 May 2022 14:58:20 +0200 (CEST) Received: from [2001:4bb8:184:7881:740a:25f7:2c13:ef7a] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1noluw-006reJ-8R; Wed, 11 May 2022 12:58:14 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 06e89449-d12a-11ec-8fc4-03012f2f19d4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=PuV44d9nkyau+qsF4HjXIzIFX/Iz+fNa7YzgjXIuJ3M=; b=KQeqn6zw5+tBwQhIwYbD033l7U Bmwb6MGWYXuK/LkzlngzGC5Qubu/BMK8ESaRHloaJRhzEWk2cFZ1/V/l4O8B2vq2MzumWvrZwnZyr 43YpFbXIsyoZJQy96aaO4hEVBhMn/WC+lJu9Jachezx9QLwtVxUwduoKFfDF9UeSfgMQ0fx1hRZPt 0LdWe9KjdTrTGyLfYx27s5kfHuPEf7J0GvFg765OJZ9W+F+cSaDYoHAsubZYbwp7D1ACiauoX1v40 kA0XO3cMZuMkKnKm1x7RM+oVv6dCiF4gS3wqNXal5uhZKXl0X7LATlX/cCqQQx1QPxg0OGZS0cmVF GL+aA81w==; From: Christoph Hellwig To: iommu@lists.linux-foundation.org Cc: Stefano Stabellini , Boris Ostrovsky , Conor.Dooley@microchip.com, xen-devel@lists.xenproject.org Subject: [PATCH 2/3] swiotlb: use the right nslabs value in swiotlb_init_remap Date: Wed, 11 May 2022 14:58:04 +0200 Message-Id: <20220511125805.1377025-3-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511125805.1377025-1-hch@lst.de> References: <20220511125805.1377025-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html default_nslabs should only be used to initialize nslabs, after that we need to use the local variable that can shrink when allocations or the remap don't succeed. Fixes: 6424e31b1c05 ("swiotlb: remove swiotlb_init_with_tbl and swiotlb_init_late_with_tbl") Signed-off-by: Christoph Hellwig Reviewed-by: Stefano Stabellini --- kernel/dma/swiotlb.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 3e992a308c8a1..113e1e8aaca37 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -234,7 +234,7 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, { struct io_tlb_mem *mem = &io_tlb_default_mem; unsigned long nslabs = default_nslabs; - size_t alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs)); + size_t alloc_size; size_t bytes; void *tlb; @@ -249,7 +249,7 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, * memory encryption. */ retry: - bytes = PAGE_ALIGN(default_nslabs << IO_TLB_SHIFT); + bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT); if (flags & SWIOTLB_ANY) tlb = memblock_alloc(bytes, PAGE_SIZE); else @@ -269,12 +269,13 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, goto retry; } + alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs)); mem->slots = memblock_alloc(alloc_size, PAGE_SIZE); if (!mem->slots) panic("%s: Failed to allocate %zu bytes align=0x%lx\n", __func__, alloc_size, PAGE_SIZE); - swiotlb_init_io_tlb_mem(mem, __pa(tlb), default_nslabs, false); + swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); mem->force_bounce = flags & SWIOTLB_FORCE; if (flags & SWIOTLB_VERBOSE) From patchwork Wed May 11 12:58:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12846157 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4EADEC433F5 for ; Wed, 11 May 2022 12:58:37 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.326806.549462 (Exim 4.92) (envelope-from ) id 1nolv4-0002il-Qn; Wed, 11 May 2022 12:58:22 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 326806.549462; Wed, 11 May 2022 12:58:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nolv4-0002ia-NZ; Wed, 11 May 2022 12:58:22 +0000 Received: by outflank-mailman (input) for mailman id 326806; Wed, 11 May 2022 12:58:21 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nolv3-0002DF-E7 for xen-devel@lists.xenproject.org; Wed, 11 May 2022 12:58:21 +0000 Received: from bombadil.infradead.org (bombadil.infradead.org [2607:7c80:54:e::133]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 083730a7-d12a-11ec-a406-831a346695d4; Wed, 11 May 2022 14:58:20 +0200 (CEST) Received: from [2001:4bb8:184:7881:740a:25f7:2c13:ef7a] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1noluy-006red-V1; Wed, 11 May 2022 12:58:17 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 083730a7-d12a-11ec-a406-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=toUCmk68b0VIqulfilEOK3I3y91LJ0y0OH/f7d0EES0=; b=GGIU1i8lkayqDGAplfrTSrhaor BLksm6LWRMLoDZZbshdWfl1zTZJw7+5EApQCx/OIN5M0EU1RtOm2OP3lgcCxN0zEnNoGvF+GODe3G ofQic3DC0LKOfP7Fkx2AeGIxyEVZb/kbjHRCDleY4wfI1k9Lp2EjpIw2epikU2kUn3o/FWk+ND78J W6sXXNB9s8fIRSBTPGBi4SnVmC8afXImzQtCfps0xo0YrB6WG5ivAudedbhd+z0axlhNGkH7zCTqI iY5Bf3S8J4EU/B25UhLHUqveOZLR1f11oopGMGBSmVNXdElnUxTs231jO3i0K+ajnFFfdiXi2/Hf5 siB5FhhA==; From: Christoph Hellwig To: iommu@lists.linux-foundation.org Cc: Stefano Stabellini , Boris Ostrovsky , Conor.Dooley@microchip.com, xen-devel@lists.xenproject.org Subject: [PATCH 3/3] swiotlb: use the right nslabs-derived sizes in swiotlb_init_late Date: Wed, 11 May 2022 14:58:05 +0200 Message-Id: <20220511125805.1377025-4-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220511125805.1377025-1-hch@lst.de> References: <20220511125805.1377025-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html nslabs can shrink when allocations or the remap don't succeed, so make sure to use it for all sizing. For that remove the bytes value that can get stale and replace it with local calculations and a boolean to indicate if the originally requested size could not be allocated. Fixes: 6424e31b1c05 ("swiotlb: remove swiotlb_init_with_tbl and swiotlb_init_late_with_tbl") Signed-off-by: Christoph Hellwig Reviewed-by: Stefano Stabellini --- kernel/dma/swiotlb.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 113e1e8aaca37..d6e62a6a42ceb 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -297,9 +297,9 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, { struct io_tlb_mem *mem = &io_tlb_default_mem; unsigned long nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE); - unsigned long bytes; unsigned char *vstart = NULL; unsigned int order; + bool retried = false; int rc = 0; if (swiotlb_force_disable) @@ -308,7 +308,6 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, retry: order = get_order(nslabs << IO_TLB_SHIFT); nslabs = SLABS_PER_PAGE << order; - bytes = nslabs << IO_TLB_SHIFT; while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { vstart = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN, @@ -316,16 +315,13 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, if (vstart) break; order--; + nslabs = SLABS_PER_PAGE << order; + retried = true; } if (!vstart) return -ENOMEM; - if (order != get_order(bytes)) { - pr_warn("only able to allocate %ld MB\n", - (PAGE_SIZE << order) >> 20); - nslabs = SLABS_PER_PAGE << order; - } if (remap) rc = remap(vstart, nslabs); if (rc) { @@ -334,9 +330,15 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE); if (nslabs < IO_TLB_MIN_SLABS) return rc; + retried = true; goto retry; } + if (retried) { + pr_warn("only able to allocate %ld MB\n", + (PAGE_SIZE << order) >> 20); + } + mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(array_size(sizeof(*mem->slots), nslabs))); if (!mem->slots) { @@ -344,7 +346,8 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask, return -ENOMEM; } - set_memory_decrypted((unsigned long)vstart, bytes >> PAGE_SHIFT); + set_memory_decrypted((unsigned long)vstart, + (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT); swiotlb_init_io_tlb_mem(mem, virt_to_phys(vstart), nslabs, true); swiotlb_print_info();