Hi, On 28.08.2018 11:53, Jiri Olsa wrote: > On Mon, Aug 27, 2018 at 09:16:55PM +0300, Alexey Budankov wrote: > > SNIP > >> if ((md->start & md->mask) + size != (md->end & md->mask)) { >> buf = &data[md->start & md->mask]; >> - size = md->mask + 1 - (md->start & md->mask); >> - md->start += size; >> - >> - if (push(to, buf, size) < 0) { >> - rc = -1; >> - goto out; >> - } >> + size0 = md->mask + 1 - (md->start & md->mask); >> + md->start += size0; >> + memcpy(md->data, buf, size0); >> } >> >> buf = &data[md->start & md->mask]; >> size = md->end - md->start; >> md->start += size; >> + memcpy(md->data + size0, buf, size); >> >> - if (push(to, buf, size) < 0) { >> - rc = -1; >> + rc = push(to, md, size0 + size, *off) < 0 ? -1 : 1; >> + if (rc == -1) >> goto out; >> - } >> + >> + perf_mmap__get(md); >> + *off += size0 + size; > > this get is for the perf_mmap pointer storage in the mmap_aio array right?
Right. perf_mmap__get() here guards whole perf_mmap object and memory referenced thru it from premature deallocation because mmap->base kernel buffer can be released earlier than aio requests started on mmap->data complete and the both buffers are referenced thru the same perf_mmap object. > > I see it's released in record__mmap_read_sync, which might also return > without releasing it.. this needs to be fixed and explained in here, > why we take the reference in the first place So we increment after successful push() from map->base to map->data with following aio_write() and decrement when aio_write() is fully complete, because it may require restart if the kernel didn't write whole chunk at once. Probably we are still missing one more perf_mmap__put() after: pr_err("failed to write perf data, error: %m\n"); prior nullifying the appropriate cblock. Updated [PATCH v4 2/2]: - Written comment in perf_mmap__push() just before perf_mmap__get(); - Written comment in record__mmap_read_sync() on possible restarting of aio_write() operation and releasing perf_mmap object after all; - added perf_mmap__put() for the cases of failed aio_write(); Thanks! > > thanks, > jirka >