linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/1] kernel/ucounts: expose current inotify watch count
@ 2019-01-21 16:45 Albert Vaca Cintora
  2019-01-21 16:45 ` [PATCH 1/1] " Albert Vaca Cintora
  0 siblings, 1 reply; 3+ messages in thread
From: Albert Vaca Cintora @ 2019-01-21 16:45 UTC (permalink / raw)
  To: jack, ebiederm, linux-kernel, nsaenzjulienne; +Cc: Albert Vaca Cintora

This patch enables reading the amount of inotify watches in use.

Inotify watches are a finite resource, in a similar way to available file
descriptors. However, there is no way to check how many watches we have
available or in use, we can only read the max.

This patch adds a 'current_inotify_watches' entry to the per-user sysctl table
that gets mounted under /proc/sys/user/. This is the same table that contains
'max_inotify_watches'.

The motivation for this patch is to be able to set up monitoring and alerting
before an application starts failing because it runs out of inotify watches.

Albert Vaca Cintora (1):
  kernel/ucounts: expose current inotify watch count

 kernel/ucount.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

--
2.20.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/1] kernel/ucounts: expose current inotify watch count
  2019-01-21 16:45 [PATCH 0/1] kernel/ucounts: expose current inotify watch count Albert Vaca Cintora
@ 2019-01-21 16:45 ` Albert Vaca Cintora
  2019-01-22 10:35   ` Jan Kara
  0 siblings, 1 reply; 3+ messages in thread
From: Albert Vaca Cintora @ 2019-01-21 16:45 UTC (permalink / raw)
  To: jack, ebiederm, linux-kernel, nsaenzjulienne; +Cc: Albert Vaca Cintora

Adds a readonly 'current_inotify_watches' entry to the user sysctl table.
The handler for this entry is a custom function that ends calling
proc_dointvec.

Signed-off-by: Albert Vaca Cintora <albertvaka@gmail.com>
---
 kernel/ucount.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/kernel/ucount.c b/kernel/ucount.c
index f48d1b6376a4..afa3b89e3373 100644
--- a/kernel/ucount.c
+++ b/kernel/ucount.c
@@ -57,6 +57,11 @@ static struct ctl_table_root set_root = {
 	.permissions = set_permissions,
 };

+#ifdef CONFIG_INOTIFY_USER
+int proc_read_inotify_watches(struct ctl_table *table, int write,
+		     void __user *buffer, size_t *lenp, loff_t *ppos);
+#endif
+
 static int zero = 0;
 static int int_max = INT_MAX;
 #define UCOUNT_ENTRY(name)				\
@@ -79,6 +84,12 @@ static struct ctl_table user_table[] = {
 #ifdef CONFIG_INOTIFY_USER
 	UCOUNT_ENTRY("max_inotify_instances"),
 	UCOUNT_ENTRY("max_inotify_watches"),
+	{
+		.procname	= "current_inotify_watches",
+		.maxlen		= sizeof(int),
+		.mode		= 0444,
+		.proc_handler	= proc_read_inotify_watches,
+	},
 #endif
 	{ }
 };
@@ -226,6 +237,24 @@ void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
 	put_ucounts(ucounts);
 }

+#ifdef CONFIG_INOTIFY_USER
+int proc_read_inotify_watches(struct ctl_table *table, int write,
+		     void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct ucounts *ucounts;
+	struct ctl_table fake_table;
+	int count;
+
+	ucounts = get_ucounts(current_user_ns(), current_euid());
+	count = atomic_read(&ucounts->ucount[UCOUNT_INOTIFY_WATCHES]);
+	put_ucounts(ucounts);
+
+	fake_table.data = &count;
+	fake_table.maxlen = sizeof(count);
+	return proc_dointvec(&fake_table, write, buffer, lenp, ppos);
+}
+#endif
+
 static __init int user_namespace_sysctl_init(void)
 {
 #ifdef CONFIG_SYSCTL
--
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/1] kernel/ucounts: expose current inotify watch count
  2019-01-21 16:45 ` [PATCH 1/1] " Albert Vaca Cintora
@ 2019-01-22 10:35   ` Jan Kara
  0 siblings, 0 replies; 3+ messages in thread
From: Jan Kara @ 2019-01-22 10:35 UTC (permalink / raw)
  To: Albert Vaca Cintora; +Cc: jack, ebiederm, linux-kernel, nsaenzjulienne

On Mon 21-01-19 17:45:11, Albert Vaca Cintora wrote:
> Adds a readonly 'current_inotify_watches' entry to the user sysctl table.
> The handler for this entry is a custom function that ends calling
> proc_dointvec.
> 
> Signed-off-by: Albert Vaca Cintora <albertvaka@gmail.com>

FWIW this makes sense to me. I'd just copy a lot of the rationale from the
cover latter to this commit log so that it gets preseved in git history.
Other than that feel free to add:

Acked-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  kernel/ucount.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/kernel/ucount.c b/kernel/ucount.c
> index f48d1b6376a4..afa3b89e3373 100644
> --- a/kernel/ucount.c
> +++ b/kernel/ucount.c
> @@ -57,6 +57,11 @@ static struct ctl_table_root set_root = {
>  	.permissions = set_permissions,
>  };
> 
> +#ifdef CONFIG_INOTIFY_USER
> +int proc_read_inotify_watches(struct ctl_table *table, int write,
> +		     void __user *buffer, size_t *lenp, loff_t *ppos);
> +#endif
> +
>  static int zero = 0;
>  static int int_max = INT_MAX;
>  #define UCOUNT_ENTRY(name)				\
> @@ -79,6 +84,12 @@ static struct ctl_table user_table[] = {
>  #ifdef CONFIG_INOTIFY_USER
>  	UCOUNT_ENTRY("max_inotify_instances"),
>  	UCOUNT_ENTRY("max_inotify_watches"),
> +	{
> +		.procname	= "current_inotify_watches",
> +		.maxlen		= sizeof(int),
> +		.mode		= 0444,
> +		.proc_handler	= proc_read_inotify_watches,
> +	},
>  #endif
>  	{ }
>  };
> @@ -226,6 +237,24 @@ void dec_ucount(struct ucounts *ucounts, enum ucount_type type)
>  	put_ucounts(ucounts);
>  }
> 
> +#ifdef CONFIG_INOTIFY_USER
> +int proc_read_inotify_watches(struct ctl_table *table, int write,
> +		     void __user *buffer, size_t *lenp, loff_t *ppos)
> +{
> +	struct ucounts *ucounts;
> +	struct ctl_table fake_table;
> +	int count;
> +
> +	ucounts = get_ucounts(current_user_ns(), current_euid());
> +	count = atomic_read(&ucounts->ucount[UCOUNT_INOTIFY_WATCHES]);
> +	put_ucounts(ucounts);
> +
> +	fake_table.data = &count;
> +	fake_table.maxlen = sizeof(count);
> +	return proc_dointvec(&fake_table, write, buffer, lenp, ppos);
> +}
> +#endif
> +
>  static __init int user_namespace_sysctl_init(void)
>  {
>  #ifdef CONFIG_SYSCTL
> --
> 2.20.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-01-22 10:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-21 16:45 [PATCH 0/1] kernel/ucounts: expose current inotify watch count Albert Vaca Cintora
2019-01-21 16:45 ` [PATCH 1/1] " Albert Vaca Cintora
2019-01-22 10:35   ` Jan Kara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).