crypto/scatterwalk.c

Source file repositories/reference/linux-study-clean/crypto/scatterwalk.c

File Facts

System
Linux kernel
Corpus path
crypto/scatterwalk.c
Extension
.c
Size
5587 bytes
Lines
205
Domain
Kernel Services
Bucket
crypto
Inferred role
Kernel Services: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

if (IS_ENABLED(CONFIG_HIGHMEM)) {
			/* HIGHMEM: we may have to actually map the pages. */
			const unsigned int src_oip = offset_in_page(src_offset);
			const unsigned int dst_oip = offset_in_page(dst_offset);
			const unsigned int limit = PAGE_SIZE;

			/* Further limit len to not cross a page boundary. */
			len = min3(len, limit - src_oip, limit - dst_oip);

			/* Compute the source and destination pages. */
			src_page += src_offset / PAGE_SIZE;
			dst_page += dst_offset / PAGE_SIZE;

			if (src_page != dst_page) {
				/* Copy between different pages. */
				memcpy_page(dst_page, dst_oip,
					    src_page, src_oip, len);
				flush_dcache_page(dst_page);
			} else if (src_oip != dst_oip) {
				/* Copy between different parts of same page. */
				dst_virt = kmap_local_page(dst_page);
				memcpy(dst_virt + dst_oip, dst_virt + src_oip,
				       len);
				kunmap_local(dst_virt);
				flush_dcache_page(dst_page);
			} /* Else, it's the same memory.  No action needed. */
		} else {
			/*
			 * !HIGHMEM: no mapping needed.  Just work in the linear
			 * buffer of each sg entry.  Note that we can cross page
			 * boundaries, as they are not significant in this case.
			 */
			src_virt = page_address(src_page) + src_offset;
			dst_virt = page_address(dst_page) + dst_offset;
			if (src_virt != dst_virt) {
				memcpy(dst_virt, src_virt, len);
				if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE)
					__scatterwalk_flush_dcache_pages(
						dst_page, dst_offset, len);
			} /* Else, it's the same memory.  No action needed. */
		}
		nbytes -= len;
		if (nbytes == 0) /* No more to copy? */
			break;

		/*
		 * There's more to copy.  Advance the offsets by the length
		 * copied this step, and advance the sg entries as needed.
		 */
		src_offset += len;
		if (src_offset >= src->offset + src->length) {
			src = sg_next(src);
			src_offset = src->offset;
		}
		dst_offset += len;
		if (dst_offset >= dst->offset + dst->length) {
			dst = sg_next(dst);
			dst_offset = dst->offset;
		}
	}
}
EXPORT_SYMBOL_GPL(memcpy_sglist);

struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
				     struct scatterlist *src,
				     unsigned int len)
{
	for (;;) {
		if (!len)
			return src;

		if (src->length > len)
			break;

		len -= src->length;
		src = sg_next(src);
	}

	sg_init_table(dst, 2);
	sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
	scatterwalk_crypto_chain(dst, sg_next(src), 2);

	return dst;
}
EXPORT_SYMBOL_GPL(scatterwalk_ffwd);

Annotation

Implementation Notes