Skip to contentSkip to conversation

max_user_watches overflows on 16TB system.

PATCH In review Robin Holt 4 participants 10 messages Oct 1, 2010 – Oct 14, 2010

Subsystems (2)
lore
Share & tools
lore

Robin Holt

author
#0 lore raw

Following a boot of a 16TB system, we noticed that the max_user_watches sysctl was negative. Is there any downside to converting that to a static long and handling the fallout from the change? I believe that fallout includes changing the definition of epoll_watches over to an atomic_long_t as well.

Alternatively should we just limit max_user_watches to INT_MAX?

Thanks,
Robin Holt

#1 lore raw
Quoted 8 lines · Robin Holt

Following a boot of a 16TB system, we noticed that the max_user_watches sysctl was negative. Is there any downside to converting that to a static long and handling the fallout from the change? I believe that fallout includes changing the definition of epoll_watches over to an atomic_long_t as well.

Alternatively should we just limit max_user_watches to INT_MAX?

2B watches looks an acceptable limit to me, at least for now. Nobody complained about not having enough of them so far.

- Davide

#2 lore raw
Quoted 9 lines · Davide Libenzi
Quoted 7 lines · Robin Holt

Following a boot of a 16TB system, we noticed that the max_user_watches sysctl was negative. Is there any downside to converting that to a static long and handling the fallout from the change? I believe that fallout includes changing the definition of epoll_watches over to an atomic_long_t as well.

Quoted 4 lines
Quoted 1 line

Alternatively should we just limit max_user_watches to INT_MAX?

2B watches looks an acceptable limit to me, at least for now. Nobody complained about not having enough of them so far.

Which suggests that we need to force the boot time calculation to not exceed 2B.

Quoted 1 line

From the sysctl interface perspective now that all of it is exported as

ascii strings I don't see a problem there.

Eric

#3 lore raw
Quoted 13 lines · Eric W. Biederman
Quoted 11 lines · Davide Libenzi
Quoted 8 lines · Robin Holt

Following a boot of a 16TB system, we noticed that the max_user_watches sysctl was negative. Is there any downside to converting that to a static long and handling the fallout from the change? I believe that fallout includes changing the definition of epoll_watches over to an atomic_long_t as well.

Alternatively should we just limit max_user_watches to INT_MAX?

Quoted 8 lines
Quoted 2 lines

2B watches looks an acceptable limit to me, at least for now. Nobody complained about not having enough of them so far.

Which suggests that we need to force the boot time calculation to not exceed 2B.

From the sysctl interface perspective now that all of it is exported as ascii strings I don't see a problem there.

Thinking with actual code at hands, you're right. It just makes more sense the 'long' variable conversion, even though MAX_INT watches are a huge number today.
Robin, sorry for the confusion. Can you post the 'long' conversion patch?

- Davide

author
#4 lore raw

PATCH 1/1 Convert max_user_watches to long.

On a 16TB machine, max_user_watches has an integer overflow. Convert it to use a long and handle the associated fallout.

To: "Eric W. Biederman" <[email protected]>
To: Davide Libenzi <[email protected]>
To: [email protected]
To: Pekka Enberg <[email protected]>

---

Davide, I changed the logic a bit in ep_insert. It looked to me like there was a window between when the epoll_watches is checked and when it is incremented where multiple epoll_insert callers could be adding watches at the same time and allow epoll_watches to exceed max_user_watches. Not sure of the case where this could happen, but I assume something like that must be possible or we would not be using atomics. If that is not to your liking, I will happily remove it.

 fs/eventpoll.c        |   22 +++++++++++++---------
 include/linux/sched.h |    2 +-
 2 files changed, 14 insertions(+), 10 deletions(-)
fs/eventpoll.c +13 -9
Index: pv1010933/fs/eventpoll.c
===================================================================
@@ -220,7 +220,7 @@ struct ep_send_events_data {
  * Configuration options available inside /proc/sys/fs/epoll/
  */
 /* Maximum number of epoll watched descriptors, per user */
-static int max_user_watches __read_mostly;
+static long max_user_watches __read_mostly;
 
 /*
  * This mutex is used to serialize ep_free() and eventpoll_release_file().
@@ -243,16 +243,18 @@ static struct kmem_cache *pwq_cache __re
 
 #include <linux/sysctl.h>
 
-static int zero;
+static long zero;
+static long long_max = LONG_MAX;
 
 ctl_table epoll_table[] = {
 	{
 		.procname	= "max_user_watches",
 		.data		= &max_user_watches,
-		.maxlen		= sizeof(int),
+		.maxlen		= sizeof(max_user_watches),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= proc_doulongvec_minmax,
 		.extra1		= &zero,
+		.extra2		= &long_max,
 	},
 	{ }
 };
@@ -564,7 +566,7 @@ static int ep_remove(struct eventpoll *e
 	/* At this point it is safe to free the eventpoll item */
 	kmem_cache_free(epi_cache, epi);
 
-	atomic_dec(&ep->user->epoll_watches);
+	atomic_long_dec(&ep->user->epoll_watches);
 
 	return 0;
 }
