Skip to contentSkip to conversation

scatterlist: enable sg chaining for all architectures

PATCH In review Akinobu Mita 5 participants 8 messages Apr 25, 2015 – Apr 30, 2015

Subsystems (10)
Arm (32-bit) x86 SCSI PowerPC +6 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scatterlist: enable sg chaining for all architectures

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Cc Arnd Bergmann <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Nicholas A. Bellinger <[email protected]>
---
 arch/arm/Kconfig                |  6 ------
 arch/arm64/Kconfig              |  1 -
 arch/ia64/Kconfig               |  1 -
 arch/powerpc/Kconfig            |  1 -
 arch/s390/Kconfig               |  1 -
 arch/sparc/Kconfig              |  1 -
 arch/x86/Kconfig                |  1 -
 drivers/target/target_core_rd.c | 45 -----------------------------------------
 include/linux/scatterlist.h     |  4 ----
 include/scsi/scsi.h             |  8 ++------
 lib/Kconfig                     |  7 -------
 lib/scatterlist.c               |  8 --------
 12 files changed, 2 insertions(+), 82 deletions(-)
arch/arm/Kconfig +0 -6
index 45df48b..4436000 100644
@@ -89,16 +89,11 @@ config ARM
 	  Europe.  There is an ARM Linux project with a web page at
 	  <http://www.arm.linux.org.uk/>.
 
-config ARM_HAS_SG_CHAIN
-	select ARCH_HAS_SG_CHAIN
-	bool
-
 config NEED_SG_DMA_LENGTH
 	bool
 
 config ARM_DMA_USE_IOMMU
 	bool
-	select ARM_HAS_SG_CHAIN
 	select NEED_SG_DMA_LENGTH
 
 if ARM_DMA_USE_IOMMU
@@ -318,7 +313,6 @@ config ARCH_MULTIPLATFORM
 	bool "Allow multiple platforms to be selected"
 	depends on MMU
 	select ARCH_WANT_OPTIONAL_GPIOLIB
-	select ARM_HAS_SG_CHAIN
 	select ARM_PATCH_PHYS_VIRT
 	select AUTO_ZRELADDR
 	select CLKSRC_OF
arch/arm64/Kconfig +0 -1
index 4269dba..582462a 100644
@@ -5,7 +5,6 @@ config ARM64
 	select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
 	select ARCH_HAS_ELF_RANDOMIZE
 	select ARCH_HAS_GCOV_PROFILE_ALL
-	select ARCH_HAS_SG_CHAIN
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_USE_CMPXCHG_LOCKREF
 	select ARCH_SUPPORTS_ATOMIC_RMW
arch/ia64/Kconfig +0 -1
index 76d25b2..a9f896e 100644
@@ -32,7 +32,6 @@ config IA64
 	select HAVE_MEMBLOCK
 	select HAVE_MEMBLOCK_NODE_MAP
 	select HAVE_VIRT_CPU_ACCOUNTING
-	select ARCH_HAS_SG_CHAIN
 	select VIRT_TO_BUS
 	select ARCH_DISCARD_MEMBLOCK
 	select GENERIC_IRQ_PROBE
arch/powerpc/Kconfig +0 -1
index 190cc48..6f3b6768 100644
@@ -112,7 +112,6 @@ config PPC
 	select HAVE_DMA_API_DEBUG
 	select HAVE_OPROFILE
 	select HAVE_DEBUG_KMEMLEAK
-	select ARCH_HAS_SG_CHAIN
 	select GENERIC_ATOMIC64 if PPC32
 	select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
 	select HAVE_PERF_EVENTS
arch/s390/Kconfig +0 -1
index 8e58c61..2854e52 100644
@@ -67,7 +67,6 @@ config S390
 	select ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS
 	select ARCH_HAS_ELF_RANDOMIZE
 	select ARCH_HAS_GCOV_PROFILE_ALL
-	select ARCH_HAS_SG_CHAIN
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 	select ARCH_INLINE_READ_LOCK
 	select ARCH_INLINE_READ_LOCK_BH
arch/sparc/Kconfig +0 -1
index e49502a..d0512f5 100644
@@ -42,7 +42,6 @@ config SPARC
 	select MODULES_USE_ELF_RELA
 	select ODD_RT_SIGACTION
 	select OLD_SIGSUSPEND
-	select ARCH_HAS_SG_CHAIN
 
 config SPARC32
 	def_bool !64BIT
arch/x86/Kconfig +0 -1
index 226d569..eeb52b8 100644
@@ -101,7 +101,6 @@ config X86
 	select HAVE_BPF_JIT if X86_64
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE
 	select HAVE_ARCH_HUGE_VMAP if X86_64 || (X86_32 && X86_PAE)
