Skip to contentSkip to conversation

lib/string: add memrchr function

PATCH In review Xiang Xiao 2 participants 9 messages Feb 14, 2019 – Feb 16, 2019

Subsystems (3)
lore
Share & tools
lore

Xiang Xiao

author
#0 lore raw

PATCH v2 1/2 lib/string: add memrchr function

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

---
 include/linux/string.h |  3 +++
 lib/string.c           | 23 +++++++++++++++++++++++
 2 files changed, 26 insertions(+)
include/linux/string.h +3 -0
index 7927b87..915c617 100644
@@ -167,6 +167,9 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+#ifndef __HAVE_ARCH_MEMRCHR
+void *memrchr(const void *s, int c, size_t n);
+#endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
 
lib/string.c +23 -0
index 38e4ca0..595e0b3 100644
@@ -964,6 +964,29 @@ void *memchr(const void *s, int c, size_t n)
 EXPORT_SYMBOL(memchr);
 #endif
 
+#ifndef __HAVE_ARCH_MEMRCHR
+/**
+ * memrchr - Find the last character in an area of memory.
+ * @s: The memory area
+ * @c: The byte to search for
+ * @n: The size of the area.
+ *
+ * Return: the address of the last occurrence of @c, or %NULL
+ * if @c is not found
+ */
+void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *p = s + n;
+
+	while (n-- != 0) {
+		if ((unsigned char)c == *--p)
+			return (void *)p;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL(memrchr);
+#endif
+
 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
 {
 	while (bytes) {
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 2/2 rpmsg: add syslog redirection driver

From: Guiding Li <[email protected]>

This driver allows the remote processor to redirect the output of syslog or printf into the kernel log, which is very useful to see what happen in the remote side.

---
 drivers/rpmsg/Kconfig        |  12 ++++
 drivers/rpmsg/Makefile       |   1 +
 drivers/rpmsg/rpmsg_syslog.c | 163 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+)
 create mode 100644 drivers/rpmsg/rpmsg_syslog.c
drivers/rpmsg/Kconfig +12 -0
index d0322b4..13ead55 100644
@@ -15,6 +15,18 @@ config RPMSG_CHAR
 	  in /dev. They make it possible for user-space programs to send and
 	  receive rpmsg packets.
 
+config RPMSG_SYSLOG
+	tristate "RPMSG syslog redirection"
+	depends on RPMSG
+	help
+	  Say Y here to redirect the syslog/printf from remote processor into
+	  the kernel log which is very useful to see what happened in the remote
+	  side.
+
+	  If the remote processor hangs during bootup or panics during runtime,
+	  we can even cat /sys/kernel/debug/remoteproc/remoteprocX/trace0 to
+	  get the last log which hasn't been output yet.
+
 config RPMSG_QCOM_GLINK_NATIVE
 	tristate
 	select RPMSG
drivers/rpmsg/Makefile +1 -0
index 9aa8595..bfd22df 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_RPMSG)		+= rpmsg_core.o
 obj-$(CONFIG_RPMSG_CHAR)	+= rpmsg_char.o
+obj-$(CONFIG_RPMSG_SYSLOG)	+= rpmsg_syslog.o
 obj-$(CONFIG_RPMSG_QCOM_GLINK_RPM) += qcom_glink_rpm.o
 obj-$(CONFIG_RPMSG_QCOM_GLINK_NATIVE) += qcom_glink_native.o
 obj-$(CONFIG_RPMSG_QCOM_GLINK_SMEM) += qcom_glink_smem.o
drivers/rpmsg/rpmsg_syslog.c new file +163 -0

163 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH V2 2/2] rpmsg: add syslog redirection driver

drivers/rpmsg/rpmsg_syslog.c line 20

Quoted 10 lines · Xiang Xiao

From: Guiding Li <[email protected]>

This driver allows the remote processor to redirect the output of syslog or printf into the kernel log, which is very useful to see what happen in the remote side.

drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+struct rpmsg_syslog_header {
+	u32				command;
+	s32				result;
+} __packed;

Isn't packed already?

drivers/rpmsg/rpmsg_syslog.c line 26

Quoted 5 lines
drivers/rpmsg/rpmsg_syslog.c new file +5 -0
+struct rpmsg_syslog_transfer {
+	struct rpmsg_syslog_header	header;
+	u32				count;
+	char				data[0];
+} __packed;

Ditto.

