public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* sound/pci/ctxfi/ctdaio.c: duplicate function removal question
@ 2024-12-24 23:16 Ethan Carter Edwards
  2024-12-29  8:32 ` Takashi Iwai
  2024-12-29  9:02 ` David Laight
  0 siblings, 2 replies; 8+ messages in thread
From: Ethan Carter Edwards @ 2024-12-24 23:16 UTC (permalink / raw)
  To: wychay@ctl.creative.com, ryan_richards@creativelabs.com,
	tiwai@suse.de
  Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org

Hello all,

First of all, happy holidays. 

I was browsing the ctdaio.c code and I noticed a lot of 
duplicate code and functions, specifically:

dao_set_{right,left}_input and
dao_clear_{right,left}_input functions.

The functions are pretty much identical. They only 
differ in the side (left, right). What was the original
idea in doing this? Wouldn't it make more since to just
have an ENUM (left, right) as an argument that would 
determine the side and just reduce the function to 
dao_set_input and dao_clear_input. 

I would be more than happy to send in a patch doing
these changes, but before I did I wanted to ask if
there was a reason the code was written in this way.
I am pretty new to kernel development.

Thanks,
Ethan
--
Ethan Carter Edwards
CompTIA A+, Security+, and ISC2 (CC)
Ham Radio: AE4CE
Website: https://ethancedwards.com


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-24 23:16 sound/pci/ctxfi/ctdaio.c: duplicate function removal question Ethan Carter Edwards
@ 2024-12-29  8:32 ` Takashi Iwai
  2024-12-29  9:02 ` David Laight
  1 sibling, 0 replies; 8+ messages in thread
From: Takashi Iwai @ 2024-12-29  8:32 UTC (permalink / raw)
  To: Ethan Carter Edwards
  Cc: wychay@ctl.creative.com, ryan_richards@creativelabs.com,
	tiwai@suse.de, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Wed, 25 Dec 2024 00:16:17 +0100,
Ethan Carter Edwards wrote:
> 
> Hello all,
> 
> First of all, happy holidays. 
> 
> I was browsing the ctdaio.c code and I noticed a lot of 
> duplicate code and functions, specifically:
> 
> dao_set_{right,left}_input and
> dao_clear_{right,left}_input functions.
> 
> The functions are pretty much identical. They only 
> differ in the side (left, right). What was the original
> idea in doing this? Wouldn't it make more since to just
> have an ENUM (left, right) as an argument that would 
> determine the side and just reduce the function to 
> dao_set_input and dao_clear_input. 
> 
> I would be more than happy to send in a patch doing
> these changes, but before I did I wanted to ask if
> there was a reason the code was written in this way.
> I am pretty new to kernel development.

Yes, the code simplification would be appreciated.
You can create a common function and call with the left/right
argument from each callback or change the call pattern from ctatc.c.


thanks,

