Skip to contentSkip to conversation

Allocating DMA buffer for non-PCM

DISCUSSION Adrian Knoth 3 participants 9 messages Feb 14, 2013 – Feb 15, 2013

lore
Share & tools
lore
#0 lore raw

Hi!

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

struct hdspm {
	struct pci_dev *pci;	/* and an pci info */
	struct snd_dma_buffer dmaLevelBuffer;
	u32 *level_buffer;	/* suitably aligned address */
    [..]
}
	/* allocate level buffer */
	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);
	if (err < 0) {
    /* error */ [..]
    }

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

	memset(hdspm->level_buffer, 0, MADIFX_LEVEL_BUFFER_SIZE);

This used to work on my development machine (kernel 3.6.x), but now a kernel 3.2.0 user reports a NULL pointer dereference of hdspm->level_buffer, so apparently, snd_sgbuf_get_ptr() returned NULL for him.

How could this possibly happen? Am I missing something? Better use SNDRV_DMA_TYPE_DEV instead?

TIA

#1 lore raw

At Thu, 14 Feb 2013 18:03:53 +0100,

Quoted 13 lines · Adrian Knoth

Hi!

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

struct hdspm {
	struct pci_dev *pci;	/* and an pci info */
	struct snd_dma_buffer dmaLevelBuffer;
	u32 *level_buffer;	/* suitably aligned address */
    [..]
}
Quoted 9 lines
	/* allocate level buffer */
	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);
	if (err < 0) {
    /* error */ [..]
    }

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

hdspm->level_buffer = (u32*)hdspm.dmaLevelBuffer.area;

Quoted 7 lines
	memset(hdspm->level_buffer, 0, MADIFX_LEVEL_BUFFER_SIZE);

This used to work on my development machine (kernel 3.6.x), but now a kernel 3.2.0 user reports a NULL pointer dereference of hdspm->level_buffer, so apparently, snd_sgbuf_get_ptr() returned NULL for him.

What's level_buffer? Is a SG-buffer for the audio stream?

Quoted 1 line

How could this possibly happen?

A wrong usage :)

Takashi

Quoted 11 lines

Am I missing something? Better use
SNDRV_DMA_TYPE_DEV instead?

TIA
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#2 lore raw
Quoted 11 lines · Takashi Iwai
Quoted 9 lines
	/* allocate level buffer */
	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);
	if (err < 0) {
    /* error */ [..]
    }

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

hdspm->level_buffer = (u32*)hdspm.dmaLevelBuffer.area;

Looks good, TNX.

Quoted 5 lines
Quoted 4 lines

This used to work on my development machine (kernel 3.6.x), but now a kernel 3.2.0 user reports a NULL pointer dereference of hdspm->level_buffer, so apparently, snd_sgbuf_get_ptr() returned NULL for him.

What's level_buffer?

The card does hardware metering and stores RMS/peak values in a DMA buffer. I want to later pass this to userspace to show signal levels, either via memcpy()ing the DMA buffer or maybe via mmap(). But since I no longer have access to such a card, work on this is on halt atm.

Quoted 1 line

Is a SG-buffer for the audio stream?

The audio buffers use

	     snd_pcm_lib_preallocate_pages_for_all(pcm,
						   SNDRV_DMA_TYPE_DEV_SG,
						   snd_dma_pci_data(hdspm->pci),

so they're SG, yes.

Quoted 2 lines
Quoted 1 line

How could this possibly happen?

A wrong usage :)

I thought so, that's why I was asking. ;)

Cheers and thanks again

#3 lore raw

At Thu, 14 Feb 2013 18:26:53 +0100,

Quoted 30 lines · Adrian Knoth
Quoted 11 lines · Takashi Iwai
Quoted 9 lines
	/* allocate level buffer */
	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);
	if (err < 0) {
    /* error */ [..]
    }

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

hdspm->level_buffer = (u32*)hdspm.dmaLevelBuffer.area;

Looks good, TNX.

Quoted 5 lines
Quoted 4 lines

This used to work on my development machine (kernel 3.6.x), but now a kernel 3.2.0 user reports a NULL pointer dereference of hdspm->level_buffer, so apparently, snd_sgbuf_get_ptr() returned NULL for him.

What's level_buffer?

The card does hardware metering and stores RMS/peak values in a DMA buffer. I want to later pass this to userspace to show signal levels, either via memcpy()ing the DMA buffer or maybe via mmap(). But since I no longer have access to such a card, work on this is on halt atm.

Quoted 1 line

Is a SG-buffer for the audio stream?

Quoted 8 lines

