Skip to contentSkip to conversation

libata-eh: Use switch() instead of sparse array for protocol strings

PATCH Applied Geert Uytterhoeven 3 participants 14 messages Jan 9, 2017 libata

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libata-eh: Use switch() instead of sparse array for protocol strings

Replace the sparse 256-pointer array for looking up protocol strings by a switch() statement to reduce kernel size.

According to bloat-o-meter, this saves 910 bytes on m68k (32-bit), and 1892 bytes on arm64 (64-bit).

---
 drivers/ata/libata-eh.c | 44 +++++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 13 deletions(-)
drivers/ata/libata-eh.c +31 -13
index 0e1ec37070d19b64..e7196fc29ff09434 100644
@@ -2606,21 +2606,39 @@ static void ata_eh_link_report(struct ata_link *link)
 				[DMA_TO_DEVICE]		= "out",
 				[DMA_FROM_DEVICE]	= "in",
 			};
-			static const char *prot_str[] = {
-				[ATA_PROT_UNKNOWN]	= "unknown",
-				[ATA_PROT_NODATA]	= "nodata",
-				[ATA_PROT_PIO]		= "pio",
-				[ATA_PROT_DMA]		= "dma",
-				[ATA_PROT_NCQ]		= "ncq dma",
-				[ATA_PROT_NCQ_NODATA]	= "ncq nodata",
-				[ATAPI_PROT_NODATA]	= "nodata",
-				[ATAPI_PROT_PIO]	= "pio",
-				[ATAPI_PROT_DMA]	= "dma",
-			};
+			const char *prot_str = NULL;
 
+			switch (qc->tf.protocol) {
+			case ATA_PROT_UNKNOWN:
+				prot_str = "unknown";
+				break;
+			case ATA_PROT_NODATA:
+				prot_str = "nodata";
+				break;
+			case ATA_PROT_PIO:
+				prot_str = "pio";
+				break;
+			case ATA_PROT_DMA:
+				prot_str = "dma";
+				break;
+			case ATA_PROT_NCQ:
+				prot_str = "ncq dma";
+				break;
+			case ATA_PROT_NCQ_NODATA:
+				prot_str = "ncq nodata";
+				break;
+			case ATAPI_PROT_NODATA:
+				prot_str = "nodata";
+				break;
+			case ATAPI_PROT_PIO:
+				prot_str = "pio";
+				break;
+			case ATAPI_PROT_DMA:
+				prot_str = "dma";
+				break;
+			}
 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
