cpuhw = this_cpu_ptr(&cpu_hw_events);
                        power_pmu_bhrb_read(event, cpuhw);
                        data.br_stack = &cpuhw->bhrb_stack;
+                       data.sample_flags |= PERF_SAMPLE_BRANCH_STACK;
                }
 
                if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
 
                if (!x86_perf_event_set_period(event))
                        continue;
 
-               if (has_branch_stack(event))
+               if (has_branch_stack(event)) {
                        data.br_stack = &cpuc->lbr_stack;
+                       data.sample_flags |= PERF_SAMPLE_BRANCH_STACK;
+               }
 
                if (perf_event_overflow(event, &data, regs))
                        x86_pmu_stop(event, 0);
 
 
                perf_sample_data_init(&data, 0, event->hw.last_period);
 
-               if (has_branch_stack(event))
+               if (has_branch_stack(event)) {
                        data.br_stack = &cpuc->lbr_stack;
+                       data.sample_flags |= PERF_SAMPLE_BRANCH_STACK;
+               }
 
                if (perf_event_overflow(event, &data, regs))
                        x86_pmu_stop(event, 0);
 
 
                perf_sample_data_init(&data, 0, event->hw.last_period);
 
-               if (has_branch_stack(event))
+               if (has_branch_stack(event)) {
                        data.br_stack = &cpuc->lbr_stack;
+                       data.sample_flags |= PERF_SAMPLE_BRANCH_STACK;
+               }
 
                if (perf_event_overflow(event, &data, regs))
                        x86_pmu_stop(event, 0);
 
                data->sample_flags |= PERF_SAMPLE_TIME;
        }
 
-       if (has_branch_stack(event))
+       if (has_branch_stack(event)) {
                data->br_stack = &cpuc->lbr_stack;
+               data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
+       }
 }
 
 static void adaptive_pebs_save_regs(struct pt_regs *regs,
                if (has_branch_stack(event)) {
                        intel_pmu_store_pebs_lbrs(lbr);
                        data->br_stack = &cpuc->lbr_stack;
+                       data->sample_flags |= PERF_SAMPLE_BRANCH_STACK;
                }
        }
 
 
        u64                             sample_flags;
        u64                             addr;
        struct perf_raw_record          *raw;
-       struct perf_branch_stack        *br_stack;
        u64                             period;
        union perf_sample_weight        weight;
        u64                             txn;
         * The other fields, optionally {set,used} by
         * perf_{prepare,output}_sample().
         */
+       struct perf_branch_stack        *br_stack;
+
        u64                             type;
        u64                             ip;
        struct {
        data->sample_flags = 0;
        data->addr = addr;
        data->raw  = NULL;
-       data->br_stack = NULL;
        data->period = period;
        data->weight.full = 0;
        data->data_src.val = PERF_MEM_NA;
 
        }
 
        if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
-               if (data->br_stack) {
+               if (data->sample_flags & PERF_SAMPLE_BRANCH_STACK) {
                        size_t size;
 
                        size = data->br_stack->nr
 
        if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
                int size = sizeof(u64); /* nr */
-               if (data->br_stack) {
+               if (data->sample_flags & PERF_SAMPLE_BRANCH_STACK) {
                        if (perf_sample_save_hw_index(event))
                                size += sizeof(u64);