From patchwork Wed Feb 28 13:39:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207861 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3351427dyb; Wed, 28 Feb 2024 05:41:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXjp1PdVaROxWVKX5D8ecCB/fLUpZXIc2Pf2iBlNHqyPTCdPvVq2wdUI+RmMowexanojuyo27H6/8ApCDGubpPUKWshiA== X-Google-Smtp-Source: AGHT+IFckZ8DTBYrLd5/rmrWMtHreWSHHNWRbmAXmlgvSNrsTu6xS2GUShnZEZLVIBSErxZ4JpFi X-Received: by 2002:a05:6214:2461:b0:68f:e855:52c3 with SMTP id im1-20020a056214246100b0068fe85552c3mr6532908qvb.45.1709127663417; Wed, 28 Feb 2024 05:41:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709127663; cv=pass; d=google.com; s=arc-20160816; b=hqB6e7E3xJjjdIVVeotyaSJo40cBmoOBUX8yqbFSFOCpx70zRVd95qmfTITzkpd4Ng I2yp2OTdYIw10zHjYBTPff+95afubdIf/ebofL25dpMsuxrI3LM8RhYcBNnY8z8YrsmG NvjYMFR970Lc3bHrUKaj7heqJMZ8yODKavlGxka/U1QQ6Bb+Val9YujxLHmSIuBLJ749 XzTRd1mZ0LYeg1x3+Poe4gicM9oZEh/YQ1EyzN15za827czqCoQJ4GHvSjMgooL4ZUen 6npiqktgyUCjtzt+QcyCfu5Eqz/VWJCa5vHeEhCoTAe3epdFa/oXrUyb+5xVXPaUUulo NntA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Y1/EKRATIU/aqMaJhexRC9e1cWN3fDUy6BrOLkel3w4=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=GAglUAdROkS1W9dk4qwslAQ6bWjSX3tpHdEjD6H69WehSwZTkpllWgoa/EeLR4EpO6 O0FMkrCOZKy6On1E+62uZ7AKjj//wnFkPKGNZ7CzsJ1EuE3qhH4Oseud0N5Sl0EzFkOe 1ZL71SyEAJitRFeC4+Pbb/27+VdCU4/FygJjgqSmvOVY8xtwt033RAUPSZFv9JhozHid XYgIJ8MBet+wUIWIC5Dj1eVGsJtuu4xq4VYBfIZuwiCyMTVYc2MyJaCGfvgdZkZxppqh 9AOUm7oyn5FPg3T0hyXUNHYPHDrrYTfsgHTtM01vHaZY6SBUFB++DqeXQSqo9INEpgSl fgjA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jxi0zrK2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s3-20020a0562140ca300b0068f533c6dc2si9546103qvs.261.2024.02.28.05.41.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:41:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jxi0zrK2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BDD521C2494F for ; Wed, 28 Feb 2024 13:40:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C92911552F8; Wed, 28 Feb 2024 13:39:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Jxi0zrK2" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E00B151CF9; Wed, 28 Feb 2024 13:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127582; cv=none; b=WZrOiRASfEPXVXkQFGRFiTI1DDBVxqkfozwhoLRTblsORLudfRTonqTISiU2jhDoxD4TI2bIYL1v3fsMImogkbOBdKYH4Kl5Q3x3j6S+3YnG4J1wklQ8Hm4cIpsVajGZA+CtqBMSPEWUQOyteoURxjUxcj/X5tFbPfD6EXvRJME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127582; c=relaxed/simple; bh=JqSwzF4JV3f0dyOOThKfJ10MqkgAb5PBoxHTEu2M6eg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X5W7imoC8C5ThSPNr+Ko2rHuge0iqKfyBbUy8lLQJJYTONgTOanAZQ5UsryC7lLZgNT9mzlVM0fobTL2AY2fGwt+zmMNG2B0+6WPeQTY30MMGNAWx3Eti8phyaDZuW5HclJUiymTUuEcM+bBstPpNujSwrbb7CU/NWxda7L0T20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Jxi0zrK2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7B80C43394; Wed, 28 Feb 2024 13:39:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127581; bh=JqSwzF4JV3f0dyOOThKfJ10MqkgAb5PBoxHTEu2M6eg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jxi0zrK22NyJ2K60C8LRKYeTxeUHMJEq7GqPktlk5+DIsCkQIAr+TGTo9D4W6SdNp DG6L/DLwEdTtaeomm5mlZU/EQ29z0bvJpMhgo87TEeI99ayBh2zS6049zeu6mU8JL+ lCWFgcY/rgwS2x0fXiNevJ8b17JGZUS2kD2iwsSzBjA6b/n0Xc4YMIztZcFANd+MFo G4PQ71ToheQ78cyICvgvOX7KDBjYxh21tll2HifVEQKmJmgRmXxXMTGAL9u4LkubtN vVgNU54CFM7rSubQVGftxnILgKwLu5Utx/zVHoj5y4dUbsNoEs6Kpur7vDG6a6WmDe eOOPet1TtYmLQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: kernel-team@android.com, Will Deacon , iommu@lists.linux.dev, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Dexuan Cui , Nicolin Chen , Michael Kelley Subject: [PATCH v5 2/6] swiotlb: Enforce page alignment in swiotlb_alloc() Date: Wed, 28 Feb 2024 13:39:26 +0000 Message-Id: <20240228133930.15400-3-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240228133930.15400-1-will@kernel.org> References: <20240228133930.15400-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792150248819270201 X-GMAIL-MSGID: 1792150248819270201 When allocating pages from a restricted DMA pool in swiotlb_alloc(), the buffer address is blindly converted to a 'struct page *' that is returned to the caller. In the unlikely event of an allocation bug, page-unaligned addresses are not detected and slots can silently be double-allocated. Add a simple check of the buffer alignment in swiotlb_alloc() to make debugging a little easier if something has gone wonky. Cc: Christoph Hellwig Cc: Marek Szyprowski Cc: Robin Murphy Cc: Dexuan Cui Reviewed-by: Petr Tesarik Tested-by: Nicolin Chen Reviewed-by: Michael Kelley Signed-off-by: Will Deacon --- kernel/dma/swiotlb.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 2ec2cc81f1a2..ab7fbb40bc55 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1643,6 +1643,12 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) return NULL; tlb_addr = slot_addr(pool->start, index); + if (unlikely(!PAGE_ALIGNED(tlb_addr))) { + dev_WARN_ONCE(dev, 1, "Cannot allocate pages from non page-aligned swiotlb addr 0x%pa.\n", + &tlb_addr); + swiotlb_release_slots(dev, tlb_addr); + return NULL; + } return pfn_to_page(PFN_DOWN(tlb_addr)); }