@@ -900,12 +902,15 @@ static int ep_insert(struct eventpoll *e
 {
 	int error, revents, pwake = 0;
 	unsigned long flags;
+	long user_watches;
 	struct epitem *epi;
 	struct ep_pqueue epq;
 
-	if (unlikely(atomic_read(&ep->user->epoll_watches) >=
-		     max_user_watches))
+	user_watches = atomic_long_inc_return(&ep->user->epoll_watches);
+	if (unlikely(user_watches > max_user_watches)) {
+		atomic_long_dec(&ep->user->epoll_watches);
 		return -ENOSPC;
+	}
 	if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
 		return -ENOMEM;
 
@@ -968,8 +973,6 @@ static int ep_insert(struct eventpoll *e
 
 	spin_unlock_irqrestore(&ep->lock, flags);
 
-	atomic_inc(&ep->user->epoll_watches);
-
 	/* We have to call this outside the lock */
 	if (pwake)
 		ep_poll_safewake(&ep->poll_wait);
@@ -1422,6 +1425,7 @@ static int __init eventpoll_init(void)
 	 */
 	max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
 		EP_ITEM_COST;
+	BUG_ON(max_user_watches < 0);
 
 	/* Initialize the structure used to perform safe poll wait head wake ups */
 	ep_nested_calls_init(&poll_safewake_ncalls);
include/linux/sched.h +1 -1
Index: pv1010933/include/linux/sched.h
===================================================================
@@ -666,7 +666,7 @@ struct user_struct {
 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
 #endif
 #ifdef CONFIG_EPOLL
-	atomic_t epoll_watches;	/* The number of file descriptors currently watched */
+	atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
 #endif
 #ifdef CONFIG_POSIX_MQUEUE
 	/* protected by mq_lock	*/
#5 lore raw

Re: [Patch] Convert max_user_watches to long.

Quoted 12 lines · Robin Holt

On a 16TB machine, max_user_watches has an integer overflow. Convert it to use a long and handle the associated fallout.

To: "Eric W. Biederman" <[email protected]>
To: Davide Libenzi <[email protected]>
To: [email protected]
To: Pekka Enberg <[email protected]>

---

Quoted 7 lines

Davide, I changed the logic a bit in ep_insert. It looked to me like there was a window between when the epoll_watches is checked and when it is incremented where multiple epoll_insert callers could be adding watches at the same time and allow epoll_watches to exceed max_user_watches. Not sure of the case where this could happen, but I assume something like that must be possible or we would not be using atomics. If that is not to your liking, I will happily remove it.

The case can happen, but the effect is not something we should be too worried about.
You seem to be leaking a count in case kmem_cache_alloc() and following fail.
I'd rather not have that code there, and have the patch cover the 'long' conversion only. Or you need a proper cleanup goto target.

Quoted 98 lines
 fs/eventpoll.c        |   22 +++++++++++++---------
 include/linux/sched.h |    2 +-
 2 files changed, 14 insertions(+), 10 deletions(-)

Index: pv1010933/fs/eventpoll.c
===================================================================

fs/eventpoll.c +13 -9
@@ -220,7 +220,7 @@ struct ep_send_events_data {
  * Configuration options available inside /proc/sys/fs/epoll/
  */
 /* Maximum number of epoll watched descriptors, per user */
-static int max_user_watches __read_mostly;
+static long max_user_watches __read_mostly;
 
 /*
  * This mutex is used to serialize ep_free() and eventpoll_release_file().
@@ -243,16 +243,18 @@ static struct kmem_cache *pwq_cache __re
 
 #include <linux/sysctl.h>
 
-static int zero;
+static long zero;
+static long long_max = LONG_MAX;
 
 ctl_table epoll_table[] = {
 	{
 		.procname	= "max_user_watches",
 		.data		= &max_user_watches,
-		.maxlen		= sizeof(int),
+		.maxlen		= sizeof(max_user_watches),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= proc_doulongvec_minmax,
 		.extra1		= &zero,
+		.extra2		= &long_max,
 	},
 	{ }
 };
@@ -564,7 +566,7 @@ static int ep_remove(struct eventpoll *e
 	/* At this point it is safe to free the eventpoll item */
 	kmem_cache_free(epi_cache, epi);
 
-	atomic_dec(&ep->user->epoll_watches);
+	atomic_long_dec(&ep->user->epoll_watches);
 
 	return 0;
 }
@@ -900,12 +902,15 @@ static int ep_insert(struct eventpoll *e
 {
 	int error, revents, pwake = 0;
 	unsigned long flags;
+	long user_watches;
 	struct epitem *epi;
 	struct ep_pqueue epq;
 
-	if (unlikely(atomic_read(&ep->user->epoll_watches) >=
-		     max_user_watches))
+	user_watches = atomic_long_inc_return(&ep->user->epoll_watches);
+	if (unlikely(user_watches > max_user_watches)) {
+		atomic_long_dec(&ep->user->epoll_watches);
 		return -ENOSPC;
+	}
 	if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
 		return -ENOMEM;
 
@@ -968,8 +973,6 @@ static int ep_insert(struct eventpoll *e
 
 	spin_unlock_irqrestore(&ep->lock, flags);
 
-	atomic_inc(&ep->user->epoll_watches);
-
 	/* We have to call this outside the lock */
 	if (pwake)
 		ep_poll_safewake(&ep->poll_wait);
@@ -1422,6 +1425,7 @@ static int __init eventpoll_init(void)
 	 */
 	max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
 		EP_ITEM_COST;
+	BUG_ON(max_user_watches < 0);
 
 	/* Initialize the structure used to perform safe poll wait head wake ups */
 	ep_nested_calls_init(&poll_safewake_ncalls);

Index: pv1010933/include/linux/sched.h
===================================================================

include/linux/sched.h +1 -1
@@ -666,7 +666,7 @@ struct user_struct {
 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
 #endif
 #ifdef CONFIG_EPOLL
-	atomic_t epoll_watches;	/* The number of file descriptors currently watched */
+	atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
 #endif
 #ifdef CONFIG_POSIX_MQUEUE
 	/* protected by mq_lock	*/
author
#6 lore raw

Re: [Patch] Convert max_user_watches to long.

Quoted 22 lines · Davide Libenzi
Quoted 12 lines · Robin Holt

On a 16TB machine, max_user_watches has an integer overflow. Convert it to use a long and handle the associated fallout.

To: "Eric W. Biederman" <[email protected]>
To: Davide Libenzi <[email protected]>
To: [email protected]
To: Pekka Enberg <[email protected]>

---

Quoted 7 lines

Davide, I changed the logic a bit in ep_insert. It looked to me like there was a window between when the epoll_watches is checked and when it is incremented where multiple epoll_insert callers could be adding watches at the same time and allow epoll_watches to exceed max_user_watches. Not sure of the case where this could happen, but I assume something like that must be possible or we would not be using atomics. If that is not to your liking, I will happily remove it.

Quoted 6 lines

The case can happen, but the effect is not something we should be too worried about.
You seem to be leaking a count in case kmem_cache_alloc() and following fail.
I'd rather not have that code there, and have the patch cover the 'long' conversion only. Or you need a proper cleanup goto target.

Bah. Too rushed when I made that. Here is the conversion only patch. If this is acceptable, what is the normal submission path for fs/eventpoll.c?

Robin

------------------------------------------------------------------------

On a 16TB machine, max_user_watches has an integer overflow. Convert it to use a long and handle the associated fallout.

To: "Eric W. Biederman" <[email protected]>
To: Davide Libenzi <[email protected]>
To: [email protected]
To: Pekka Enberg <[email protected]>

---

 fs/eventpoll.c        |   20 ++++++++++++--------
 include/linux/sched.h |    2 +-
 2 files changed, 13 insertions(+), 9 deletions(-)
fs/eventpoll.c +12 -8
Index: pv1010933/fs/eventpoll.c
===================================================================
@@ -220,7 +220,7 @@ struct ep_send_events_data {
  * Configuration options available inside /proc/sys/fs/epoll/
  */
 /* Maximum number of epoll watched descriptors, per user */
-static int max_user_watches __read_mostly;
+static long max_user_watches __read_mostly;
 
 /*
  * This mutex is used to serialize ep_free() and eventpoll_release_file().
@@ -243,16 +243,18 @@ static struct kmem_cache *pwq_cache __re
 
 #include <linux/sysctl.h>
 
-static int zero;
+static long zero;
+static long long_max = LONG_MAX;
 
 ctl_table epoll_table[] = {
 	{
 		.procname	= "max_user_watches",
 		.data		= &max_user_watches,
-		.maxlen		= sizeof(int),
+		.maxlen		= sizeof(max_user_watches),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= proc_doulongvec_minmax,
 		.extra1		= &zero,
+		.extra2		= &long_max,
 	},
 	{ }
 };
@@ -564,7 +566,7 @@ static int ep_remove(struct eventpoll *e
 	/* At this point it is safe to free the eventpoll item */
 	kmem_cache_free(epi_cache, epi);
 
-	atomic_dec(&ep->user->epoll_watches);
+	atomic_long_dec(&ep->user->epoll_watches);
 
 	return 0;
 }
@@ -900,11 +902,12 @@ static int ep_insert(struct eventpoll *e
 {
 	int error, revents, pwake = 0;
 	unsigned long flags;
+	long user_watches;
 	struct epitem *epi;
 	struct ep_pqueue epq;
 
-	if (unlikely(atomic_read(&ep->user->epoll_watches) >=
-		     max_user_watches))
+	user_watches = atomic_long_read(&ep->user->epoll_watches);
+	if (user_watches >= max_user_watches)
 		return -ENOSPC;
 	if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
 		return -ENOMEM;
@@ -968,7 +971,7 @@ static int ep_insert(struct eventpoll *e
 
 	spin_unlock_irqrestore(&ep->lock, flags);
 
-	atomic_inc(&ep->user->epoll_watches);
+	atomic_long_inc(&ep->user->epoll_watches);
 
 	/* We have to call this outside the lock */
 	if (pwake)
@@ -1422,6 +1425,7 @@ static int __init eventpoll_init(void)
 	 */
 	max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
 		EP_ITEM_COST;
+	BUG_ON(max_user_watches < 0);
 
 	/* Initialize the structure used to perform safe poll wait head wake ups */
 	ep_nested_calls_init(&poll_safewake_ncalls);
include/linux/sched.h +1 -1
Index: pv1010933/include/linux/sched.h
===================================================================
@@ -666,7 +666,7 @@ struct user_struct {
 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
 #endif
 #ifdef CONFIG_EPOLL
-	atomic_t epoll_watches;	/* The number of file descriptors currently watched */
+	atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
 #endif
 #ifdef CONFIG_POSIX_MQUEUE
 	/* protected by mq_lock	*/
#7 lore raw

Re: [Patch] Convert max_user_watches to long.

Quoted 30 lines · On Sat, 9 Oct 2010 02:50:02 -0500 Robin Holt wrote:
Quoted 22 lines · Davide Libenzi
Quoted 12 lines · Robin Holt

On a 16TB machine, max_user_watches has an integer overflow. Convert it to use a long and handle the associated fallout.

To: "Eric W. Biederman" <[email protected]>
To: Davide Libenzi <[email protected]>
To: [email protected]
To: Pekka Enberg <[email protected]>

---

Quoted 7 lines

Davide, I changed the logic a bit in ep_insert. It looked to me like there was a window between when the epoll_watches is checked and when it is incremented where multiple epoll_insert callers could be adding watches at the same time and allow epoll_watches to exceed max_user_watches. Not sure of the case where this could happen, but I assume something like that must be possible or we would not be using atomics. If that is not to your liking, I will happily remove it.

Quoted 6 lines

The case can happen, but the effect is not something we should be too worried about.
You seem to be leaking a count in case kmem_cache_alloc() and following fail.
I'd rather not have that code there, and have the patch cover the 'long' conversion only. Or you need a proper cleanup goto target.

Quoted 2 lines

Bah. Too rushed when I made that. Here is the conversion only patch. If this is acceptable, what is the normal submission path for fs/eventpoll.c?

git log looks mostly like either thru Andrew or straight to Linus.

Quoted 101 lines

Robin

------------------------------------------------------------------------

On a 16TB machine, max_user_watches has an integer overflow. Convert it to use a long and handle the associated fallout.

To: "Eric W. Biederman" <[email protected]>
To: Davide Libenzi <[email protected]>
To: [email protected]
To: Pekka Enberg <[email protected]>

---

 fs/eventpoll.c        |   20 ++++++++++++--------
 include/linux/sched.h |    2 +-
 2 files changed, 13 insertions(+), 9 deletions(-)

Index: pv1010933/fs/eventpoll.c
===================================================================

fs/eventpoll.c +12 -8
@@ -220,7 +220,7 @@ struct ep_send_events_data {
  * Configuration options available inside /proc/sys/fs/epoll/
  */
 /* Maximum number of epoll watched descriptors, per user */
-static int max_user_watches __read_mostly;
+static long max_user_watches __read_mostly;
 
 /*
  * This mutex is used to serialize ep_free() and eventpoll_release_file().
@@ -243,16 +243,18 @@ static struct kmem_cache *pwq_cache __re
 
 #include <linux/sysctl.h>
 
-static int zero;
+static long zero;
+static long long_max = LONG_MAX;
 
 ctl_table epoll_table[] = {
 	{
 		.procname	= "max_user_watches",
 		.data		= &max_user_watches,
-		.maxlen		= sizeof(int),
+		.maxlen		= sizeof(max_user_watches),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= proc_doulongvec_minmax,
 		.extra1		= &zero,
+		.extra2		= &long_max,
 	},
 	{ }
 };
@@ -564,7 +566,7 @@ static int ep_remove(struct eventpoll *e
 	/* At this point it is safe to free the eventpoll item */
 	kmem_cache_free(epi_cache, epi);
 
-	atomic_dec(&ep->user->epoll_watches);
+	atomic_long_dec(&ep->user->epoll_watches);
 
 	return 0;
 }
@@ -900,11 +902,12 @@ static int ep_insert(struct eventpoll *e
 {
 	int error, revents, pwake = 0;
 	unsigned long flags;
+	long user_watches;
 	struct epitem *epi;
 	struct ep_pqueue epq;
 
-	if (unlikely(atomic_read(&ep->user->epoll_watches) >=
-		     max_user_watches))
+	user_watches = atomic_long_read(&ep->user->epoll_watches);
+	if (user_watches >= max_user_watches)
 		return -ENOSPC;
 	if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
 		return -ENOMEM;
@@ -968,7 +971,7 @@ static int ep_insert(struct eventpoll *e
 
 	spin_unlock_irqrestore(&ep->lock, flags);
 
-	atomic_inc(&ep->user->epoll_watches);
+	atomic_long_inc(&ep->user->epoll_watches);
 
 	/* We have to call this outside the lock */
 	if (pwake)
@@ -1422,6 +1425,7 @@ static int __init eventpoll_init(void)
 	 */
 	max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
 		EP_ITEM_COST;
+	BUG_ON(max_user_watches < 0);
 
 	/* Initialize the structure used to perform safe poll wait head wake ups */
 	ep_nested_calls_init(&poll_safewake_ncalls);

Index: pv1010933/include/linux/sched.h
===================================================================

include/linux/sched.h struct user_struct {

Quoted 9 lines
include/linux/sched.h +1 -2
@@ -666,7 +666,7 @@ struct user_struct {
 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
 #endif
 #ifdef CONFIG_EPOLL
-	atomic_t epoll_watches;	/* The number of file descriptors currently watched */
+	atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
 #endif
 #ifdef CONFIG_POSIX_MQUEUE
 	/* protected by mq_lock	*/
--

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#8 lore raw

Re: [Patch] Convert max_user_watches to long.

In reply to Robin Holt

fs/eventpoll.c static int ep_insert(struct eventpoll *e · line 911

Quoted 13 lines · Robin Holt
fs/eventpoll.c +3 -2
@@ -900,11 +902,12 @@ static int ep_insert(struct eventpoll *e
 {
 	int error, revents, pwake = 0;
 	unsigned long flags;
+	long user_watches;
 	struct epitem *epi;
 	struct ep_pqueue epq;
 
-	if (unlikely(atomic_read(&ep->user->epoll_watches) >=
-		     max_user_watches))
+	user_watches = atomic_long_read(&ep->user->epoll_watches);
+	if (user_watches >= max_user_watches)
 		return -ENOSPC;

Is there a particular reason for adding an extra, otherwise unused, user_watches variable?

- Davide

author
#9 lore raw

Re: [Patch] Convert max_user_watches to long.

Quoted 10 lines · Davide Libenzi
Quoted 8 lines · Robin Holt
fs/eventpoll.c +1 -0
@@ -900,11 +902,12 @@ static int ep_insert(struct eventpoll *e
 {
 	int error, revents, pwake = 0;
 	unsigned long flags;
+	long user_watches;
 	struct epitem *epi;
 	struct ep_pqueue epq;
 
Quoted 8 lines

fs/eventpoll.c static int ep_insert(struct eventpoll *e · line 911

Quoted 5 lines
fs/eventpoll.c +2 -2
-	if (unlikely(atomic_read(&ep->user->epoll_watches) >=
-		     max_user_watches))
+	user_watches = atomic_long_read(&ep->user->epoll_watches);
+	if (user_watches >= max_user_watches)
 		return -ENOSPC;

Is there a particular reason for adding an extra, otherwise unused, user_watches variable?

Keeps the line length and readability complaints down. No other reason.

Robin