Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1620374rdb; Wed, 31 Jan 2024 04:26:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgiCidBsVgNpGGm5cyZLaVJEyfjrEGPchGxJzIhOjvlgH0KoZSnJEdsNMSW1TEZOOdkc8h X-Received: by 2002:a05:6a00:230a:b0:6de:1d04:c600 with SMTP id h10-20020a056a00230a00b006de1d04c600mr1492771pfh.34.1706703994086; Wed, 31 Jan 2024 04:26:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706703994; cv=pass; d=google.com; s=arc-20160816; b=0rlPlmGDC3oXxSw6QRuY4ztNCjU1SiZJsqv52NLTUatxPSCiic+nkBVLSsuItRdHFg egvuesVZHaukyGwgqvragrJaMegqG7EaSM2DdkCFT1QjGyvD5ae08KX6r+bZwwibqeA4 DlIuyX3j9EpJWnyYqWe0jGi7Q5PNO1Okmn/lJi3AyZqyKGk8enYjjdF0w5T176dPRTP3 OVwuTKk28pvL1tH9FsyVDoSlP+9w+tGmiR6vdJQJ+PkiwFPv+4da3DmYa+D1d4jUhl2E OP3qCN7mKidFrnjjvVL5mnYb1BcjQfBC/X2bnHSCoWwxJtNdi4rN15HMOHHr43VMPikg wXsA== 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=gfrJ/MvOpfNo3jOw+ZQoYujZ58OWTw2bWKwzL7hKU98=; fh=sBME79IxMgZz4Huucan3ScWg1q6r8OzHXz7SCbzWXfI=; b=fHGF1nE96hW5Hw0ohxyFUxU/hP9R+svO7OzuKnBOEanCV289kvgxoAa0FLrd9CsPLL enhf0uX/F5LvVvX0u7sW3FGmMFnM0jwnxKPRmeE9almxtcKkfuSFxE3uaE7ObUUeREHE Bthp0cJl/bKokRHU9aLs8d9j29ECQBIQFnUCs5Z2Ll8P3CLNttoaUtAhBQw+BEW8DTfO OLD1KtHo6igQmc2XB2YNk37Rg9edYzSJtfqUvW6LAx4pM/YhfePTUdp1kDxqIDduTn1s Lon60M46qzXSB95oaNG7mwPktpyvvcgScq93LVAKxxbf/WD9usacLWnqo7A2CfugjNMq WE6w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=psoPX5uw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-46417-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46417-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCWVqD6fKRiGNfIL13OySTGzUpj52+R4OzPTn+D+8Gp7qWyWr+2q7pBMO3z2m2MQsEsFK/7Je1YZzqKN1Rt9j/3uw+G1sa8PeYlXO7NCvg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id a15-20020aa78e8f000000b006ddb83a5da9si9252161pfr.190.2024.01.31.04.26.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 04:26:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46417-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=psoPX5uw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-46417-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46417-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B7C28294739 for ; Wed, 31 Jan 2024 12:26:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EDC1C7AE68; Wed, 31 Jan 2024 12:25:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="psoPX5uw" 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 250757AE43; Wed, 31 Jan 2024 12:25:53 +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=1706703954; cv=none; b=oYlFCJIPrL/AiPggh29n6+X8ZcJmx3RhANmp5uRX9RLXQuWPhx8le0Q9Hde7HiF4AGhhktjj1Gi3PLrNsQaFPY8CsNbn5HZOF6JNrOa3Bh5bzxnlwZuN2yUGPqoLyVXSQV6ikf3JeB3UfAezpO05X9oW38x31o5etlmjOjCRChI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706703954; c=relaxed/simple; bh=tvZfP5ZDpz93rBnkWzn8NpisHuCrYWdGw0rDfTGSi0w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WBCC5JC6QIQ4WZkaYHAfzdB49yS88jDXXkjjrb2kSScPCdP0RuRz/LkLNXHGEibV6IDwZ04VnHCq+K/UbtrB5vuaio+pu4SxOJCh1bikLym/Q2wAzGLOaz+SQs/Fwvc0d3ut0BvkmM00TP4idac45nc0j14rP9p9keb7W0X4E2U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=psoPX5uw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF82CC43399; Wed, 31 Jan 2024 12:25:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706703953; bh=tvZfP5ZDpz93rBnkWzn8NpisHuCrYWdGw0rDfTGSi0w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=psoPX5uw05sqXOPDqQR+NcDpcVMXHTxr+ToULGVDXA91pOO4sDKAW6xGUtj1rRygW D1EtgsNsBW9z5MOfJUcBMMPzBLOjsRe5XTga8bzLOa9b4zvWWAc2G2Q2iTA5t4xNLl KtKJroPbgWfp2SuaPIwX5pRBEAo2p5SdfaIqjyriutK+J0vITqUdA8IevRtxbZ7C04 OzPdkiEMEfkJhGuV3ncoGiA+2hyHBoPDwsAJF/MLOQHkHbTppxE4W+zQfG4r1Zm9El u4b0V5kMmLRzZWIqNX2v763gsFq1lQNl7pyDMsvD0ODRbE5iBJ23OxIZf3HL5EqGZG v8VuNBl1Xzb+w== 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 Subject: [PATCH v2 2/3] swiotlb: Enforce page alignment in swiotlb_alloc() Date: Wed, 31 Jan 2024 12:25:42 +0000 Message-Id: <20240131122543.14791-3-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240131122543.14791-1-will@kernel.org> References: <20240131122543.14791-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: Petr Tesarik Cc: Dexuan Cui 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 56cc08b1fbd6..4485f216e620 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1642,6 +1642,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.43.0.429.g432eaa2c6b-goog