From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26323 invoked by alias); 13 Sep 2017 12:28:35 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 26105 invoked by uid 89); 13 Sep 2017 12:28:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy= X-HELO: mail-wm0-f66.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:reply-to:in-reply-to:references :from:date:message-id:subject:to:cc; bh=iRLl3JhQUItx6jHeYzoiK1dxfnEi8ljARPSnm/M3eVY=; b=P36rm1nWdPUzAwCuK1G/vhyGaPibbyqOzYe+GCJuBHYPNHK9B094YpH10mjqh9wyCU aIigwzoDi2pPXK19abBfO1NL8mTgUYO41CMP+z52ZHN+Uj8ZmpHC+H39MTPwlVyvYHpu fxylbKwCA21ZPPQtcZkyJtqzeXatKMYZAzk6+iWoZT8WEjOXH6GnrbDpQuDAmXMFg3u+ eXKP72XHCRzARY9dbcRtlqjlqU2NR/w2g5c0Vggg45e5S80LK1gvnZunpvAJnRnAo8hF jok0QQxrg5JPqZPmkz73Fq20JUc9STJcajHERIcToQGpOBUbKDu+0YCnHog9RW/3rlqi WvzQ== X-Gm-Message-State: AHPjjUgdAb0EtSbwV03U85N2/jHT3BzGzns+FcS+YmZM8W3YtAyeIeOo Oy50eiXiIXEEXKW+GFFyLe56FgJpi6NIxyFDP98= X-Google-Smtp-Source: AOwi7QC+ZndqvhHEL/w6dSB4WcadiWc4PVXdX8g20Fj5J77KLEed5/Wvn4aMkRS5hGhYEVYbVTAgv0hhK/NxO4fqr8M= X-Received: by 10.28.165.75 with SMTP id o72mr2248955wme.12.1505305711503; Wed, 13 Sep 2017 05:28:31 -0700 (PDT) MIME-Version: 1.0 Reply-To: mtk.manpages@gmail.com In-Reply-To: <20170913083446.GA16265@HP.internal.baidu.com> References: <36ab9ec0-b496-c007-c12f-065fd618e7fd@gmail.com> <20170826210528.GA32472@HP.internal.baidu.com> <20170913083446.GA16265@HP.internal.baidu.com> From: "Michael Kerrisk (man-pages)" Date: Wed, 13 Sep 2017 12:28:00 -0000 Message-ID: Subject: Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent() To: Yubin Ruan Cc: linux-man , "libc-alpha@sourceware.org" Content-Type: text/plain; charset="UTF-8" X-SW-Source: 2017-09/txt/msg00546.txt.bz2 Hello Yubin, On 13 September 2017 at 10:34, Yubin Ruan wrote: > On Tue, Sep 12, 2017 at 02:41:29PM +0200, Michael Kerrisk (man-pages) wrote: >> Hello Yubin, >> >> [...] >> > +.B PTHREAD_MUTEX_ROBUST >> > +can be set on a mutex attribute object so that when the owner of the mutex >> > +dies or when the process containing such a locked mutex performs >> > +.IR execve (2) >> > +, any future attempts to call >> > +.IR pthread_mutex_lock (3) >> > +on this mutex will suceed and return >> > +.B EOWNERDEAD >> > +to indicate that the original owner no longer exists and the mutex is left in >> > +an inconsistent state. >> How did you verify the point regarding execve(2)? I don't see this >> detailed mentioned in the standards or in the glibc source. > > Please see below the program I used to verify that. I haven't go into too much > detail in the POSIX standard, though. I think I must have read it at [1] or > somewhere else (don't remember...). Thanks for the details and example program. So, I see the kernel code that deals with this now: in fs/exec.c::exec_mmap(), there is a call to mm_release() which in turn calls exit_robust_list() So, this detail isn't in POSIX. I think what I will do is move discussion of that point into the NOTES. > And also, it is mentioned at [1] that when the process containing such a locked > mutex unmaps the memory containing the mutex, the mutex is unlocked... I think > this is trivial so I don't add it. It also appears not to be true on Linux, at least in some short tests I just did.[1] See my modified version of your program, below. Also when looking at the kernel code, I can see no call to mm_release() that would lead to this behavior. Cheers, Michael [1] And I wonder if the Solaris documentation is even correct, since, if we are talking about a shared anonymous mapping, the memory would not be released until *all* processes have unmapped the memory /************ verify-execve.c *****************/ #define _POSIX_C_SOURCE 200809L #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #define MEMSIZE 4096 #define ERROR_ON(func_name) \ fprintf(stderr, "error: " #func_name ": line[%d]: %s\n", __LINE__, strerror(errno)); int main(int argc, char *argv[]) { struct shm *shm = NULL; int ret_code = 0; pthread_mutex_t *mutexp = NULL; pthread_mutexattr_t attr; pid_t pid = 0; shm = mmap(NULL, MEMSIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED,-1, 0); if ((void *)-1 == shm) { ERROR_ON(shmat); return -1; } memset(shm, 0, sizeof(pthread_mutex_t)); printf("Successfully attached shared memory, trying to lock\n"); //initialize the lock mutexp = (pthread_mutex_t *)shm; pthread_mutexattr_init(&attr); pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(mutexp, &attr); pid = fork(); if (0 == pid) { sleep(1); printf("Child about to call pthread_mutex-Lock()\n"); ret_code = pthread_mutex_lock(mutexp); if (EOWNERDEAD == ret_code) { printf("child see EOWNERDEAD returned. Verification completed\n"); pthread_mutex_consistent(mutexp); pthread_mutex_unlock(mutexp); exit(0); } else { printf("child see [%d] returned\n", ret_code); exit(1); } } else { ret_code = pthread_mutex_lock(mutexp); if (0 == ret_code) { printf("parent successfully acquired the lock\n"); } else { ERROR_ON(pthread_mutex_lock); return -1; } printf("Parent sleeping 3 seconds\n"); sleep(3); if (munmap(shm, MEMSIZE) == -1) ERROR_ON(munmap); printf("Parent has unmapped the memory\n"); sleep(1); printf("Parent about to exit\n"); exit(0); /* printf("parent going to execve(/bin/true)\n"); execl("/bin/true", "true", (char *) NULL); */ } return 0; }