From nobody Mon Feb 9 14:51:02 2026 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F20386348 for ; Sun, 26 Jan 2025 09:09:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737882564; cv=none; b=b+8yQTp+7yTsVCbfZwdTg8gchnPwkHf5dv49NM0KwjnszooPEop6QIf9NXxmvWq3Tudup+EJTd52Y+tbBkGNNaJvnnXLE2KAmOY3y3Lt9qegNwLwfx6QmV6UZZZsGWDY58XcvpoGY6NlAtHbErNGHcFj0Wvqldyj4QzhNBKe78A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737882564; c=relaxed/simple; bh=YaGoWGpsklGIQt67nkm2fqrq/O3GNFyu96dmoP7FMbA=; h=MIME-Version:From:Date:Message-ID:Subject:To:Cc:Content-Type; b=kJQrBP9xzUwrz7Xq1MNkIk6NdNVNF8jtSIX5cQysRpqwWBKn3h5RELfb3WZCyJFXoL4utUCJS1cz7Xt3u0Pgq9/Sa8By0cR1jZOxMPdckysNomRWblpQK1CBkgwJDhN46MHlUV6XJarJZ7r7srSPFIxNldDjzw4Y45sp1M2Qco8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=B9AwK5yp; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="B9AwK5yp" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2f440e152fdso834764a91.0 for ; Sun, 26 Jan 2025 01:09:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737882562; x=1738487362; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=3+cfvIoxVPtQPbBiDtAArxp4pruZxdup7yQD95mYg5w=; b=B9AwK5yp/aMgSZvzsWyk9arD2gvmQ0//ROR/FKPSPetc5WEAH/s/a+AdFS4QXUEf2U hW6Tz7MwXUphJ7pm0SVH9pZ65hbV8zUA7Tw1rx2m8wWnPTY71Axvjfu4eYZ2HlmIyMRw eImHI1SDcdtjmGvF7B84H33BxWSnqJ/am9zHjsuvBgrCRN1XWs4yy6W6C1vm8Ex99S0P ZQbnIdBwCCH1YwboMgfvnLair1Fgolufve9ZJyBGpQa5Yk8rBiB5sv1qh0bE1eX/2/To ZBp4f2+QBvq/XmPGKYekzZGSLfQnZ6AqNAAjy2PsOMQr/ggwCioYoLS1YGmr3AqoTvwU oALg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737882562; x=1738487362; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=3+cfvIoxVPtQPbBiDtAArxp4pruZxdup7yQD95mYg5w=; b=iCVhdBqEScJScs1DH7uv40Z07gBLU/YQqrvdNbrqCQaR5GE9jMLJjXOJSfPCOvAv42 bo/efozRcwOW8DNFKsYnd226BtqR0D+vjjPO3xMU2XxQaB9JhG7SgFWdx4imqrVQ6+um utGXdTIrgvQp62XPxBSc1Qm3I7xoqRr5GgcDSnhepFnGJeXo9DYcXreFkG7kQkhZbytl TQXlYIfQiaPYRgBOonKlpuGDjylYFx9q0P/2vyTXzPYzzkddOBe7Ggx8+GbGnM37l6WP A5RYxK1RsPWnttJuZx2ScgGKC+7quYwyCGaXgeTPNZL8D8H3BYlnrro1d9xivLeZeSFg Mvtw== X-Forwarded-Encrypted: i=1; AJvYcCUlNtYccEPCy4o9PnefMg1QXAcj2C9ybpDohuu1ZCt0lyzrmdJVdqHaG/2cBBMZzSlD3te2VS+a4iVKmZY=@vger.kernel.org X-Gm-Message-State: AOJu0YxNdiPm/4PT7HuxHsb1i8asXPnX9JwniPqne3tH7UDeotYZbuqE tEdGfcnj4uKCBOd0jF+VQVJdYrnH5xGEF7FqDtSA4InYWAlnRLP2g1aC5T5B6bTIepf2lMYMTOW 9A5pzn7vO/33fuEBhYvZYY46pa/0= X-Gm-Gg: ASbGncs+hcYq3Urt3Z3mGRehsB+sl7ZV6kIJlags94BEzy9Ba/HDUE+pOYvaurRtI7U V/ygYghjtHkPyNNZ5Zf+69CUPCm+kGnCVu6vN7AUayz+M1Z25YxhBAfWmFVf7Aglw/gVpvS12bd BQKo+qIbsluohmDYqqq90= X-Google-Smtp-Source: AGHT+IGyFrPT4kR55B4ZMMZHTE8tGSsiEWF19KzvgoA/wX6bcBPgwPkzKXnYGeJ3GAz1YYzHx47vq+dmkRrs5wTKm9A= X-Received: by 2002:a17:90b:6c3:b0:2f2:a9bd:afe9 with SMTP id 98e67ed59e1d1-2f782c562e7mr19339344a91.2.1737882562298; Sun, 26 Jan 2025 01:09:22 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Dima Stepanov Date: Sun, 26 Jan 2025 10:09:10 +0100 X-Gm-Features: AWEUYZm5hLwacDRU2jedBsltQ2YaAUYDFf65Lzvj2nTxQ-1bOx2-UciHK0b6pYs Message-ID: Subject: [PATCH] kernel/dma: dma_common_find_pages returns pages for requested address To: Christoph Hellwig , Marek Szyprowski , Robin Murphy , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: Dima Stepanov Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Recently hit an issue on an attempt to mmap allocated DMA memory to the user space. It isn't the case for any device, but only if dma_mmap_attrs call will use the iommu_dma_mmap() function as a backend. If the kernel address (cpu_addr) passed to the iommu_dma_mmap function is the same as returned by allocator (dma_alloc_coherent) then memory will be mapped correctly. But if the address passed is inside the allocated region, then the mapping in the user space will still refer to the start of the region and not to the middle of it. The reason for it is the dma_common_find_pages() call, which returns the very first page of the vm structure regardless of the cpu_addr passed. The idea for the fix is to return not the first page in the vm structure, but the page related to the requested cpu_addr. Signed-off-by: Dima Stepanov --- kernel/dma/remap.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/dma/remap.c b/kernel/dma/remap.c index 9e2afad1c615..238fbf2821a6 100644 --- a/kernel/dma/remap.c +++ b/kernel/dma/remap.c @@ -9,12 +9,15 @@ struct page **dma_common_find_pages(void *cpu_addr) { struct vm_struct *area =3D find_vm_area(cpu_addr); + int i; if (!area || !(area->flags & VM_DMA_COHERENT)) return NULL; WARN(area->flags !=3D VM_DMA_COHERENT, "unexpected flags in area: %p\n", cpu_addr); - return area->pages; + i =3D (PAGE_ALIGN((uintptr_t)cpu_addr) - (uintptr_t)area->addr) >> PAGE_SHIFT; + + return &area->pages[i]; } /* --=20 2.43.0