The audio buffers use

	     snd_pcm_lib_preallocate_pages_for_all(pcm,
						   SNDRV_DMA_TYPE_DEV_SG,
						   snd_dma_pci_data(hdspm->pci),

so they're SG, yes.

But isn't the level meter buffer a single page? If the buffer is only for peak meters, it can't be that big even for multi-channel cards. If so, SNDRV_DMA_TYPE_DEV_SG is utterly nonsense. Otherwise, if it's a SG buffer, you'll have to some code to set up TLB in anyway.

Takashi

#4 lore raw
In reply to Adrian Knoth
Quoted 8 lines · Adrian Knoth

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

I don't see the code asking for the address of the second page, so I guess there isn't one. But then you don't need SG in the first place.

Quoted 1 line

Better use SNDRV_DMA_TYPE_DEV instead?

Yes.

Regards,
Clemens

#5 lore raw
Quoted 10 lines · Clemens Ladisch
Quoted 8 lines

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

I don't see the code asking for the address of the second page, so I guess there isn't one.

Exactly.

Just to be sure: Takashi has recommended to use (u32*)dmaLevelBuffer.area. Even in the case of SG buffers, is this virtual address continuous and safe for memset?

Cheers

Signature · 1 line
mail: [email protected]  	http://adi.thur.de	PGP/GPG: key via keyserver
#6 lore raw
Quoted 8 lines · Adrian Knoth
Quoted 7 lines · Clemens Ladisch
Quoted 7 lines

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

I don't see the code asking for the address of the second page, so I guess there isn't one.

Exactly.

So MADIFX_LEVEL_BUFFER_SIZE is guaranteed to not exceed the page size on all architectures?

Quoted 3 lines

Just to be sure: Takashi has recommended to use (u32*)dmaLevelBuffer.area. Even in the case of SG buffers, is this virtual address continuous and safe for memset?

Yes, the SG buffer's pages are vmap()ed there.

Regards,
Clemens

#7 lore raw
Quoted 10 lines · Clemens Ladisch
Quoted 10 lines · Clemens Ladisch
Quoted 9 lines
Quoted 8 lines

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I don't see the code asking for the address of the second page, so I guess there isn't one.

Exactly.

So MADIFX_LEVEL_BUFFER_SIZE is guaranteed to not exceed the page size on all architectures?

Absolutely not, it's known to be multiples of the page size, but the purpose of hdspm->level_buffer is to hold the continuous virtual address, so apparently, I was using the wrong getter function in the first place (I was actually looking for .area).

The code for accessing the other pages was omitted for the sake of simplicity, but since you've asked, here it is:

/* Fill level page table */

	for (i = 0; i < MADIFX_NUM_LEVEL_PAGES; i++) {
		levelPageTable[i] = snd_sgbuf_get_addr(&(hdspm->dmaLevelBuffer),
				i * MADIFX_HARDWARE_PAGE_SIZE);

	}

	/* Write level page table to device */
	lpti = (MADIFX == hdspm->io_type) ? MADIFX_LPTI_HMFX :
		MADIFX_LPTI_MFXT;

	for (i = 0; i < MADIFX_NUM_LEVEL_PAGES; i++) {
		madifx_write(hdspm, MADIFX_PAGE_ADDRESS_LIST + (4 * (lpti + i)),
				levelPageTable[i]);
	}

Cheers

#8 lore raw

At Thu, 14 Feb 2013 19:46:39 +0100,

Quoted 18 lines · Adrian Knoth
Quoted 15 lines · Clemens Ladisch
Quoted 14 lines · Clemens Ladisch
Quoted 11 lines
Quoted 8 lines

Would you say that the following is the proper way to allocate a DMA buffer used to hold level data?

	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG,
			snd_dma_pci_data(hdspm->pci),
			MADIFX_LEVEL_BUFFER_SIZE, &hdspm->dmaLevelBuffer);

    hdspm->level_buffer = snd_sgbuf_get_ptr(&(hdspm->dmaLevelBuffer), 0);

I don't see the code asking for the address of the second page, so I guess there isn't one.

Exactly.

Quoted 7 lines
Quoted 2 lines

So MADIFX_LEVEL_BUFFER_SIZE is guaranteed to not exceed the page size on all architectures?

Absolutely not, it's known to be multiples of the page size, but the purpose of hdspm->level_buffer is to hold the continuous virtual address, so apparently, I was using the wrong getter function in the first place (I was actually looking for .area).

OK, then disregard my previous comment. The SG-buffer is the right thing.

FWIW, snd_sgbuf_get_ptr() return the pointer of the particular page. If you access only the data in the page size, it'll work even with that.

Takashi

Quoted 27 lines

The code for accessing the other pages was omitted for the sake of simplicity, but since you've asked, here it is:

   	/* Fill level page table */
	for (i = 0; i < MADIFX_NUM_LEVEL_PAGES; i++) {
		levelPageTable[i] = snd_sgbuf_get_addr(&(hdspm->dmaLevelBuffer),
				i * MADIFX_HARDWARE_PAGE_SIZE);

	}

	/* Write level page table to device */
	lpti = (MADIFX == hdspm->io_type) ? MADIFX_LPTI_HMFX :
		MADIFX_LPTI_MFXT;

	for (i = 0; i < MADIFX_NUM_LEVEL_PAGES; i++) {
		madifx_write(hdspm, MADIFX_PAGE_ADDRESS_LIST + (4 * (lpti + i)),
				levelPageTable[i]);
	}

Cheers
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel