From patchwork Wed Jul 12 18:01:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1806919 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=YOI5ZjUg; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1QYR49P0z242Y for ; Thu, 13 Jul 2023 04:02:03 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qJeA1-0001ht-37; Wed, 12 Jul 2023 18:01:57 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qJe9y-0001fn-A5 for kernel-team@lists.ubuntu.com; Wed, 12 Jul 2023 18:01:54 +0000 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id CBF6B3F189 for ; Wed, 12 Jul 2023 18:01:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1689184910; bh=Alms61XeDY2zJem4j1VEfKvjNtdkHmTHZcSOQ/pOSfs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YOI5ZjUgjlLUWe4QPU/9YnTsL2ATgy8nL1Mtq9ZdAXvCwBudT5CzjHme6prCYj8Vj FkqD+cFSXlDGX6KfIP5rlF+IBFwHYJzbg1tkoZ1OwSKudXHi06liU8KFSfzW00amJR 4tjo3qbEmDn2wiX+PytTzSL0T3r/gDA2izhAhhsgXaTihB4qqxc1QsbFfp6NFu1Z2f jy2jSSI3K5aIbpRVE8RItViyjJ0d9fi4jHQRj84oXuiZteiSW5pTEYntZLavBWYEau l/cMETX6ZFlGSAVhlGUl+nFukZLby3zNnBbRbjhySBMGC2bI2DWJgcMfFc8s2KgpSJ Nlfsq6V6vDMgQ== Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-1b8b30f781cso73841905ad.2 for ; Wed, 12 Jul 2023 11:01:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689184907; x=1691776907; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Alms61XeDY2zJem4j1VEfKvjNtdkHmTHZcSOQ/pOSfs=; b=M64T8DwSP4n9nkEDT+B31PFxGzmdHa00MBMmKwsIf8W34LG32QwI5ZeZ2jx+uZSNdk SIgvALhxiWQFIVlxl4oWc4ss67JosfDEa0A4+AT5Q3JUmUHOePY53Maqahn/xzB8JUhS 8vxCASVHGN2sWTr6iqBuRXqlQr6rJpWW+Wx+w54juMY+N9mqbHR3mk0MR0VeJzbz6J7K k8ztIRrnCFsA3eHe3FCp8d+hH10ArHCAo2r8cYP1rMprooTw7rMGzvuIfMtuA0lnHrbK h/rZzNl7L4EhpjQ82hzkONLYs3N9x8lUl2hqeEYlmWP+0jjCvoMAgtA8xvvUml0i9dQJ ImWA== X-Gm-Message-State: ABy/qLahruSiwK4ocqL8i0s0oWlkgZ0o6PvWp/APS4H8+ze5sUkpcNpM bLNNA4sqY+UHyvbmgyP/hIVDvUo9GU5CBRwuFso+KYMiicXbKCmatk2SAJ4NyQZlOb8e5/SaWya c1RnfkNEya781rmeXYJdlX+LMiGre3UI5a9woToRiwzmJJFA39w== X-Received: by 2002:a17:903:182:b0:1b8:944a:a932 with SMTP id z2-20020a170903018200b001b8944aa932mr17445428plg.2.1689184907169; Wed, 12 Jul 2023 11:01:47 -0700 (PDT) X-Google-Smtp-Source: APBJJlFfRZtahBGw5+4Kzm9i8wupxloEsVvkbE3DFjSG2CTxfME0WG+RHDKQN1FzrkRNGUUNXwSNZg== X-Received: by 2002:a17:903:182:b0:1b8:944a:a932 with SMTP id z2-20020a170903018200b001b8944aa932mr17445402plg.2.1689184906856; Wed, 12 Jul 2023 11:01:46 -0700 (PDT) Received: from smtp.gmail.com (174-045-099-030.res.spectrum.com. [174.45.99.30]) by smtp.gmail.com with ESMTPSA id n19-20020a170902969300b001b0358848b0sm4276551plp.161.2023.07.12.11.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 11:01:46 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH 1/6] swiotlb: remove swiotlb_max_segment Date: Wed, 12 Jul 2023 12:01:36 -0600 Message-Id: <20230712180141.713006-2-tim.gardner@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712180141.713006-1-tim.gardner@canonical.com> References: <20230712180141.713006-1-tim.gardner@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Christoph Hellwig BugLink: https://bugs.launchpad.net/bugs/2026736 swiotlb_max_segment has always been a bogus API, so remove it now that the remaining callers are gone. Signed-off-by: Christoph Hellwig Reviewed-by: Robin Murphy (backported from commit 5e7b9a6ae8c352819a2d998a065910b536de0e8c) [rtg - context adjustment] Signed-off-by: Tim Gardner --- include/linux/swiotlb.h | 5 ----- kernel/dma/swiotlb.c | 13 ------------- 2 files changed, 18 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index bbf4dbfe7d26..d3bc1d49a22d 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -122,7 +122,6 @@ static inline bool is_swiotlb_force_bounce(struct device *dev) void swiotlb_init(bool addressing_limited, unsigned int flags); void __init swiotlb_exit(void); -unsigned int swiotlb_max_segment(void); size_t swiotlb_max_mapping_size(struct device *dev); bool is_swiotlb_active(struct device *dev); void __init swiotlb_adjust_size(unsigned long size); @@ -141,10 +140,6 @@ static inline bool is_swiotlb_force_bounce(struct device *dev) static inline void swiotlb_exit(void) { } -static inline unsigned int swiotlb_max_segment(void) -{ - return 0; -} static inline size_t swiotlb_max_mapping_size(struct device *dev) { return SIZE_MAX; diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 6610af73f405..cb13374db6a4 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -156,19 +156,6 @@ setup_io_tlb_npages(char *str) } early_param("swiotlb", setup_io_tlb_npages); -unsigned int swiotlb_max_segment(void) -{ - if (!io_tlb_default_mem.nslabs) - return 0; - return rounddown(io_tlb_default_mem.nslabs << IO_TLB_SHIFT, PAGE_SIZE); -} -EXPORT_SYMBOL_GPL(swiotlb_max_segment); - -void swiotlb_set_alloc_from_low_pages(bool low) -{ - swiotlb_alloc_from_low_pages = low; -} - unsigned long swiotlb_size_or_default(void) { return default_nslabs << IO_TLB_SHIFT; From patchwork Wed Jul 12 18:01:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1806917 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=b+/PF5ex; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1QYR173Wz20cD for ; Thu, 13 Jul 2023 04:02:02 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qJe9x-0001fo-DZ; Wed, 12 Jul 2023 18:01:53 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qJe9u-0001fY-OQ for kernel-team@lists.ubuntu.com; Wed, 12 Jul 2023 18:01:50 +0000 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 4259C3F438 for ; Wed, 12 Jul 2023 18:01:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1689184910; bh=ZSiW0uzGIozF4k+aDxrJCR+61Q0fzCfTSebV1cb/O+Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=b+/PF5exQzsmjNBWUYTB4NvDJnFbjAgOQ54R3AGF3dPsNBJS8KH5wZeNG9qK6D2cA aCYPl8i9p87PZIGDOd8vZAO6H2JGDy47p5QehEB2vuiKSZ8pxMBSI8SrWpSPyXLBKp CU7SzGLH3d+ndHaMH7VH+1WHHxwUkNKUWPyHiJ2yE95IOXLmfCSPKDqXJhqEOzEtJY 96AExkElV+YnvxL89EBm9Jb2Tgw9CQhOAg1uLav+hk2RNSr2aRTpluvk8pt+oCjYuG gCeW6EeqsItiiw/d2OrPirHKR90vYFWiARUV+Jsq4HedYOagrTV3ei5SUHfhgjl/Gn PlhV9PdBoFhKw== Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-1b9de8fbfc9so30027715ad.1 for ; Wed, 12 Jul 2023 11:01:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689184908; x=1691776908; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZSiW0uzGIozF4k+aDxrJCR+61Q0fzCfTSebV1cb/O+Q=; b=PslZ7OJcPhnnqTIYe4Ipvs/y7FkQt5rjRTeyUUctxhLLm0d1MBBg7Gnoo0lLkxlI1B N+A3UFCuWtvrUFCLAip7UgLjfZMCugqaXc7hDyTh7XoYOQlRnOfHAEpIj170TuHtZRzo YPqLrrPUNl+61Rk5XhQVUQ/wcsWj/+oDtDfRzmwJ4LNAHgd78S123oNRbMMT7R1wvGIX GBd0juLqqofiCJBx6dLgd88eRrxZ7boeg/iYIZGhIvsBia4L0Pvo3ugvChlEUcZbgjpy Sl1l1WfRpf/QcX9VXmmVnF5ilYIZUS/ld4EZ2YFtYooQChw76GsDDAOLoUbHZUTi8wWa rukw== X-Gm-Message-State: ABy/qLad+li0qKqXia5MGPzeJM7zzViAedcaOLSB69pSG9cMxPx/hDBO HZWpDgM2Z8JC2m9LBPsfCj4yKu7DFoSRd3gzCuUCJYku7Z5+AdFSMeWwfLzs4i6fJXA81ACsBdm P455Ec9GtGFWI6Xn4VMfFwKw7WarKXQwyQtyqMbkkV1Xzd6fx8w== X-Received: by 2002:a17:902:c94c:b0:1b2:4852:9a5f with SMTP id i12-20020a170902c94c00b001b248529a5fmr18950553pla.54.1689184908429; Wed, 12 Jul 2023 11:01:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlFU4970ra/ziW3eQ/eizWhA+BjGKFy16X32lRKvcOfJcciFDkj8zFGZhFRtw2wyE99Q/CojPg== X-Received: by 2002:a17:902:c94c:b0:1b2:4852:9a5f with SMTP id i12-20020a170902c94c00b001b248529a5fmr18950527pla.54.1689184908110; Wed, 12 Jul 2023 11:01:48 -0700 (PDT) Received: from smtp.gmail.com (174-045-099-030.res.spectrum.com. [174.45.99.30]) by smtp.gmail.com with ESMTPSA id n19-20020a170902969300b001b0358848b0sm4276551plp.161.2023.07.12.11.01.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 11:01:47 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH 2/6] swiotlb: fix the deadlock in swiotlb_do_find_slots Date: Wed, 12 Jul 2023 12:01:37 -0600 Message-Id: <20230712180141.713006-3-tim.gardner@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712180141.713006-1-tim.gardner@canonical.com> References: <20230712180141.713006-1-tim.gardner@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: "GuoRui.Yu" BugLink: https://bugs.launchpad.net/bugs/2026736 In general, if swiotlb is sufficient, the logic of index = wrap_area_index(mem, index + 1) is fine, it will quickly take a slot and release the area->lock; But if swiotlb is insufficient and the device has min_align_mask requirements, such as NVME, we may not be able to satisfy index == wrap and exit the loop properly. In this case, other kernel threads will not be able to acquire the area->lock and release the slot, resulting in a deadlock. The current implementation of wrap_area_index does not involve a modulo operation, so adjusting the wrap to ensure the loop ends is not trivial. Introduce a new variable to record the number of loops and exit the loop after completing the traversal. Backtraces: Other CPUs are waiting this core to exit the swiotlb_do_find_slots loop. [10199.924391] RIP: 0010:swiotlb_do_find_slots+0x1fe/0x3e0 [10199.924403] Call Trace: [10199.924404] [10199.924405] swiotlb_tbl_map_single+0xec/0x1f0 [10199.924407] swiotlb_map+0x5c/0x260 [10199.924409] ? nvme_pci_setup_prps+0x1ed/0x340 [10199.924411] dma_direct_map_page+0x12e/0x1c0 [10199.924413] nvme_map_data+0x304/0x370 [10199.924415] nvme_prep_rq.part.0+0x31/0x120 [10199.924417] nvme_queue_rq+0x77/0x1f0 ... [ 9639.596311] NMI backtrace for cpu 48 [ 9639.596336] Call Trace: [ 9639.596337] [ 9639.596338] _raw_spin_lock_irqsave+0x37/0x40 [ 9639.596341] swiotlb_do_find_slots+0xef/0x3e0 [ 9639.596344] swiotlb_tbl_map_single+0xec/0x1f0 [ 9639.596347] swiotlb_map+0x5c/0x260 [ 9639.596349] dma_direct_map_sg+0x7a/0x280 [ 9639.596352] __dma_map_sg_attrs+0x30/0x70 [ 9639.596355] dma_map_sgtable+0x1d/0x30 [ 9639.596356] nvme_map_data+0xce/0x370 ... [ 9639.595665] NMI backtrace for cpu 50 [ 9639.595682] Call Trace: [ 9639.595682] [ 9639.595683] _raw_spin_lock_irqsave+0x37/0x40 [ 9639.595686] swiotlb_release_slots.isra.0+0x86/0x180 [ 9639.595688] dma_direct_unmap_sg+0xcf/0x1a0 [ 9639.595690] nvme_unmap_data.part.0+0x43/0xc0 Fixes: 1f221a0d0dbf ("swiotlb: respect min_align_mask") Signed-off-by: GuoRui.Yu Signed-off-by: Xiaokang Hu Signed-off-by: Christoph Hellwig (cherry picked from commit 7c3940bf81e5664cdb50c3fedfec8f0a756a34fb) Signed-off-by: Tim Gardner --- kernel/dma/swiotlb.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index cb13374db6a4..9e1f88cebb17 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -585,8 +585,8 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, unsigned int iotlb_align_mask = dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); unsigned int nslots = nr_slots(alloc_size), stride; - unsigned int index, wrap, count = 0, i; unsigned int offset = swiotlb_align_offset(dev, orig_addr); + unsigned int index, slots_checked, count = 0, i; unsigned long flags; unsigned int slot_base; unsigned int slot_index; @@ -609,15 +609,16 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, goto not_found; slot_base = area_index * mem->area_nslabs; - index = wrap = wrap_area_index(mem, ALIGN(area->index, stride)); + index = wrap_area_index(mem, ALIGN(area->index, stride)); - do { + for (slots_checked = 0; slots_checked < mem->area_nslabs; ) { slot_index = slot_base + index; if (orig_addr && (slot_addr(tbl_dma_addr, slot_index) & iotlb_align_mask) != (orig_addr & iotlb_align_mask)) { index = wrap_area_index(mem, index + 1); + slots_checked++; continue; } @@ -633,7 +634,8 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, goto found; } index = wrap_area_index(mem, index + stride); - } while (index != wrap); + slots_checked += stride; + } not_found: spin_unlock_irqrestore(&area->lock, flags); From patchwork Wed Jul 12 18:01:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1806916 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=A5oWgofc; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1QYR1B4Lz20cd for ; Thu, 13 Jul 2023 04:02:02 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qJe9y-0001gF-Jp; Wed, 12 Jul 2023 18:01:54 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qJe9v-0001ff-Hr for kernel-team@lists.ubuntu.com; Wed, 12 Jul 2023 18:01:51 +0000 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 4A1EA3F438 for ; Wed, 12 Jul 2023 18:01:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1689184911; bh=0QqBv/JQGa1IMEGoRXA2Q6Mj+HIcWpasgGRz1IJf36Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A5oWgofcqMNihU1ItddUUmeQt2Xul/H6FwFTckWiKXqxU26GAlOvATrDbhyqTjaF/ HDg5eReGb9r+SsynF4fXJTnbxgyPS9VKy7DboRv+r5DDKYIWJu8EOK7r9GvqjHk9ko FbV74snrDS8h8bZi7Kk8mGLlIBrS+UlPom2Cqa7CoZ3ABoRMMV/fLjF+gxZVaRrs3L vNg41mxyw5obj06Z1yrTlSXnezrNGPwvfwAXeMV9ncExT/xWguIlUNGxX09LYXtkGb 7xDKNIS/KS6o5WcIxTIOabxtVkfswIAftjyU7L1yEX6JXkP3kTLSkEum42xGuOuEgu ZXR0QsJTGFOdw== Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-1b8959fb3c7so70263345ad.0 for ; Wed, 12 Jul 2023 11:01:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689184909; x=1691776909; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0QqBv/JQGa1IMEGoRXA2Q6Mj+HIcWpasgGRz1IJf36Y=; b=G7UmweKd7Xnfnt0jqKqBwc4xp9HYeQkc/+n/vCsMftYQ4Q9fJnRen/7tJs9xa/rIKA gFxO647+r14942+DEmr6/ktBD2oa8OZuyiTH+VbxUVfwR03FMLbb1wNEKMPM7RZfhBWn ztfgrF65ey99TZSVNUQ0YOdltIKoQF26afHYAh1+JK60ChvpxnuJ94oOyo9MJ0OcLhPu eP78qrGg1J5Gp3KyU2Q9wnzRIZc9RbpQVtm1R2PRgCb8vwGsj3JpdtuAugyCGIJrsbT2 bS8+Z/+1coNpx+O8eBYmMkzp1nVgbNP/DWT8jbGMC/AciUAnt+JjCSuCCoJ94VtDp7nj Tgyg== X-Gm-Message-State: ABy/qLZTWiSQKPClDbqLboNE1rZAEcX3qShjr4BFnuekjX7nd2g2DEHh 7XlCCDbPudXQDElqYErYvHmtT7hTpXLT9clEn3nbQAixgkbz/8G0HT0pbNzSBGZG/tPBdQ21lxl 2Ki4BXAygpSqUV8if0hJTysND1jbbuGJvuMBF5zm+Pysm4yg8zA== X-Received: by 2002:a17:902:ee91:b0:1ac:a28e:4b29 with SMTP id a17-20020a170902ee9100b001aca28e4b29mr4652241pld.26.1689184909710; Wed, 12 Jul 2023 11:01:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlGd/+ipdHfmMV28u3v5uXMU0HdF4V3PlOaGLxqh02B/5zqPhbKwnbTUEZH2MW5OHzAsm6zsGQ== X-Received: by 2002:a17:902:ee91:b0:1ac:a28e:4b29 with SMTP id a17-20020a170902ee9100b001aca28e4b29mr4652226pld.26.1689184909397; Wed, 12 Jul 2023 11:01:49 -0700 (PDT) Received: from smtp.gmail.com (174-045-099-030.res.spectrum.com. [174.45.99.30]) by smtp.gmail.com with ESMTPSA id n19-20020a170902969300b001b0358848b0sm4276551plp.161.2023.07.12.11.01.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 11:01:48 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH 3/6] swiotlb: use wrap_area_index() instead of open-coding it Date: Wed, 12 Jul 2023 12:01:38 -0600 Message-Id: <20230712180141.713006-4-tim.gardner@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712180141.713006-1-tim.gardner@canonical.com> References: <20230712180141.713006-1-tim.gardner@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Petr Tesarik BugLink: https://bugs.launchpad.net/bugs/2026736 No functional change, just use an existing helper. Signed-off-by: Petr Tesarik Signed-off-by: Christoph Hellwig (cherry picked from commit 39e7d2ab6ea9fd6b389091ec223d566934fe7be5) Signed-off-by: Tim Gardner --- kernel/dma/swiotlb.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 9e1f88cebb17..7d55c0fb2678 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -655,10 +655,7 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, /* * Update the indices to avoid searching in the next round. */ - if (index + nslots < mem->area_nslabs) - area->index = index + nslots; - else - area->index = 0; + area->index = wrap_area_index(mem, index + nslots); area->used += nslots; spin_unlock_irqrestore(&area->lock, flags); return slot_index; From patchwork Wed Jul 12 18:01:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1806920 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=o8VGL/nw; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1QYS3MG6z242Z for ; Thu, 13 Jul 2023 04:02:04 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qJeA2-0001iu-Dg; Wed, 12 Jul 2023 18:01:58 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qJe9y-0001g8-QR for kernel-team@lists.ubuntu.com; Wed, 12 Jul 2023 18:01:54 +0000 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id AF7883F18D for ; Wed, 12 Jul 2023 18:01:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1689184913; bh=I9yDzy29Qk8ln9RyyHTjqDU5Afn6NDooN6bEalJFPsg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o8VGL/nwMAb7/DDvfdY0KOkxgXh3DA0XBv+15hbxn7jdV4Gcr5ZHwujP5lCs+ynsV m7jR75vuzILy5HR3gdw7wMosf/rhxL8Ml+oFA3TzJy+aHP7jxCft26jqrpBnzsKG7U 7fO3znjEVFao1n9NjGv/T3hd2un8jBAPtJqwWyK5XCNeifsaXfvaGUdE/ND3aif4B4 9bcUZHhuAmeQvPeb0eCVSBlqv4Z7DDCSVQkHjlhZQriT3WAEd6q2pztQ8bdbrUpTDl AdEm4h7s2xxrQU3xCbIu9KXCu+O2LFR/k2BCziaWD8QqtmuBK4YMJKqZWJrm0RpYVq LCX1RQ5moHSOg== Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2631231fed0so7036149a91.3 for ; Wed, 12 Jul 2023 11:01:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689184911; x=1691776911; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I9yDzy29Qk8ln9RyyHTjqDU5Afn6NDooN6bEalJFPsg=; b=BXW2xIXCP6cW6+amnqFQ9lKQB7gBimnyN0xEaBTZpIZAqhMPqmyi/GTO1IpjUBss7u DfdOTXdTYhPFj9RM3GExizrJV2uO+XXPl05Wq8w5Z6u/9t6hiD+zAPJF4y7WUARgatt5 WGrEZWjjkNkrI5YJi8K6nraXY4fUF8lpJe8VyHT+CyLaTkt/NUgbNjHF8lA0QTxlD3Ij agC8gjwzBc2C7PyJhVe3Nx/RIhhV4XdJGtpHfRdHIn2nDUPYAuCijxEh0sUmNHQwTH+y rljpKMS+KM3KuVElUgf5pRSFv9ixPif0HJ4gy+qtGteNz0ldO02Qra9e//XFDhyb9ym+ DhIA== X-Gm-Message-State: ABy/qLZJhgIEteNhWE0Lc6bvM8oYFDadhDe3qhfoVpbyUqAcZ9sGLMD2 UifM/0zB9d4IOpk1+4AgHrlbTZTUm2h8adSy3cmHpYJZh1M8nQKWCyqbUHPwwK4bB5guJrw0nuQ b3iGjXV8Q7iZM9wxW6UYLaZxomLz9u9gGZVp9VvFKzhAJzldRwg== X-Received: by 2002:a17:902:e74b:b0:1b8:9b5e:a218 with SMTP id p11-20020a170902e74b00b001b89b5ea218mr19308383plf.42.1689184911073; Wed, 12 Jul 2023 11:01:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlE9rhv1TBCG57l4+8FJnONT7cgaZYsoQBcmbmnm//tic9EFVy75D2mXTAXfdRaGlgdn6loZYg== X-Received: by 2002:a17:902:e74b:b0:1b8:9b5e:a218 with SMTP id p11-20020a170902e74b00b001b89b5ea218mr19308359plf.42.1689184910687; Wed, 12 Jul 2023 11:01:50 -0700 (PDT) Received: from smtp.gmail.com (174-045-099-030.res.spectrum.com. [174.45.99.30]) by smtp.gmail.com with ESMTPSA id n19-20020a170902969300b001b0358848b0sm4276551plp.161.2023.07.12.11.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 11:01:50 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH 4/6] swiotlb: fix slot alignment checks Date: Wed, 12 Jul 2023 12:01:39 -0600 Message-Id: <20230712180141.713006-5-tim.gardner@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712180141.713006-1-tim.gardner@canonical.com> References: <20230712180141.713006-1-tim.gardner@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Petr Tesarik BugLink: https://bugs.launchpad.net/bugs/2026736 Explicit alignment and page alignment are used only to calculate the stride, not when checking actual slot physical address. Originally, only page alignment was implemented, and that worked, because the whole SWIOTLB is allocated on a page boundary, so aligning the start index was sufficient to ensure a page-aligned slot. When commit 1f221a0d0dbf ("swiotlb: respect min_align_mask") added support for min_align_mask, the index could be incremented in the search loop, potentially finding an unaligned slot if minimum device alignment is between IO_TLB_SIZE and PAGE_SIZE. The bug could go unnoticed, because the slot size is 2 KiB, and the most common page size is 4 KiB, so there is no alignment value in between. IIUC the intention has been to find a slot that conforms to all alignment constraints: device minimum alignment, an explicit alignment (given as function parameter) and optionally page alignment (if allocation size is >= PAGE_SIZE). The most restrictive mask can be trivially computed with logical AND. The rest can stay. Fixes: 1f221a0d0dbf ("swiotlb: respect min_align_mask") Fixes: e81e99bacc9f ("swiotlb: Support aligned swiotlb buffers") Signed-off-by: Petr Tesarik Signed-off-by: Christoph Hellwig (cherry picked from commit 0eee5ae1025699ea93d44fdb6ef2365505082103) Signed-off-by: Tim Gardner --- kernel/dma/swiotlb.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 7d55c0fb2678..9f0eacabe8e2 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -594,22 +594,26 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, BUG_ON(!nslots); BUG_ON(area_index >= mem->nareas); + /* + * For allocations of PAGE_SIZE or larger only look for page aligned + * allocations. + */ + if (alloc_size >= PAGE_SIZE) + iotlb_align_mask &= PAGE_MASK; + iotlb_align_mask &= alloc_align_mask; + /* * For mappings with an alignment requirement don't bother looping to - * unaligned slots once we found an aligned one. For allocations of - * PAGE_SIZE or larger only look for page aligned allocations. + * unaligned slots once we found an aligned one. */ stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1; - if (alloc_size >= PAGE_SIZE) - stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT)); - stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1); spin_lock_irqsave(&area->lock, flags); if (unlikely(nslots > mem->area_nslabs - area->used)) goto not_found; slot_base = area_index * mem->area_nslabs; - index = wrap_area_index(mem, ALIGN(area->index, stride)); + index = area->index; for (slots_checked = 0; slots_checked < mem->area_nslabs; ) { slot_index = slot_base + index; From patchwork Wed Jul 12 18:01:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1806922 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=A/Yi+T0L; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1QYY12Thz20cD for ; Thu, 13 Jul 2023 04:02:09 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qJeA7-0001pC-3g; Wed, 12 Jul 2023 18:02:03 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qJeA4-0001id-5i for kernel-team@lists.ubuntu.com; Wed, 12 Jul 2023 18:02:00 +0000 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 02EF63F189 for ; Wed, 12 Jul 2023 18:01:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1689184915; bh=Z01qNeeAn6VMXpQHUgfDL5OqvtOoqLhDmbO3DGxac+0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A/Yi+T0LAVlqPSBY5nyhgdEqUBGeVFE0/1L2fZzLwJQqmE+j1jzg6Osxp3ZqhZdy1 nJGdFJHydFn75MUM92LWw5yMp+5lkzsqGGX5WuqDH6n2+270Ek15/gJrj/N0vnQGm5 /PmKNr7+Nq2M+0S+wXryq3bqWKzkP5rdiP9otyTvHHWFPequarSzo928bdNtNOy1YX p5N1LI/DXBfPn26TxcFd9cjMBIkpiafhmUqSOTdGg+BiOGcag8+Iu+zIv3ztMA8CNU Z4gVo0HjLmNtLy/t+hgMzKTBKxCnKlvy3En9fUxJW7J10EzegcpxPF4R6ziW81pSbz 2OFX7Yl7FS2xw== Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-1b888bdacbcso68893255ad.2 for ; Wed, 12 Jul 2023 11:01:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689184912; x=1691776912; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z01qNeeAn6VMXpQHUgfDL5OqvtOoqLhDmbO3DGxac+0=; b=JWmHhrT+nMxXKYgbyhM+XY5MQj8NEQ+X35p6tbBGxWIb1akO3B/M5OyjakMUlNFS4t FevB2ydLzdY0LkYYtNbaoAhB7IPQnrmmyLA6eLDs/NRdyIlFtSzTLHhZDGKnUfCtO6iP srEcKcur68mB0qIvCDMGImYpy9YUy/uN7FGqUdnl+LyEktyvzCt/oSYeAfkPYvilSnMW e6/dmEYO8tp2TkHAaogxT0gYFv6mu76l60/6RYGYxZ6ucWwwZBqYomDoXEgm2eKeMk8z pSelWJ+NIJP9zh2aMjGZgEUjtTzmhTr3Lx9o0VFVK9w/aLrGqVA0xjRYBf2VUYqCOS1m 13eA== X-Gm-Message-State: ABy/qLa5PTq3E/vo9QgsUHPRJxeckfrqXD2VdfHJppHG5aGZF5Oo6k8Q xzEuI61I7RyWn2EY8Yu/Fabsx0YnlKjM5/mmGWDR/JwvB4RYAB8jQTXbv/9T1jIxMAymr4rYqfU w4nUfuYwHfPv0pX5alnm/RgC6ZYdZzSVNcfO2pIoaSQeNQIQn1Q== X-Received: by 2002:a17:903:24c:b0:1b1:99c9:8ce1 with SMTP id j12-20020a170903024c00b001b199c98ce1mr16929581plh.51.1689184912495; Wed, 12 Jul 2023 11:01:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlFxr06x3ROiMR3k3VTUPdu8A6qYkFxP9uj+xII/fO4Rq8nDOBCrR12mF4pkIUAqLu4lJ3wPNg== X-Received: by 2002:a17:903:24c:b0:1b1:99c9:8ce1 with SMTP id j12-20020a170903024c00b001b199c98ce1mr16929554plh.51.1689184912119; Wed, 12 Jul 2023 11:01:52 -0700 (PDT) Received: from smtp.gmail.com (174-045-099-030.res.spectrum.com. [174.45.99.30]) by smtp.gmail.com with ESMTPSA id n19-20020a170902969300b001b0358848b0sm4276551plp.161.2023.07.12.11.01.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 11:01:51 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH 5/6] swiotlb: fix a braino in the alignment check fix Date: Wed, 12 Jul 2023 12:01:40 -0600 Message-Id: <20230712180141.713006-6-tim.gardner@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712180141.713006-1-tim.gardner@canonical.com> References: <20230712180141.713006-1-tim.gardner@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Petr Tesarik BugLink: https://bugs.launchpad.net/bugs/2026736 The alignment mask in swiotlb_do_find_slots() masks off the high bits which are not relevant for the alignment, so multiple requirements are combined with a bitwise OR rather than AND. In plain English, the stricter the alignment, the more bits must be set in iotlb_align_mask. Confusion may arise from the fact that the same variable is also used to mask off the offset within a swiotlb slot, which is achieved with a bitwise AND. Fixes: 0eee5ae10256 ("swiotlb: fix slot alignment checks") Reported-by: Dexuan Cui Link: https://lore.kernel.org/all/CAA42JLa1y9jJ7BgQvXeUYQh-K2mDNHd2BYZ4iZUz33r5zY7oAQ@mail.gmail.com/ Reported-by: Kelsey Steele Link: https://lore.kernel.org/all/20230405003549.GA21326@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net/ Signed-off-by: Petr Tesarik Tested-by: Dexuan Cui Signed-off-by: Christoph Hellwig (cherry picked from commit bbb73a103fbbed6f63cb738d3783261c4241b4b2) Signed-off-by: Tim Gardner --- kernel/dma/swiotlb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 9f0eacabe8e2..2048709cd733 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -583,7 +583,7 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, phys_to_dma_unencrypted(dev, mem->start) & boundary_mask; unsigned long max_slots = get_max_slots(boundary_mask); unsigned int iotlb_align_mask = - dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); + dma_get_min_align_mask(dev) | alloc_align_mask; unsigned int nslots = nr_slots(alloc_size), stride; unsigned int offset = swiotlb_align_offset(dev, orig_addr); unsigned int index, slots_checked, count = 0, i; @@ -599,8 +599,8 @@ static int swiotlb_do_find_slots(struct device *dev, int area_index, * allocations. */ if (alloc_size >= PAGE_SIZE) - iotlb_align_mask &= PAGE_MASK; - iotlb_align_mask &= alloc_align_mask; + iotlb_align_mask |= ~PAGE_MASK; + iotlb_align_mask &= ~(IO_TLB_SIZE - 1); /* * For mappings with an alignment requirement don't bother looping to From patchwork Wed Jul 12 18:01:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tim Gardner X-Patchwork-Id: 1806921 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=JZur0+BZ; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R1QYW2Xt3z20cD for ; Thu, 13 Jul 2023 04:02:07 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1qJeA5-0001mc-3v; Wed, 12 Jul 2023 18:02:01 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1qJe9z-0001gq-RI for kernel-team@lists.ubuntu.com; Wed, 12 Jul 2023 18:01:55 +0000 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 6B5283F438 for ; Wed, 12 Jul 2023 18:01:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1689184915; bh=gq0+zwBcVlMSUdEk041qtKMPL4n8+YyjDGmEm2NfmFU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JZur0+BZiVvq5HHqrveTA4VTY6VgEBQyFlKXH3osysZrL1wmcFSeQuznQwDOpF7oR AiXhq+k1Vb9Ih0KPo/X1NlTdfrciO4WWVm4nN1ZiAPyh5seMsHxir0lZJr5PuDDwim f0NagEVVbgclqFX/2eP9CcSRSjIpjP35IiwoTPR6VdiguwHJkQrg7xUUVmtmgkipR7 H17/eeMIgB0E+Rh6QtGsm2Y6Dk74Q4jxOADz9p6o9o0p7PV7qlpFuemRDeLjT17yf3 xyok/ZcMJbjAWCDtmgTWjsfEHe1RT5R4+ugPcLTNH7da6HTAdAtgPkittseGTXSwhF mk9X1kerC9Mug== Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-1b895fa8929so73905245ad.0 for ; Wed, 12 Jul 2023 11:01:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689184913; x=1691776913; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gq0+zwBcVlMSUdEk041qtKMPL4n8+YyjDGmEm2NfmFU=; b=MiCjmaVX6fwDNZG4grlz1jAEUJVSf+CLIPqVREUR3lex6lLuOCtGw/fWJa+cMrPNo5 2tm28qnlvE/3upX+THad4/25TExG3ICnwZd3UeU1OvaePUCNl2CYnJzxoO/p6q1ntnZ2 b39lUm979+/4bD28B73M0faZgwZVpJDiG2QcGoYrJSap+j4Bf4lfQW+mEgGFE4Ukv0T0 YhLYxfxzkYOinIU2Csx5HYvz7eaiOYz2GU+SX/M++VtGorYya8ozQ2QH8STXLd8Y3xX3 xMdpRCKqLA2MRoXiaOmIo1+yLGJFqypAuGULyNkb2zliWRwwoCcQBqsuKC1pZN8+3ihM mdEQ== X-Gm-Message-State: ABy/qLbeMT6I0UMvr9QHLOrfCOJxtIHAm8XCf21eUcrNwZExTvjf2Pf2 f15O9e/yqzvJONv45qGUEvJ4PPsALIORmELDIoGIIG9/NtA3DKOo0HTFwSR65vVx0yviJBm/78J JnAiepu1adHvlxz6NvexUXcRadWHUaNvfRS0rTqXuKbYIzBWcLQ== X-Received: by 2002:a17:902:c20c:b0:1b5:1787:d23f with SMTP id 12-20020a170902c20c00b001b51787d23fmr14610331pll.5.1689184913695; Wed, 12 Jul 2023 11:01:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlFeJIbEUF3tXaAkxlHoijYYuC7oer+GZqK6n+QFfQc3H6+GPV9wuzCLEvEHx+k3fYO79aO4MA== X-Received: by 2002:a17:902:c20c:b0:1b5:1787:d23f with SMTP id 12-20020a170902c20c00b001b51787d23fmr14610316pll.5.1689184913386; Wed, 12 Jul 2023 11:01:53 -0700 (PDT) Received: from smtp.gmail.com (174-045-099-030.res.spectrum.com. [174.45.99.30]) by smtp.gmail.com with ESMTPSA id n19-20020a170902969300b001b0358848b0sm4276551plp.161.2023.07.12.11.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 11:01:52 -0700 (PDT) From: Tim Gardner To: kernel-team@lists.ubuntu.com Subject: [PATCH 6/6] swiotlb: relocate PageHighMem test away from rmem_swiotlb_setup Date: Wed, 12 Jul 2023 12:01:41 -0600 Message-Id: <20230712180141.713006-7-tim.gardner@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712180141.713006-1-tim.gardner@canonical.com> References: <20230712180141.713006-1-tim.gardner@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Doug Berger BugLink: https://bugs.launchpad.net/bugs/2026736 The reservedmem_of_init_fn's are invoked very early at boot before the memory zones have even been defined. This makes it inappropriate to test whether the page corresponding to a PFN is in ZONE_HIGHMEM from within one. Removing the check allows an ARM 32-bit kernel with SPARSEMEM enabled to boot properly since otherwise we would be de-referencing an uninitialized sparsemem map to perform pfn_to_page() check. The arm64 architecture happens to work (and also has no high memory) but other 32-bit architectures could also be having similar issues. While it would be nice to provide early feedback about a reserved DMA pool residing in highmem, it is not possible to do that until the first time we try to use it, which is where the check is moved to. Fixes: 0b84e4f8b793 ("swiotlb: Add restricted DMA pool initialization") Signed-off-by: Doug Berger Signed-off-by: Florian Fainelli Signed-off-by: Christoph Hellwig (cherry picked from commit a90922fa25370902322e9de6640e58737d459a50) Signed-off-by: Tim Gardner --- kernel/dma/swiotlb.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 2048709cd733..6de2a907aa5f 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -958,6 +958,11 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem, /* Set Per-device io tlb area to one */ unsigned int nareas = 1; + if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) { + dev_err(dev, "Restricted DMA pool must be accessible within the linear mapping."); + return -EINVAL; + } + /* * Since multiple devices can share the same pool, the private data, * io_tlb_mem struct, will be initialized by the first device attached @@ -1019,11 +1024,6 @@ static int __init rmem_swiotlb_setup(struct reserved_mem *rmem) of_get_flat_dt_prop(node, "no-map", NULL)) return -EINVAL; - if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) { - pr_err("Restricted DMA pool must be accessible within the linear mapping."); - return -EINVAL; - } - rmem->ops = &rmem_swiotlb_ops; pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n", &rmem->base, (unsigned long)rmem->size / SZ_1M);