-	select ARCH_HAS_SG_CHAIN
 	select CLKEVT_I8253
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 	select GENERIC_IOMAP
drivers/target/target_core_rd.c +0 -45

80 lines not on this page · raw message

include/linux/scatterlist.h +0 -4
index ed8f9e7..bee59ea 100644
@@ -136,10 +136,6 @@ static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
 			    struct scatterlist *sgl)
 {
-#ifndef CONFIG_ARCH_HAS_SG_CHAIN
-	BUG();
-#endif
-
 	/*
 	 * offset and length are unused for chain entry.  Clear them.
 	 */
include/scsi/scsi.h +2 -6
index d0a66aa..f01c5bb 100644
@@ -28,14 +28,10 @@ enum scsi_timeouts {
 #define SCSI_MAX_SG_SEGMENTS	128
 
 /*
- * Like SCSI_MAX_SG_SEGMENTS, but for archs that have sg chaining. This limit
- * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
+ * This limit is totally arbitrary, a setting of 2048 will get you at least
+ * 8mb ios.
  */
-#ifdef CONFIG_ARCH_HAS_SG_CHAIN
 #define SCSI_MAX_SG_CHAIN_SEGMENTS	2048
-#else
-#define SCSI_MAX_SG_CHAIN_SEGMENTS	SCSI_MAX_SG_SEGMENTS
-#endif
 
 /*
  * DIX-capable adapters effectively support infinite chaining for the
lib/Kconfig +0 -7
index 601965a..0b938d2 100644
@@ -515,11 +515,4 @@ config UCS2_STRING
 
 source "lib/fonts/Kconfig"
 
-#
-# sg chaining option
-#
-
-config ARCH_HAS_SG_CHAIN
-	def_bool n
-
 endmenu
lib/scatterlist.c +0 -8
index c9f2e8c..6d9d477 100644
@@ -73,16 +73,12 @@ EXPORT_SYMBOL(sg_nents);
  **/
 struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
 {
-#ifndef CONFIG_ARCH_HAS_SG_CHAIN
-	struct scatterlist *ret = &sgl[nents - 1];
-#else
 	struct scatterlist *sg, *ret = NULL;
 	unsigned int i;
 
 	for_each_sg(sgl, sg, nents, i)
 		ret = sg;
 
-#endif
 #ifdef CONFIG_DEBUG_SG
 	BUG_ON(sgl[0].sg_magic != SG_MAGIC);
 	BUG_ON(!sg_is_last(ret));
@@ -255,10 +251,6 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
 
 	if (nents == 0)
 		return -EINVAL;
-#ifndef CONFIG_ARCH_HAS_SG_CHAIN
-	if (WARN_ON_ONCE(nents > max_ents))
-		return -EINVAL;
-#endif
 
 	left = nents;
 	prv = NULL;
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 8 lines · On Sat, 25 Apr 2015 23:56:16 +0900 Akinobu Mita <[email protected]> wrote:

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

Quoted 4 lines

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Thanks, I'll grab this. If anyone has concerns, speak now or hold both pieces!

maintainer
#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Sat, 25 Apr 2015 23:56:16 +0900 Akinobu Mita <[email protected]> wrote:

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

Quoted 7 lines
Quoted 4 lines

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Thanks, I'll grab this. If anyone has concerns, speak now or hold both pieces!

It breaks a host of architectures doesn't it? I can specifically speak for PARISC: The problem is the way our iommus are consuming scatterlists. They're assuming we can dereference the scatterlist as an array (like this code in ccio-dma.c):

static int
ccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
enum dma_data_direction direction)
[...]

	for(i = 0; i < nents; i++)
		prev_len += sglist[i].length;

If you turn on sg chaining on our architecture, we'll run off the end of that array dereference and crash.

This can all be fixed by making our architecture dma mapping code use iterators instead of array lists, but that needs more code than this patch provides. I assume there are similar issues on a lot of other architectures, so before you can contemplate a patch like this, surely all the architecture consumers have to be converted to iterator instead of array format?

The first place to start would be a survey of who's still using the array format.

James

#3 lore raw

2015-04-29 7:16 GMT+09:00 James Bottomley
<[email protected]>:

Quoted 41 lines · Andrew Morton
Quoted 10 lines · On Sat, 25 Apr 2015 23:56:16 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 9 lines

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

Quoted 7 lines
Quoted 4 lines

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Thanks, I'll grab this. If anyone has concerns, speak now or hold both pieces!

It breaks a host of architectures doesn't it? I can specifically speak for PARISC: The problem is the way our iommus are consuming scatterlists. They're assuming we can dereference the scatterlist as an array (like this code in ccio-dma.c):

static int
ccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
enum dma_data_direction direction)
[...]

        for(i = 0; i < nents; i++)
                prev_len += sglist[i].length;

If you turn on sg chaining on our architecture, we'll run off the end of that array dereference and crash.

This can all be fixed by making our architecture dma mapping code use iterators instead of array lists, but that needs more code than this patch provides. I assume there are similar issues on a lot of other architectures, so before you can contemplate a patch like this, surely all the architecture consumers have to be converted to iterator instead of array format?

Quoted 2 lines

The first place to start would be a survey of who's still using the array format.

Agreed. I could find similar issues in arch/m68k/kernel/dma.c. (git grep '[^a-z]sg++' shows that there are a lot of similar issues)

Andrew, could you drop this patch from -mm for now?

maintainer
#4 lore raw
Quoted 43 lines · Akinobu Mita

2015-04-29 7:16 GMT+09:00 James Bottomley
<[email protected]>:

Quoted 41 lines · Andrew Morton
Quoted 10 lines · On Sat, 25 Apr 2015 23:56:16 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 9 lines

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

Quoted 7 lines
Quoted 4 lines

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Thanks, I'll grab this. If anyone has concerns, speak now or hold both pieces!

It breaks a host of architectures doesn't it? I can specifically speak for PARISC: The problem is the way our iommus are consuming scatterlists. They're assuming we can dereference the scatterlist as an array (like this code in ccio-dma.c):

static int
ccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
enum dma_data_direction direction)
[...]

        for(i = 0; i < nents; i++)
                prev_len += sglist[i].length;

