[pulseaudio-commits] 2 commits - configure.ac man/pulseaudio.1.xml.in man/pulse-client.conf.5.xml.in man/pulse-daemon.conf.5.xml.in PROTOCOL shell-completion/bash shell-completion/zsh src/daemon src/pulse src/pulsecore
Arun Raghavan
arun at kemper.freedesktop.org
Wed Apr 27 13:08:50 UTC 2016
PROTOCOL | 37 ++++++++++++++++++++
configure.ac | 2 -
man/pulse-client.conf.5.xml.in | 8 ++++
man/pulse-daemon.conf.5.xml.in | 7 +++
man/pulseaudio.1.xml.in | 15 ++++++--
shell-completion/bash/pulseaudio | 4 +-
shell-completion/zsh/_pulseaudio | 1
src/daemon/cmdline.c | 13 ++++++-
src/daemon/daemon-conf.c | 2 +
src/daemon/daemon-conf.h | 1
src/daemon/main.c | 4 +-
src/pulse/client-conf.c | 1
src/pulse/client-conf.h | 2 -
src/pulse/context.c | 44 +++++++++++++++++++++---
src/pulse/internal.h | 3 +
src/pulsecore/core.c | 9 +++-
src/pulsecore/core.h | 2 -
src/pulsecore/mem.h | 9 ++++
src/pulsecore/protocol-native.c | 71 +++++++++++++++++++++++++++++++++------
19 files changed, 207 insertions(+), 28 deletions(-)
New commits:
commit d2a6afcab31f9f8122200ce383fdd5479ca0ebd1
Author: Ahmed S. Darwish <darwish.07 at gmail.com>
Date: Fri Apr 15 23:07:36 2016 +0200
core: Support memfd transport; bump protocol version
Now that all layers in the stack support memfd blocks, add memfd
support for the daemon's global core mempool. Also introduce
"enable-memfd=" daemon argument and configuration option.
For now, memfd support is an opt-in feature to be activated only
when daemon's enable-memfd= is set to yes.
Signed-off-by: Ahmed S. Darwish <darwish.07 at gmail.com>
diff --git a/PROTOCOL b/PROTOCOL
index 3c08fea..5191397 100644
--- a/PROTOCOL
+++ b/PROTOCOL
@@ -371,6 +371,43 @@ PA_COMMAND_DISABLE_SRBCHANNEL
Tells the client to stop listening on the additional SHM ringbuffer channel.
Acked by client by sending PA_COMMAND_DISABLE_SRBCHANNEL back.
+## v31, implemented by >= 9.0
+
+Memfd shared-memory support is now added to PulseAudio as an opt-in feature.
+Add 'enable-memfd=yes' to daemon's configuration to use memfds, instead of
+POSIX shm, by default.
+
+Memfd is a simple memory sharing mechanism, added by the systemd/kdbus
+developers, to share pages between processes in an anonymous, no global
+registry needed, no mount-point required, relatively secure, manner.
+
+PulseAudio memfd support builds the necessary (but not yet sufficient)
+groundwork for a better integration with per-app containers (e.g. xdg-app)
+
+For further details on memfds in general, please check:
+
+ https://dvdhrm.wordpress.com/2014/06/10/memfd_create2/
+ Archived at: http://www.webcitation.org/6gnHTy9Kr
+
+Moreover, for both client and server, the second most-significant bit of
+the version tag is now used to flag memfd SHM support. On the way forward,
+the two most-significant _bytes_ of the version tag are now also reserved
+for flags.
+
+PA_COMMAND_REGISTER_MEMFD_SHMID
+New command that can be sent both ways, from client to server and vice versa.
+This is needed to transfer a memfd pool's blocks without passing its fd every
+time, thus minimizing overhead and avoiding fd leaks.
+
+The registration command above sends a packet with the pool's memfd fd as
+ancillary data. Such packet has an ID that uniquely identifies the pool's
+memfd memory area. Upon arrival, the other end (client or server) creates a
+permanent ID<->memfd mapping.
+
+By doing so, there's need to reference the pool's memfd file descriptor any
+further -- just its ID. Thus both endpoints can then quickly and safely
+close their memfd file descriptors.
+
#### If you just changed the protocol, read this
## module-tunnel depends on the sink/source/sink-input/source-input protocol
## internals, so if you changed these, you might have broken module-tunnel.
diff --git a/configure.ac b/configure.ac
index 623475d..cac5eff 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,7 +40,7 @@ AC_SUBST(PA_MINOR, pa_minor)
AC_SUBST(PA_MAJORMINOR, pa_major.pa_minor)
AC_SUBST(PA_API_VERSION, 12)
-AC_SUBST(PA_PROTOCOL_VERSION, 30)
+AC_SUBST(PA_PROTOCOL_VERSION, 31)
# The stable ABI for client applications, for the version info x:y:z
# always will hold y=z
diff --git a/man/pulse-daemon.conf.5.xml.in b/man/pulse-daemon.conf.5.xml.in
index 0367b1f..1abc94f 100644
--- a/man/pulse-daemon.conf.5.xml.in
+++ b/man/pulse-daemon.conf.5.xml.in
@@ -189,12 +189,17 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
<option>
<p><opt>enable-shm=</opt> Enable data transfer via POSIX
- shared memory. Takes a boolean argument, defaults to
+ or memfd shared memory. Takes a boolean argument, defaults to
<opt>yes</opt>. The <opt>--disable-shm</opt> command line
argument takes precedence.</p>
</option>
<option>
+ <p><opt>enable-memfd=</opt>. Enable memfd shared memory. Takes
+ a boolean argument, defaults to <opt>no</opt>.</p>
+ </option>
+
+ <option>
<p><opt>shm-size-bytes=</opt> Sets the shared memory segment
size for the daemon, in bytes. If left unspecified or is set to 0
it will default to some system-specific default, usually 64
diff --git a/man/pulseaudio.1.xml.in b/man/pulseaudio.1.xml.in
index 650b417..3187ef5 100644
--- a/man/pulseaudio.1.xml.in
+++ b/man/pulseaudio.1.xml.in
@@ -292,14 +292,23 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
<p><opt>--disable-shm</opt><arg>[=BOOL]</arg></p>
<optdesc><p>PulseAudio clients and the server can exchange audio
- data via POSIX shared memory segments (on systems that support
- this). If disabled PulseAudio will communicate exclusively over
- sockets. Please note that data transfer via shared memory
+ data via POSIX or memfd shared memory segments (on systems that
+ support this). If disabled PulseAudio will communicate exclusively
+ over sockets. Please note that data transfer via shared memory
segments is always disabled when PulseAudio is running with
<opt>--system</opt> enabled (see above).</p></optdesc>
</option>
<option>
+ <p><opt>--enable-memfd</opt><arg>[=BOOL]</arg></p>
+
+ <optdesc><p>PulseAudio clients and the server can exchange audio
+ data via memfds - the anonymous Linux Kernel shared memory mechanism
+ (on kernels that support this). If disabled PulseAudio will
+ communicate via POSIX shared memory.</p></optdesc>
+ </option>
+
+ <option>
<p><opt>-L | --load</opt><arg>="MODULE ARGUMENTS"</arg></p>
<optdesc><p>Load the specified plugin module with the specified
diff --git a/shell-completion/bash/pulseaudio b/shell-completion/bash/pulseaudio
index cfcf7ff..e473b9c 100644
--- a/shell-completion/bash/pulseaudio
+++ b/shell-completion/bash/pulseaudio
@@ -500,13 +500,13 @@ _pulseaudio()
--realtime= --disallow-module-loading= --disallow-exit= --exit-idle-time=
--scache-idle-time= --log-level= -v --log-target= --log-meta= --log-time=
--log-backtrace= -p --dl-search-path= --resample-method= --use-pit-file=
- --no-cpu-limit= --disable-shm= -L --load= -F --file= -C -n'
+ --no-cpu-limit= --disable-shm= --enable-memfd= -L --load= -F --file= -C -n'
_init_completion -n = || return
case $cur in
--system=*|--daemonize=*|--fail=*|--high-priority=*|--realtime=*| \
--disallow-*=*|--log-meta=*|--log-time=*|--use-pid-file=*| \
- --no-cpu-limit=*|--disable-shm=*)
+ --no-cpu-limit=*|--disable-shm=*|--enable-memfd=*)
cur=${cur#*=}
COMPREPLY=($(compgen -W 'true false' -- "$cur"))
;;
diff --git a/shell-completion/zsh/_pulseaudio b/shell-completion/zsh/_pulseaudio
index c7d68f5..e065085 100644
--- a/shell-completion/zsh/_pulseaudio
+++ b/shell-completion/zsh/_pulseaudio
@@ -690,6 +690,7 @@ _pulseaudio_completion() {
'--use-pid-file=[create a PID file]:bool:(true false)' \
'--no-cpu-limit=[do not install CPU load limiter]:bool:(true false)' \
'--disable-shm=[disable shared memory support]:bool:(true false)' \
+ '--enable-memfd=[enable memfd shared memory support]:bool:(true false)' \
{-L,--load=}'[load the specified module]:modules:_all_modules' \
{-F,--file=}'[run the specified script]:file:_files' \
'-C[open a command line on the running tty]' \
diff --git a/src/daemon/cmdline.c b/src/daemon/cmdline.c
index 117147d..0454b6d 100644
--- a/src/daemon/cmdline.c
+++ b/src/daemon/cmdline.c
@@ -63,6 +63,7 @@ enum {
ARG_CHECK,
ARG_NO_CPU_LIMIT,
ARG_DISABLE_SHM,
+ ARG_ENABLE_MEMFD,
ARG_DUMP_RESAMPLE_METHODS,
ARG_SYSTEM,
ARG_CLEANUP_SHM,
@@ -100,6 +101,7 @@ static const struct option long_options[] = {
{"system", 2, 0, ARG_SYSTEM},
{"no-cpu-limit", 2, 0, ARG_NO_CPU_LIMIT},
{"disable-shm", 2, 0, ARG_DISABLE_SHM},
+ {"enable-memfd", 2, 0, ARG_ENABLE_MEMFD},
{"dump-resample-methods", 2, 0, ARG_DUMP_RESAMPLE_METHODS},
{"cleanup-shm", 2, 0, ARG_CLEANUP_SHM},
{NULL, 0, 0, 0}
@@ -152,7 +154,8 @@ void pa_cmdline_help(const char *argv0) {
" --use-pid-file[=BOOL] Create a PID file\n"
" --no-cpu-limit[=BOOL] Do not install CPU load limiter on\n"
" platforms that support it.\n"
- " --disable-shm[=BOOL] Disable shared memory support.\n\n"
+ " --disable-shm[=BOOL] Disable shared memory support.\n"
+ " --enable-memfd[=BOOL] Enable memfd shared memory support.\n\n"
"STARTUP SCRIPT:\n"
" -L, --load=\"MODULE ARGUMENTS\" Load the specified plugin module with\n"
@@ -389,6 +392,14 @@ int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d
conf->disable_shm = !!b;
break;
+ case ARG_ENABLE_MEMFD:
+ if ((b = optarg ? pa_parse_boolean(optarg) : 1) < 0) {
+ pa_log(_("--enable-memfd expects boolean argument"));
+ goto fail;
+ }
+ conf->disable_memfd = !b;
+ break;
+
default:
goto fail;
}
diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index 288aed2..965a5c8 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -92,6 +92,7 @@ static const pa_daemon_conf default_conf = {
#endif
.no_cpu_limit = true,
.disable_shm = false,
+ .disable_memfd = true,
.lock_memory = false,
.deferred_volume = true,
.default_n_fragments = 4,
@@ -526,6 +527,7 @@ int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
{ "cpu-limit", pa_config_parse_not_bool, &c->no_cpu_limit, NULL },
{ "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL },
{ "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL },
+ { "enable-memfd", pa_config_parse_not_bool, &c->disable_memfd, NULL },
{ "flat-volumes", pa_config_parse_bool, &c->flat_volumes, NULL },
{ "lock-memory", pa_config_parse_bool, &c->lock_memory, NULL },
{ "enable-deferred-volume", pa_config_parse_bool, &c->deferred_volume, NULL },
diff --git a/src/daemon/daemon-conf.h b/src/daemon/daemon-conf.h
index 458784c..82b619f 100644
--- a/src/daemon/daemon-conf.h
+++ b/src/daemon/daemon-conf.h
@@ -66,6 +66,7 @@ typedef struct pa_daemon_conf {
system_instance,
no_cpu_limit,
disable_shm,
+ disable_memfd,
disable_remixing,
disable_lfe_remixing,
load_default_script_file,
diff --git a/src/daemon/main.c b/src/daemon/main.c
index c2f47b6..ae1185d 100644
--- a/src/daemon/main.c
+++ b/src/daemon/main.c
@@ -1017,7 +1017,9 @@ int main(int argc, char *argv[]) {
pa_assert_se(mainloop = pa_mainloop_new());
- if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm, conf->shm_size))) {
+ if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm,
+ !conf->disable_shm && !conf->disable_memfd && pa_memfd_is_locally_supported(),
+ conf->shm_size))) {
pa_log(_("pa_core_new() failed."));
goto finish;
}
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
index 56cbe9d..6d102f5 100644
--- a/src/pulsecore/core.c
+++ b/src/pulsecore/core.c
@@ -61,16 +61,19 @@ static int core_process_msg(pa_msgobject *o, int code, void *userdata, int64_t o
static void core_free(pa_object *o);
-pa_core* pa_core_new(pa_mainloop_api *m, bool shared, size_t shm_size) {
+pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size) {
pa_core* c;
pa_mempool *pool;
+ pa_mem_type_t type;
int j;
pa_assert(m);
if (shared) {
- if (!(pool = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, shm_size, false))) {
- pa_log_warn("Failed to allocate shared memory pool. Falling back to a normal memory pool.");
+ type = (enable_memfd) ? PA_MEM_TYPE_SHARED_MEMFD : PA_MEM_TYPE_SHARED_POSIX;
+ if (!(pool = pa_mempool_new(type, shm_size, false))) {
+ pa_log_warn("Failed to allocate %s memory pool. Falling back to a normal memory pool.",
+ pa_mem_type_to_string(type));
shared = false;
}
}
diff --git a/src/pulsecore/core.h b/src/pulsecore/core.h
index 9f5c445..1a3c490 100644
--- a/src/pulsecore/core.h
+++ b/src/pulsecore/core.h
@@ -218,7 +218,7 @@ enum {
PA_CORE_MESSAGE_MAX
};
-pa_core* pa_core_new(pa_mainloop_api *m, bool shared, size_t shm_size);
+pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size);
/* Check whether no one is connected to this core */
void pa_core_check_idle(pa_core *c);
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 566ba6f..5619b9c 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -2598,7 +2598,7 @@ static void command_exit(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
}
-static void setup_srbchannel(pa_native_connection *c) {
+static void setup_srbchannel(pa_native_connection *c, pa_mem_type_t shm_type) {
pa_srbchannel_template srbt;
pa_srbchannel *srb;
pa_memchunk mc;
@@ -2631,20 +2631,25 @@ static void setup_srbchannel(pa_native_connection *c) {
return;
}
- if (!(c->rw_mempool = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, c->protocol->core->shm_size, true))) {
+ if (!(c->rw_mempool = pa_mempool_new(shm_type, c->protocol->core->shm_size, true))) {
pa_log_warn("Disabling srbchannel, reason: Failed to allocate shared "
"writable memory pool.");
return;
}
+
+ if (shm_type == PA_MEM_TYPE_SHARED_MEMFD) {
+ const char *reason;
+ if (pa_pstream_register_memfd_mempool(c->pstream, c->rw_mempool, &reason)) {
+ pa_log_warn("Disabling srbchannel, reason: Failed to register memfd mempool: %s", reason);
+ goto fail;
+ }
+ }
pa_mempool_set_is_remote_writable(c->rw_mempool, true);
srb = pa_srbchannel_new(c->protocol->core->mainloop, c->rw_mempool);
if (!srb) {
pa_log_debug("Failed to create srbchannel");
-
- pa_mempool_unref(c->rw_mempool);
- c->rw_mempool = NULL;
- return;
+ goto fail;
}
pa_log_debug("Enabling srbchannel...");
pa_srbchannel_export(srb, &srbt);
@@ -2664,6 +2669,13 @@ static void setup_srbchannel(pa_native_connection *c) {
pa_pstream_send_memblock(c->pstream, 0, 0, 0, &mc);
c->srbpending = srb;
+ return;
+
+fail:
+ if (c->rw_mempool) {
+ pa_mempool_unref(c->rw_mempool);
+ c->rw_mempool = NULL;
+ }
}
static void command_enable_srbchannel(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
@@ -2682,7 +2694,7 @@ static void command_enable_srbchannel(pa_pdispatch *pd, uint32_t command, uint32
static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
const void*cookie;
- bool memfd_on_remote = false;
+ bool memfd_on_remote = false, do_memfd = false;
pa_tagstruct *reply;
pa_mem_type_t shm_type;
bool shm_on_remote = false, do_shm;
@@ -2777,7 +2789,7 @@ static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
}
}
- /* Enable shared memory support if possible */
+ /* Enable shared memory and memfd support if possible */
do_shm =
pa_mempool_is_shared(c->protocol->core->mempool) &&
c->is_local;
@@ -2803,9 +2815,12 @@ static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm));
pa_pstream_enable_shm(c->pstream, do_shm);
+ do_memfd =
+ do_shm && pa_mempool_is_memfd_backed(c->protocol->core->mempool);
+
shm_type = PA_MEM_TYPE_PRIVATE;
if (do_shm) {
- if (c->version >= 31 && memfd_on_remote && pa_memfd_is_locally_supported()) {
+ if (c->version >= 31 && memfd_on_remote && do_memfd) {
pa_pstream_enable_memfd(c->pstream);
shm_type = PA_MEM_TYPE_SHARED_MEMFD;
} else
@@ -2817,7 +2832,7 @@ static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
reply = reply_new(tag);
pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION | (do_shm ? 0x80000000 : 0) |
- (pa_memfd_is_locally_supported() ? 0x40000000 : 0));
+ (do_memfd ? 0x40000000 : 0));
#ifdef HAVE_CREDS
{
@@ -2834,7 +2849,19 @@ static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
pa_pstream_send_tagstruct(c->pstream, reply);
#endif
- setup_srbchannel(c);
+ /* The client enables memfd transport on its pstream only after
+ * inspecting our version flags to see if we support memfds too.
+ *
+ * Thus register any pools after sending the server's version
+ * flags and _never_ before it. */
+ if (shm_type == PA_MEM_TYPE_SHARED_MEMFD) {
+ const char *reason;
+
+ if (pa_pstream_register_memfd_mempool(c->pstream, c->protocol->core->mempool, &reason))
+ pa_log("Failed to register memfd mempool. Reason: %s", reason);
+ }
+
+ setup_srbchannel(c, shm_type);
}
static void command_register_memfd_shmid(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
commit b1d47d60fc3f5dcc098f0ccc52a0f29dca8ce29e
Author: Ahmed S. Darwish <darwish.07 at gmail.com>
Date: Fri Apr 15 23:06:02 2016 +0200
client audio: Support memfd transport
Now that all layers in the stack support memfd blocks, add memfd
pools support for client context and audio playback data.
Use such memfd pools by default only if the server signals memfd
support in its connection negotiations.
Also add ability for clients to force-disable memfd transport
through the `enable-memfd=' client configuration option.
Signed-off-by: Ahmed S. Darwish <darwish.07 at gmail.com>
diff --git a/man/pulse-client.conf.5.xml.in b/man/pulse-client.conf.5.xml.in
index cca2219..b88898c 100644
--- a/man/pulse-client.conf.5.xml.in
+++ b/man/pulse-client.conf.5.xml.in
@@ -102,6 +102,14 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
<option>
<p><opt>enable-shm=</opt> Enable data transfer via POSIX
+ or memfd shared memory. Takes a boolean argument, defaults to
+ <opt>yes</opt>. If set to <opt>no</opt>, communication with
+ the server will be exclusively done through data-copy over
+ sockets.</p>
+ </option>
+
+ <option>
+ <p><opt>enable-memfd=</opt>. Enable data transfer via memfd
shared memory. Takes a boolean argument, defaults to
<opt>yes</opt>.</p>
</option>
diff --git a/src/pulse/client-conf.c b/src/pulse/client-conf.c
index c23aa6b..a3c9486 100644
--- a/src/pulse/client-conf.c
+++ b/src/pulse/client-conf.c
@@ -141,6 +141,7 @@ void pa_client_conf_load(pa_client_conf *c, bool load_from_x11, bool load_from_e
{ "cookie-file", pa_config_parse_string, &c->cookie_file_from_client_conf, NULL },
{ "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL },
{ "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL },
+ { "enable-memfd", pa_config_parse_not_bool, &c->disable_memfd, NULL },
{ "shm-size-bytes", pa_config_parse_size, &c->shm_size, NULL },
{ "auto-connect-localhost", pa_config_parse_bool, &c->auto_connect_localhost, NULL },
{ "auto-connect-display", pa_config_parse_bool, &c->auto_connect_display, NULL },
diff --git a/src/pulse/client-conf.h b/src/pulse/client-conf.h
index eac705a..7691ec7 100644
--- a/src/pulse/client-conf.h
+++ b/src/pulse/client-conf.h
@@ -37,7 +37,7 @@ typedef struct pa_client_conf {
bool cookie_from_x11_valid;
char *cookie_file_from_application;
char *cookie_file_from_client_conf;
- bool autospawn, disable_shm, auto_connect_localhost, auto_connect_display;
+ bool autospawn, disable_shm, disable_memfd, auto_connect_localhost, auto_connect_display;
size_t shm_size;
} pa_client_conf;
diff --git a/src/pulse/context.c b/src/pulse/context.c
index ef39416..69be5f4 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -173,7 +173,12 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
c->srb_template.readfd = -1;
c->srb_template.writefd = -1;
- type = !c->conf->disable_shm ? PA_MEM_TYPE_SHARED_POSIX : PA_MEM_TYPE_PRIVATE;
+ c->memfd_on_local = (!c->conf->disable_memfd && pa_memfd_is_locally_supported());
+
+ type = (c->conf->disable_shm) ? PA_MEM_TYPE_PRIVATE :
+ ((!c->memfd_on_local) ?
+ PA_MEM_TYPE_SHARED_POSIX : PA_MEM_TYPE_SHARED_MEMFD);
+
if (!(c->mempool = pa_mempool_new(type, c->conf->shm_size, true))) {
if (!c->conf->disable_shm) {
@@ -482,6 +487,7 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
case PA_CONTEXT_AUTHORIZING: {
pa_tagstruct *reply;
bool shm_on_remote = false;
+ bool memfd_on_remote = false;
if (pa_tagstruct_getu32(t, &c->version) < 0 ||
!pa_tagstruct_eof(t)) {
@@ -500,7 +506,15 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
not. */
if (c->version >= 13) {
shm_on_remote = !!(c->version & 0x80000000U);
- c->version &= 0x7FFFFFFFU;
+
+ /* Starting with protocol version 31, the second MSB of the version
+ * tag reflects whether memfd is supported on the other PA end. */
+ if (c->version >= 31)
+ memfd_on_remote = !!(c->version & 0x40000000U);
+
+ /* Reserve the two most-significant _bytes_ of the version tag
+ * for flags. */
+ c->version &= 0x0000FFFFU;
}
pa_log_debug("Protocol version: remote %u, local %u", c->version, PA_PROTOCOL_VERSION);
@@ -526,6 +540,26 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
pa_log_debug("Negotiated SHM: %s", pa_yes_no(c->do_shm));
pa_pstream_enable_shm(c->pstream, c->do_shm);
+ c->shm_type = PA_MEM_TYPE_PRIVATE;
+ if (c->do_shm) {
+ if (c->version >= 31 && memfd_on_remote && c->memfd_on_local) {
+ const char *reason;
+
+ pa_pstream_enable_memfd(c->pstream);
+ if (pa_mempool_is_memfd_backed(c->mempool))
+ if (pa_pstream_register_memfd_mempool(c->pstream, c->mempool, &reason))
+ pa_log("Failed to regester memfd mempool. Reason: %s", reason);
+
+ /* Even if memfd pool registration fails, the negotiated SHM type
+ * shall remain memfd as both endpoints claim to support it. */
+ c->shm_type = PA_MEM_TYPE_SHARED_MEMFD;
+ } else
+ c->shm_type = PA_MEM_TYPE_SHARED_POSIX;
+ }
+
+ pa_log_debug("Memfd possible: %s", pa_yes_no(c->memfd_on_local));
+ pa_log_debug("Negotiated SHM type: %s", pa_mem_type_to_string(c->shm_type));
+
reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
if (c->version >= 13) {
@@ -593,8 +627,10 @@ static void setup_context(pa_context *c, pa_iochannel *io) {
pa_log_debug("SHM possible: %s", pa_yes_no(c->do_shm));
/* Starting with protocol version 13 we use the MSB of the version
- * tag for informing the other side if we could do SHM or not */
- pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION | (c->do_shm ? 0x80000000U : 0));
+ * tag for informing the other side if we could do SHM or not.
+ * Starting from version 31, second MSB is used to flag memfd support. */
+ pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION | (c->do_shm ? 0x80000000U : 0) |
+ (c->memfd_on_local ? 0x40000000 : 0));
pa_tagstruct_put_arbitrary(t, cookie, sizeof(cookie));
#ifdef HAVE_CREDS
diff --git a/src/pulse/internal.h b/src/pulse/internal.h
index eefd181..9bbb903 100644
--- a/src/pulse/internal.h
+++ b/src/pulse/internal.h
@@ -88,6 +88,7 @@ struct pa_context {
bool is_local:1;
bool do_shm:1;
+ bool memfd_on_local:1;
bool server_specified:1;
bool no_fail:1;
bool do_autospawn:1;
@@ -95,6 +96,8 @@ struct pa_context {
bool filter_added:1;
pa_spawn_api spawn_api;
+ pa_mem_type_t shm_type;
+
pa_strlist *server_list;
char *server;
diff --git a/src/pulsecore/mem.h b/src/pulsecore/mem.h
index 11a8086..cba1410 100644
--- a/src/pulsecore/mem.h
+++ b/src/pulsecore/mem.h
@@ -22,6 +22,7 @@
#include <stdbool.h>
+#include <pulsecore/creds.h>
#include <pulsecore/macro.h>
typedef enum pa_mem_type {
@@ -48,4 +49,12 @@ static inline bool pa_mem_type_is_shared(pa_mem_type_t t) {
return (t == PA_MEM_TYPE_SHARED_POSIX) || (t == PA_MEM_TYPE_SHARED_MEMFD);
}
+static inline bool pa_memfd_is_locally_supported() {
+#if defined(HAVE_CREDS) && defined(HAVE_MEMFD)
+ return true;
+#else
+ return false;
+#endif
+}
+
#endif
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 93a7ce0..566ba6f 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -48,6 +48,7 @@
#include <pulsecore/core-scache.h>
#include <pulsecore/core-subscribe.h>
#include <pulsecore/log.h>
+#include <pulsecore/mem.h>
#include <pulsecore/strlist.h>
#include <pulsecore/shared.h>
#include <pulsecore/sample-util.h>
@@ -2681,7 +2682,9 @@ static void command_enable_srbchannel(pa_pdispatch *pd, uint32_t command, uint32
static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
const void*cookie;
+ bool memfd_on_remote = false;
pa_tagstruct *reply;
+ pa_mem_type_t shm_type;
bool shm_on_remote = false, do_shm;
pa_native_connection_assert_ref(c);
@@ -2705,7 +2708,15 @@ static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
not. */
if (c->version >= 13) {
shm_on_remote = !!(c->version & 0x80000000U);
- c->version &= 0x7FFFFFFFU;
+
+ /* Starting with protocol version 31, the second MSB of the version
+ * tag reflects whether memfd is supported on the other PA end. */
+ if (c->version >= 31)
+ memfd_on_remote = !!(c->version & 0x40000000U);
+
+ /* Reserve the two most-significant _bytes_ of the version tag
+ * for flags. */
+ c->version &= 0x0000FFFFU;
}
pa_log_debug("Protocol version: remote %u, local %u", c->version, PA_PROTOCOL_VERSION);
@@ -2792,8 +2803,21 @@ static void command_auth(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ta
pa_log_debug("Negotiated SHM: %s", pa_yes_no(do_shm));
pa_pstream_enable_shm(c->pstream, do_shm);
+ shm_type = PA_MEM_TYPE_PRIVATE;
+ if (do_shm) {
+ if (c->version >= 31 && memfd_on_remote && pa_memfd_is_locally_supported()) {
+ pa_pstream_enable_memfd(c->pstream);
+ shm_type = PA_MEM_TYPE_SHARED_MEMFD;
+ } else
+ shm_type = PA_MEM_TYPE_SHARED_POSIX;
+
+ pa_log_debug("Memfd possible: %s", pa_yes_no(pa_memfd_is_locally_supported()));
+ pa_log_debug("Negotiated SHM type: %s", pa_mem_type_to_string(shm_type));
+ }
+
reply = reply_new(tag);
- pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION | (do_shm ? 0x80000000 : 0));
+ pa_tagstruct_putu32(reply, PA_PROTOCOL_VERSION | (do_shm ? 0x80000000 : 0) |
+ (pa_memfd_is_locally_supported() ? 0x40000000 : 0));
#ifdef HAVE_CREDS
{
More information about the pulseaudio-commits
mailing list