From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) (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 9FCF73ACA71 for ; Sun, 10 May 2026 16:08:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.20 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778429320; cv=none; b=fr+qQtyaofTQKkkQCL3xJY+lMhGTbC4qQQSnEess6LFgzDiFFOyw62ywEd7n5QJU0BZOsUDBH5ExV/I2eWpRwn8DCzfmCbmaW9KF+8SB20Ml3PbdJD+1J8/88UGlQMeY5RlvR95b+vEr50DG2ql65JnnLkE/wVqfDGXl6iMnQsc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778429320; c=relaxed/simple; bh=onu89cyHWnkz3NrS06+KYzaHKBNH4Rq7nvFQqAeSGOg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hWqN9IMTsN1zuCYdbFtMY3ciTHBf8ws0fkPv5bpQAKRRfvN5pZUTzuBrqvFmTHPFD74kMpwD3N68CwNJJwGjdn+FvLvl5SgDMdaBPYtReqeeCMzl1kjuwT9ihfyTxoKpEgXBTB+kt0d10ggN3S/dKla97s4zKdFQ2nPE6n7Z1ko= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ff4nDV86; arc=none smtp.client-ip=198.175.65.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ff4nDV86" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1778429318; x=1809965318; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=onu89cyHWnkz3NrS06+KYzaHKBNH4Rq7nvFQqAeSGOg=; b=ff4nDV86FdvvutwYZQJW+VxkYeRn4arsIwbKn631q+BZ8Gh9mPoCNleS yaBd4HOs7WM7WAx03QSlr/A8G7HLt8gT4ziN43BD2hG8e/xpzLrPWU1SW yKXICvXvYPQfNUcHgxWvMQs6eHgaXD5tf9S46pRa3afNkwztna3gsAtOY v7ZkIealPNLRoN5tDziacxL+ANZbr6l3UDvSrC/JOzF5q4O2hJxhGHdJB TUDzNeyMXFCP++jitdf6hV5Ce9aJLy8B4xnpdzX6wY/F6hlDvf8A6xgtZ WQznDWjhzWcHovO1X7zDO+bUOBhANvjcW1uOxOyj5vvbVJK/rFbMxjMdp w==; X-CSE-ConnectionGUID: IcfATQKeTOu4y6n4fJkGjg== X-CSE-MsgGUID: 8ivNrqHrSoqBLNJHhgzvyA== X-IronPort-AV: E=McAfee;i="6800,10657,11782"; a="79056537" X-IronPort-AV: E=Sophos;i="6.23,227,1770624000"; d="scan'208";a="79056537" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 May 2026 09:08:38 -0700 X-CSE-ConnectionGUID: M8f5CxXsQDaoYdr/YXJY4g== X-CSE-MsgGUID: +OjxzGf5TRCbxWQXKdR0bQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,227,1770624000"; d="scan'208";a="230851406" Received: from chenyu-dev.sh.intel.com ([10.239.62.107]) by fmviesa009.fm.intel.com with ESMTP; 10 May 2026 09:08:36 -0700 From: Chen Yu To: kprateek.nayak@amd.com, tim.c.chen@linux.intel.com, peterz@infradead.org Cc: pan.deng@intel.com, mingo@kernel.org, linux-kernel@vger.kernel.org, tianyou.li@intel.com, Chen Yu Subject: [PATCH 2/3] lib/sbm: Use dynamically sized bitmap in sbm_leaf Date: Sun, 10 May 2026 23:59:18 +0800 Message-Id: <20260510155920.2587431-3-yu.c.chen@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260510155920.2587431-1-yu.c.chen@intel.com> References: <729726b9-c669-41e2-887d-bdf9da703034@amd.com> <20260510155920.2587431-1-yu.c.chen@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The original sbm_leaf uses a single unsigned long (u64) as its bitmap, which limits each leaf to representing at most 64 CPUs. When a LLC domain contains more than 64 logical CPUs, the within-leaf bit position (computed as apicid & arch_sbm_mask) can exceed 63. Since set_bit(nr, addr) treats addr as an arbitrarily long bitmap array, set_bit(65, &leaf->bitmap) would write to (&leaf->bitmap)[1], memory beyond the single unsigned long field. While ____cacheline_aligned padding may prevent corrupting adjacent leaves, the bits written into the padding are never read back by sbm_find_next_bit(), silently making those CPUs invisible. Fix this by converting the fixed u64 bitmap to a flexible array member (unsigned long bitmap[]) whose size is determined at allocation time from the number of CPUs that of the TILE domain(1 << arch_sbm_shift). A subsequent patch will switch to use the number CPUs shared LLC rather than TILE domain. --- include/linux/sbm.h | 5 +++-- lib/sbm.c | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/linux/sbm.h b/include/linux/sbm.h index a25a96366694..8d60f4bc7004 100644 --- a/include/linux/sbm.h +++ b/include/linux/sbm.h @@ -28,7 +28,8 @@ struct sbm_root { struct sbm_leaf { enum sbm_type type; - unsigned long bitmap; + unsigned int nbits; + unsigned long bitmap[]; } ____cacheline_aligned; struct sbm { @@ -48,7 +49,7 @@ extern int sbm_find_next_bit(struct sbm *sbm, int start); leaf = root->leafs[nr]; \ } \ int bit = idx & arch_sbm_mask; \ - func(bit, &leaf->bitmap); \ + func(bit, leaf->bitmap); \ }) static inline void sbm_cpu_set(struct sbm *sbm, int cpu) diff --git a/lib/sbm.c b/lib/sbm.c index 8006f9b04b62..76670ce14291 100644 --- a/lib/sbm.c +++ b/lib/sbm.c @@ -4,6 +4,8 @@ struct sbm *sbm_alloc(void) { unsigned int nr = arch_sbm_leafs; + unsigned int nbits = 1U << arch_sbm_shift; + unsigned int nlongs = BITS_TO_LONGS(nbits); struct sbm_root *root = kzalloc_flex(*root, leafs, nr); struct sbm_leaf *leaf; if (!root) @@ -12,10 +14,12 @@ struct sbm *sbm_alloc(void) root->type = st_root; for (int i = 0; i < nr; i++) { - leaf = kzalloc_obj(*leaf); + leaf = kzalloc(struct_size(leaf, bitmap, nlongs), + GFP_KERNEL); if (!leaf) goto fail; leaf->type = st_leaf; + leaf->nbits = nbits; root->leafs[i] = leaf; } @@ -40,18 +44,20 @@ int sbm_find_next_bit(struct sbm *sbm, int start) struct sbm_root *root = (void *)sbm; int nr = start >> arch_sbm_shift; int bit = start & arch_sbm_mask; - unsigned long tmp, mask = (~0UL) << bit; + unsigned int found; + if (sbm->type == st_root) { - for (; nr < arch_sbm_leafs; nr++, mask = ~0UL) { + do { leaf = root->leafs[nr]; - tmp = leaf->bitmap & mask; - if (tmp) - break; - } + found = find_next_bit(leaf->bitmap, leaf->nbits, bit); + if (found < leaf->nbits) + return (nr << arch_sbm_shift) | found; + bit = 0; + } while (++nr < arch_sbm_leafs); } else { - tmp = leaf->bitmap & mask; + found = find_next_bit(leaf->bitmap, leaf->nbits, bit); + if (found < leaf->nbits) + return found; } - if (!tmp) - return -1; - return (nr << arch_sbm_shift) | __ffs(tmp); + return -1; } -- 2.25.1