From patchwork Tue Feb 19 06:11:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Kartashov X-Patchwork-Id: 2161881 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id CB60E3FDF1 for ; Tue, 19 Feb 2013 06:14:32 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1U7gPl-0006wx-Ns; Tue, 19 Feb 2013 06:10:53 +0000 Received: from mx0.parallels.com ([199.115.104.20]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1U7gPh-0006wW-U6 for linux-arm-kernel@lists.infradead.org; Tue, 19 Feb 2013 06:10:50 +0000 Received: from [199.115.105.252] (helo=mail.parallels.com) by mx0.parallels.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.80.1) (envelope-from ) id 1U7gPb-0008LU-Lk; Tue, 19 Feb 2013 01:10:43 -0500 Received: from alex-pc.sw.ru (195.214.232.10) by mail.parallels.com (10.255.249.32) with Microsoft SMTP Server (TLS) id 14.2.247.3; Mon, 18 Feb 2013 22:10:42 -0800 From: Alexander Kartashov To: Subject: [PATCH] arm: align shared memory unconditionally to the SHMLBA boundary Date: Tue, 19 Feb 2013 10:11:09 +0400 Message-ID: <1361254269-3444-1-git-send-email-alekskartashov@parallels.com> X-Mailer: git-send-email 1.7.9.5 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130219_011050_041434_888D6B4B X-CRM114-Status: GOOD ( 16.25 ) X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.8 SPF_NEUTRAL SPF: sender does not match SPF record (neutral) -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Cyrill Gorcunov , Russell King - ARM Linux , Alexander Kartashov , Pavel Emelyanov X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Currently IPC SHM works in a strange way on ARM: the syscall sys_shmat() requires the argument shmaddr be SHMLBA-aligned (ARM has the macro __ARCH_FORCE_SHMLBA unconditionally defined) but allocates memory that isn't SHMLBA-aligned because the value of memory alignment depends on presense of certain cache aliases. Such a behavior raises the following problem. Consider the program: static int p[2]; static pid_t task2_pid; static int task1() { int task2_status; int *n; int shmid; shmid = shmget(0x94646337, 40960, IPC_CREAT); if (shmid < 0) { perror("Failed to get a SHM descriptor"); return 10; } n = (int*)shmat(shmid, 0, 0); if (n == (int*)-1) { perror("task1: failed to attach the SHM segment"); return 11; } *n = MAGIC; shmdt(n); if (write(p[1], &n, sizeof(n)) != sizeof(n)) { perror("task1: failed to send the SHM address"); return 12; } if (waitpid(task2_pid, &task2_status, 0) != task2_pid) { perror("task2: failed to wait for task2"); return 13; } if (!WIFEXITED(task2_status)) { printf("task1: task2 hasn't exited!\n"); return 14; } printf("task1: task2 exited with code %d, " "the SHM was attached to %p.\n", WEXITSTATUS(task2_status), n); return 0; } static int task2() { int *n = 0; int res; void *addr; int shmid; shmid = shmget(0x94646337, 40960, IPC_CREAT); if (shmid < 0) { perror("Failed to get a SHM descriptor"); return 20; } if (read(p[0], &n, sizeof(n)) != sizeof(n)) { perror("task2: failed to receive the SHM address"); return 21; } addr = shmat(shmid, n, 0); if (addr == (void*)-1) { printf("task2: failed to attach the SHM to the address %p: %s\n", n, strerror(errno)); res = 22; goto exit_task2; } if ((void*)n != addr) { printf("task2: the SHM isn't attached where it's expected:" "expected %p, got %p\n", n, addr); res = 23; goto exit_task2; } if (*n != MAGIC) { printf("task2: *n (%d) isn't %d\n", *n, MAGIC); res = 24; goto exit_task2; } exit_task2: shmdt(addr); return res; } int main() { int res = 0; if (pipe(p) < 0) { perror("Failed to create a pipe"); return 2; } task2_pid = fork(); if (task2_pid) res = task1(); else res = task2(); close(p[0]); close(p[1]); return res; } This program sometimes fails generating the output like that: root@test:~/shmfail# ./shmfail task2: failed to attach the SHM to the address 0xb6e3a000: Invalid argument task1: task2 exited with code 22, the SHM was attached to 0xb6e3a000. root@test:~/shmfail# ./shmfail task2: failed to attach the SHM to the address 0xb6e63000: Invalid argument task1: task2 exited with code 22, the SHM was attached to 0xb6e63000. and sometimes succeeds: root@test:~/shmfail# ./shmfail task1: task2 exited with code 0, the SHM was attached to 0xb6e94000. The problem is reproducible in the mainstream kernel branch as well as in a stable (3.7.5) kernel release. The program isn't reproducible on x86_64. As you can see the program fails when the address returned by the function shmat() isn't SHMLBA-aligned. This patch makes file-mmapped memory be unconditionaly SHMLBA-aligned. Signed-off-by: Alexander Kartashov CC: Russell King - ARM Linux CC: Cyrill Gorcunov CC: Pavel Emelyanov --- arch/arm/mm/mmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index 10062ce..eca577e3 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -65,8 +65,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, * We only need to do colour alignment if either the I or D * caches alias. */ - if (aliasing) - do_align = filp || (flags & MAP_SHARED); + do_align = filp || (flags & MAP_SHARED); /* * We enforce the MAP_FIXED case.