Quoted 5 lines
drivers/rpmsg/rpmsg_syslog.c new file +5 -0
+static int rpmsg_syslog_callback(struct rpmsg_device *rpdev,
+				 void *data, int len, void *priv_, u32 src)
+{
+	struct rpmsg_syslog *priv = dev_get_drvdata(&rpdev->dev);
+	struct rpmsg_syslog_transfer *msg = data;

drivers/rpmsg/rpmsg_syslog.c line 52

Quoted 10 lines
drivers/rpmsg/rpmsg_syslog.c new file +10 -0
+	struct rpmsg_syslog_transfer_done done;
+	unsigned int copied = msg->count;
+	unsigned int printed = 0;
+	const char *nl;
+
+	if (msg->header.command != RPMSG_SYSLOG_TRANSFER)
+		return -EINVAL;
+
+	/* output the message before '\n' to the kernel log */
+	nl = memrchr(msg->data, '\n', msg->count);

Hmm... To me it sounds somehow fragile.

If your text contains binary data, how can you guarantee that it would be not in the middle of two \n:s? OTOH, if it text data, why do you need to take all strings at once? It might be worse from performance prospective (if you know how and when printk() supplies buffer to the console).

Quoted 25 lines
drivers/rpmsg/rpmsg_syslog.c new file +21 -0
+	if (nl) {
+		printed = nl + 1 - msg->data;
+		copied = msg->count - printed;
+
+		if (priv->next) {
+			pr_info("%.*s%.*s", priv->next,
+				priv->buf, printed, msg->data);
+			priv->next = 0;
+		} else {
+			pr_info("%.*s", printed, msg->data);
+		}
+	}
+
+	/* append the message after '\n' to the buffer */
 
+	if (copied != 0) {
 
 
 
+		unsigned int newsize = priv->next + copied;
+
+		if (newsize > priv->size) {
+			char *newbuf;
+
+			newbuf = krealloc(priv->buf, newsize, GFP_KERNEL);

drivers/rpmsg/rpmsg_syslog.c line 82

Quoted 10 lines
drivers/rpmsg/rpmsg_syslog.c new file +9 -0
+			if (newbuf) {
+				priv->buf  = newbuf;
+				priv->size = newsize;
+			} else {
+				copied = priv->size - priv->next;
+			}
+		}
+
 
+		strncpy(priv->buf + priv->next, msg->data + printed, copied);

Hmm... shouldn't be memcpy()?

Quoted 19 lines
drivers/rpmsg/rpmsg_syslog.c new file +19 -0
+		priv->next += copied;
+	}
+
+	done.command = RPMSG_SYSLOG_TRANSFER_DONE;
+	done.result  = printed + copied;
+	return rpmsg_send(rpdev->ept, &done, sizeof(done));
+}
+
+static int rpmsg_syslog_probe(struct rpmsg_device *rpdev)
+{
+	struct rpmsg_syslog *priv;
+
+	priv = devm_kzalloc(&rpdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	dev_set_drvdata(&rpdev->dev, priv);
+	return 0;
+}

drivers/rpmsg/rpmsg_syslog.c line 110

Quoted 10 lines
drivers/rpmsg/rpmsg_syslog.c new file +9 -0
+
+static void rpmsg_syslog_remove(struct rpmsg_device *rpdev)
+{
+	struct rpmsg_syslog *priv = dev_get_drvdata(&rpdev->dev);
+
 
+	/* flush the buffered log if need */
+	if (priv->next)
+		pr_info("%.*s\n", priv->next, priv->buf);
+	kfree(priv->buf);

I don't see how it's serialized. Does rpmsg core take care of this?

drivers/rpmsg/rpmsg_syslog.c line 113

Quoted 3 lines
drivers/rpmsg/rpmsg_syslog.c new file +2 -0
+}
 
+#ifdef CONFIG_PM_SLEEP

You can consider to use __maybe_unused annotation to the below function.

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +3 -0
+static int rpmsg_syslog_dev_suspend(struct device *dev)
+{
 
+}

drivers/rpmsg/rpmsg_syslog.c line 141

Quoted 10 lines
drivers/rpmsg/rpmsg_syslog.c new file +8 -0
+
+static int rpmsg_syslog_dev_resume(struct device *dev)
+{
 
+}
+#endif
 
+static const struct rpmsg_device_id rpmsg_syslog_id_table[] = {
+	{ .name = "rpmsg-syslog" },
+	{ },

Terminator better without comma.

Quoted 1 line

+};

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH V2 2/2] rpmsg: add syslog redirection driver

Quoted 5 lines · Andy Shevchenko

drivers/rpmsg/rpmsg_syslog.c line 67

