From nobody Fri Dec 19 19:54:14 2025 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (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 1F3E417A2FA for ; Mon, 31 Mar 2025 12:31:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743424285; cv=none; b=jhCOhWA2jhAlaVgcz2wJmjm68C/ZIbySIp1TwQPxyi55EItYZOeA72v6V3wiSrsPVpeAKldI3CrrJdmAKop8Emx0B1wmfnY28npSKjS/rkGDlKu0umf5j8pDp45wrEz54F0SxSmEyYJLfBIiwlS8tfFISrbLU2llpeg1xclttZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743424285; c=relaxed/simple; bh=xqAtN/TbCCA7DKGAfkHpOId1GLziI/YG+jt+tgsCs0E=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=grK2b9PDE8h/t0Ft9qPtHhogTv7MzxvCcm3KVQrpDPqK6jWtMGOxh51J/RRjeqtnl8+WX+eARaT9jyjMn/rTudDLASB5JWWjl2KvBmFcMiqmwOS7tzbaSg7iG1lwmb5ClaGOpR+AZG4d46+XC1WidDmPJRawvVnC6dGG1Cicvaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Oa0MvpIp; arc=none smtp.client-ip=95.215.58.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Oa0MvpIp" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1743424281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=7XEWEmd5LWdFQWEvEb923ykZHXXlz1bJMpcel5KSrlk=; b=Oa0MvpIpXxMjZP67+MGyMm07znRrgBk/7woerEUR91JCGfKX0ao4riXEcBgUZKdL4eJmlK HLRw0sZrKd4OLPCwGa5kEdoVSsLOWBZyX9gFj4LpumWBEOOCEEHaW662OW8IBnZguoYSpj xXGFfPlRfH9Vpg5iTewB44kqgOqpEFE= From: Thorsten Blum To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: Thorsten Blum , Ingo Molnar , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] perf/x86: Rename bts_buffer variables and use struct_size() Date: Mon, 31 Mar 2025 14:29:38 +0200 Message-ID: <20250331122938.1837255-2-thorsten.blum@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Rename struct bts_buffer objects from 'buf' to 'bb' to improve the readability when accessing the structure's 'buf' member. For example, 'buf->buf[]' becomes 'bb->buf[]'. Use struct_size() to calculate the number of bytes to allocate for a new bts_buffer. Compared to offsetof(), struct_size() provides additional compile-time checks (e.g., __must_be_array()). Indent line 327 using tabs to silence a checkpatch warning. No functional changes intended. Suggested-by: Ingo Molnar Signed-off-by: Thorsten Blum --- arch/x86/events/intel/bts.c | 146 ++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 74 deletions(-) diff --git a/arch/x86/events/intel/bts.c b/arch/x86/events/intel/bts.c index a95e6c91c4d7..bbc7d914050c 100644 --- a/arch/x86/events/intel/bts.c +++ b/arch/x86/events/intel/bts.c @@ -80,54 +78,54 @@ static void * bts_buffer_setup_aux(struct perf_event *event, void **pages, int nr_pages, bool overwrite) { - struct bts_buffer *buf; + struct bts_buffer *bb; struct page *page; int cpu =3D event->cpu; int node =3D (cpu =3D=3D -1) ? cpu : cpu_to_node(cpu); unsigned long offset; size_t size =3D nr_pages << PAGE_SHIFT; - int pg, nbuf, pad; + int pg, nr_buf, pad; =20 /* count all the high order buffers */ - for (pg =3D 0, nbuf =3D 0; pg < nr_pages;) { + for (pg =3D 0, nr_buf =3D 0; pg < nr_pages;) { page =3D virt_to_page(pages[pg]); pg +=3D buf_nr_pages(page); - nbuf++; + nr_buf++; } =20 /* * to avoid interrupts in overwrite mode, only allow one physical */ - if (overwrite && nbuf > 1) + if (overwrite && nr_buf > 1) return NULL; =20 - buf =3D kzalloc_node(offsetof(struct bts_buffer, buf[nbuf]), GFP_KERNEL, = node); - if (!buf) + bb =3D kzalloc_node(struct_size(bb, buf, nr_buf), GFP_KERNEL, node); + if (!bb) return NULL; =20 - buf->nr_pages =3D nr_pages; - buf->nr_bufs =3D nbuf; - buf->snapshot =3D overwrite; - buf->data_pages =3D pages; - buf->real_size =3D size - size % BTS_RECORD_SIZE; + bb->nr_pages =3D nr_pages; + bb->nr_bufs =3D nr_buf; + bb->snapshot =3D overwrite; + bb->data_pages =3D pages; + bb->real_size =3D size - size % BTS_RECORD_SIZE; =20 - for (pg =3D 0, nbuf =3D 0, offset =3D 0, pad =3D 0; nbuf < buf->nr_bufs; = nbuf++) { + for (pg =3D 0, nr_buf =3D 0, offset =3D 0, pad =3D 0; nr_buf < bb->nr_buf= s; nr_buf++) { unsigned int __nr_pages; =20 page =3D virt_to_page(pages[pg]); __nr_pages =3D buf_nr_pages(page); - buf->buf[nbuf].page =3D page; - buf->buf[nbuf].offset =3D offset; - buf->buf[nbuf].displacement =3D (pad ? BTS_RECORD_SIZE - pad : 0); - buf->buf[nbuf].size =3D buf_size(page) - buf->buf[nbuf].displacement; - pad =3D buf->buf[nbuf].size % BTS_RECORD_SIZE; - buf->buf[nbuf].size -=3D pad; + bb->buf[nr_buf].page =3D page; + bb->buf[nr_buf].offset =3D offset; + bb->buf[nr_buf].displacement =3D (pad ? BTS_RECORD_SIZE - pad : 0); + bb->buf[nr_buf].size =3D buf_size(page) - bb->buf[nr_buf].displacement; + pad =3D bb->buf[nr_buf].size % BTS_RECORD_SIZE; + bb->buf[nr_buf].size -=3D pad; =20 pg +=3D __nr_pages; offset +=3D __nr_pages << PAGE_SHIFT; } =20 - return buf; + return bb; } =20 static void bts_buffer_free_aux(void *data) @@ -135,25 +133,25 @@ static void bts_buffer_free_aux(void *data) kfree(data); } =20 -static unsigned long bts_buffer_offset(struct bts_buffer *buf, unsigned in= t idx) +static unsigned long bts_buffer_offset(struct bts_buffer *bb, unsigned int= idx) { - return buf->buf[idx].offset + buf->buf[idx].displacement; + return bb->buf[idx].offset + bb->buf[idx].displacement; } =20 static void -bts_config_buffer(struct bts_buffer *buf) +bts_config_buffer(struct bts_buffer *bb) { int cpu =3D raw_smp_processor_id(); struct debug_store *ds =3D per_cpu(cpu_hw_events, cpu).ds; - struct bts_phys *phys =3D &buf->buf[buf->cur_buf]; + struct bts_phys *phys =3D &bb->buf[bb->cur_buf]; unsigned long index, thresh =3D 0, end =3D phys->size; struct page *page =3D phys->page; =20 - index =3D local_read(&buf->head); + index =3D local_read(&bb->head); =20 - if (!buf->snapshot) { - if (buf->end < phys->offset + buf_size(page)) - end =3D buf->end - phys->offset - phys->displacement; + if (!bb->snapshot) { + if (bb->end < phys->offset + buf_size(page)) + end =3D bb->end - phys->offset - phys->displacement; =20 index -=3D phys->offset + phys->displacement; =20 @@ -168,7 +166,7 @@ bts_config_buffer(struct bts_buffer *buf) ds->bts_buffer_base =3D (u64)(long)page_address(page) + phys->displacemen= t; ds->bts_index =3D ds->bts_buffer_base + index; ds->bts_absolute_maximum =3D ds->bts_buffer_base + end; - ds->bts_interrupt_threshold =3D !buf->snapshot + ds->bts_interrupt_threshold =3D !bb->snapshot ? ds->bts_buffer_base + thresh : ds->bts_absolute_maximum + BTS_RECORD_SIZE; } @@ -184,16 +182,16 @@ static void bts_update(struct bts_ctx *bts) { int cpu =3D raw_smp_processor_id(); struct debug_store *ds =3D per_cpu(cpu_hw_events, cpu).ds; - struct bts_buffer *buf =3D perf_get_aux(&bts->handle); + struct bts_buffer *bb =3D perf_get_aux(&bts->handle); unsigned long index =3D ds->bts_index - ds->bts_buffer_base, old, head; =20 - if (!buf) + if (!bb) return; =20 - head =3D index + bts_buffer_offset(buf, buf->cur_buf); - old =3D local_xchg(&buf->head, head); + head =3D index + bts_buffer_offset(bb, bb->cur_buf); + old =3D local_xchg(&bb->head, head); =20 - if (!buf->snapshot) { + if (!bb->snapshot) { if (old =3D=3D head) return; =20 @@ -205,9 +203,9 @@ static void bts_update(struct bts_ctx *bts) * old and head are always in the same physical buffer, so we * can subtract them to get the data size. */ - local_add(head - old, &buf->data_size); + local_add(head - old, &bb->data_size); } else { - local_set(&buf->data_size, head); + local_set(&bb->data_size, head); } =20 /* @@ -218,7 +216,7 @@ static void bts_update(struct bts_ctx *bts) } =20 static int -bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle= ); +bts_buffer_reset(struct bts_buffer *bb, struct perf_output_handle *handle); =20 /* * Ordering PMU callbacks wrt themselves and the PMI is done by means @@ -232,17 +230,17 @@ bts_buffer_reset(struct bts_buffer *buf, struct perf_= output_handle *handle); static void __bts_event_start(struct perf_event *event) { struct bts_ctx *bts =3D this_cpu_ptr(bts_ctx); - struct bts_buffer *buf =3D perf_get_aux(&bts->handle); + struct bts_buffer *bb =3D perf_get_aux(&bts->handle); u64 config =3D 0; =20 - if (!buf->snapshot) + if (!bb->snapshot) config |=3D ARCH_PERFMON_EVENTSEL_INT; if (!event->attr.exclude_kernel) config |=3D ARCH_PERFMON_EVENTSEL_OS; if (!event->attr.exclude_user) config |=3D ARCH_PERFMON_EVENTSEL_USR; =20 - bts_config_buffer(buf); + bts_config_buffer(bb); =20 /* * local barrier to make sure that ds configuration made it @@ -261,13 +259,13 @@ static void bts_event_start(struct perf_event *event,= int flags) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct bts_ctx *bts =3D this_cpu_ptr(bts_ctx); - struct bts_buffer *buf; + struct bts_buffer *bb; =20 - buf =3D perf_aux_output_begin(&bts->handle, event); - if (!buf) + bb =3D perf_aux_output_begin(&bts->handle, event); + if (!bb) goto fail_stop; =20 - if (bts_buffer_reset(buf, &bts->handle)) + if (bts_buffer_reset(bb, &bts->handle)) goto fail_end_stop; =20 bts->ds_back.bts_buffer_base =3D cpuc->ds->bts_buffer_base; @@ -306,27 +304,27 @@ static void bts_event_stop(struct perf_event *event, = int flags) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct bts_ctx *bts =3D this_cpu_ptr(bts_ctx); - struct bts_buffer *buf =3D NULL; + struct bts_buffer *bb =3D NULL; int state =3D READ_ONCE(bts->state); =20 if (state =3D=3D BTS_STATE_ACTIVE) __bts_event_stop(event, BTS_STATE_STOPPED); =20 if (state !=3D BTS_STATE_STOPPED) - buf =3D perf_get_aux(&bts->handle); + bb =3D perf_get_aux(&bts->handle); =20 event->hw.state |=3D PERF_HES_STOPPED; =20 if (flags & PERF_EF_UPDATE) { bts_update(bts); =20 - if (buf) { - if (buf->snapshot) + if (bb) { + if (bb->snapshot) bts->handle.head =3D - local_xchg(&buf->data_size, - buf->nr_pages << PAGE_SHIFT); + local_xchg(&bb->data_size, + bb->nr_pages << PAGE_SHIFT); perf_aux_output_end(&bts->handle, - local_xchg(&buf->data_size, 0)); + local_xchg(&bb->data_size, 0)); } =20 cpuc->ds->bts_index =3D bts->ds_back.bts_buffer_base; @@ -382,19 +380,19 @@ void intel_bts_disable_local(void) } =20 static int -bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle) +bts_buffer_reset(struct bts_buffer *bb, struct perf_output_handle *handle) { unsigned long head, space, next_space, pad, gap, skip, wakeup; unsigned int next_buf; struct bts_phys *phys, *next_phys; int ret; =20 - if (buf->snapshot) + if (bb->snapshot) return 0; =20 - head =3D handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1); + head =3D handle->head & ((bb->nr_pages << PAGE_SHIFT) - 1); =20 - phys =3D &buf->buf[buf->cur_buf]; + phys =3D &bb->buf[bb->cur_buf]; space =3D phys->offset + phys->displacement + phys->size - head; pad =3D space; if (space > handle->size) { @@ -403,10 +401,10 @@ bts_buffer_reset(struct bts_buffer *buf, struct perf_= output_handle *handle) } if (space <=3D BTS_SAFETY_MARGIN) { /* See if next phys buffer has more space */ - next_buf =3D buf->cur_buf + 1; - if (next_buf >=3D buf->nr_bufs) + next_buf =3D bb->cur_buf + 1; + if (next_buf >=3D bb->nr_bufs) next_buf =3D 0; - next_phys =3D &buf->buf[next_buf]; + next_phys =3D &bb->buf[next_buf]; gap =3D buf_size(phys->page) - phys->displacement - phys->size + next_phys->displacement; skip =3D pad + gap; @@ -431,8 +429,8 @@ bts_buffer_reset(struct bts_buffer *buf, struct perf_ou= tput_handle *handle) * anymore, so we must not be racing with * bts_update(). */ - buf->cur_buf =3D next_buf; - local_set(&buf->head, head); + bb->cur_buf =3D next_buf; + local_set(&bb->head, head); } } } @@ -445,7 +443,7 @@ bts_buffer_reset(struct bts_buffer *buf, struct perf_ou= tput_handle *handle) space -=3D space % BTS_RECORD_SIZE; } =20 - buf->end =3D head + space; + bb->end =3D head + space; =20 /* * If we have no space, the lost notification would have been sent when @@ -462,7 +460,7 @@ int intel_bts_interrupt(void) struct debug_store *ds =3D this_cpu_ptr(&cpu_hw_events)->ds; struct bts_ctx *bts; struct perf_event *event; - struct bts_buffer *buf; + struct bts_buffer *bb; s64 old_head; int err =3D -ENOSPC, handled =3D 0; =20 @@ -485,8 +483,8 @@ int intel_bts_interrupt(void) if (READ_ONCE(bts->state) =3D=3D BTS_STATE_STOPPED) return handled; =20 - buf =3D perf_get_aux(&bts->handle); - if (!buf) + bb =3D perf_get_aux(&bts->handle); + if (!bb) return handled; =20 /* @@ -494,26 +492,26 @@ int intel_bts_interrupt(void) * there's no other way of telling, because the pointer will * keep moving */ - if (buf->snapshot) + if (bb->snapshot) return 0; =20 - old_head =3D local_read(&buf->head); + old_head =3D local_read(&bb->head); bts_update(bts); =20 /* no new data */ - if (old_head =3D=3D local_read(&buf->head)) + if (old_head =3D=3D local_read(&bb->head)) return handled; =20 - perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0)); + perf_aux_output_end(&bts->handle, local_xchg(&bb->data_size, 0)); =20 - buf =3D perf_aux_output_begin(&bts->handle, event); - if (buf) - err =3D bts_buffer_reset(buf, &bts->handle); + bb =3D perf_aux_output_begin(&bts->handle, event); + if (bb) + err =3D bts_buffer_reset(bb, &bts->handle); =20 if (err) { WRITE_ONCE(bts->state, BTS_STATE_STOPPED); =20 - if (buf) { + if (bb) { /* * BTS_STATE_STOPPED should be visible before * cleared handle::event