From nobody Mon Jun 8 06:36:27 2026 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 BE1083ED12C for ; Wed, 3 Jun 2026 03:18:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780456700; cv=none; b=asHNx2g28hREk22gfy6hisAWtt+5M8S6HNGGhSDCACGGl7x1NksgA5OO7svJ3/xvdz3diu3/mpS+sCxvIyEad1TFO+VywKj4g3bAjXB3sShcP6FyS6zZiGi5AQ6Aa28qFnfrV02i6eTYegANrsLODY98VU7SvIiidE9Ii0vxtMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780456700; c=relaxed/simple; bh=BrLl55rtH6dYClV7fN4/ASsckeGkMGxQmJmComhF1Zo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=eMMttHoSbr78VSm2ULanPLVj398vXRDMIx18Jh86ggGwvdVGS+hwpw0GQlsCXXaHEF70S3AtdlTPgQSY+5YdNFGazS5WkNP95jfSoqL8yzicqykpCRNU/PKWZi5yoDXHTmDrIlJB6g0Rkbc4DUGztkZ+Ec4FMLB0ZXJF4Y2iSXM= 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=FYl466D2; arc=none smtp.client-ip=209.85.215.175 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="FYl466D2" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-c859878eb48so1333815a12.0 for ; Tue, 02 Jun 2026 20:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780456697; x=1781061497; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=1w/uqKkKsuzaL77QabRVI67EugYIJNkq2g/yoq3O5NI=; b=FYl466D2tAo8OlQuJpthU01UIgt4M5vWZiwXJ/gsg5Swxsry3yTMgrFys/t36TyXAY PxylWD21tDIKPHCSZ0cfJz2t1RSJQJcUTunbUzn37PVjxhpAQne3lzQWoUyiqCgC5j4U JRsuLCbwcNp5uDXH/yUFLoW9lilH0/XFL3GWv8DCw60Y+PqZyHlFOdlOPQ/W6afS0Qxt 2P3+lJRPGXp4auNbWTZzN0XudLYvxgOaT8za1/Ge1YX2TU0m1u3IzDHWSI63/qff1AFW lebzorpGi9IIUfZrH1vPpzjqPdabRd9S+UBH/ORneXVBTwkSqEyIciSP3Az6V9tSzSGE 68Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780456697; x=1781061497; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=1w/uqKkKsuzaL77QabRVI67EugYIJNkq2g/yoq3O5NI=; b=RKwpBSvKO1JUmQWzw9tjm3AkpJgYeG7gh7wqPQUSzr7Yv/pFu4PmXfUirBmkiJXWIN Y/216NNeVczGAmMp98kj3V8ExiU0g/NNkx3GSRuWN5jUKRJRpjIrFPh9MNan+t3Ku7Iy RrrGAGAMbCzql5X81vISUNHgkl1ntb0RuvJe0wNlX6BIdWKfrXWrAQ0syT5zCnS2LN8/ A2lO8qaxZz1Rf2LqFzsxeGp6sZ+0Xywcfwz3LmyJyKuNqBwW11cbXVhnzjku8/+Sn26D 3eGrGn5tsQ5BYAuza48/6HOWIBDRYGMQqk6x47Nx+x66OGRPtUQvt+r+JHBq1yxe+OG6 B9AQ== X-Forwarded-Encrypted: i=1; AFNElJ9QuGL32rMtDvp/WfrlaoGfuZUSezPei8/mCoJMiX4mTda+z0gNafYgh2pmIKKHSxvCtDRYVbMnEwQvZFc=@vger.kernel.org X-Gm-Message-State: AOJu0YxCqicDZYgVKPrb29PO6CyCPlHYWE2EG7ckUykuMS3kSOsKCT7p yJDL7MMjjGpWBswmsNqyiefDXXyKBt8YD5ohuoCZ6d8PP6O68Nf2Qv4H X-Gm-Gg: Acq92OEdMmYzgv49C3/QURK6xX2nxFRz1YNUrPwWUGASpMgEfB8MvteQnTFvptD/Xbs tiAnX9g7XvQdzf63HyJ3BO877QxK9Q/MNMvgkFwa+mag09Dry/SSV28N9+b2Qnpl/hX3HDc6kvV hsTzYJ/fqg/hDlMbzod3eckcsY4SRg7PhJTDm2RVJ848P1Gm+n9U1jI8mNzIiUIG1bnIO2DH60i COhg4KQHWmMC+a6PWsaUeVfkbOgUugm+z3Ry3MQL1D6AvgQ1rIW7nfNSQEID/d3PUIZi5fOJHqD WCk81NeCIoz21C9pcVDU00KfpCPn9s6cMCXwbvZqYq+waQV9VmO/4Fnmdz44xxYOTObKg7QBaW2 SMQ6JeckSbSJewNq1iVS2sB1GHuqKfdgn/LW0o7Yiow6S1+pPr96i+kwRqlVOy6v9NDyeRay/jY Kk5vYO3rsm4vcgvaucDmv2fKMmNKc0lR+NxOPPwIPgyjIKI7lhLc8S1XOpF0paZC2qo2MRuEnJ1 WKUW6q7P7UlfkZVfTiVyp/zPfUkZqimhaXm/BT4I+760g== X-Received: by 2002:a17:903:234e:b0:2c0:c3ac:4af3 with SMTP id d9443c01a7336-2c163a203admr14348355ad.15.1780456697015; Tue, 02 Jun 2026 20:18:17 -0700 (PDT) Received: from ryzen ([2601:644:8000:5b5d:7285:c2ff:fe45:8a32]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c164f94a7fsm7457345ad.28.2026.06.02.20.18.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2026 20:18:16 -0700 (PDT) From: Rosen Penev To: iommu@lists.linux.dev Cc: Barry Song , Qinxin Xia , Marek Szyprowski , Robin Murphy , Kees Cook , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org (open list), linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be|_ptr)?\b) Subject: [PATCHv2] dma: map_benchmark: turn dma_sg_map_param buf into a flexible array Date: Tue, 2 Jun 2026 20:17:58 -0700 Message-ID: <20260603031758.290538-1-rosenp@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The buf pointer was kmalloc_array()'d immediately after the parent struct allocation, with the count (granule, validated to 1..1024 by the ioctl) trivially available beforehand. Move buf to the struct tail as a flexible array member and fold the two allocations into a single kzalloc_flex(), dropping the kfree(params->buf) in both the prepare error path and unprepare. Add __counted_by for extra runtime analysis. Assisted-by: Claude:Opus-4.7 Signed-off-by: Rosen Penev Reviewed-by: Qinxin Xia --- v2: use params->npages in sg_alloc_table kernel/dma/map_benchmark.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/kernel/dma/map_benchmark.c b/kernel/dma/map_benchmark.c index 29eeb5fdf199..fdc070f419f6 100644 --- a/kernel/dma/map_benchmark.c +++ b/kernel/dma/map_benchmark.c @@ -121,35 +121,35 @@ static struct map_benchmark_ops dma_single_map_benchm= ark_ops =3D { struct dma_sg_map_param { struct sg_table sgt; struct device *dev; - void **buf; u32 npages; u32 dma_dir; + void *buf[] __counted_by(npages); }; =20 static void *dma_sg_map_benchmark_prepare(struct map_benchmark_data *map) { + struct dma_sg_map_param *params; struct scatterlist *sg; + u32 npages; int i; =20 - struct dma_sg_map_param *params =3D kzalloc(sizeof(*params), GFP_KERNEL); - - if (!params) - return NULL; /* * Set the number of scatterlist entries based on the granule. * In SG mode, 'granule' represents the number of scatterlist entries. * Each scatterlist entry corresponds to a single page. */ - params->npages =3D map->bparam.granule; + npages =3D map->bparam.granule; + + params =3D kzalloc_flex(*params, buf, npages); + if (!params) + return NULL; + + params->npages =3D npages; params->dma_dir =3D map->bparam.dma_dir; params->dev =3D map->dev; - params->buf =3D kmalloc_array(params->npages, sizeof(*params->buf), - GFP_KERNEL); - if (!params->buf) - goto out; =20 if (sg_alloc_table(¶ms->sgt, params->npages, GFP_KERNEL)) - goto free_buf; + goto free_params; =20 for_each_sgtable_sg(¶ms->sgt, sg, i) { params->buf[i] =3D (void *)__get_free_page(GFP_KERNEL); @@ -166,9 +166,7 @@ static void *dma_sg_map_benchmark_prepare(struct map_be= nchmark_data *map) free_page((unsigned long)params->buf[i]); =20 sg_free_table(¶ms->sgt); -free_buf: - kfree(params->buf); -out: +free_params: kfree(params); return NULL; } @@ -183,7 +181,6 @@ static void dma_sg_map_benchmark_unprepare(void *mparam) =20 sg_free_table(¶ms->sgt); =20 - kfree(params->buf); kfree(params); } =20 --=20 2.54.0