Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp630615lqc; Fri, 8 Mar 2024 07:29:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXvXOJ/EPdJdD34kniyCs5IM8whdIsdUq+6LdtCsnTqGSeSSiR4L9tzt+kENE9fZqOp1EE6fQQnIGDHsH3wu4fsIf2usG1x7MNMrEDzdw== X-Google-Smtp-Source: AGHT+IHvIq+wkqY9FZTKT9C5yPUbOjC4Pu3GALqYOyH0cmsefO1pt48qlULI3iSQeV5R1lYY52zd X-Received: by 2002:a17:907:d50f:b0:a45:f379:a162 with SMTP id wb15-20020a170907d50f00b00a45f379a162mr1250733ejc.72.1709911750220; Fri, 08 Mar 2024 07:29:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709911750; cv=pass; d=google.com; s=arc-20160816; b=zQVZwGVxofOAx5xxTfDKxtm6UKrNz32/ARhMlpJs2Low1iOa29S2+5xhXFhMm7cEUv neZc8uXsrHsOBEz1AzLUdiFiquuYD9p5NMvayubkeAGBk3tZuFwh1FIjs5D1mQLAE9wo 8hY2JkBTx2hohX/Uk3GxAf2O5vSfuoesaZPJdaUMQKoHnQ980i0AWh8MPv2WuEg2BTBY TgTp9MPPnfbg4KFZDSsT7luiIdptpUPxDIHUFnlfXMq+50KcvxddMcQNBrIacsPxFFMi na/4O4u8bx3kHrF/lgE2ndP+FNcpi712qaesEY/Cb3Pp+WE/MVr1YhVKQhLI87L9jPBX Q0Mg== 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=fQCtgBzW5q9c8+SZVvC2q0L9cCTVqxSsHBR6pXLpBxg=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=VWDQl0xQ1uAvNhxX4dEyjxEQ7uEGs85H/CcifDhN07bZ7RpeWEnjbSRnqJdKMV5BEG emK7ND1H99gA7W3gh2yhxLmisTi+ji7080A/jkQFgjlHmwcIpgdVBBj3HRvUXzyzmsO3 AYa4pPX3cvgSgagIuz7U4XnVBs4CWn/8jtpNRTk/TAFvNSeW+wFO4Pc3Gukd/Zmk0iPm 66kitWpZrsXZ3WHiPZQcKYornC/A1ZQH0bIiWL4AZMBk0+mTo7+VcAIG2lgQKBAgWUgv vh8zRnHx5/5mqh5yY2DjnAfEoJSDoRrHaoUKbVESgqSMFA9DOKCNTyNf+e1zMhtq0Xfw BmDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iw1bQR64; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-97232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97232-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j16-20020a170906475000b00a3ebf421c69si7840921ejs.804.2024.03.08.07.29.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 07:29:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iw1bQR64; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-97232-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97232-linux.lists.archive=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 am.mirrors.kernel.org (Postfix) with ESMTPS id EDF741F2330A for ; Fri, 8 Mar 2024 15:29:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 063FD1EA84; Fri, 8 Mar 2024 15:28:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iw1bQR64" 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 2C086250F3; Fri, 8 Mar 2024 15:28:44 +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=1709911725; cv=none; b=fIrEKoqdoPlSQuggCwdPKo4wa78LYzZ4XRrcIMLx9KZpRJDFw/xRsMczNM0VlXCCkAsfXUJMOw+bMEJpRQP9EcTyBRm9LprtCN5MgAm9mu1FGQ4pAH0kFvHDleAIQAYfjKa3Kc7dmMQR/FxitYrIRImeCnE6SEIiX87TJcgVH8E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709911725; c=relaxed/simple; bh=Ow9SAlH4+n7rZYPGZozjvHYRa/8sNh9HhIRpID4khJg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gysD9VMnDuMDt5ylEIX0mp5rPxXGAQ8WULKv6gdgzKnD2500ugaxFCIHk9lo2KYqC5S/l8y+gQKxwYvUJ3XY50dVQ8YyomA8K7ABwaK2fBlVrJBtMTyRj6Jea/ygtKCo69fkezlJ1uIDxShyK8L2YfDTDREOtqZwEVaN20GJvg8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iw1bQR64; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9FF7FC43394; Fri, 8 Mar 2024 15:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709911724; bh=Ow9SAlH4+n7rZYPGZozjvHYRa/8sNh9HhIRpID4khJg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iw1bQR64cRNIugB/M++lMYhdds6aAyuYjHFy/xL/Q/MPZU0xLKAqIIc5LyKOud1Ca Fj+I8H4cqEse5HtWYHmCtj0xqJx3m5duWWck29kK/BX8NzOtfem5CKeyMSJ47JLPzX f/N4k6UnKPLon3uxounK1uVGwOm9WJBmQKuEHH9oS5pVzIFRhye+yj0HYzQp41rTzE t/h5LCGix5T+eWrKJM/G+ytqq7cqAs7vV05cz60RvMjAuhAZEMnnBFdaSaRMI7cewn kw2KyUypJWVXd3U+t+pSad6ZJ2FOuo+0jbAlIA1J29K5fzNpcXgIT0oU0raecjL0MM iJUW3y67dkWqw== 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 v6 2/6] swiotlb: Enforce page alignment in swiotlb_alloc() Date: Fri, 8 Mar 2024 15:28:25 +0000 Message-Id: <20240308152829.25754-3-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240308152829.25754-1-will@kernel.org> References: <20240308152829.25754-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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)); } -- 2.44.0.278.ge034bb2e1d-goog