Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d898ba65 authored by Phil Burk's avatar Phil Burk Committed by Gerrit - the friendly Code Review server
Browse files

audio_hw: close mmap file descriptor to fix leak

The file descriptor for MMAP shared memory was created
and passed to AudioFlinger but never closed.
Now FD is closed when the stream is goes to standby.

Bug: 134381208
Test: Get HAL pid.
Test:     adb shell ps | grep audio
Test:     adb shell ls -l /proc/{halpid}/fd | wc
Test: Run Oboetester or other app that uses MMAP
Test: Completely close the app.
Test:     adb shell ls -l /proc/{halpid}/fd | wc
Test: Count should NOT grow each time.
Change-Id: Ieaaf1c6bdc96e7ecf01cee23215fb39f79662111
(cherry picked from commit 2e27354fd3c5adec2524813d7c34114922d3e8f4)
parent 418acb11
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -4180,6 +4180,13 @@ static int out_standby(struct audio_stream *stream)
            if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
                do_stop = out->playback_started;
                out->playback_started = false;

                if (out->mmap_shared_memory_fd >= 0) {
                    ALOGV("%s: closing mmap_shared_memory_fd = %d",
                          __func__, out->mmap_shared_memory_fd);
                    close(out->mmap_shared_memory_fd);
                    out->mmap_shared_memory_fd = -1;
                }
            }
        } else {
            ALOGD("copl(%p):standby", out);
@@ -6107,6 +6114,9 @@ static int out_create_mmap_buffer(const struct audio_stream_out *stream,
        // Fall back to non exclusive mode
        info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
    } else {
        out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
        ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);

        if (mmap_size < buffer_size) {
            step = "mmap";
            goto exit;
@@ -6253,6 +6263,12 @@ static int in_standby(struct audio_stream *stream)
        } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
            do_stop = in->capture_started;
            in->capture_started = false;
            if (in->mmap_shared_memory_fd >= 0) {
                ALOGV("%s: closing mmap_shared_memory_fd = %d",
                      __func__, in->mmap_shared_memory_fd);
                close(in->mmap_shared_memory_fd);
                in->mmap_shared_memory_fd = -1;
            }
        } else {
            if (audio_extn_cin_attached_usecase(in->usecase))
                audio_extn_cin_close_input_stream(in);
@@ -6981,6 +6997,9 @@ static int in_create_mmap_buffer(const struct audio_stream_in *stream,
        // Fall back to non exclusive mode
        info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
    } else {
        in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
        ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);

        if (mmap_size < buffer_size) {
            step = "mmap";
            goto exit;
@@ -7196,6 +7215,7 @@ int adev_open_output_stream(struct audio_hw_device *dev,
    out->set_dual_mono = false;
    out->prev_card_status_offline = false;
    out->pspd_coeff_sent = false;
    out->mmap_shared_memory_fd = -1; // not open

    if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
        (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
@@ -8704,6 +8724,7 @@ static int adev_open_input_stream(struct audio_hw_device *dev,
    in->zoom = 0;
    list_init(&in->aec_list);
    list_init(&in->ns_list);
    in->mmap_shared_memory_fd = -1; // not open

    ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
    if (source == AUDIO_SOURCE_VOICE_UPLINK ||
+2 −0
Original line number Diff line number Diff line
@@ -366,6 +366,7 @@ struct stream_out {
    bool muted;
    uint64_t written; /* total frames written, not cleared when entering standby */
    int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
    int     mmap_shared_memory_fd; /* file descriptor associated with MMAP NOIRQ shared memory */
    audio_io_handle_t handle;
    struct stream_app_type_cfg app_type_cfg;

@@ -454,6 +455,7 @@ struct stream_in {
    struct listnode aec_list;
    struct listnode ns_list;
    int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
    int     mmap_shared_memory_fd; /* file descriptor associated with MMAP NOIRQ shared memory */
    audio_io_handle_t capture_handle;
    audio_input_flags_t flags;
    char profile[MAX_STREAM_PROFILE_STR_LEN];