Hot!alloc coherent issue

Author
friesen
Super Member
  • Total Posts : 2006
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: online
2018/01/13 11:27:37 (permalink)
0

alloc coherent issue

Just a note here, in doing some work with a similar function of mine, there is a bad potential here.
 
# define __PIC32_UNCACHED_PTR(v) __PIC32_KVA0_TO_KVA1_PTR(v)

static __inline__ void * __pic32_alloc_coherent (size_t size)
{
  void *retptr;
  retptr = malloc (size);
  if (retptr == NULL) {
    return NULL;
  }
  /* malloc returns a cached pointer, but convert it to an uncached pointer */
  return __PIC32_UNCACHED_PTR (retptr);
}

 
The reason this is bad is, suppose this happens.
 
void CacheProblem(void){
        int a;
    char * ptr;
    ptr = malloc(128);
    memset(ptr,0xAA,128);
    free(ptr);
    ptr = __pic32_alloc_coherent(128);
    memset(ptr,0xBB,128);
    //Do some thing big here that forces the cache to flush/write down
    char * cacheflushptr = malloc(32767);
    for(i=0;i<32767;i++){
        printc(cacheflushptr[i]);
    }
    //Whoops, suddenly ptr == 0xAA again?
}

 
 
post edited by friesen - 2018/01/13 11:38:35

Erik Friesen
#1

10 Replies Related Threads

    friesen
    Super Member
    • Total Posts : 2006
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: online
    Re: alloc coherent issue 2018/01/13 11:30:10 (permalink)
    0
    Sorry about the space name mangling, one of those words isn't appreciated by the 403 overlords.
     
    Edit:  p u t c h a r  is not allowed folks.
    post edited by friesen - 2018/01/13 11:38:56

    Erik Friesen
    #2
    andersm
    Super Member
    • Total Posts : 2485
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: alloc coherent issue 2018/01/13 12:36:35 (permalink)
    0
    Sharing a cacheline between cached and uncached data is obviously a bad idea, so the easy answer is not to do that.
    #3
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: alloc coherent issue 2018/01/13 14:44:02 (permalink)
    0
    friesen
    ...
    Edit:  p u t c h a r  is not allowed folks.

    Just char followed by "(" is enough to trigger it.
    http://www.microchip.com/forums/m816728.aspx
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #4
    friesen
    Super Member
    • Total Posts : 2006
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: online
    Re: alloc coherent issue 2018/01/13 16:26:41 (permalink)
    0
    I'm just saying, the code in the first box is direct from Microchip.

    Erik Friesen
    #5
    NorthGuy
    Super Member
    • Total Posts : 5101
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: alloc coherent issue 2018/01/14 07:08:15 (permalink)
    0
    It is just a bug. The memory allocator should invalidate or write back the cache after you allocate coherent.
    #6
    andersm
    Super Member
    • Total Posts : 2485
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: alloc coherent issue 2018/01/14 08:38:21 (permalink)
    0
    NorthGuyIt is just a bug. The memory allocator should invalidate or write back the cache after you allocate coherent.

    That would do nothing to solve the root issue of allocating both cached and uncached storage from the same cache line. You should either use a custom allocator, or allocate large enough chunks that you can manage the alignment and padding requirements manually. For peripheral buffers, static allocation usually works well.
    #7
    friesen
    Super Member
    • Total Posts : 2006
    • Reward points : 0
    • Joined: 2008/05/08 05:23:35
    • Location: Indiana, USA
    • Status: online
    Re: alloc coherent issue 2018/01/14 10:50:37 (permalink)
    0
    I have built a custom allocator, thus I ran into this. What I have done is allocate extra and make sure the used portion is in uncached space on 16 byte alignment, then write back and invalidate before use. The malloc code should only work in cache space, unless I am still missing something, as no cache work will be done to malloc overhead bytes, at least per the way umm_malloc works.

    Erik Friesen
    #8
    NorthGuy
    Super Member
    • Total Posts : 5101
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: alloc coherent issue 2018/01/14 12:08:32 (permalink)
    0
    andersm
    That would do nothing to solve the root issue of allocating both cached and uncached storage from the same cache line.



    The root issue is the coherent allocation getting the same memory from malloc() which has been cached during its previous allocation.
     
    malloc() should allocate in some chunks which are relatively large. Small chunks promote memory fragmentation. Thus I would assume cache lines don't get split between allocations. If not, malloc() can be easily fixed/configured.
     
    #9
    andersm
    Super Member
    • Total Posts : 2485
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: alloc coherent issue 2018/01/14 14:56:24 (permalink)
    0
    NorthGuyThe root issue is the coherent allocation getting the same memory from malloc() which has been cached during its previous allocation.

    That's not a problem, since you can invalidate the cache lines before using the memory. It is a problem if the same 16-byte aligned span of memory is used both bypassing and via the cache. The specification for malloc only guarantees that the returned block of memory is suitable for any scalar type, and eg. the XC32 library documentation do not provide any stronger guarantees. So you can either use a custom allocator, or increase the size of your allocations so that you can make the alignment adjustment yourself.
    #10
    moser
    Super Member
    • Total Posts : 406
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: alloc coherent issue 2018/01/15 03:10:03 (permalink)
    0
    The MPLAB® XC32 C/C++ Compiler User’s Guide says:
    The __pic32_alloc_coherent(size_t) and __pic32_free_coherent(void*) functions allocate and free memory from the uncached kseg1_data_mem region. The default stack is allocated to the cached kseg0_data_mem region, but you may want to create an uncached DMA buffer, so you can use these functions to allocate an uncached buffer. These functions call the standard malloc()/free() functions, but the pointers that they use are translated from kseg0 to kseg1.
    #include<xc.h>
     
    void jak(void){
     
       char* buffer = __pic32_alloc_coherent(1024);
       if (buffer){
         /* do somehing */
       }
       else{
         /* handle error */
       }
       if (buffer){
         __pic32_free_coherent(buffer);
     
       }
     
    }

    The example given by friesen isn't really different from the usage example. Therefore, I get the feeling that either the implementation of __pic32_alloc_coherent(size_t) or the usage example is wrong. The usage example from the guide would only work in some conditions where no free() has been used before, or by coincidence if you are lucky.
     
    PS: Or at least the example fails to point out, that you need to invalidate the corresponding cache line before using the buffer.
    post edited by moser - 2018/01/15 03:34:02
    #11
    Jump to:
    © 2018 APG vNext Commercial Version 4.5