Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2302637rdb; Wed, 21 Feb 2024 03:35:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWqMWEDV/0iEOVIdPD0yW/7uECnbTd5jiTHIUtXwxZqxfjj8cMX6jyb8K+XXTrR/rLH/HKl9QSC/M6qAHEWhTJrsbycTfNlXBVA9v48TA== X-Google-Smtp-Source: AGHT+IFrHjH6ZVEZJEktClm1VXhgBoL7FxyI/2sMqQhLuB/IRudBFdl7vIfRuJyxePebHkRZpWjW X-Received: by 2002:aa7:dccc:0:b0:564:6c39:ba51 with SMTP id w12-20020aa7dccc000000b005646c39ba51mr6488704edu.36.1708515346295; Wed, 21 Feb 2024 03:35:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708515346; cv=pass; d=google.com; s=arc-20160816; b=Os8gwruLjYO8T8GlJf502dfsUdZHCf2ybITLUsTyHMcwA1vj14BWSVD9ad77H6Sh7X A/BShnC29wuK9IyzHoijBMFzlI2kjH5tMB7W8ZvznUGMgwGwwms+KJxUnEdqAED8V9iH uvrEUx1KJMl4ZhzA0l/G7GBcqSdw3m45za5n1dWmol1lgey2oQjl2Pr+H3/fuQOHoKUJ OiQVWI83KZw/DtQJY57TNEZoH56oQTG1d9gvRNFf6WoTAoX38T0//Nh4Yqs8urBbbQ9/ GmS6Hd8XMG94bgNmkadHgtj52RU+vqWpixGqBu6AgC1ttN5kNdAfIML4nyDU0fycGdYO ylfA== 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=eMN0a9GbRFQ7R6hnKZAnMJT7UaW50yLaLLV9QwlYaAg=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=hsmJ8WSMrpfkCkpAKz8CnVVZuGLY8kwyMvv68o4rX3cvTzLnXqPGdSI1FszbO6Pvnr 8DSk7c+nG0rmb5Y+yzbbxRzPYF2r4mdpenXrsQylcrxrK/8+PJ5UeEWyRA48WtG6OXC3 nM805MXufGlTYbGiCY4lTYRhOGtxac1VsC5XLXLez3t0OadgdDAIlNzuD+/qgTDggT+3 aLkeffHoOVjzghXKNee5fWCJFREahSvhTUF+XTayX4PYkxmFbwMbeDdpIV8illJy+SM4 DAIfSJs6I3yLaAH79BiCygfMVZ9SvKrNmsWiknZxYJv2LjqdSEViJNO7wHU9TpdUICBH oCIA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=j7lVKM9m; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-74626-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74626-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s11-20020aa7c54b000000b005647c58343asi2594588edr.28.2024.02.21.03.35.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 03:35:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74626-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=j7lVKM9m; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-74626-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74626-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 1018C1F226E9 for ; Wed, 21 Feb 2024 11:35:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6346F4596E; Wed, 21 Feb 2024 11:35:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j7lVKM9m" 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 8B35545BE3; Wed, 21 Feb 2024 11:35:17 +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=1708515317; cv=none; b=OeJAV+jVStidBVpIlb55HhQz0fzUQWwwQu34fJfQMbv5xWYi/wbknlMh/HnVs5Dg57OHb6YrNvPV9c94Ga/GPmafP4wiUm4Z+5SAHjsrphgLUp4dqZG/YRuDpXoMjssSGt0hQ+7qdVyTORuZUHo44+hfRfiwo0ZpA5rDSrk2Xk8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708515317; c=relaxed/simple; bh=E1k4A1Ppx138Pm5Tuk5+a08g4wuk3s7KWHOxJ3SgNxE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ps/3JgfCbFOqxtXgcRB/rNH/Q4hZ5GTrAuadPAUIw8Vl+0o04dNlPOFYZnj4Pa6eiV2VXt70wwUQjaD7PwKaKjwJi47UO+TQY3RwL9nJ5K/Dy0Fi+FwVHZhUhZ7HfJryBh/FmG0x6ljhiG2MDpXUk3w2CVncfawp87EEp/AvQlk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j7lVKM9m; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15FF9C433F1; Wed, 21 Feb 2024 11:35:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708515317; bh=E1k4A1Ppx138Pm5Tuk5+a08g4wuk3s7KWHOxJ3SgNxE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j7lVKM9mW0eOPd7Z7L8Sci+7dRRS5SYzVi7djmh1te6RU4EO7uXYko2hb05EiHxRl WMHYWNCE5ctRJpXh1aHPoxbOufpcrWemNyMEH+zcvUhacqARTYtSB+4F0bwKDONsW8 xaQlRge7TKqehUCMJ6d1qFxh3kFYRB3oHsFPJLO5ytfyiUvuQqTrnAJI7U/rS8whqR UtWzPkur31+3Xu2I6RTYkbNW7K6V1TMC2nbPEPJSxJxfi3j2bvqUzZf0A069pvLSbv 07Q/cCavfmcNgMzEh8e1AtA2fISLjKqkXqMp5uQ4iQ+a0P1ksbGFTRFRRNOUXvfi32 EpxqduiXZCBdQ== 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 v4 2/5] swiotlb: Enforce page alignment in swiotlb_alloc() Date: Wed, 21 Feb 2024 11:35:01 +0000 Message-Id: <20240221113504.7161-3-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240221113504.7161-1-will@kernel.org> References: <20240221113504.7161-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 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.rc0.258.g7320e95886-goog