Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1106620rdb; Mon, 19 Feb 2024 04:41:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUzpDhJEunwlnZIJknE6t/OubT0hMA2r8iONIrddTqWzZLFYyBzzEzS0etT+YOfTXyRTWdnANMo/KscKDIvQa5t6sWjWBXeQDDMuH5okQ== X-Google-Smtp-Source: AGHT+IFmH2zS2GB+axaVnsx1SWhZHR9IoIozUUBDTQp4KghCdbBXKpGdm4ZL1mwCpRPFViJwessl X-Received: by 2002:a92:c8ca:0:b0:365:2ffc:b747 with SMTP id c10-20020a92c8ca000000b003652ffcb747mr3167489ilq.17.1708346484817; Mon, 19 Feb 2024 04:41:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708346484; cv=pass; d=google.com; s=arc-20160816; b=MwjJ5FMzjLaRShiSEfK8KQ+ai3fQZcZErFN3LzFXRrsAFclLcEiLgYOxW51WQTLgs5 SpGcctaG20c47l5GggHA94uQsSk3mdo+WDWs8ra702tj3bbG5LNX/Ecw6uCIV0fxE8XA YY1iJukGNYSAUtc3nGE5fKKbYjPopQMhjjSap2QlKfeHCaa8wC3s6RbNcBGQh1YW6ywp WAP1bRVvei9DCMjSYBKU7Pr3GNXbp8K6jysm/RZ7Et4xmqLxlxcxRqWrcgnuWEFshkcR +WpPTVEZZuJtw6op7jrNtBfSk7u3nq+GKvAoDQo3ChfPWQ5gkQ+eTDPHVGy68o2/r5sP 3pgg== 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 :subject:cc:to:from:date:dkim-signature; bh=kcbZxawIF5pJIF3e6Fe0CSzj4Wcoglr3TFCIIff5vXc=; fh=Pt/vLgYoachKTmrGCk8iX6cxHu3EQKHMuGxf+QrSHvI=; b=irJsHFPlemU0RTuG1i9783xCXGifa5B8MIpma85flH8MX1gAjH/6IUfkQ4MZnVwZP6 CUtjwcJ3DSi6YCD/b7icxY5e7hS51ZMichvQWAtc25IPgcevb8D6GmcbW/qWGi5PRF9i 7XTrUX3fGxFHJ6XKlTUrlUdTEmdOBh5HpkNeryvwmVs+CiTwTS97dL9l2hmPm7npPIt8 wV16ytqUI705vk0X7a7ciOX4y5Hmp8Pir4I3v/gB1kyxkskyYPSDTxDgR8mpyeX3R5gK MRyCv+MO4qoDosT30ahcXjYXc7j7UrYg/q4GmM4nnRm/0WuTAwz2GWXdZxbQmFcIDjXa 4ADQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=R3Z54+BX; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-71336-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71336-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id n30-20020a63721e000000b005dca1e01c0asi4321523pgc.880.2024.02.19.04.41.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 04:41:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-71336-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=R3Z54+BX; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-71336-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71336-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz 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 818782824B8 for ; Mon, 19 Feb 2024 12:41:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2B8F62E834; Mon, 19 Feb 2024 12:41:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b="R3Z54+BX" Received: from bee.tesarici.cz (bee.tesarici.cz [77.93.223.253]) (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 DFC492D05D for ; Mon, 19 Feb 2024 12:41:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=77.93.223.253 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708346478; cv=none; b=DXk0GWLHSD72rU+lQuLji+hCF8XHQXnPvXFcTub7+PPbm32bs2q7SGXtNDBKFP77Dnomlt8kRmP1RtPHaw57FsTJkP90+WFF91LChQzg2bTvx6GYCJ5B65A0oWaTJjHPyr5lrEwh50+t1nNv/j6IaIJhumYtLqZ3MtNzOZqpN4w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708346478; c=relaxed/simple; bh=e2xLVu+6ot5sg/6bm7jkOGA6ufHpcMGxIdDhHPg0WHs=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tnW/ctGMm0WWZnk2Gx0Y8ksi/5wXShzh5A3JafVrWDmTJNt59pwRUCi26KWuNXB+9IAIX5tWQhaui/CaeKWXYyW2AR2s0LumJbHNYDPlI3bJLyWBBBb6PYPxsjsBoLKlkwhsXFqdxmaFN37QUKUkDtiu43nlCUHnCfLgwW8FTuI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz; spf=pass smtp.mailfrom=tesarici.cz; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b=R3Z54+BX; arc=none smtp.client-ip=77.93.223.253 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tesarici.cz Received: from meshulam.tesarici.cz (dynamic-2a00-1028-83b8-1e7a-4427-cc85-6706-c595.ipv6.o2.cz [IPv6:2a00:1028:83b8:1e7a:4427:cc85:6706:c595]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bee.tesarici.cz (Postfix) with ESMTPSA id 43CA11AE035; Mon, 19 Feb 2024 13:41:15 +0100 (CET) Authentication-Results: mail.tesarici.cz; dmarc=fail (p=quarantine dis=none) header.from=tesarici.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tesarici.cz; s=mail; t=1708346475; bh=kcbZxawIF5pJIF3e6Fe0CSzj4Wcoglr3TFCIIff5vXc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=R3Z54+BXqzlS3OPPkzdjwGOCVJRRmQm2XfS7SDi44D1NjRiwciIalt7KU3NlYAGfE nULPo6DRkwbiuZJHh+dS1qT7N3sNZj4chcFQt2HQAkv9nfBh1rH91txI0R9IEbYrHK Lm+nIAqXY5GOX3MKDpNT5L0/p3HyLqMuz2B52GBC9dBYFiUltM/vrYNu5+05ez82lx 0qVPalhqT/63k1b8ZOED7rFlUNl49SScAKB6p/cQynuea0jKTOAxNN3Z2zbd8kztin 4P06m1lck+QpKTQA2KH4KAybPvi3HcHdQfJpxRa5XAIBHjMbUw0Wa1lKsNPNFqNKq8 MtJ3YzI2Ig6MA== Date: Mon, 19 Feb 2024 13:41:13 +0100 From: Petr =?UTF-8?B?VGVzYcWZw61r?= To: Will Deacon Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, iommu@lists.linux.dev, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Dexuan Cui Subject: Re: [PATCH v3 2/3] swiotlb: Enforce page alignment in swiotlb_alloc() Message-ID: <20240219134113.24396ad9@meshulam.tesarici.cz> In-Reply-To: <20240205190127.20685-3-will@kernel.org> References: <20240205190127.20685-1-will@kernel.org> <20240205190127.20685-3-will@kernel.org> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.39; x86_64-suse-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 5 Feb 2024 19:01:26 +0000 Will Deacon wrote: > 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 Reviewed-by: Petr Tesarik Petr T > --- > kernel/dma/swiotlb.c | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > index 9ff909a0039a..adbb3143238b 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)); > }