Takashi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-24 23:16 sound/pci/ctxfi/ctdaio.c: duplicate function removal question Ethan Carter Edwards
  2024-12-29  8:32 ` Takashi Iwai
@ 2024-12-29  9:02 ` David Laight
  2024-12-29  9:10   ` Takashi Iwai
  1 sibling, 1 reply; 8+ messages in thread
From: David Laight @ 2024-12-29  9:02 UTC (permalink / raw)
  To: Ethan Carter Edwards
  Cc: wychay@ctl.creative.com, ryan_richards@creativelabs.com,
	tiwai@suse.de, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Tue, 24 Dec 2024 23:16:17 +0000
Ethan Carter Edwards <ethan@ethancedwards.com> wrote:

> Hello all,
> 
> First of all, happy holidays. 
> 
> I was browsing the ctdaio.c code and I noticed a lot of 
> duplicate code and functions, specifically:
> 
> dao_set_{right,left}_input and
> dao_clear_{right,left}_input functions.
> 
> The functions are pretty much identical. They only 
> differ in the side (left, right). What was the original
> idea in doing this? Wouldn't it make more since to just
> have an ENUM (left, right) as an argument that would 
> determine the side and just reduce the function to 
> dao_set_input and dao_clear_input.

Hmmm... you'd have a lot of conditionals inside the function.

They also look like a memory leak just waiting to happen.
I guess that an earlier implementation used a separate kmalloc()
for each imappers[].

Why is imappers[] an array of pointers not an array of the items?
Each is just 8 bytes plus a 'list_head' (2 pointers?).

	David

> 
> I would be more than happy to send in a patch doing
> these changes, but before I did I wanted to ask if
> there was a reason the code was written in this way.
> I am pretty new to kernel development.
> 
> Thanks,
> Ethan
> --
> Ethan Carter Edwards
> CompTIA A+, Security+, and ISC2 (CC)
> Ham Radio: AE4CE
> Website: https://ethancedwards.com
> 
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-29  9:02 ` David Laight
@ 2024-12-29  9:10   ` Takashi Iwai
  2024-12-29 10:57     ` David Laight
  0 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2024-12-29  9:10 UTC (permalink / raw)
  To: David Laight
  Cc: Ethan Carter Edwards, wychay@ctl.creative.com,
	ryan_richards@creativelabs.com, tiwai@suse.de,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org

On Sun, 29 Dec 2024 10:02:28 +0100,
David Laight wrote:
> 
> On Tue, 24 Dec 2024 23:16:17 +0000
> Ethan Carter Edwards <ethan@ethancedwards.com> wrote:
> 
> > Hello all,
> > 
> > First of all, happy holidays. 
> > 
> > I was browsing the ctdaio.c code and I noticed a lot of 
> > duplicate code and functions, specifically:
> > 
> > dao_set_{right,left}_input and
> > dao_clear_{right,left}_input functions.
> > 
> > The functions are pretty much identical. They only 
> > differ in the side (left, right). What was the original
> > idea in doing this? Wouldn't it make more since to just
> > have an ENUM (left, right) as an argument that would 
> > determine the side and just reduce the function to 
> > dao_set_input and dao_clear_input.
> 
> Hmmm... you'd have a lot of conditionals inside the function.
> 
> They also look like a memory leak just waiting to happen.
> I guess that an earlier implementation used a separate kmalloc()
> for each imappers[].
> 
> Why is imappers[] an array of pointers not an array of the items?
> Each is just 8 bytes plus a 'list_head' (2 pointers?).

AFAIUC, it's a setup of a chained element, so no leak there as of
now.

I agree with that the code is unnecessarily complex, though.


thanks,

Takashi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-29  9:10   ` Takashi Iwai
@ 2024-12-29 10:57     ` David Laight
  2024-12-29 12:03       ` Takashi Iwai
  0 siblings, 1 reply; 8+ messages in thread
From: David Laight @ 2024-12-29 10:57 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Ethan Carter Edwards, wychay@ctl.creative.com,
	ryan_richards@creativelabs.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Sun, 29 Dec 2024 10:10:09 +0100
Takashi Iwai <tiwai@suse.de> wrote:

> On Sun, 29 Dec 2024 10:02:28 +0100,
> David Laight wrote:
> > 
> > On Tue, 24 Dec 2024 23:16:17 +0000
> > Ethan Carter Edwards <ethan@ethancedwards.com> wrote:
> >   
> > > Hello all,
> > > 
> > > First of all, happy holidays. 
> > > 
> > > I was browsing the ctdaio.c code and I noticed a lot of 
> > > duplicate code and functions, specifically:
> > > 
> > > dao_set_{right,left}_input and
> > > dao_clear_{right,left}_input functions.
> > > 
> > > The functions are pretty much identical. They only 
> > > differ in the side (left, right). What was the original
> > > idea in doing this? Wouldn't it make more since to just
> > > have an ENUM (left, right) as an argument that would 
> > > determine the side and just reduce the function to 
> > > dao_set_input and dao_clear_input.  
> > 
> > Hmmm... you'd have a lot of conditionals inside the function.
> > 
> > They also look like a memory leak just waiting to happen.
> > I guess that an earlier implementation used a separate kmalloc()
> > for each imappers[].
> > 
> > Why is imappers[] an array of pointers not an array of the items?
> > Each is just 8 bytes plus a 'list_head' (2 pointers?).  
> 
> AFAIUC, it's a setup of a chained element, so no leak there as of
> now.

Unless someone calls the functions in the wrong order.
And that seems to be outside the control of this code.

	David

> 
> I agree with that the code is unnecessarily complex, though.
> 
> 
> thanks,
> 
> Takashi


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-29 10:57     ` David Laight
@ 2024-12-29 12:03       ` Takashi Iwai
  2024-12-29 13:34         ` David Laight
  0 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2024-12-29 12:03 UTC (permalink / raw)
  To: David Laight
  Cc: Takashi Iwai, Ethan Carter Edwards, wychay@ctl.creative.com,
	ryan_richards@creativelabs.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Sun, 29 Dec 2024 11:57:22 +0100,
David Laight wrote:
> 
> On Sun, 29 Dec 2024 10:10:09 +0100
> Takashi Iwai <tiwai@suse.de> wrote:
> 
> > On Sun, 29 Dec 2024 10:02:28 +0100,
> > David Laight wrote:
> > > 
> > > On Tue, 24 Dec 2024 23:16:17 +0000
> > > Ethan Carter Edwards <ethan@ethancedwards.com> wrote:
> > >   
> > > > Hello all,
> > > > 
> > > > First of all, happy holidays. 
> > > > 
> > > > I was browsing the ctdaio.c code and I noticed a lot of 
> > > > duplicate code and functions, specifically:
> > > > 
> > > > dao_set_{right,left}_input and
> > > > dao_clear_{right,left}_input functions.
> > > > 
> > > > The functions are pretty much identical. They only 
> > > > differ in the side (left, right). What was the original
> > > > idea in doing this? Wouldn't it make more since to just
> > > > have an ENUM (left, right) as an argument that would 
> > > > determine the side and just reduce the function to 
> > > > dao_set_input and dao_clear_input.  
> > > 
> > > Hmmm... you'd have a lot of conditionals inside the function.
> > > 
> > > They also look like a memory leak just waiting to happen.
> > > I guess that an earlier implementation used a separate kmalloc()
> > > for each imappers[].
> > > 
> > > Why is imappers[] an array of pointers not an array of the items?
> > > Each is just 8 bytes plus a 'list_head' (2 pointers?).  
> > 
> > AFAIUC, it's a setup of a chained element, so no leak there as of
> > now.
> 
> Unless someone calls the functions in the wrong order.
> And that seems to be outside the control of this code.

Could you elaborate?  The chain element gets released also by the
destructor, dao_rsc_uninit().  And this is no exported function to be
used by other drivers, but an internal one only for ctxfi driver.


thanks,

Takashi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-29 12:03       ` Takashi Iwai
@ 2024-12-29 13:34         ` David Laight
  2024-12-30 15:53           ` Ethan Carter Edwards
  0 siblings, 1 reply; 8+ messages in thread
From: David Laight @ 2024-12-29 13:34 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Ethan Carter Edwards, wychay@ctl.creative.com,
	ryan_richards@creativelabs.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org

On Sun, 29 Dec 2024 13:03:56 +0100
Takashi Iwai <tiwai@suse.de> wrote:

> On Sun, 29 Dec 2024 11:57:22 +0100,
> David Laight wrote:
> > 
> > On Sun, 29 Dec 2024 10:10:09 +0100
> > Takashi Iwai <tiwai@suse.de> wrote:
> >   
> > > On Sun, 29 Dec 2024 10:02:28 +0100,
> > > David Laight wrote:  
> > > > 
> > > > On Tue, 24 Dec 2024 23:16:17 +0000
> > > > Ethan Carter Edwards <ethan@ethancedwards.com> wrote:
> > > >     
> > > > > Hello all,
> > > > > 
> > > > > First of all, happy holidays. 
> > > > > 
> > > > > I was browsing the ctdaio.c code and I noticed a lot of 
> > > > > duplicate code and functions, specifically:
> > > > > 
> > > > > dao_set_{right,left}_input and
> > > > > dao_clear_{right,left}_input functions.
> > > > > 
> > > > > The functions are pretty much identical. They only 
> > > > > differ in the side (left, right). What was the original
> > > > > idea in doing this? Wouldn't it make more since to just
> > > > > have an ENUM (left, right) as an argument that would 
> > > > > determine the side and just reduce the function to 
> > > > > dao_set_input and dao_clear_input.    
> > > > 
> > > > Hmmm... you'd have a lot of conditionals inside the function.
> > > > 
> > > > They also look like a memory leak just waiting to happen.
> > > > I guess that an earlier implementation used a separate kmalloc()
> > > > for each imappers[].
> > > > 
> > > > Why is imappers[] an array of pointers not an array of the items?
> > > > Each is just 8 bytes plus a 'list_head' (2 pointers?).    
> > > 
> > > AFAIUC, it's a setup of a chained element, so no leak there as of
> > > now.  
> > 
> > Unless someone calls the functions in the wrong order.
> > And that seems to be outside the control of this code.  
> 
> Could you elaborate?  The chain element gets released also by the
> destructor, dao_rsc_uninit().  And this is no exported function to be
> used by other drivers, but an internal one only for ctxfi driver.

The code is just too horrid :-)
I missed that when dao_set_right_input() calls dao->ops->clear_right_input()
it is just calling the static function just below.

I think the two 'clear' functions could be combined as:

static int dao_clear_input(struct dao *dao, unsigned int start, unsigned int end)
{
	struct imapper *to_free = dao->imappers[start];
	unsigned int i;

	if (!to_free)
		return 0;
	for (i = start; i < end; i++) {
		dao->mgr->imap_delete(dao->mgr, dao->imappers[i]);
		dao->imappers[i] = NULL;
	}

	kfree(to_free);
	return 0;
}

static int dao_clear_left_input(struct dao *dao)
{
	return dao_clear_input(dao, 0, dao->rscl.msr);
}

static int dao_clear_right_input(struct dao *dao)
{
	return dao_clear_input(dao, dao->rscl.msr, dao->rscl.msr + dao->rscr.msr);
}

Although I'm missing any other code that actually indexes that imappers[] array.
So I suspect only [0] and [dao->rscl.msr] are ever used??

Stanger things happen in the set functions.

	David

> 
> 
> thanks,
> 
> Takashi


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: sound/pci/ctxfi/ctdaio.c: duplicate function removal question
  2024-12-29 13:34         ` David Laight
@ 2024-12-30 15:53           ` Ethan Carter Edwards
  0 siblings, 0 replies; 8+ messages in thread
From: Ethan Carter Edwards @ 2024-12-30 15:53 UTC (permalink / raw)
  To: David Laight
  Cc: Takashi Iwai, wychay@ctl.creative.com,
	ryan_richards@creativelabs.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org

On 24/12/29 01:34PM, David Laight wrote:
> On Sun, 29 Dec 2024 13:03:56 +0100
> Takashi Iwai <tiwai@suse.de> wrote:
> 
> > On Sun, 29 Dec 2024 11:57:22 +0100,
> > David Laight wrote:
> > >
> > > On Sun, 29 Dec 2024 10:10:09 +0100
> > > Takashi Iwai <tiwai@suse.de> wrote:
> > >
> > > > On Sun, 29 Dec 2024 10:02:28 +0100,
> > > > David Laight wrote:
> > > > >
> > > > > On Tue, 24 Dec 2024 23:16:17 +0000
> > > > > Ethan Carter Edwards <ethan@ethancedwards.com> wrote:
> > > > >
> > > > > > Hello all,
> > > > > >
> > > > > > First of all, happy holidays.
> > > > > >
> > > > > > I was browsing the ctdaio.c code and I noticed a lot of
> > > > > > duplicate code and functions, specifically:
> > > > > >
> > > > > > dao_set_{right,left}_input and
> > > > > > dao_clear_{right,left}_input functions.
> > > > > >
> > > > > > The functions are pretty much identical. They only
> > > > > > differ in the side (left, right). What was the original
> > > > > > idea in doing this? Wouldn't it make more since to just
> > > > > > have an ENUM (left, right) as an argument that would
> > > > > > determine the side and just reduce the function to
> > > > > > dao_set_input and dao_clear_input.
> > > > >
> > > > > Hmmm... you'd have a lot of conditionals inside the function.
> > > > >
> > > > > They also look like a memory leak just waiting to happen.
> > > > > I guess that an earlier implementation used a separate kmalloc()
> > > > > for each imappers[].
> > > > >
> > > > > Why is imappers[] an array of pointers not an array of the items?
> > > > > Each is just 8 bytes plus a 'list_head' (2 pointers?).
> > > >
> > > > AFAIUC, it's a setup of a chained element, so no leak there as of
> > > > now.
> > >
> > > Unless someone calls the functions in the wrong order.
> > > And that seems to be outside the control of this code.
> >
> > Could you elaborate?  The chain element gets released also by the
> > destructor, dao_rsc_uninit().  And this is no exported function to be
> > used by other drivers, but an internal one only for ctxfi driver.
> 
> The code is just too horrid :-)
> I missed that when dao_set_right_input() calls dao->ops->clear_right_input()
> it is just calling the static function just below.
> 
> I think the two 'clear' functions could be combined as:
> 
> static int dao_clear_input(struct dao *dao, unsigned int start, unsigned int end)
> {
> 	struct imapper *to_free = dao->imappers[start];
> 	unsigned int i;
> 
> 	if (!to_free)
> 		return 0;
> 	for (i = start; i < end; i++) {
> 		dao->mgr->imap_delete(dao->mgr, dao->imappers[i]);
> 		dao->imappers[i] = NULL;
> 	}
> 
> 	kfree(to_free);
> 	return 0;
> }
> 
> static int dao_clear_left_input(struct dao *dao)
> {
> 	return dao_clear_input(dao, 0, dao->rscl.msr);
> }
> 
> static int dao_clear_right_input(struct dao *dao)
> {
> 	return dao_clear_input(dao, dao->rscl.msr, dao->rscl.msr + dao->rscr.msr);
> }
> 
> Although I'm missing any other code that actually indexes that imappers[] array.
> So I suspect only [0] and [dao->rscl.msr] are ever used??
> 
> Stanger things happen in the set functions.
> 
> 	David
> 

Hey David,

I used large parts of this email (with some modifications) to submit my
patch [1]. Do I have your permission to add Co-developed-by: and
Signed-off-by: to it? I want to give you proper credit and attribution
and I figured I should ask before putting your name on something. 

Thanks

[1] https://lore.kernel.org/lkml/rwfdemj7ic7rnxnycnv6sxixtfmyqnoi7u6wgulzpsjf7qm6oh@xwkyteg6hak4/T/#t
-- 
Ethan Carter Edwards
CompTIA A+, Security+, and ISC2 (CC)
Ham Radio: AE4CE
Website: https://ethancedwards.com
GPG: 2E51 F618 39D1 FA94 7A73  00C2 34C0 4305 D581 DBFE


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-12-30 15:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-24 23:16 sound/pci/ctxfi/ctdaio.c: duplicate function removal question Ethan Carter Edwards
2024-12-29  8:32 ` Takashi Iwai
2024-12-29  9:02 ` David Laight
2024-12-29  9:10   ` Takashi Iwai
2024-12-29 10:57     ` David Laight
2024-12-29 12:03       ` Takashi Iwai
2024-12-29 13:34         ` David Laight
2024-12-30 15:53           ` Ethan Carter Edwards

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox