Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Feb 2014 19:40:15 +0000
From:      "Gumpula, Suresh" <Suresh.Gumpula@netapp.com>
To:        Ian Lepore <ian@FreeBSD.org>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@FreeBSD.org>
Subject:   RE: malloc(9)  and its alignment
Message-ID:  <D29CB80EBA4DEA4D91181928AAF51538438EF8DC@SACEXCMBX04-PRD.hq.netapp.com>
In-Reply-To: <1392214788.1145.52.camel@revolution.hippie.lan>
References:  <D29CB80EBA4DEA4D91181928AAF51538438EED0A@SACEXCMBX04-PRD.hq.netapp.com> <1392214788.1145.52.camel@revolution.hippie.lan>

next in thread | previous in thread | raw e-mail | index | archive | help
Thanks Ian for the reply.   I will look at the ARM code, but I was  thinkin=
g  why malloc(9) does not return bucket size aligned pointers.=20


Regards,
Suresh

-----Original Message-----
From: Ian Lepore [mailto:ian@FreeBSD.org]=20
Sent: Wednesday, February 12, 2014 9:20 AM
To: Gumpula, Suresh
Cc: freebsd-hackers@freebsd.org
Subject: Re: malloc(9) and its alignment

On Wed, 2014-02-12 at 02:02 +0000, Gumpula, Suresh wrote:
> Hi,
> It appears  the malloc(9) returns 8 byte aligned ( UMA_ALIGN_PTR)=20
> pointers, but in bus_dmamem_alloc  we might end up checking for greater a=
lignment if we take malloc(9) path instead contig_malloc.
> Can someone please confirm if malloc(9) returns different alignment point=
ers ?
>=20
> bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
>                  bus_dmamap_t *mapp)
> {
>         /*
>          * XXX:
>          * (dmat->alignment < dmat->maxsize) is just a quick hack; the ex=
act
>          * alignment guarantees of malloc need to be nailed down, and the
>          * code below should be rewritten to take that into account.
>          *
>          * In the meantime, we'll warn the user if malloc gets it wrong.
>          */
>         if ((dmat->maxsize <=3D PAGE_SIZE) &&
>            (dmat->alignment < dmat->maxsize) &&
>             dmat->lowaddr >=3D ptoa((vm_paddr_t)Maxmem)) {
>                 *vaddr =3D malloc(dmat->maxsize, M_DEVBUF, mflags);
>         } else {
>=20
>                 *vaddr =3D contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
>                     0ul, dmat->lowaddr, dmat->alignment? dmat->alignment =
: 1ul,
>                     dmat->boundary);
>        }=20
>         if (vtophys(*vaddr) & (dmat->alignment - 1)) {
>                 NETAPP_MUTED_PRINTF("bus_dmamem_alloc failed to align=20
> memory properly.\n");
>=20
> Regards,
> Suresh

In my experience, the effective malloc(9) alignment ends up being the same =
as MINALLOCSIZE, which is UMA_SMALLEST_UNIT, which is 16 bytes on a system =
with 4K pages.  At $work we overrode MINALLOCSIZE to 32 to work around cach=
e line alignment problems in busdma for ARM systems a few years ago.

There is a newer set of busdma allocator routines available in kern/subr_bu=
sdma_bufalloc.c which are designed to give a platform more control over ali=
gnment of busdma buffers smaller than a page.  An example of using them can=
 be found in arm/busdma_machdep[-v6].c.  As far as I know, they're only bei=
ng used on ARM platforms right now.

-- Ian





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?D29CB80EBA4DEA4D91181928AAF51538438EF8DC>