• Ian Rogers's avatar
    perf cpumap: Fix alignment for masks in event encoding · b2f10cd4
    Ian Rogers authored
    A mask encoding of a cpu map is laid out as:
    
      u16 nr
      u16 long_size
      unsigned long mask[];
    
    However, the mask may be 8-byte aligned meaning there is a 4-byte pad
    after long_size. This means 32-bit and 64-bit builds see the mask as
    being at different offsets. On top of this the structure is in the byte
    data[] encoded as:
    
      u16 type
      char data[]
    
    This means the mask's struct isn't the required 4 or 8 byte aligned, but
    is offset by 2. Consequently the long reads and writes are causing
    undefined behavior as the alignment is broken.
    
    Fix the mask struct by creating explicit 32 and 64-bit variants, use a
    union to avoid data[] and casts; the struct must be packed so the
    layout matches the existing perf.data layout. Taking an address of a
    member of a packed struct breaks alignment so pass the packed
    perf_record_cpu_map_data to functions, so they can access variables with
    the right alignment.
    
    As the 64-bit version has 4 bytes of padding, optimizing writing to only
    write the 32-bit version.
    
    Committer notes:
    
    Disable warnings about 'packed' that break the build in some arches like
    riscv64, but just around that specific struct.
    Signed-off-by: default avatarIan Rogers <irogers@google.com>
    Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
    Cc: Adrian Hunter <adrian.hunter@intel.com>
    Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
    Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
    Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
    Cc: Colin Ian King <colin.king@intel.com>
    Cc: Dave Marchevsky <davemarchevsky@fb.com>
    Cc: German Gomez <german.gomez@arm.com>
    Cc: Gustavo A. R. Silva <gustavoars@kernel.org>
    Cc: Ingo Molnar <mingo@redhat.com>
    Cc: James Clark <james.clark@arm.com>
    Cc: Kees Kook <keescook@chromium.org>
    Cc: Leo Yan <leo.yan@linaro.org>
    Cc: Mark Rutland <mark.rutland@arm.com>
    Cc: Namhyung Kim <namhyung@kernel.org>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Riccardo Mancini <rickyman7@gmail.com>
    Cc: Song Liu <songliubraving@fb.com>
    Cc: Stephane Eranian <eranian@google.com>
    Link: https://lore.kernel.org/r/20220614143353.1559597-5-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
    b2f10cd4
cpumap.c 14.1 KB