If you turn on sg chaining on our architecture, we'll run off the end of that array dereference and crash.

This can all be fixed by making our architecture dma mapping code use iterators instead of array lists, but that needs more code than this patch provides. I assume there are similar issues on a lot of other architectures, so before you can contemplate a patch like this, surely all the architecture consumers have to be converted to iterator instead of array format?

Quoted 5 lines
Quoted 2 lines

The first place to start would be a survey of who's still using the array format.

Agreed. I could find similar issues in arch/m68k/kernel/dma.c. (git grep '[^a-z]sg++' shows that there are a lot of similar issues)

OK, so the original idea of the chained SG lists was that most of the older architectures have fixed length lists for their IOMMUs, or simply wouldn't see a benefit with IO lengths > 0.5MB (which was the default before chaining) so there wasn't much point converting them to chaining if they wouldn't see any benefit from it.

ARCH_HAS_SG_CHAIN is supposed to be completely transparent to all driver side consumers, so there was never thought to be much point removing it. It looks like there's some sort of cockup going on in the target driver but otherwise, your removal patch is pretty empty, confirming this.

Perhaps the best thing to do is just fix target and call it quits?

James

#5 lore raw
Quoted 3 lines · James Bottomley

Perhaps the best thing to do is just fix target and call it quits?

Right! drivers write code for sg_chaining and on ARCHs that do not support it the code just works.
Only the max_sg is smaller and the chaining code never kicks in and is dead code for these ARCHs.

Quoted 1 line

James

Cheers
Boaz

#6 lore raw
In reply to James Bottomley
Quoted 56 lines · James Bottomley
Quoted 43 lines · Akinobu Mita

2015-04-29 7:16 GMT+09:00 James Bottomley
<[email protected]>:

Quoted 41 lines · Andrew Morton
Quoted 10 lines · On Sat, 25 Apr 2015 23:56:16 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 9 lines

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

Quoted 7 lines
Quoted 4 lines

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Thanks, I'll grab this. If anyone has concerns, speak now or hold both pieces!

It breaks a host of architectures doesn't it? I can specifically speak for PARISC: The problem is the way our iommus are consuming scatterlists. They're assuming we can dereference the scatterlist as an array (like this code in ccio-dma.c):

static int
ccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
enum dma_data_direction direction)
[...]

        for(i = 0; i < nents; i++)
                prev_len += sglist[i].length;

If you turn on sg chaining on our architecture, we'll run off the end of that array dereference and crash.

This can all be fixed by making our architecture dma mapping code use iterators instead of array lists, but that needs more code than this patch provides. I assume there are similar issues on a lot of other architectures, so before you can contemplate a patch like this, surely all the architecture consumers have to be converted to iterator instead of array format?

Quoted 5 lines
Quoted 2 lines

The first place to start would be a survey of who's still using the array format.

