From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sfi-mx-1.v28.ch3.sourceforge.com ([172.29.28.121] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.69) (envelope-from ) id 1NSMcX-0003q9-QC for ltp-list@lists.sourceforge.net; Wed, 06 Jan 2010 03:31:41 +0000 Received: from [222.73.24.84] (helo=song.cn.fujitsu.com) by sfi-mx-1.v28.ch3.sourceforge.com with esmtp (Exim 4.69) id 1NSMcR-0001I4-AS for ltp-list@lists.sourceforge.net; Wed, 06 Jan 2010 03:31:41 +0000 Message-ID: <4B4403A8.2040502@cn.fujitsu.com> Date: Wed, 06 Jan 2010 11:29:44 +0800 From: Miao Xie MIME-Version: 1.0 Subject: [LTP] [PATCH] fix the bug of the smaller cpu_set_t's length in sched_getaffinity01 Reply-To: miaox@cn.fujitsu.com List-Id: Linux Test Project General Discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ltp-list-bounces@lists.sourceforge.net To: Subrata Modak Cc: LTP-ML sched_getaffinity01 test failed because the len of cpu_set_t in the glibc is smaller than the length of the cpumask in the kernel. So we must use the dynamically sized CPU sets instead of the standard cpu_set_t. This patch fix this problem. Before using this patch, the test result is following: sched_getaffinity01 0 TINFO : system has 4 processor(s). sched_getaffinity01 1 TPASS : sched_getaffinity(0, len, (cpu_set_t *)-1): TEST_ERRNO=EINVAL(22): Invalid argument sched_getaffinity01 2 TPASS : sched_getaffinity(0, 0, &mask): TEST_ERRNO=EINVAL(22): Invalid argument sched_getaffinity01 3 TPASS : sched_getaffinity(getpid() + 1, len, &mask): TEST_ERRNO=EINVAL(22): Invalid argument sched_getaffinity01 4 TFAIL : could not get cpu affinity: TEST_ERRNO=EINVAL(22): Invalid argument The subcases1-3 of this test also failed though they were successful according to the log. Becuase the errnos returned were wrong. After using this patch, the test result is following: sched_getaffinity01 0 TINFO : system has 4 processor(s). sched_getaffinity01 0 TINFO : cpusetsize is 512 sched_getaffinity01 0 TINFO : mask.__bits[0] = 15 sched_getaffinity01 1 TPASS : sched_getaffinity() succeed ,this process 2239 is running processor: 0 sched_getaffinity01 2 TPASS : sched_getaffinity() succeed ,this process 2239 is running processor: 1 sched_getaffinity01 3 TPASS : sched_getaffinity() succeed ,this process 2239 is running processor: 2 sched_getaffinity01 4 TPASS : sched_getaffinity() succeed ,this process 2239 is running processor: 3 sched_getaffinity01 5 TPASS : sched_getaffinity(0, len, (cpu_set_t *)-1): TEST_ERRNO=EFAULT(14): Bad address sched_getaffinity01 6 TPASS : sched_getaffinity(0, 0, mask): TEST_ERRNO=EINVAL(22): Invalid argument sched_getaffinity01 7 TPASS : sched_getaffinity(getpid() + 1, len, mask): TEST_ERRNO=ESRCH(3): No such process Signed-off-by: Miao Xie --- .../sched_getaffinity/sched_getaffinity01.c | 81 +++++++++++++++----- 1 files changed, 61 insertions(+), 20 deletions(-) diff --git a/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c b/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c index 7c4a254..7dfb0ec 100644 --- a/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c +++ b/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c @@ -119,11 +119,15 @@ do { \ tst_resm((TEST_RETURN == -1 ? TPASS : TFAIL) | TTERRNO, #t); \ } while (0) +#if !(__GLIBC_PREREQ(2,7)) +#define CPU_FREE(ptr) free(ptr) +#endif int main(int ac, char **av) { int lc,num,i; /* loop counter */ char *msg; /* message returned from parse_opts */ - cpu_set_t mask; - unsigned int len = sizeof(cpu_set_t); + cpu_set_t *mask; + int nrcpus = 1024; + unsigned int len; /* parse standard options */ if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ @@ -140,30 +144,67 @@ int main(int ac, char **av) { for (lc = 0; TEST_LOOPING(lc); ++lc) { Tst_count = 0; for (testno = 0; testno < TST_TOTAL; ++testno) { - QUICK_TEST(sched_getaffinity(0, len, (cpu_set_t *)-1)); - QUICK_TEST(sched_getaffinity(0, 0, &mask)); - QUICK_TEST(sched_getaffinity(getpid() + 1, len, &mask)); - - CPU_ZERO(&mask); //clear - TEST(sched_getaffinity(0,len,&mask)); //call sched_getaffinity() +#if __GLIBC_PREREQ(2,7) +realloc: + mask = CPU_ALLOC(nrcpus); +# else + mask = malloc(sizeof(cpu_set_t)); +#endif + if (mask == NULL) { + tst_resm(TFAIL|TTERRNO, "cann't get enough memory"); + cleanup(); + tst_exit(); + } + +#if __GLIBC_PREREQ(2,7) + len = CPU_ALLOC_SIZE(nrcpus); + CPU_ZERO_S(len, mask); //clear +#else + len = sizeof(cpu_set_t); + CPU_ZERO(mask); //clear +#endif + TEST(sched_getaffinity(0, len, mask)); //call sched_getaffinity() if(TEST_RETURN == -1) { - tst_resm(TFAIL|TTERRNO, "could not get cpu affinity"); - cleanup(); - tst_exit(); + CPU_FREE(mask); +#if __GLIBC_PREREQ(2,7) + if (errno == EINVAL && nrcpus < (1024 << 8)) { + nrcpus = nrcpus << 2; + goto realloc; + } +#else + if (errno == EINVAL) + tst_resm(TFAIL, "NR_CPUS of the kernel is more than 1024, so we'd better use a newer glibc(>= 2.7)"); + else +#endif + tst_resm(TFAIL|TTERRNO, "could not get cpu affinity"); + cleanup(); + tst_exit(); } else { tst_resm(TINFO,"cpusetsize is %d", len); - tst_resm(TINFO,"mask.__bits[0] = %lu ",mask.__bits[0]); + tst_resm(TINFO,"mask.__bits[0] = %lu ",mask->__bits[0]); for(i=0;i