From patchwork Thu Oct 7 18:06:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Khalid Elmously X-Patchwork-Id: 1537890 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=SYAr19Uc; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HQK6X5YFSz9sR4 for ; Fri, 8 Oct 2021 05:07:32 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1mYXnf-0004ju-Vs; Thu, 07 Oct 2021 18:07:24 +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 1mYXnM-0004R1-CT for kernel-team@lists.ubuntu.com; Thu, 07 Oct 2021 18:07:04 +0000 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.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 946653FFFC for ; Thu, 7 Oct 2021 18:07:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1633630023; bh=tjhw2gU6lEPSSGd1jmOBGmO7xJ1fCJrzcjnJJJTb1pQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=SYAr19UcuGQeI2pY/8rlWoe8M8ag5wXJtWKZnFneQNQKIM2opyrjzelYpgnxIMnU/ Zk0jUbDn01Qft+8btbAHmSgh7nFjL+v9l0YxXnIDHSN75gaWQVpveJv1IGWY+er0pb wKREvLKb+zI8WHlwj970mg6jlQdk4abi85LiYZo68lZh2Jh55GTZbOXNRii0IjqaFS SSMjs9bS/prgU89otQDfEnDafhSCTiU4bzwej861/jY8Or/7Huwr1K1jvo+eTKSB/E JePdi/Trs152sE7rmSg/3xdfVwUOFzN+gfOcDrXHML9IIiMwBVbP2lTitSWgOLD45V MC5bM9r0Fh4Xw== Received: by mail-qt1-f199.google.com with SMTP id d9-20020ac86149000000b002a6d33107c5so5873384qtm.8 for ; Thu, 07 Oct 2021 11:07:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tjhw2gU6lEPSSGd1jmOBGmO7xJ1fCJrzcjnJJJTb1pQ=; b=OphK7hkgFCkaniF1MN1CxF/XUR+JUZAmkkU8PEu2ZHj5C54NQd/IgMSeGrZeEQzVxc 4gSYunhWESg4nYLbv74IHEwuTcAbenpBanGu4OS/2TLhdSHaQeLyPswfGtP7HDR8nCT5 +qiHsJLJc78VGwhBslVhv5XEFe8kel2IJIW62K7uaQu/DIMHTX7INukZmGp9tvWCfL2g g4fxNc11wNcqnomZmPHtVkOpYp3es5KGW3WgqCX6sox23r9vY9x7apyvvDvyka7C3LGs N63H9Y37+XByZ7XmPNs0tdpOcF0+I7+HwHMyPRULjpALhRQXszybUrHkppV/h8kbmp1Z HGSQ== X-Gm-Message-State: AOAM533h52U4gmoIAHlg6cVFqsCZE+BAwCOQMyAYkNzEksVYnYAlhKzJ qXJlpnxqOTTGQ0uEMXsxpQL1r5FiJOa9rYhhDwAUgSx4xoMv7nURR4D5AQlK66HABC59Cno0dcJ HpaVz71+3A2cAA830FQReNFk/rpCoJi1jsPxO53dcog== X-Received: by 2002:a0c:b341:: with SMTP id a1mr5662905qvf.21.1633630022443; Thu, 07 Oct 2021 11:07:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1QonDCRH+p2Wtw6UcgCL4Ho4/YjyOB/BUGdAKSCODEynf342i1STdUju5+6Lj1PT1i2FaAw== X-Received: by 2002:a0c:b341:: with SMTP id a1mr5662850qvf.21.1633630021984; Thu, 07 Oct 2021 11:07:01 -0700 (PDT) Received: from kbuntu2.fuzzbuzz.org (dhcp-24-53-240-12.cable.user.start.ca. [24.53.240.12]) by smtp.gmail.com with ESMTPSA id o22sm72382qkk.132.2021.10.07.11.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Oct 2021 11:07:01 -0700 (PDT) From: Khalid Elmously To: kernel-team@lists.ubuntu.com Subject: [SRU][PATCH 13/13] swiotlb: manipulate orig_addr when tlb_addr has offset Date: Thu, 7 Oct 2021 14:06:44 -0400 Message-Id: <20211007180644.9187-14-khalid.elmously@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211007180644.9187-1-khalid.elmously@canonical.com> References: <20211007180644.9187-1-khalid.elmously@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: Bumyong Lee BugLink: https://bugs.launchpad.net/bugs/1943902 in case of driver wants to sync part of ranges with offset, swiotlb_tbl_sync_single() copies from orig_addr base to tlb_addr with offset and ends up with data mismatch. It was removed from "swiotlb: don't modify orig_addr in swiotlb_tbl_sync_single", but said logic has to be added back in. From Linus's email: "That commit which the removed the offset calculation entirely, because the old (unsigned long)tlb_addr & (IO_TLB_SIZE - 1) was wrong, but instead of removing it, I think it should have just fixed it to be (tlb_addr - mem->start) & (IO_TLB_SIZE - 1); instead. That way the slot offset always matches the slot index calculation." (Unfortunatly that broke NVMe). The use-case that drivers are hitting is as follow: 1. Get dma_addr_t from dma_map_single() dma_addr_t tlb_addr = dma_map_single(dev, vaddr, vsize, DMA_TO_DEVICE); |<---------------vsize------------->| +-----------------------------------+ | | original buffer +-----------------------------------+ vaddr swiotlb_align_offset |<----->|<---------------vsize------------->| +-------+-----------------------------------+ | | | swiotlb buffer +-------+-----------------------------------+ tlb_addr 2. Do something 3. Sync dma_addr_t through dma_sync_single_for_device(..) dma_sync_single_for_device(dev, tlb_addr + offset, size, DMA_TO_DEVICE); Error case. Copy data to original buffer but it is from base addr (instead of base addr + offset) in original buffer: swiotlb_align_offset |<----->|<- offset ->|<- size ->| +-------+-----------------------------------+ | | |##########| | swiotlb buffer +-------+-----------------------------------+ tlb_addr |<- size ->| +-----------------------------------+ |##########| | original buffer +-----------------------------------+ vaddr The fix is to copy the data to the original buffer and take into account the offset, like so: swiotlb_align_offset |<----->|<- offset ->|<- size ->| +-------+-----------------------------------+ | | |##########| | swiotlb buffer +-------+-----------------------------------+ tlb_addr |<- offset ->|<- size ->| +-----------------------------------+ | |##########| | original buffer +-----------------------------------+ vaddr [One fix which was Linus's that made more sense to as it created a symmetry would break NVMe. The reason for that is the: unsigned int offset = (tlb_addr - mem->start) & (IO_TLB_SIZE - 1); would come up with the proper offset, but it would lose the alignment (which this patch contains).] Fixes: 16fc3cef33a0 ("swiotlb: don't modify orig_addr in swiotlb_tbl_sync_single") Signed-off-by: Bumyong Lee Signed-off-by: Chanho Park Reviewed-by: Christoph Hellwig Reported-by: Dominique MARTINET Reported-by: Horia Geantă Tested-by: Horia Geantă CC: stable@vger.kernel.org Signed-off-by: Konrad Rzeszutek Wilk (backported from commit 5f89468e2f060031cd89fd4287298e0eaf246bf6) [ kmously: omitted part about alloc_size - not applicable and not needed ] Signed-off-by: Khalid Elmously --- kernel/dma/swiotlb.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 5f97ad0fc0e92..1465b555476df 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -413,6 +413,14 @@ void __init swiotlb_exit(void) swiotlb_cleanup(); } +/* + * Return the offset into a iotlb slot required to keep the device happy. + */ +static unsigned int swiotlb_align_offset(struct device *dev, u64 addr) +{ + return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1); +} + /* * Bounce: copy the swiotlb buffer from or back to the original dma location */ @@ -423,10 +431,16 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size phys_addr_t orig_addr = io_tlb_orig_addr[index]; unsigned long pfn = PFN_DOWN(orig_addr); unsigned char *vaddr = phys_to_virt(tlb_addr); + unsigned int tlb_offset; if (orig_addr == INVALID_PHYS_ADDR) return; + tlb_offset = (tlb_addr & (IO_TLB_SIZE - 1)) - + swiotlb_align_offset(dev, orig_addr); + + orig_addr += tlb_offset; + if (PageHighMem(pfn_to_page(pfn))) { /* The buffer does not have a mapping. Map it in and copy */ unsigned int offset = orig_addr & ~PAGE_MASK; @@ -460,14 +474,6 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size #define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT)) -/* - * Return the offset into a iotlb slot required to keep the device happy. - */ -static unsigned int swiotlb_align_offset(struct device *dev, u64 addr) -{ - return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1); -} - /* * Carefully handle integer overflow which can occur when boundary_mask == ~0UL. */