From nobody Thu Apr 2 20:28:13 2026 Received: from mail-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41]) (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 1847B425CF7 for ; Thu, 26 Mar 2026 18:55:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774551323; cv=none; b=D/ef+tBWTelvF0c/X328IDmkfaGfwfcdBChirODx/OKuewMmYbvUH7IqMVu0/waSloAxfR+VSkG+LkFN/DsD7db7NnTYfaW5yxm3G51tEHRIHtFpj2eStrQm9jO0Z4X1Ff622/tkD0u6N5X1bEh9X3nDN24yr1QIsx13hNQQtM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774551323; c=relaxed/simple; bh=oq1YShVuokabewzhhC4F4u//3loNomj7t/HzAoIQxeY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DAl2Rk+XF2sg38vtbVqvA49sgcWgRMjbj+Yt0PqS/m1olf9DyC/zbFPCMDWHv5NQqkLXy0k17BHlea1ByCW7SlTt9M9iGF4HMjI0kulQ7Deo0livZU7QREC5jUJYMM0Oq/QDkuYCRVOkNcq4/m+47upY6Gq8y69vm1q+NvGtJQg= 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=TzZPWtLI; arc=none smtp.client-ip=209.85.161.41 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="TzZPWtLI" Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-673ee2a98b1so763563eaf.0 for ; Thu, 26 Mar 2026 11:55:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774551321; x=1775156121; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kmGyHMtly7Qz0CqR2wXNKvZRnLsbvPnYKpSxXzkbtSQ=; b=TzZPWtLILbCI1fx2pbsRhjqEChqvopFZnfUo7m479bv9sNaATF3/YFHjGzKNl4qJXV v/avyz6pFKXCDzXaSfQAcTLjeEQGNzEL5r+5r4EMbG1SrUs4dHL7YkhBlo9sZOTixozT PDpxXXZneXE93GPsvw4Deu11XxVfL7KBDh6dMWVVw267izBv3Fi58wVbsjpJghUSnd34 7L3qCghWH6p7bEyN1elmd3T66r4/T+06jVQBQOLOLgPNscY3peU47kH+JvVH2HOkufcL sxB7UC/z/eDnTkVPbyuRjF3fLggjHPk6dIry2BovQejqkZ6OQoyFRYw65YF/Q+ZvbKtg qYIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774551321; x=1775156121; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=kmGyHMtly7Qz0CqR2wXNKvZRnLsbvPnYKpSxXzkbtSQ=; b=UOKVZ/hMAD5+a93nxwW9z+uDp2G8XTEB5UCjBfzr139bQF/C2g76jpSj3P4mk/7rDA MwjCP276fdIkLtPL59g2OrJxE/ZewhzKCpWtb12IfpboV9k5fNpN8oCfymsl+Dqi6NKL YHHaEsJNKDE9nlKLL4oxkPDyHZQw1JKgGwoPLwpb1/ySJrY5Ls3TCnP9928WC0LW3ygz uDXc8HM/huoajC1HHPkiKTse3hJ4OWmX4aEgABHgxduhhN/9AgHNHmFeAOxrxL1NTAfC NREHxMlR5QMSPy3WVXNb37ht0QanT9/QUafvQ0/cUGeI8JWvf7uSVVNzzuDPTFFt/A76 fEhA== X-Gm-Message-State: AOJu0YwyExNnS9FgExrGzBaKTq2ORWcfBVcKfZPHhpoMC+Pfp6udzzQF JhZoa+PiCS1hZn+jZ6gpZ+7YVxF833Ls2qfdhXtC1jHQeJd+TpFAQTLPMxtDvQ== X-Gm-Gg: ATEYQzzAiRyifV/EoZsi6q6hE8kJ578xYA6GuzGxUdvJluZ9uNrNG5uzo8QRTeJMhZY /OcgLPWZbBTbwcb/akjgZPR2+xmYl2F9D5QeylsqwpYzwclr6egaSNh39jZYVZyJSqc+Q9e0Cg6 omeKzI+InGidv/IyxpHPiaKbiS3tkQG04OjlI+TIVDhEipwmzqZtJkXC9hRZ6Wgbln2wNI/Eybg J5nXg0qY16XboxwtjIgIuBiWbG+vvE8M8iCLnAhLQ6HMTy+UNHbz135M+eitNUkc9K8OG73gMGO 1eosWGttnkZt8Lsw7xkpgS8k3uX4tzCma8P/OM2Xgq0Ms1exuFAdF+AWjozN5Qm7cur87dIJo+L HgIwKHKkMcdYZREtZfkBZFmDdo04hZjoR2BPDIcG5O4Gh4omx1AQEcj75xzVW+xrxMfu22UqVbf DADnoayaEZFhS0BbA/g0hT8E0bWxzhsf2dN6kfFcOboDGW43uaWnng8GNW2hA= X-Received: by 2002:a05:6820:4715:b0:67e:d8c:9f87 with SMTP id 006d021491bc7-67e0d8caa1amr1460403eaf.5.1774551320853; Thu, 26 Mar 2026 11:55:20 -0700 (PDT) Received: from frodo (c-98-38-17-99.hsd1.co.comcast.net. [98.38.17.99]) by smtp.googlemail.com with ESMTPSA id 586e51a60fabf-41cc7760c08sm3075171fac.4.2026.03.26.11.55.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2026 11:55:20 -0700 (PDT) From: Jim Cromie To: linux-kernel@vger.kernel.org, airlied@gmail.com, simona@ffwll.ch, jbaron@akamai.com, gregkh@linuxfoundation.org Cc: jim.cromie@gmail.com, mripard@kernel.org, tzimmermann@suse.de, maarten.lankhorst@linux.intel.com, jani.nikula@intel.com, ville.syrjala@linux.intel.com, christian.koenig@amd.com, matthew.auld@intel.com, arunpravin.paneerselvam@amd.com, louis.chauvet@bootlin.com, skhan@linuxfoundation.org, pmladek@suse.com, ukaszb@chromium.org, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, amd-gfx@lists.freedesktop.org Subject: [PATCH v12 34/69] dyndbg: harden classmap and descriptor validation Date: Thu, 26 Mar 2026 12:53:38 -0600 Message-ID: <20260326185413.1205870-35-jim.cromie@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260326185413.1205870-1-jim.cromie@gmail.com> References: <20260326185413.1205870-1-jim.cromie@gmail.com> 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" Dynamic debug classmaps allow modules to _DEFINE and/or _USE multiple classmaps, but this requires coordination amongst the classmaps. Previously, class validation done by DYNAMIC_DEBUG_CLASSMAP_DEFINE at compile-time, and ddebug_class_range_overlap() at modprobe-time, was incomplete, and DYNAMIC_DEBUG_CLASSMAP_USE_ had no validation. This could allow broken classmaps, making them harder to use well. This commit improves classmap and descriptor validation: - Mirror the compile-time limits of _DEFINE by adding a static_assert to validate the _offset value passed to DYNAMIC_DEBUG_CLASSMAP_USE_. - Add run-time overlap checks for _USEd classmaps in ddebug_add_module() to prevent collisions between private maps and imported APIs. - Scan module descriptors at load time to print a single warning per missing class_id, rather than waiting for a user query to trip over it. - Downgrade the global WARN_ONCE in ddebug_match_desc() to a pr_warn_ratelimited, since orphaned class IDs are now tracked and warned about early at module load. Signed-off-by: Jim Cromie --- -v12 - squash several enhancments together drop run-time USE check, now done at compile-time s/WARN_ONCE/pr_err/, dont need stack trace for this, and do want multiple error reports, so dont quit on 1st err. Now that DYNAMIC_DEBUG_CLASSMAP_USE_() has an offset parameter, it is possible for a user to specify an illegal value - one that shifts the bit-range past the 64 bit max. The macro detects an offset > 63, but this isn't enough; the legal max is: map.length - 1 + map.base + user.offset < 64 Testing class-map vs class-user overlap is nonsense if the class-user range extends past the implemented limit. So check that 1st, before looking for map/user overlap. To validate this, add ifdef DD_RUNTIME_CLASS_CHECK code to test_dynamic_debug_submod.ko. When its enabled, it creates a bad class-user record via: DYNAMIC_DEBUG_CLASSMAP_USE_(map_level_num, 55); bash-5.3# modprobe test_dynamic_debug_submod [ 19.359818] dyndbg: 23 debug prints in module test_dynamic_debug [ 19.366239] dyndbg: module test_dynamic_debug_submod: base:16 + classes.= len:8 + cli.offset:55 must be < 63 [ 19.366612] dyndbg: dyndbg multi-classmap conflict in test_dynamic_debug= _submod [ 19.366945] dyndbg: dyndbg: failed to add module test_dynamic_debug_subm= od: -22 Finally, replace the misleading "Failed to allocate memory" WARN in the module notifier with a pr_err that reports the specific failure code without the stack-trace. Signed-off-by: Jim Cromie --- lib/dynamic_debug.c | 67 ++++++++++++++++++++++++++++++++++++---- lib/test_dynamic_debug.c | 9 +++++- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c index 66879a40b822..3cd9b67bd995 100644 --- a/lib/dynamic_debug.c +++ b/lib/dynamic_debug.c @@ -320,7 +320,8 @@ static bool ddebug_match_desc(const struct ddebug_query= *query, /* site is class'd */ site_map =3D ddebug_find_map_by_class_id(di, dp->class_id); if (!site_map) { - WARN_ONCE(1, "unknown class_id %d, check %s's CLASSMAP definitions", dp-= >class_id, di->mod_name); + pr_warn_ratelimited("unknown class_id %d, check %s's CLASSMAP definition= s\n", + dp->class_id, di->mod_name); return false; } /* module(-param) decides protection */ @@ -1420,6 +1421,23 @@ static int ddebug_class_range_overlap(struct _ddebug= _class_map *cm, u64 *reserve return 0; } =20 +static int ddebug_class_user_overlap(struct _ddebug_class_user *cli, + u64 *reserved_ids) +{ + struct _ddebug_class_map *cm =3D cli->map; + int base =3D cm->base + cli->offset; + u64 range =3D (((1ULL << cm->length) - 1) << base); + + if (range & *reserved_ids) { + pr_err("module %s: [%d..%d] (from %s) conflicts with %llx\n", + cli->mod_name, base, base + cm->length - 1, + cm->class_names[0], *reserved_ids); + return -EINVAL; + } + *reserved_ids |=3D range; + return 0; +} + /* * Allocate a new ddebug_table for the given module * and add it to the global list. @@ -1430,7 +1448,8 @@ static int ddebug_add_module(struct _ddebug_info *di) struct _ddebug_class_map *cm; struct _ddebug_class_user *cli; u64 reserved_ids =3D 0; - int i; + u64 bad_ids =3D 0; + int i, err =3D 0; =20 if (!di->descs.len) return 0; @@ -1454,10 +1473,46 @@ static int ddebug_add_module(struct _ddebug_info *d= i) dd_set_module_subrange(i, cli, &dt->info, users); /* now di is stale */ =20 - /* insure 2+ classmaps share the per-module 0..62 class_id space */ + /* validate the per-module shared 0..62 class_id space */ for_subvec(i, cm, &dt->info, maps) if (ddebug_class_range_overlap(cm, &reserved_ids)) - goto cleanup; + err =3D -EINVAL; + + for_subvec(i, cli, &dt->info, users) { + cm =3D cli->map; + if (!cm) { + pr_err("module %s: classmap not found for user\n", di->mod_name); + err =3D -EINVAL; + continue; + } + + if (cm->base + cm->length + cli->offset >=3D _DPRINTK_CLASS_DFLT) { + pr_err("module %s: base:%d + classes.len:%d + cli.offset:%d must be < %= d\n", + di->mod_name, cm->base, cm->length, cli->offset, _DPRINTK_CLASS_= DFLT); + err =3D -EINVAL; + continue; + } + + if (ddebug_class_user_overlap(cli, &reserved_ids)) + err =3D -EINVAL; + } + if (err) + goto cleanup; + + /* validate all class_ids against module's classmaps/users */ + for (i =3D 0; i < dt->info.descs.len; i++) { + struct _ddebug *dp =3D &dt->info.descs.start[i]; + + if (dp->class_id =3D=3D _DPRINTK_CLASS_DFLT) + continue; + if (bad_ids & (1ULL << dp->class_id)) + continue; + if (!ddebug_find_map_by_class_id(&dt->info, dp->class_id)) { + pr_warn("module %s uses unknown class_id %d\n", + dt->info.mod_name, dp->class_id); + bad_ids |=3D (1ULL << dp->class_id); + } + } =20 mutex_lock(&ddebug_lock); list_add_tail(&dt->link, &ddebug_tables); @@ -1472,7 +1527,7 @@ static int ddebug_add_module(struct _ddebug_info *di) dt->info.descs.len, dt->info.mod_name); return 0; cleanup: - WARN_ONCE(1, "dyndbg multi-classmap conflict in %s\n", di->mod_name); + pr_err("dyndbg multi-classmap conflict in %s\n", di->mod_name); kfree(dt); return -EINVAL; } @@ -1559,7 +1614,7 @@ static int ddebug_module_notify(struct notifier_block= *self, unsigned long val, mod->dyndbg_info.mod_name =3D mod->name; ret =3D ddebug_add_module(&mod->dyndbg_info); if (ret) - WARN(1, "Failed to allocate memory: dyndbg may not work properly.\n"); + pr_err("dyndbg: failed to add module %s: %d\n", mod->name, ret); break; case MODULE_STATE_GOING: ddebug_remove_module(mod->name); diff --git a/lib/test_dynamic_debug.c b/lib/test_dynamic_debug.c index 08d4e3962026..db555f5f8ea4 100644 --- a/lib/test_dynamic_debug.c +++ b/lib/test_dynamic_debug.c @@ -137,7 +137,14 @@ DYNAMIC_DEBUG_CLASSMAP_DEFINE(classid_range_conflict, = 0, D2_CORE + 1, "D3_CORE") * DEFINEd (and exported) above. */ DYNAMIC_DEBUG_CLASSMAP_USE(map_disjoint_bits); -DYNAMIC_DEBUG_CLASSMAP_USE(map_level_num); +#if !defined(DD_RUNTIME_CLASS_CHECK) + DYNAMIC_DEBUG_CLASSMAP_USE(map_level_num); +#else +/* + * force failure of runtime sanity test of classmap.length + offset < 63 + */ +DYNAMIC_DEBUG_CLASSMAP_USE_(map_level_num, 55); +#endif =20 #if defined(DD_MACRO_ARGCHECK) /* --=20 2.53.0