Revision d5a047fd92c6e0db7d56b9e20200cc30f6c7c8e2 authored by Linus Torvalds on 13 January 2018, 22:04:06 UTC, committed by Linus Torvalds on 13 January 2018, 22:04:06 UTC
Pull staging driver fix from Greg KH:
 "Here is a single android ashmem bugfix that resolves a reported issue
  in that interface. It's been in linux-next this week with no reported
  issues"

* tag 'staging-4.15-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging:
  staging: android: ashmem: fix a race condition in ASHMEM_SET_SIZE ioctl
2 parent s 9e8f8f1 + 443064c
Raw File
memmove.c
/*
 * Copyright 2010 Tilera Corporation. All Rights Reserved.
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation, version 2.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 *   NON INFRINGEMENT.  See the GNU General Public License for
 *   more details.
 */

#include <linux/types.h>
#include <linux/string.h>
#include <linux/module.h>

void *memmove(void *dest, const void *src, size_t n)
{
	if ((const char *)src >= (char *)dest + n
	    || (char *)dest >= (const char *)src + n) {
		/* We found no overlap, so let memcpy do all the heavy
		 * lifting (prefetching, etc.)
		 */
		return memcpy(dest, src, n);
	}

	if (n != 0) {
		const uint8_t *in;
		uint8_t x;
		uint8_t *out;
		int stride;

		if (src < dest) {
			/* copy backwards */
			in = (const uint8_t *)src + n - 1;
			out = (uint8_t *)dest + n - 1;
			stride = -1;
		} else {
			/* copy forwards */
			in = (const uint8_t *)src;
			out = (uint8_t *)dest;
			stride = 1;
		}

		/* Manually software-pipeline this loop. */
		x = *in;
		in += stride;

		while (--n != 0) {
			*out = x;
			out += stride;
			x = *in;
			in += stride;
		}

		*out = x;
	}

	return dest;
}
EXPORT_SYMBOL(memmove);
back to top