linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] random: do not export add_vmfork_randomness() unless needed
@ 2022-03-01 14:25 Jason A. Donenfeld
  2022-03-01 19:00 ` Theodore Ts'o
  0 siblings, 1 reply; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-03-01 14:25 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jason A. Donenfeld, Dominik Brodowski

Since add_vmfork_randomness() is only called from vmgenid.o, we can
guard it in CONFIG_VMGENID, similarly to how we do with
add_disk_randomness() and CONFIG_BLOCK. If we ever have multiple things
calling into add_vmfork_randomness(), we can add another shared Kconfig
symbol for that, but for now, this is good enough. Even though
add_vmfork_randomess() is a pretty small function, removing it means
that there are only calls to crng_reseed(false) and none to
crng_reseed(true), which means the compiler can constant propagate it
and simply crng_reseed().

Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c  | 2 ++
 include/linux/random.h | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 99cf9e829d1e..662b7edb3b7a 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1169,6 +1169,7 @@ void add_bootloader_randomness(const void *buf, size_t size)
 }
 EXPORT_SYMBOL_GPL(add_bootloader_randomness);
 
+#ifdef CONFIG_VMGENID
 /*
  * Handle a new unique VM ID, which is unique, not secret, so we
  * don't credit it, but we do immediately force a reseed after so
@@ -1183,6 +1184,7 @@ void add_vmfork_randomness(const void *unique_vm_id, size_t size)
 	}
 }
 EXPORT_SYMBOL_GPL(add_vmfork_randomness);
+#endif
 
 struct fast_pool {
 	union {
diff --git a/include/linux/random.h b/include/linux/random.h
index 117468f3a92e..e6225ac98572 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -34,7 +34,9 @@ extern void add_input_randomness(unsigned int type, unsigned int code,
 extern void add_interrupt_randomness(int irq) __latent_entropy;
 extern void add_hwgenerator_randomness(const void *buffer, size_t count,
 				       size_t entropy);
+#ifdef CONFIG_VMGENID
 extern void add_vmfork_randomness(const void *unique_vm_id, size_t size);
+#endif
 
 extern void get_random_bytes(void *buf, size_t nbytes);
 extern int wait_for_random_bytes(void);
-- 
2.35.1


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

* Re: [PATCH] random: do not export add_vmfork_randomness() unless needed
  2022-03-01 14:25 [PATCH] random: do not export add_vmfork_randomness() unless needed Jason A. Donenfeld
@ 2022-03-01 19:00 ` Theodore Ts'o
  2022-03-01 22:05   ` Jason A. Donenfeld
  0 siblings, 1 reply; 4+ messages in thread
From: Theodore Ts'o @ 2022-03-01 19:00 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: linux-kernel, Dominik Brodowski

On Tue, Mar 01, 2022 at 03:25:28PM +0100, Jason A. Donenfeld wrote:
> Since add_vmfork_randomness() is only called from vmgenid.o, we can
> guard it in CONFIG_VMGENID, similarly to how we do with
> add_disk_randomness() and CONFIG_BLOCK. If we ever have multiple things
> calling into add_vmfork_randomness(), we can add another shared Kconfig
> symbol for that, but for now, this is good enough. Even though
> add_vmfork_randomess() is a pretty small function, removing it means
> that there are only calls to crng_reseed(false) and none to
> crng_reseed(true), which means the compiler can constant propagate it
> and simply crng_reseed().

How about only exporting add_vmfork_randomness if VMGENID is compiled
as a module?  If it's built-in to the kernel, no need to export the
symbol.

					- Ted

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

* Re: [PATCH] random: do not export add_vmfork_randomness() unless needed
  2022-03-01 19:00 ` Theodore Ts'o
@ 2022-03-01 22:05   ` Jason A. Donenfeld
  2022-03-01 22:14     ` [PATCH v2] " Jason A. Donenfeld
  0 siblings, 1 reply; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-03-01 22:05 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: LKML, Dominik Brodowski

Hi Ted,

On Tue, Mar 1, 2022 at 8:00 PM Theodore Ts'o <tytso@mit.edu> wrote:
> How about only exporting add_vmfork_randomness if VMGENID is compiled
> as a module?  If it's built-in to the kernel, no need to export the
> symbol.

Sure, good idea. I'll send a v2.

Jason

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

* [PATCH v2] random: do not export add_vmfork_randomness() unless needed
  2022-03-01 22:05   ` Jason A. Donenfeld
@ 2022-03-01 22:14     ` Jason A. Donenfeld
  0 siblings, 0 replies; 4+ messages in thread
From: Jason A. Donenfeld @ 2022-03-01 22:14 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jason A. Donenfeld, Dominik Brodowski, Theodore Ts'o

Since add_vmfork_randomness() is only called from vmgenid.o, we can
guard it in CONFIG_VMGENID, similarly to how we do with
add_disk_randomness() and CONFIG_BLOCK. If we ever have multiple things
calling into add_vmfork_randomness(), we can add another shared Kconfig
symbol for that, but for now, this is good enough. Even though
add_vmfork_randomess() is a pretty small function, removing it means
that there are only calls to crng_reseed(false) and none to
crng_reseed(true), which means the compiler can constant propagate the
false, removing branches from crng_reseed() and its descendants.

Additionally, we don't even need the symbol to be exported if
CONFIG_VMGENID is not a module, so conditionalize that too.

Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
Changes v1->v2:
- [Ted] Do not export symbol if we're a built-in.

 drivers/char/random.c  | 4 ++++
 include/linux/random.h | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 99cf9e829d1e..fe477a12c1ad 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1169,6 +1169,7 @@ void add_bootloader_randomness(const void *buf, size_t size)
 }
 EXPORT_SYMBOL_GPL(add_bootloader_randomness);
 
+#if IS_ENABLED(CONFIG_VMGENID)
 /*
  * Handle a new unique VM ID, which is unique, not secret, so we
  * don't credit it, but we do immediately force a reseed after so
@@ -1182,7 +1183,10 @@ void add_vmfork_randomness(const void *unique_vm_id, size_t size)
 		pr_notice("crng reseeded due to virtual machine fork\n");
 	}
 }
+#if IS_MODULE(CONFIG_VMGENID)
 EXPORT_SYMBOL_GPL(add_vmfork_randomness);
+#endif
+#endif
 
 struct fast_pool {
 	union {
diff --git a/include/linux/random.h b/include/linux/random.h
index 117468f3a92e..f209f1a78899 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -34,7 +34,9 @@ extern void add_input_randomness(unsigned int type, unsigned int code,
 extern void add_interrupt_randomness(int irq) __latent_entropy;
 extern void add_hwgenerator_randomness(const void *buffer, size_t count,
 				       size_t entropy);
+#if IS_ENABLED(CONFIG_VMGENID)
 extern void add_vmfork_randomness(const void *unique_vm_id, size_t size);
+#endif
 
 extern void get_random_bytes(void *buf, size_t nbytes);
 extern int wait_for_random_bytes(void);
-- 
2.35.1


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

end of thread, other threads:[~2022-03-01 22:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-01 14:25 [PATCH] random: do not export add_vmfork_randomness() unless needed Jason A. Donenfeld
2022-03-01 19:00 ` Theodore Ts'o
2022-03-01 22:05   ` Jason A. Donenfeld
2022-03-01 22:14     ` [PATCH v2] " Jason A. Donenfeld

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).