Quoted 3 lines · Xiang Xiao
drivers/rpmsg/rpmsg_syslog.c new file +2 -0
+	/* append the message after '\n' to the buffer */
 
+	if (copied != 0) {

Missed comment

if (copied)

Quoted 1 line
Quoted 1 line
(quoted diff) +1 -0
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#4 lore raw

Re: [PATCH V2 2/2] rpmsg: add syslog redirection driver

In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 5 lines · Xiang Xiao

From: Guiding Li <[email protected]>

This driver allows the remote processor to redirect the output of syslog or printf into the kernel log, which is very useful to see what happen in the remote side.

Quoted 7 lines

drivers/rpmsg/rpmsg_syslog.c line 20

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+struct rpmsg_syslog_header {
+     u32                             command;
+     s32                             result;
+} __packed;

Isn't packed already?

But, I want to make it more explicitly and prepare for struct expansion later.

Quoted 26 lines

drivers/rpmsg/rpmsg_syslog.c line 26

Quoted 5 lines
drivers/rpmsg/rpmsg_syslog.c new file +5 -0
+struct rpmsg_syslog_transfer {
+     struct rpmsg_syslog_header      header;
+     u32                             count;
+     char                            data[0];
+} __packed;

Ditto.

Quoted 5 lines
drivers/rpmsg/rpmsg_syslog.c new file +5 -0
+static int rpmsg_syslog_callback(struct rpmsg_device *rpdev,
+                              void *data, int len, void *priv_, u32 src)
+{
+     struct rpmsg_syslog *priv = dev_get_drvdata(&rpdev->dev);
+     struct rpmsg_syslog_transfer *msg = data;

drivers/rpmsg/rpmsg_syslog.c line 52

Quoted 10 lines
drivers/rpmsg/rpmsg_syslog.c new file +10 -0
+     struct rpmsg_syslog_transfer_done done;
+     unsigned int copied = msg->count;
+     unsigned int printed = 0;
+     const char *nl;
+
+     if (msg->header.command != RPMSG_SYSLOG_TRANSFER)
+             return -EINVAL;
+
+     /* output the message before '\n' to the kernel log */
+     nl = memrchr(msg->data, '\n', msg->count);

Hmm... To me it sounds somehow fragile.

Quoted 2 lines

If your text contains binary data, how can you guarantee that it would be not in the middle of two \n:s?

This driver is just for log/printf redirection, so we could safely assume the data is pure text.
We have another rpmsg driver(rpmsg-tty) for binary data transfer.

Quoted 1 line

OTOH, if it text data, why do you need to take all strings at once?

Remote side may decide to buffer more log to reduce the IPC number since IPC is a time consuming operation.

Quoted 1 line

It might be worse from performance prospective (if you know how and when printk() supplies buffer to the console).

Yes, it's very slow if the log send to serial console. But in production environment, printk normally just save in ram and viewed by dmesg which is very fast.

Quoted 35 lines
Quoted 34 lines
drivers/rpmsg/rpmsg_syslog.c new file +29 -0
+     if (nl) {
+             printed = nl + 1 - msg->data;
+             copied = msg->count - printed;
+
+             if (priv->next) {
+                     pr_info("%.*s%.*s", priv->next,
+                             priv->buf, printed, msg->data);
+                     priv->next = 0;
+             } else {
+                     pr_info("%.*s", printed, msg->data);
+             }
+     }
+
+     /* append the message after '\n' to the buffer */
 
+     if (copied != 0) {
 
 
 
+             unsigned int newsize = priv->next + copied;
+
+             if (newsize > priv->size) {
+                     char *newbuf;
+
+                     newbuf = krealloc(priv->buf, newsize, GFP_KERNEL);
+                     if (newbuf) {
+                             priv->buf  = newbuf;
+                             priv->size = newsize;
+                     } else {
+                             copied = priv->size - priv->next;
+                     }
+             }
+
Quoted 3 lines
Quoted 1 line

+ strncpy(priv->buf + priv->next, msg->data + printed, copied);

Hmm... shouldn't be memcpy()?

I use memcpy initially, but found that the unaligned exception happen randomly. To avoid the cache issue, the IPC memory normally map as device memory, but ARM just allow the alignment access to this type of memory.

Quoted 26 lines
Quoted 25 lines
drivers/rpmsg/rpmsg_syslog.c new file +24 -0
+             priv->next += copied;
+     }
+
+     done.command = RPMSG_SYSLOG_TRANSFER_DONE;
+     done.result  = printed + copied;
+     return rpmsg_send(rpdev->ept, &done, sizeof(done));
+}
+
+static int rpmsg_syslog_probe(struct rpmsg_device *rpdev)
+{
+     struct rpmsg_syslog *priv;
+
+     priv = devm_kzalloc(&rpdev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     dev_set_drvdata(&rpdev->dev, priv);
+     return 0;
+}
+
+static void rpmsg_syslog_remove(struct rpmsg_device *rpdev)
+{
+     struct rpmsg_syslog *priv = dev_get_drvdata(&rpdev->dev);
+
Quoted 6 lines

drivers/rpmsg/rpmsg_syslog.c line 110

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+     /* flush the buffered log if need */
+     if (priv->next)
+             pr_info("%.*s\n", priv->next, priv->buf);
+     kfree(priv->buf);

I don't see how it's serialized. Does rpmsg core take care of this?

Yes, the callback come from a dedicated work thread.

Quoted 6 lines

drivers/rpmsg/rpmsg_syslog.c line 113

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +2 -0
+}
 
+#ifdef CONFIG_PM_SLEEP

You can consider to use __maybe_unused annotation to the below function.

Ok.

Quoted 9 lines
Quoted 8 lines
drivers/rpmsg/rpmsg_syslog.c new file +6 -0
+static int rpmsg_syslog_dev_suspend(struct device *dev)
+{
 
+}
+
+static int rpmsg_syslog_dev_resume(struct device *dev)
+{
Quoted 8 lines

drivers/rpmsg/rpmsg_syslog.c line 141

Quoted 6 lines
drivers/rpmsg/rpmsg_syslog.c new file +5 -0
+}
+#endif
 
+static const struct rpmsg_device_id rpmsg_syslog_id_table[] = {
+     { .name = "rpmsg-syslog" },
+     { },

Terminator better without comma.

Ok.

Quoted 8 lines
Quoted 2 lines

+};

--
With Best Regards,
Andy Shevchenko

#5 lore raw

Re: [PATCH V2 2/2] rpmsg: add syslog redirection driver

Quoted 16 lines · xiang xiao
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Xiang Xiao

This driver allows the remote processor to redirect the output of syslog or printf into the kernel log, which is very useful to see what happen in the remote side.

Quoted 7 lines

drivers/rpmsg/rpmsg_syslog.c line 20

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+struct rpmsg_syslog_header {
+     u32                             command;
+     s32                             result;
+} __packed;

Isn't packed already?

Quoted 1 line

But, I want to make it more explicitly and prepare for struct expansion later.

How? Why it's not in this patch / patch series?

Quoted 10 lines
Quoted 7 lines

drivers/rpmsg/rpmsg_syslog.c line 52

Quoted 2 lines
drivers/rpmsg/rpmsg_syslog.c new file +2 -0
+     /* output the message before '\n' to the kernel log */
+     nl = memrchr(msg->data, '\n', msg->count);

Hmm... To me it sounds somehow fragile.

If your text contains binary data, how can you guarantee that it would be not in the middle of two \n:s?

This driver is just for log/printf redirection, so we could safely assume the data is pure text.

Then I don't see a point to use memrchr() at all here.

Use strchr or strrchr().

Quoted 4 lines
Quoted 1 line

OTOH, if it text data, why do you need to take all strings at once?

Remote side may decide to buffer more log to reduce the IPC number since IPC is a time consuming operation.

So, you always can do something like

p = msg->data;

while (...strsep(..., "\n")) {
	pr_info("%s\n", token);
	...
}
Quoted 6 lines
Quoted 2 lines

It might be worse from performance prospective (if you know how and when printk() supplies buffer to the console).

Yes, it's very slow if the log send to serial console. But in production environment, printk normally just save in ram and viewed by dmesg which is very fast.

You may not do such assumptions. For someone it would be RAM, for some customers it might be a slow channel.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+ strncpy(priv->buf + priv->next, msg->data + printed, copied);

Hmm... shouldn't be memcpy()?

I use memcpy initially, but found that the unaligned exception happen randomly. To avoid the cache issue, the IPC memory normally map as device memory, but ARM just allow the alignment access to this type of memory.

So, than it's an architecture level issue. With strncpy() here you will get a pretty rightful GCC warning.

Quoted 8 lines
Quoted 6 lines

drivers/rpmsg/rpmsg_syslog.c line 110

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+     /* flush the buffered log if need */
+     if (priv->next)
+             pr_info("%.*s\n", priv->next, priv->buf);
+     kfree(priv->buf);

I don't see how it's serialized. Does rpmsg core take care of this?

Yes, the callback come from a dedicated work thread.

Please, add a comment explaining that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#6 lore raw

Re: [PATCH V2 2/2] rpmsg: add syslog redirection driver

Quoted 18 lines · Andy Shevchenko
Quoted 16 lines · xiang xiao
Quoted 13 lines · Andy Shevchenko

drivers/rpmsg/rpmsg_syslog.c line 20

Quoted 8 lines · Xiang Xiao

This driver allows the remote processor to redirect the output of syslog or printf into the kernel log, which is very useful to see what happen in the remote side.

drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+struct rpmsg_syslog_header {
+     u32                             command;
+     s32                             result;
+} __packed;

Isn't packed already?

Quoted 3 lines
Quoted 1 line

But, I want to make it more explicitly and prepare for struct expansion later.

How? Why it's not in this patch / patch series?

Just for future, not now:).
Since this structure is shared by the different CPU/OS, it's better to indicate the packed explicitly.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

drivers/rpmsg/rpmsg_syslog.c line 52

Quoted 3 lines
drivers/rpmsg/rpmsg_syslog.c new file +2 -0
+     /* output the message before '\n' to the kernel log */
+     nl = memrchr(msg->data, '\n', msg->count);

Hmm... To me it sounds somehow fragile.

If your text contains binary data, how can you guarantee that it would be not in the middle of two \n:s?

Quoted 6 lines
Quoted 2 lines

This driver is just for log/printf redirection, so we could safely assume the data is pure text.

Then I don't see a point to use memrchr() at all here.

Use strchr or strrchr().

Yes, use strnchr is enough, I will remove memrchr in the next review.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

OTOH, if it text data, why do you need to take all strings at once?

Remote side may decide to buffer more log to reduce the IPC number since IPC is a time consuming operation.

Quoted 7 lines

So, you always can do something like

p = msg->data;

while (...strsep(..., "\n")) {
        pr_info("%s\n", token);
        ...
}

Can't use strsep here, since log come from remote isn't terminated by '\0'.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

It might be worse from performance prospective (if you know how and when printk() supplies buffer to the console).

Yes, it's very slow if the log send to serial console. But in production environment, printk normally just save in ram and viewed by dmesg which is very fast.

You may not do such assumptions. For someone it would be RAM, for some customers it might be a slow channel.

But we need reduce the IPC number, so both fast/slow channel could get the benefit.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

+ strncpy(priv->buf + priv->next, msg->data + printed, copied);

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Hmm... shouldn't be memcpy()?

I use memcpy initially, but found that the unaligned exception happen randomly. To avoid the cache issue, the IPC memory normally map as device memory, but ARM just allow the alignment access to this type of memory.

So, than it's an architecture level issue. With strncpy() here you will get a pretty rightful GCC warning.

Why GCC warning strncpy here?

Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

drivers/rpmsg/rpmsg_syslog.c line 110

Quoted 4 lines
drivers/rpmsg/rpmsg_syslog.c new file +4 -0
+     /* flush the buffered log if need */
+     if (priv->next)
+             pr_info("%.*s\n", priv->next, priv->buf);
+     kfree(priv->buf);

I don't see how it's serialized. Does rpmsg core take care of this?

Yes, the callback come from a dedicated work thread.

Please, add a comment explaining that.

Will add in the next review.

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#7 lore raw
In reply to Xiang Xiao
Quoted 12 lines · Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

lib/string.c void *memchr(const void *s, int c, size_t n) · line 981

Quoted 5 lines
lib/string.c +5 -0
+void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *p = s + n;
+
+	while (n-- != 0) {

Simple:

	while (n--) {
Quoted 5 lines
lib/string.c +5 -0
+		if ((unsigned char)c == *--p)
+			return (void *)p;
+	}
+	return NULL;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#8 lore raw

Will fix in the next revision.

Quoted 35 lines · Andy Shevchenko
Quoted 12 lines · Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except that it searches backward from the end of the n bytes pointed to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte or NULL if the character does not occur in the given memory area.

lib/string.c void *memchr(const void *s, int c, size_t n) · line 981

Quoted 5 lines
lib/string.c +5 -0
+void *memrchr(const void *s, int c, size_t n)
+{
+     const unsigned char *p = s + n;
+
+     while (n-- != 0) {

Simple:

        while (n--) {

lib/string.c void *memchr(const void *s, int c, size_t n) · line 986

Quoted 5 lines
lib/string.c +5 -0
+             if ((unsigned char)c == *--p)
+                     return (void *)p;
+     }
+     return NULL;
+}

--
With Best Regards,
Andy Shevchenko