-				 prot_str[qc->tf.protocol], qc->nbytes,
-				 dma_str[qc->dma_dir]);
+				 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
 		}
 
 		if (ata_is_atapi(qc->tf.protocol)) {
Signature · 1 line
1.9.1
Applied
#1 lore raw
Quoted 7 lines · Geert Uytterhoeven

Replace the sparse 256-pointer array for looking up protocol strings by a switch() statement to reduce kernel size.

According to bloat-o-meter, this saves 910 bytes on m68k (32-bit), and 1892 bytes on arm64 (64-bit).

Gees, thanks for catching that.

Applied to libata/for-4.11.

Thanks.

Signature · 1 line
tejun
#2 lore raw

Hi Tejun,

Quoted 4 lines · Tejun Heo
Quoted 3 lines · Geert Uytterhoeven

Replace the sparse 256-pointer array for looking up protocol strings by a switch() statement to reduce kernel size.

Quoted 8 lines
Quoted 4 lines

According to bloat-o-meter, this saves 910 bytes on m68k (32-bit), and 1892 bytes on arm64 (64-bit).

Gees, thanks for catching that.

Applied to libata/for-4.11.

Thanks!

There are two more that annoy me, but I don't know how to fix them:

ata_scsi_rbuf                                  -    4096   +4096
ata_force_param_buf                            -    4096   +4096

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#3 lore raw

Hello,

Quoted 4 lines · Geert Uytterhoeven

There are two more that annoy me, but I don't know how to fix them:

ata_scsi_rbuf                                  -    4096   +4096
ata_force_param_buf                            -    4096   +4096

ata_force_param_buf is __initdata and shouldn't really matter. ata_scsi_rbuf, hmmm, idk. Maybe we can allocate it dynamically when registering the first ATA device so that systems w/o them can avoid the wastage.

Thanks.

Signature · 1 line
tejun
#4 lore raw
Quoted 4 lines · Tejun Heo

ata_force_param_buf is __initdata and shouldn't really matter. ata_scsi_rbuf, hmmm, idk. Maybe we can allocate it dynamically when registering the first ATA device so that systems w/o them can avoid the wastage.

Having it global is kinda weird anyway. But looking the code none of the commands actually using is in the paging path, so it could simply be replaced with a dynamic allocation in ata_scsi_rbuf_fill for the actually needed size, which often will be very small, or sometimes even 0.

#5 lore raw
Quoted 5 lines · Christoph Hellwig

Having it global is kinda weird anyway. But looking the code none of the commands actually using is in the paging path, so it could simply be replaced with a dynamic allocation in ata_scsi_rbuf_fill for the actually needed size, which often will be very small, or sometimes even 0.

Prototype here, only tested with a simple mkfs.xfs and some I/O on AHCI so far:

http://git.infradead.org/users/hch/block.git/shortlog/refs/heads/libata-kill-ata_scsi_rbuf

#7 lore raw
In reply to Tejun Heo

Hi Tejun,

Quoted 7 lines · Tejun Heo
Quoted 4 lines · Geert Uytterhoeven

There are two more that annoy me, but I don't know how to fix them:

ata_scsi_rbuf                                  -    4096   +4096
ata_force_param_buf                            -    4096   +4096

ata_force_param_buf is __initdata and shouldn't really matter.

It mainly matters because of e.g. bootloader limitations.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#8 lore raw
Quoted 3 lines · Geert Uytterhoeven
Quoted 1 line

ata_force_param_buf is __initdata and shouldn't really matter.

It mainly matters because of e.g. bootloader limitations.

Do we need a full 4k for the force parameters? What would a typical command line for it look like?

#9 lore raw

Hello,

Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Geert Uytterhoeven
Quoted 1 line

ata_force_param_buf is __initdata and shouldn't really matter.

It mainly matters because of e.g. bootloader limitations.

Do we need a full 4k for the force parameters? What would a typical command line for it look like?

Maybe a couple hundreds bytes at max, but it's a bit weird to restrict this given that it is bss, not gigantic and __initdata. What kind of bootloader limitations are we talking about?

Thanks.

Signature · 1 line
tejun
#10 lore raw
Quoted 4 lines · Tejun Heo
Quoted 3 lines · Christoph Hellwig
Quoted 2 lines · Geert Uytterhoeven
Quoted 1 line

ata_force_param_buf is __initdata and shouldn't really matter.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

It mainly matters because of e.g. bootloader limitations.

Do we need a full 4k for the force parameters? What would a typical command line for it look like?

Maybe a couple hundreds bytes at max, but it's a bit weird to restrict this given that it is bss, not gigantic and __initdata. What kind of bootloader limitations are we talking about?

Some boot loaders start overwriting themselves or the passed DTB if the kernel becomes too big.
If I'm not mistaken, bss is still expanded early (verified, increasing bss can trigger the above problem).

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#11 lore raw

Hello, Geert.

Quoted 10 lines · Geert Uytterhoeven
Quoted 9 lines · Tejun Heo
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Geert Uytterhoeven
Quoted 1 line

ata_force_param_buf is __initdata and shouldn't really matter.

It mainly matters because of e.g. bootloader limitations.

Do we need a full 4k for the force parameters? What would a typical command line for it look like?

Quoted 8 lines
Quoted 3 lines

Maybe a couple hundreds bytes at max, but it's a bit weird to restrict this given that it is bss, not gigantic and __initdata. What kind of bootloader limitations are we talking about?

Some boot loaders start overwriting themselves or the passed DTB if the kernel becomes too big.
If I'm not mistaken, bss is still expanded early (verified, increasing bss can trigger the above problem).

So, to avoid that, we can just kmalloc and kfree the buffer, but it seems like a silly complication to work around bugs in some bootloaders. There are many places in kernel where we're liberal about __initdata which is great. I'm not sure complicating all those places for a broken bootloader is a good idea.

Thanks.

Signature · 1 line
tejun
#12 lore raw

Hi Tejun,

Quoted 20 lines · Tejun Heo
Quoted 10 lines · Geert Uytterhoeven
Quoted 9 lines · Tejun Heo
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Geert Uytterhoeven
Quoted 1 line

ata_force_param_buf is __initdata and shouldn't really matter.

It mainly matters because of e.g. bootloader limitations.

Do we need a full 4k for the force parameters? What would a typical command line for it look like?

Quoted 8 lines
Quoted 3 lines

Maybe a couple hundreds bytes at max, but it's a bit weird to restrict this given that it is bss, not gigantic and __initdata. What kind of bootloader limitations are we talking about?

Some boot loaders start overwriting themselves or the passed DTB if the kernel becomes too big.
If I'm not mistaken, bss is still expanded early (verified, increasing bss can trigger the above problem).

Quoted 5 lines

So, to avoid that, we can just kmalloc and kfree the buffer, but it seems like a silly complication to work around bugs in some bootloaders. There are many places in kernel where we're liberal about __initdata which is great. I'm not sure complicating all those places for a broken bootloader is a good idea.

Sure. We cannot avoid that kernels (esp. multiplatform) keep on growing.

But when I see a new 4KiB-sized buffer, i'm always suspicious... A few years ago, I caught someone miscalculating shifts, leading to a static buffer that was 256 times larger than intended ;-)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#13 lore raw

Hello,

Quoted 11 lines · Geert Uytterhoeven
Quoted 5 lines

So, to avoid that, we can just kmalloc and kfree the buffer, but it seems like a silly complication to work around bugs in some bootloaders. There are many places in kernel where we're liberal about __initdata which is great. I'm not sure complicating all those places for a broken bootloader is a good idea.

Sure. We cannot avoid that kernels (esp. multiplatform) keep on growing.

But when I see a new 4KiB-sized buffer, i'm always suspicious... A few years ago, I caught someone miscalculating shifts, leading to a static buffer that was 256 times larger than intended ;-)

Oh, sure, things like the protocol string table are just stupid and it's great that you caught it. I just don't think it makes sense to scrutinize bss __initdata. It's not in kernel image and goes away once the kernel boots. It's okay to a bit liberal with them for the sake of simplicity.

Thanks.

Signature · 1 line
tejun