From nobody Mon Jun 8 22:51:23 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 77113388366 for ; Mon, 25 May 2026 22:06:47 +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=1779746808; cv=none; b=d1X8j0x92E4Ma2US19+IxGjuxh+XDeWHOxYrFRaFYWlKKblG+YhFanqqeU0TQ6j4rZOQGq6TJy6jtVZr2jMK+I2P7WyM9+9X4QPdsP/mpHRtB5CTWkji5csd47vr6sZldfYI+sm+O5zcBhocPaOiq4ogtZd+e8An0SaZKM7TrY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779746808; c=relaxed/simple; bh=YtQ/yTW46aJTgBS2RYdCC7Mi93eGRCfeb8vPr+OmWWs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=FIoJ0rUa4BWt4G21+ta+HBAxqoEBWpE4l06A5Oaqw3B8GXhI/h95k0Tj3iYFSHmJeyxtl8LoDPE60bWfJzp3cX3p7h/Jdl7GYiQy4PRPyL/j5HYrbgSPvTVvGWh4P5FMKswlQZ0Kd2+qeVUh/FumDaF96DljCH0xiGU2ZUeNjK8= 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=oFdiDtQn; 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="oFdiDtQn" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-36ad15213fbso810938a91.0 for ; Mon, 25 May 2026 15:06:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779746807; x=1780351607; 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=Cb+vYhTU8Eum/FCo/941Nl+1j7gWIUnZrBBYNxLzj3g=; b=oFdiDtQn0tVfBDlXvhUu0Eo9QrgA3bS9hbhqW3010U3IIOE7IMV0YrRfk92dmRSJa1 iX0LFjpTSlhzWVzERDah3+WxC6SMZkIczMKpNSITXPa6JAHrZea+LsaYhQvGV0j5wh5q 3N/tmuJTWUOIWnjfIFueD8JJK1UaaZmhGhdMYCpV09B2iJQQTph6DKLNZMsUULh+pM8a apLIjYtCeKylNQJ/okpYsrNMYZNWVkbGX7eClIqN2mKkqmghLne7F9obxgTHOyb1EA6W hm7jlbDArxCsxRjzhU1xKOob5cxwaB/fnFCzicpaadMiqIfxExp/MAABFoPj0lLumiIL 6fAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779746807; x=1780351607; 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=Cb+vYhTU8Eum/FCo/941Nl+1j7gWIUnZrBBYNxLzj3g=; b=AReufJ/SsLz22h7n9cpC51yEVk6l7ZrEJWuxvZIRx+a27g40EaWziiVHyVa8ASasNU ejRdPzmzl2pe1+xLAnLQNpgJJKR0+SY+sI+cKnDwWJoK4Miz5ydpZ1gL9MtkTYogw4Ty IkdGKvKotRty2jLy+kJ75JxGb59BaBka2E56G1+NEOW0gdjQU842g8OCISgEoDiRawTN YmnITDlKhadeiTuP2KpZKDjf+42zdLn4QfZVf0qSdSsHuNOMGtwtPb3aM4/OnfBcWN3k +NQ40Cg3Vqz5sIeewqy36a3PhVfIMbSCDQG1WR1oMeHVg86YgIXhLeP+b48rmopLg8oU w8sg== X-Forwarded-Encrypted: i=1; AFNElJ/lbiK+0LJSLzG4IePZZCImh5tlB/knUCStN/NIiGiTzhOl7S1p2zTQ0DDsbGnbMrXShHZVR9VvXoQnYxg=@vger.kernel.org X-Gm-Message-State: AOJu0YyZ2JP/r8xSKits/Nmgf1oRHEcU9wTLOJOJE7HNDxy3L0MVwD+g M+sjj0HbzvhG8VTIv8AgfLp9jmo4avNbapvNZOEABekFmekUJ/FZPt0YvijmxQ== X-Gm-Gg: Acq92OFEo9i72rSPeRUDgnCJlflwFCa2xr7s2n2yRaM8Zyj70XYFqc3t+eFWnhlg52n Rh6h/YsitpgIINTrkrbW/6JzyP1LiFbzP5MCo2p1Zd0ts/560WYgfUeqrWuXsjJ9mvNrOQlRRbU JfAEn6mN+KgPPuMefbveppyZCKJ8Q63AKLkkG62gUir/BHaDc3vk9ZjKVCC+k3huGD+AFIElsmm Hp6ZYkDRpFzyoFKBkRi3CQ0avVLdZpskHKZzFf2e8rIYjTq3PWKDymSexEGRHBtCOLvFF+rihIL cTRBiE6ROH5Lcjl7auI2Lu5QzU90ZFrL9q6t0rWaidxFR1S342n5XkSYtlekkkoF3KBAedH0g1o nvXo49Cq5E8/RL0mJbYTclHFyorkshCX7xQaU5g4ScIJ+FIo9v46dVJ/+NiFAtDWBuILT6UiLZc oNhaFJ/Uifsz+G25Zzbo3poCWOdsGNzHrYCAH9ZYnED9V7IKyAPBd+SbAR/z4/zFtfSFiXvQLIH IKIRpZVY5J5ArfxMk313tzDO65DOZgKvLw= X-Received: by 2002:a17:90b:5343:b0:368:a27f:9083 with SMTP id 98e67ed59e1d1-36a67761c51mr13108866a91.7.1779746806663; Mon, 25 May 2026 15:06:46 -0700 (PDT) Received: from ryzen ([2601:644:8000:5b5d:7285:c2ff:fe45:8a32]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36adc0a6e94sm4713224a91.4.2026.05.25.15.06.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2026 15:06:46 -0700 (PDT) From: Rosen Penev To: iommu@lists.linux.dev Cc: Barry Song , linusw@kernel.org, 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: [PATCH] dma: map_benchmark: turn dma_sg_map_param buf into a flexible array Date: Mon, 25 May 2026 15:06:28 -0700 Message-ID: <20260525220628.94833-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 --- kernel/dma/map_benchmark.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/kernel/dma/map_benchmark.c b/kernel/dma/map_benchmark.c index 29eeb5fdf199..a65da5c7710c 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; + if (sg_alloc_table(¶ms->sgt, npages, GFP_KERNEL)) + 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