Agreed. I could find similar issues in arch/m68k/kernel/dma.c. (git grep '[^a-z]sg++' shows that there are a lot of similar issues)

OK, so the original idea of the chained SG lists was that most of the older architectures have fixed length lists for their IOMMUs, or simply wouldn't see a benefit with IO lengths > 0.5MB (which was the default before chaining) so there wasn't much point converting them to chaining if they wouldn't see any benefit from it.

Quoted 7 lines

ARCH_HAS_SG_CHAIN is supposed to be completely transparent to all driver side consumers, so there was never thought to be much point removing it. It looks like there's some sort of cockup going on in the target driver but otherwise, your removal patch is pretty empty, confirming this.

Perhaps the best thing to do is just fix target and call it quits?

So the ARCH_HAS_SG_CHAIN usage in target_core_rd.c was recently added so target DIF emulation could use standard SGL iterators and correctly handle boundaries across T10-PI metadata SGL tables in the ramdisk backend.

The SGLs in question are never actually mapped to a HW IOMMU, and Akinobu's current changes in mainline do support both arch cases and make common sbc_dif_copy_prot() code a bit simpler too.

That said, I'd rather to keep the hack around for now so that both ARCH_HAS_SG_CHAIN types can still work, short of a full arch conversion of course..

Thanks,

--nab

maintainer
#7 lore raw
Quoted 70 lines · Nicholas A. Bellinger
Quoted 56 lines · James Bottomley
Quoted 43 lines · Akinobu Mita

2015-04-29 7:16 GMT+09:00 James Bottomley
<[email protected]>:

Quoted 41 lines · Andrew Morton
Quoted 10 lines · On Sat, 25 Apr 2015 23:56:16 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 9 lines

Some architectures enable sg chaining option while others do not.

The requirement to enable sg chaining is that pages must be aligned at a 32-bit boundary in order to overload the LSB of the pointer. Regardless of whether ARCH_HAS_SG_CHAIN is defined or not, the above requirement is always chacked by BUG_ON() in sg_assign_page. So all architectures can enable sg chaining.

Quoted 7 lines
Quoted 4 lines

As you can see from the changes in drivers/target/target_core_rd.c, enabling SG chaining for all architectures allows us to allocate discontiguous scatterlist tables which can be traversed throughout by sg_next() without a special handling for some architectures.

Thanks, I'll grab this. If anyone has concerns, speak now or hold both pieces!

It breaks a host of architectures doesn't it? I can specifically speak for PARISC: The problem is the way our iommus are consuming scatterlists. They're assuming we can dereference the scatterlist as an array (like this code in ccio-dma.c):

static int
ccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
enum dma_data_direction direction)
[...]

        for(i = 0; i < nents; i++)
                prev_len += sglist[i].length;

If you turn on sg chaining on our architecture, we'll run off the end of that array dereference and crash.

This can all be fixed by making our architecture dma mapping code use iterators instead of array lists, but that needs more code than this patch provides. I assume there are similar issues on a lot of other architectures, so before you can contemplate a patch like this, surely all the architecture consumers have to be converted to iterator instead of array format?

Quoted 5 lines
Quoted 2 lines

The first place to start would be a survey of who's still using the array format.

Agreed. I could find similar issues in arch/m68k/kernel/dma.c. (git grep '[^a-z]sg++' shows that there are a lot of similar issues)

OK, so the original idea of the chained SG lists was that most of the older architectures have fixed length lists for their IOMMUs, or simply wouldn't see a benefit with IO lengths > 0.5MB (which was the default before chaining) so there wasn't much point converting them to chaining if they wouldn't see any benefit from it.

Quoted 7 lines

ARCH_HAS_SG_CHAIN is supposed to be completely transparent to all driver side consumers, so there was never thought to be much point removing it. It looks like there's some sort of cockup going on in the target driver but otherwise, your removal patch is pretty empty, confirming this.

Perhaps the best thing to do is just fix target and call it quits?

So the ARCH_HAS_SG_CHAIN usage in target_core_rd.c was recently added so target DIF emulation could use standard SGL iterators and correctly handle boundaries across T10-PI metadata SGL tables in the ramdisk backend.

Quoted 7 lines

The SGLs in question are never actually mapped to a HW IOMMU, and Akinobu's current changes in mainline do support both arch cases and make common sbc_dif_copy_prot() code a bit simpler too.

That said, I'd rather to keep the hack around for now so that both ARCH_HAS_SG_CHAIN types can still work, short of a full arch conversion of course..

It looks like you might not have needed the hack if you'd used the existing sg chain allocators ....

James